beanshell script does not recognize a set of objects as a java.util.Collection - beanshell

I am using the following script to select an object from a set of input objects using an index.
It does not recognize that the input argument is a set of three values.
I assume I am doing something wrong in the line:
if(setOfObjects instanceof java.util.Collection)
if(setOfObjects != null)
{
if(setOfObjects instanceof java.util.Collection)
{
object = setOfObjects.get(index);
if (object instanceof fUML.Semantics.Classes.Kernel.Object_)
{
fUML.Semantics.Classes.Kernel.Reference r = new fUML.Semantics.Classes.Kernel.Reference(object.getOwner());
r.setReferent(object);
object = r;
}
}
Else print(index);
if(index == 1)
{
print("ok");
object = setOfObjects;
}
else
{
print("failed");
object = null;
}
}
else
{
object = null;
}

GaryMcM's approach is correct. The code worked correctly for me when I set setOfObjects as instance of java.util.HashSet in interpreter namespace, as below.
Set<String> setOfObjects = new HashSet<String>();
i.set("setOfObjects", setOfObjects );(i being beanshell interpreter's instance)
few observations :
keyword Else should be else (beanshell doesn't tolerate it)
setOfObjects.get(index); will throw error as there is no get(int) in Sets
Are you sure the setOfObjects which you are providing is not null, or with no typo error, as beanshell will assume a variable with typo error as new one. (in non strict java mode).

Related

BoldReports - ReportHelper.GetParametersWithValues() returns parameter collection with empty values

BoldReports ReportViewer Controller:
I'm using the ReportHelper.GetParametersWithValues() function in the OnReportLoaded() method to retrieve the report parameters. It is successfully retrieving the list of parameters, however, only the name attributes are populated - the values are null.
private ReportParameterInfoCollection _rptParamColl;
public void OnReportLoaded(ReportViewerOptions reportOption)
{
_rptParamColl = ReportHelper.GetParametersWithValues(jsonArray, this, _cache);
if (_rptParamColl != null)
{
foreach (ReportParameterInfo rptParam in _rptParamColl)
{
if (rptParam.Name == "OrgID")
{
if (rptParam.Values != null )
{
// perform appropriate validation on rptParam.Values[0]
}
}
}
}
}
In the code sample above, rptParam.Name has a value, but the rptParam.Values is null. I know the values exist, as when I inspect the jsonArray object, they are in there (although oddly, after calling ReportHelper.GetParametersWithValues(), the jsonArray object is cleared? Same also happens when calling ReportHelper.GetDataSources(). This is also a problem, as I want to call both methods, and after calling one, cannot call the other... )
Any ideas what I may be doing wrong?
Using the PostReportAction method you can get the client-side parameter value. Please refer to the below code snippet,
public object PostReportAction([FromBody] Dictionary<string, object> jsonResult)
{
if (jsonResult.ContainsKey("parameters") && jsonResult["parameters"] != null)
{
var parameterValues = Newtonsoft.Json.JsonConvert.DeserializeObject<BoldReports.Web.ReportParameterInfoCollection>(jsonResult["parameters"].ToString());
}
return ReportHelper.ProcessReport(jsonResult, this, this._cache);
}

Callee taking action based on type of caller

I would like to know if there is some alternate better pattern other than below where an object passes itself as a context to another object which in turn uses the context or lack as logic to build the necessary output. I have tried making the caller/sender an interface and pass itself as an interface but the real question is is it good practice for the called object to even know who its caller is, i.e. to distinguish it from other callers and take separate actions if necessary? Am I missing a piece to the puzzle?
Caller (CGQuery):
public string RenderForLoop()
{
...
sb.Append(string.Join("",this.ContentIsCGExpressions.Select(exp => exp.GetProcessedExpression(this))));
...
Callee (CGExpression):
public string GetProcessedExpression(object context = null)
{
...
retv = ReplaceCodes(retv, context);
...
private string ReplaceCodes(string retv, object context = null)
{
... retv = ReplaceContextSequenceCode(retv, context);
return retv;
}
...
private string ReplaceContextSequenceCode(string retv, object context = null)
{
var _regx = new Regex("(?i)<q_ctx_seq>");
var _matchresult = _regx.Match(retv);
while (_matchresult.Success)
{
string replacement = FetchContextSequenceQueryTableFieldExpression(context);
retv = retv.Replace(_matchresult.Groups[0].Value, replacement);
_matchresult = _matchresult.NextMatch();
}
return retv;
}
private string FetchContextSequenceQueryTableFieldExpression(object context = null)
{
if (context != null && context is CGQuery)
{
return ((CGQuery)context).FetchContextSequenceQueryTableFieldExpression();
}
return this.CGStatementsUsedAsParamsFor.Any() ?
this.CGStatementsUsedAsParamsFor.Single().FetchContextSequenceQueryTableFieldExpression(context)
:
this.CGQueriesContentFor.Single().FetchContextSequenceQueryTableFieldExpression();
}
It's this line in the above that I'm concerned about (seems to violate encapsulation?)
if (context != null && context is CGQuery)
{
return ((CGQuery)context).FetchContextSequenceQueryTableFieldExpression();
}
sorry I don't know how to highlight the code in the main block.
What about letting the caller deliver an object to the callee containing the action to execute? The caller may be queried within this action without the callee being aware of its existence.
Generally I would say it's not good OOP practice to decide behaviour on the class of the calling object.

Pig - passing Databag to UDF constructor

I have a script which is loading some data about venues:
venues = LOAD 'venues_extended_2.csv' USING org.apache.pig.piggybank.storage.CSVLoader() AS (Name:chararray, Type:chararray, Latitude:double, Longitude:double, City:chararray, Country:chararray);
Then I want to create UDF which has a constructor that is accepting venues type.
So I tried to define this UDF like that:
DEFINE GenerateVenues org.gla.anton.udf.main.GenerateVenues(venues);
And here is the actual UDF:
public class GenerateVenues extends EvalFunc<Tuple> {
TupleFactory mTupleFactory = TupleFactory.getInstance();
BagFactory mBagFactory = BagFactory.getInstance();
private static final String ALLCHARS = "(.*)";
private ArrayList<String> venues;
private String regex;
public GenerateVenues(DataBag venuesBag) {
Iterator<Tuple> it = venuesBag.iterator();
venues = new ArrayList<String>((int) (venuesBag.size() + 1)); // possible fails!!!
String current = "";
regex = "";
while (it.hasNext()){
Tuple t = it.next();
try {
current = "(" + ALLCHARS + t.get(0) + ALLCHARS + ")";
venues.add((String) t.get(0));
} catch (ExecException e) {
throw new IllegalArgumentException("VenuesRegex: requires tuple with at least one value");
}
regex += current + (it.hasNext() ? "|" : "");
}
}
#Override
public Tuple exec(Tuple tuple) throws IOException {
// expect one string
if (tuple == null || tuple.size() != 2) {
throw new IllegalArgumentException(
"BagTupleExampleUDF: requires two input parameters.");
}
try {
String tweet = (String) tuple.get(0);
for (String venue: venues)
{
if (tweet.matches(ALLCHARS + venue + ALLCHARS))
{
Tuple output = mTupleFactory.newTuple(Collections.singletonList(venue));
return output;
}
}
return null;
} catch (Exception e) {
throw new IOException(
"BagTupleExampleUDF: caught exception processing input.", e);
}
}
}
When executed the script is firing error at the DEFINE part just before (venues);:
2013-12-19 04:28:06,072 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <file script.pig, line 6, column 60> mismatched input 'venues' expecting RIGHT_PAREN
Obviously I'm doing something wrong, can you help me out figuring out what's wrong.
Is it the UDF that cannot accept the venues relation as a parameter. Or the relation is not represented by DataBag like this public GenerateVenues(DataBag venuesBag)?
Thanks!
PS I'm using Pig version 0.11.1.1.3.0.0-107.
As #WinnieNicklaus already said, you can only pass strings to UDF constructors.
Having said that, the solution to your problem is using distributed cache, you need to override public List<String> getCacheFiles() to return a list of filenames that will be made available via distributed cache. With that, you can read the file as a local file and build your table.
The downside is that Pig has no initialization function, so you have to implement something like
private void init() {
if (!this.initialized) {
// read table
}
}
and then call that as the first thing from exec.
You can't use a relation as a parameter in a UDF constructor. Only strings can be passed as arguments, and if they are really of another type, you will have to parse them out in the constructor.

Generate parameter list with userdefined types at runtime (using C#)

As part of my project, I am trying to build a web UI where user will select a method and pass the values. My program should be able to call the method dynamically and build a parameter list on runtime to pass it to the method.
I have created a comma separated list (string) of key and value pairs. This key/value pair is nothing but the parameter name and value of my method (methodname stored in a variable). Example: string params = "ID:123;Name:Garry;Address:addressObject;AddressLine:108 Plaza Lane;City:Avenel;State:NJ;Zip:07001;". Where ID and Name are simple string varaibles while Address is user defined type. What follows after Address i.e. AddressLine, City, State and Zip is elements of Address object. And my method definition is
public string GetInfo(string ID, string Name, Address addressObject)
{
//return something;
}
I am dynamically calling the method (GetInfo) that is stored in sMethodName variable using DynamicProxy like :
string sMethodName = "GetInfo";
object result = (object) proxy.CallMethod(sMethodName, arguments);
Challenge is how to pass the argument list dynamically? Till now I am just able to extract the values from the csv variable into NamedValueCollection. Here is the code:
public static void StoreParameterValues(string param)
{
nvc = new NameValueCollection();
param = param.TrimEnd(';');
string[] parameters = param.Split(new char[] { ';' });
foreach (string val in parameters)
{
string[] keyValue = val.Split(new char[] { ':' });
nvc.Add(keyValue[0], keyValue[1]);
}
}
..and here is the code that tries to build the parameter:
string methodName = "GetInfo";
DynamicProxyFactory factory = new DynamicProxyFactory("http://../myservice.svc");
string sContract = "";
foreach (ServiceEndpoint endpoint in factory.Endpoints)
{
sContract = endpoint.Contract.Name;
}
DynamicProxy proxy = factory.CreateProxy(sContract);
string[] values = null;
// Create the parameter list
object[] arguments = new object[nvc.Count];
int i = -1;
foreach (string key in nvc.Keys)
{
values = nvc.GetValues(key);
foreach (string value in values)
{
arguments[++i] = value;
}
}
object result = (object) proxy.CallMethod(methodName, arguments);
The above code works if I have simple primitive types but not sure how can I build the logic for any other userdefined types. How can I create a object dynamically of type stored in a variable? Not sure if I was able to put my question correctly. I hope so :)
Edit: 01/19/2011: Applied the suggestion from Chris - using Reflection instead of ComponentModel.
I have converted the code to make it more generic. This works now for all primitive and custom types (resursion). Code snippet below:
private object BuildParameterList(Type type)
{
object item = new object();
item = Activator.CreateInstance(type);
PropertyInfo[] propArray = type.GetProperties(BindingFlags.Public|BindingFlags.Instance);
for (int i = 0; i < propArray.Length; i++)
{
PropertyInfo pi = (PropertyInfo)propArray[i];
////Check for custom type
if (IsCustomType(pi.PropertyType))
{
object item1 = BuildParameterList(pi.PropertyType);
pi.SetValue(item, item1, null);
}
else
{
if (property.ContainsKey(pi.Name))
{
pi.SetValue(item, Convert.ChangeType(property[pi.Name], pi.PropertyType), null);
}
}
}
return item;
}
But if one of the property is Color (I just tested with Color type, will fail with other system types aswell-i guess), then it fails at the following line. Not sure how to handle system types - Color or something similar.
pi.SetValue(item, Convert.ChangeType(property[pi.Name], pi.PropertyType), null);
Can you not find what types are expected by the method, by inspecting its ParameterInfos:
endpoint.Contract.ContractType.GetMethod(methodName).GetParameters();
and then instantiating the custom types using:
Activator.CreateInstance(parameterType);

Trouble defining method for Javascript class definition

I'm somewhat new to object oriented programming in Javascript and I'm trying to build a handler object and library for a list of items I get back from an API call. Ideally, I'd like the library functions to be members of the handler class. I'm having trouble getting my class method to work however. I defined as part of the class bcObject the method getModifiedDateTime, but when I try to echo the result of the objects call to this method, I get this error:
Error on line 44 position 26: Expected ';'
this.getModifiedDateTime: function(epochtime) {
which leads me to believe that I simply have a syntax issue with my method definition but I can't figure out where.
response(
{
"items":
[
{"id":711,"name":"Shuttle","lastModifiedDate":"1268426336727"},
{"id":754,"name":"Formula1","lastModifiedDate":"1270121717721"}
],
"extraListItemsAttr1":"blah",
"extraListItemsAttr2":"blah2"
});
function response(MyObject) {
bcObject = new bcObject(MyObject);
thing = bcObject.getModifiedDateTime(bcObject.videoItem[0].lastModifiedDate);
SOSE.Echo(thing);
}
function bcObject(listObject) {
// define class members
this.responseList = {};
this.videoCount = 0;
this.videoItem = [];
this.responseListError = "";
// instantiate members
this.responseList = listObject;
this.videoCount = listObject.items.length;
// populate videoItem array
for (i=0;i<this.videoCount;i++) {
this.videoItem[i] = listObject.items[i];
}
this.getModifiedDateTime: function(epochtime) {
var dateStringOutput = "";
var myDate = new Date(epochtime);
dateStringOutput = myDate.toLocaleString();
return dateStringOutput;
};
}
You use = to assign values in JS, not ::
this.getModifiedDateTime = function(epochtime) {
You should use the = operator for methods defined as you did there (this.<methodName> = function (...) {).
The colon notation is used when declaring object literals.