Populating the list in g.select - select

I need to programmatically load a list.
Instead of:
<g:select
name="cars"
from="${Car.list()}"
value="${person?.cars*.id}"
optionKey="id"
multiple="true" />
I would like to do it this because, the list is not always coming from the same source
g.select(name : searchfield.fieldName,
class : "fillWidth searchfield",
multiple : "true",
from : ${ searchfield.fieldFrom },
optionKey : searchfield.fieldKey,
optionValue : searchfield.fieldValue)
The from does not load. with the list, I get an error message:
No signature of method: sample.SearchTagLib.$() is applicable for argument types: (sample.SearchTagLib$_getSelectField_closure5) values: [sample.SearchTagLib$_getSelectField_closure5#1187b50] Possible solutions: is(java.lang.Object), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure), wait(), grep()

You don't need the ${} in the from option
g.select(name : searchfield.fieldName,
class : "fillWidth searchfield",
multiple : "true",
from : searchfield.fieldFrom,
optionKey : searchfield.fieldKey,
optionValue : searchfield.fieldValue)
In Groovy code ${} is a way to put Groovy expressions inside double quoted GStrings, if you're not in a GString you can just use the expression directly without wrapping it in ${}.
Edit from your comment
The fieldFrom at this point is a string which would get its value from a database. So the value in the DB is "Car.list()" which in the prototype I need to convert to a bound able or execute-able line of code.
It's not generally recommended to allow your app to execute arbitrary snippets of Groovy code provided by users (for obvious security reasons). As long as the code snippets come from a secure source such as a trusted admin user then fair enough, it is possible using GroovyShell
def from = new GroovyShell().evaluate(searchfield.fieldFrom)
but this is likely to be rather inefficient, creating a new classloader and parsing and compiling a whole Groovy script class every time. If the fieldFrom values are intended to always be pulling something from the database (i.e. they'll always be something like Car.list() or Vehicle.findAllByNumberOfWheelsGreaterThan(2), rather than arbitrary Groovy like [1,2,3]) then it might be better to store HQL expressions in fieldFrom and run them using executeQuery
def from = AnyDomainClass.executeQuery(searchfield.fieldFrom)
(executeQuery is a static GORM method, you need to call it on a specific domain class but it can return results of any type). The HQL equivalent of Car.list() would be "from Car", the equivalent of Vehicle.findAllByNumberOfWheelsGreaterThan(2) would be "from Vehicle where numberOfWheels > 2", etc.

I think you need to use strings as the attribute name:
g.select('name' : searchfield.fieldName,
'class' : "fillWidth searchfield",
'multiple' : "true",
'from' : ${ searchfield.fieldFrom },
'optionKey' : searchfield.fieldKey,
'optionValue' : searchfield.fieldValue)

Related

Drools : how to assign value to variable "from" predefined method when using PackageDescr to generate rule?

How can I generate something like this in my rule using PackageDescr ?
$var: Number (doubleValue > 100 ) from myPredefinedFunction()
I tried the following :
PatternDescr pt = new PatternDescr("Number","$var");
RelationalExprDescr ex = new RelationalExprDescr(">", false, null, new ExprConstraintDescr("myPredefinedFunction()"), new ExprConstraintDescr("100"));
pt.addConstraint(ex);
but this is what I get :
$var : Number( myPredefinedFunction() > 100 )
You're trying to set the myPredefinedFuntion() as a constraint. Constraints are the part of the drools declaration between the parentheses, eg. MyObject( foo == "bar" ) ... the foo == "bar" is a constraint.
Instead you need to set the source using the setSource method. This is the 'from' part of the declaration. This method takes a instance of a PatternSourceDescr subclass -- likely a FromDescr for this particular scenario.
(Alternatively, you might need setResource instead of setSource. The problem with using internal-only APIs is that they're not documented and subject to change without notice. I strongly suggest not going down this route.)

Vue Conditional Class Binding

I am trying to dynamically render class based off actionTypeCreate. This is a method that simply returns a boolean value based off the prop actionType that is passed. I am triggering this method on the mounted hook and confirmed it is returning properly.
Now I am trying to return the class value of 'col-md-4' if actionTypeCreate. If not actionTypeCreate I want to return the class 'col-md-6'.
This is what I have but it is not working:
:class="{toggleActionType : 'col-md-4' ? 'col-md-6'}"
I tried to reference this existing question, but I did not get it.
You can do it as follows:
:class="{'col-md-4' : toggleActionType , 'col-md-6' : !toggleActionType }"
According to the Vue documentation itself you can do it in two ways. First, you can use Array Syntax and this is broadly used to apply a list of classes.
Array Syntax
:class="[toggleActionType ? 'col-md-4' : 'col-md-6']"
Or you can do it as normal by Object Syntax but it does not accept ternary operations, so you have to do it this way:
Object Syntax
:class="{'col-md-4' : toggleActionType , 'col-md-6' : !toggleActionType}"
Try this:
:class="[toggleActionType : 'col-md-4' ? 'col-md-6']"

get an element index in ocl collection ? [ocl_Eclipse]

i'm working on the following domain :
my domain diagram
i want to express the folowing constraint :
" a succession of two actions of Type Rotate is not allowed "
i tried this declaration but eclipse is not recognizing indexOf(element ) :
class Choreography
{
property actions : Action[+|1] { ordered composes };
attribute name : String[?];
/*Succession of two actions of Type is not permitted */
invariant rotate_succ:
self.actions->asSequence()->forAll(a1:Action,a2:Action
|
a1.oclIsTypeOf(Rotate) and (indexOf(a1)=indexOf(a2)+1) implies
not a2.oclIsTypeOf(Rotate)
)
;
Does anyone have an idea about how to work with the index of a random element from an ocl colletion ?
The
OrderedCollection(T):indexOf(obj : OclAny[?]) : Integer[1]
operation requires an OrderedCollection (Sequence/OrderedSet) as its source and an OclAny as its argument. You have not identified a source so OCL will consider first an implicit iterator leading to an
a1.indexOf(a1)
a2.indexOf(a1)
ambiguity which would be an error if an Action had an indexOf operation. Then it considers an implicit self which also fails since there is no Choreography.indexOf() operation.
I presume you meant
self.actions->indexOf(a1)
etc etc, or more readably put self.actions in a let-variable for multi-use.
(Use of oclIsTypeOf is very rarely right. Use oclIsKindOf unless you have a specific intent.)
(self.actions is already an OrderedCollection so there is no need for the asSequence()).
Your use of indexOf will lead to quadratic performance. Better to use indexes - something like:
let theActions = self.actions in
Sequence{1..theActions->size()-1}->forAll(index |
theActions->at(index).oclIsKindOf(Rotate)
implies not theActions->at(index+1).oclIsKindOf(Rotate))

Is there any way of using 'in' keyword in 'eval' function in drl file of drools

I want use the 'in' keyword in 'eval' function in which I am getting error that 'in' is not recognized by drools. So I have multiple values which I want check against a particular fact's attribute
when
$person : Person(PIN in ("123","456","789"))
then
//do something
end
//Like this I want use it in eval
when
$person : Person()
eval($person.PIN in ("123","456","789"))
then
//do something
end
But it is showing compile time error.
is there any other way to do it.
Edited
So I have some conditions in Decision Table where I want to use eval because other ways are not helpful in my scenario, below snapshot will Explain
SnapShot 1: Decision Table without eval()
SnapShot 2: Decision Table with eval()
Issue in first snapshot:
When compiling the spreadsheet the condition goes to the second lines object like below code : this is how it gets interpreted
when
personMap : PersonMap ()
basicEligiblePerson : Person( personalAddress.PIN in ($param) ) from
personMap.AddressesList
addresses : Address() from basicEligiblePerson.AddressesList
personalAddress : PersonalAddress() from addresses.PersonalAddress
then
basicEligiblePerson.setEligibility(true);
end
Issue in second snapshot :
When compiling this spreadsheet the condition goes to eval() function but 'in' keyword does not work in eval().
when
personMap : PersonMap ()
basicEligiblePerson : Person( personalAddress.PIN in ($param) ) from
personMap.AddressesList
addresses : Address() from basicEligiblePerson.AddressesList
personalAddress : PersonalAddress() from addresses.PersonalAddress
eval( personalAddress.PIN in ($param) )
then
basicEligiblePerson.setEligibility(true);
end
what should I do?
First sample given in your question is sufficient for the validation. You don't need to use eval.
when
$person : Person(PIN in ("123","456","789"))
then
//do something
end
If your requirement is to set eligibility to true for a given set of PINs, then you don't really need a decision table. I don't completely understand your POJO structure, so if Person class has a member variable addressList and AddressList class has a member personalAddress which has the member variable pin, you can achieve the results using the following rule. Please note that the nested fields are referred using the member variable names, not the class names. Also when you access the nested elements, if any of the elements can be null, please add the null check as well to avoid null pointer exceptions.
when
$basicEligiblePerson : Person( addressesList.personalAddress.pin in ("1234", "4567") )
then
$basicEligiblePerson.setEligibility(true);
end

Haxe: Creating a code completion macro for JSON files

I am trying to implement a code completion macro for JSON files by generating field definitions
var classOfChild = Type.getClass(child); // `child` may look like this array: [[1,2,3],[0.1,0.2,0.3]]
fields.push({
"name": childName,
"pos" : _pos,
"kind": childType,
});
I already watched some videos and read some tutorials on this, but there is no information how to get ComplexType from a Type.typeof(object) result.
I have tried code that doesn't work like:
//"kind": childType,
//"kind": childType.toString(),
//"kind": FVar(macro {childType.toString(); }),
//"kind": FVar(macro Array<$v{arrType}>)
but none of them worked (all of them raise some Error or )
Edit 1: here is my json data:
{"floatVar1":0.1, "str":"some string", "nullValueObject":null, "arrayOfInts":[11,20,9], "matrixLikeArray":[[14, 12, 13, 11, 18]], "floatMatrix":[[14.4, 12.3, 13.7, 11.9, 18.0]], "symbolPayouts":[0.05], "objectInObject":{"prop1":"some str", "prop2": "some str2", "prop3":10.17, "prop4":[[1,2,3],[19.3,20.4]]}}
I would like to create Definitions for prop4, ("prop4":[[1,2,3],[19.3,20.4]])
Edit 2: I have already figured out how to create the "kind" for the simple types ("kind": FVar(macro:Dynamic)) and the object (kind : FVar(TAnonymous( jsonFields ))). But how to do that for arrays, arrays of arrays, etc.
Edit 3: code in gist.github.com
When generating code with macros it is easy to attempt to generate something which isn't efficient and you did get side tracked by trying to define explicit types.
Based on your gist, you just need to define a json field and give it the content of your JSON file as the field's value as if you were defining the value as a Haxe literal object.
Your goal is then to generate something you could have written as:
private var json = { prop1:'hello', prop2:42, prop3:[1,2,3] };
The haxe compiler will strongly type this json field.
To achieve that, your macro just need to add one field with an initial value obtained from the JSON file; and likewise, the Haxe compiler will strictly type it.
Creating a variable with a type to be inferred by the compiler is simply FVar(null, valueExpr), which means your entire macro can be reduced to:
var fields = Context.getBuildFields();
var json = Json.parse(src);
fields.push({
name : "json",
pos : Context.currentPos(),
kind : FVar(null, macro $v{json}),
access: [APrivate],
});
return fields;
For a more elaborated version I can point you on the following gist: ResourceGenerator.hx which will generate recursively "inlinable" and dce-friendly objects.
PS: sadly your "prop4":[[1,2,3],[19.3,20.4]] is impossible because it will be seen by the compiler as an Array of incompatible types ([Array<Int>, Array<Float>]).