A SubjectTeacherPeriod has a num_attribute_map, which is a map that maps certain attributes (such as "boringness") with their respective scores. I use the following code to sum attributes (such as "boringness") over each day of the week.
But a certain line causes an error.
rule "insertAttributeDayTotal"
//salience 1 // Do these rules first (optional, for performance)
when
$sum_regression_constraint : SumRegressionConstraint(
$class : class_,
$attribute : attribute//,
//$weight : weight;
)
$day_of_week : DayOfWeek()
$attribute_day_total : Number() from accumulate(
SubjectTeacherPeriod(
//period != null,
period.class_ == $class,
period.dayOfWeek == $day_of_week,
$total : num_attribute_map[$attribute] //PROBLEM LINE
),
sum($total)
)
then
//System.out.println("BUCKET TOTAL "+$id+" "+$bucket_total.intValue());
insertLogical(new AttributeDaySum($class, $attribute, $day_of_week, $attribute_day_total.intValue()));
end
The error is:
jesvin#Jesvin-Technovia:~/dev/drools/timetabler$ java -server in.co.technovia.timetabler.TimeTableApp
Exception in thread "main" java.lang.IllegalStateException: There are errors in the scoreDrl's:
Variables can not be used inside bindings. Variable [$attribute] is being used in binding 'num_attribute_map[$attribute]' : [Rule name='insertAttributeDayTotal']
Rule Compilation error : [Rule name='insertAttributeDayTotal']
in/co/technovia/timetabler/domain/Rule_insertAttributeDayTotal_bb39fd28b3c8457cb8d86fc15b34a0e7.java (7:905) : Syntax error on token "null", invalid Type
in/co/technovia/timetabler/domain/Rule_insertAttributeDayTotal_bb39fd28b3c8457cb8d86fc15b34a0e7.java (9:1050) : $total cannot be resolved
SubjectTeacherPeriod has the curious num_attribute_map so that I can define attributes at runtime. If I wanted a boringness (int) attribute for SubjectTeacherPeriod, I can do num_attribute_map.put("boringness",1) instead of adding a new attribute to SubjectTeacherPeriod.
A SumRegressionConstraint cares about a particular $attribute. That attribute's value is stored in num_attribute_map of SubjectTeacherPeriod. I want to access num_attribute_map[$attribute] but this problem shows up.
What am I doing wrong?
Is there any other way to get dynamic attributes to work?
At the moment, you can't bind a variable to expressions, only to field names. So instead of binding it to:
$total : num_attribute_map[$attribute]
Bind it to:
$total : num_attribute_map
Then, you can use the expression on the function. If you are using the MVEL dialect:
sum( $total[$attribute] )
Or if you are using the java dialect:
sum( $total.get($attribute) )
Related
This my When condition
$cla : cashliquidassets(
entity == "AU001",
asset_liability_indicator == "A",
product_group.contains('Loans','Bankofindia'),
product.contains("DS"),
counterparty_resident_indicator == "AU",
counterparty_type.contains("DS"),
related_entity == "Y"
)
I am getting an error at
Unable to Analyse Expression product_group.contains("Loans","Bankofindia")
Any Suggestions
Without your class model is hard to tell, but assuming that cashliquidassets.product_group is a String, you are trying to use an unexisting method String.contains(String, String).
One option would be to use an OR or and AND between 2 contains:
...
(product_group.contains('Loans') || product_group.contains('Bankofindia')),
...
or
...
product_group.contains('Loans'),
product_group.contains('Bankofindia'),
...
Another option could be to use the matches operator with a regular expression.
Hope it helps,
I am just curious as to why my Eclipse Drools compiler (6.5.0) requires semi-colons at the end of statements in the For loop, as below:
Map businessRulesRequest = $root.containsKey("BusinessRulesRequest") ? $root.get("BusinessRulesRequest") : null
Map quoteRequest = businessRulesRequest!=null && businessRulesRequest.containsKey("QuoteRequest") ? businessRulesRequest.get("QuoteRequest") : null
List resultsByKey = quoteRequest!=null && quoteRequest.containsKey("resultsByKey") ? quoteRequest.get("resultsByKey") : new ArrayList()
for (Map search : resultsByKey) {
Map searchInfo = (search.containsKey("searchInfo") ? search.get("searchInfo") : null);
String searchName = searchInfo!=null && searchInfo.containsKey("searchName") ? searchInfo.get("searchName").toString() : "";
List results = (searchName=="quotesMotor" && search.containsKey("results") ? search.get("results") : new ArrayList());
}
If I remove the semi-colons from the first or second lines in the For loop, I get an "unexpected token" error, but not if I remove it from the last line in the loop.
Is it due to Drools evaluating RHS lines as a single statement and so they must be separated inside any loops?
Note: I understand it is not best practice to code assuming semi-colons are not required, however I came across this issue while experimenting and am just interested to know the reason for the compiler error. Thanks.
I guess the answer is because of MVEL itself. Drools may be delegating the entire chunk of code to MVEL to evaluate and execute.
According to this guide, in MVEL the use of a semi-colon is not mandatory in cases where you have 1 statement, or in the last statement of a script.
Hope it helps,
Consider the following scenario in Drools:
We have a rule, matching objects of type A and B against each other.
rule 1
when
$a : A()
$b : B($a.matches($b), flagged == false)
then
mofidy($b) { flag($a) }
end
However, A objects have a field called priority, and when a B arrives in the working memory, a matching A with the highest priority should flag it, above all other matching As.
How is this possible in Drools? How does it affect performance?
Assuming that B.flag() is setting its flagged attribute to true, you can try something like this:
rule 1
when
$a : A()
not A(this != $a, priority > $a.priority)
$b : B($a.matches($b), flagged == false)
then
mofidy($b) { flag($a) }
end
One thing to notice in this example is that if an A object with a high priority is inserted, any B that was already flagged with a lower A will not be reflagged.
If you need to reflag yours Bs, then you can try something like this:
rule 1
when
$a : A()
not A(this != $a, priority > $a.priority)
$b : B($a.matches($b), flag != $a)
then
mofidy($b) { flag($a) }
end
Hope it helps,
I am new to WinBUGS/OpenBUGS and having difficulty de-bugging my code. The error message is "expected variable name". However I could not find any variable, which is not defined. My code is as follows:
model {
y[1:3]~dmulti(p[1:3],M)
p[1:3]~ddirch(alpha[])
}
list (
y=c(383465, 467074, 142852), M=993391
)
I have found the errors as follows:
1.The space can't follow the "list". Therefore, it should be
list(
y=c(383465, 467074, 142852), M=993391
)
2.The full codes should add the definition for alpha[] as follows:
model {
y[1:3]~dmulti(p[1:3],M)
p[1:3]~ddirch(alpha[])
for (r in 1:3){alpha[r]<-1}
}
list(
y=c(383465, 467074, 142852), M=993391
)
This seems a small problem, which occurs in the newcomers!!
When calling a macro I have the following error in response:
code=SUB_ASSIGN
Message = Assignment failed
Location= Reduce at line 65 of file mymacro.zms
the line 65 is
/** Reduce */
var b = gda(GDA).reduce {
table: 'myTable',
start: __parameters.key + '##',
stop: __parameters.key + '##a',
page: {
pageNumber: 0,
pageSize: 100000000
},
initialValue: {
tR: tR,
count: 0
},
'columns': ['col1', 'col2'],
'function': usr:myfunc
};
Seen on Android SDK (not on JS SDK) and not 100% reproducible.
What's the meaning of this error and how can I correct it?
Thanks.
The (poorly documented) meaning of SUB_ASSIGN is that an assignment with a syntax like a.b.c = d; has failed.
More precisely, the left hand operand is made of several hierarchical sub-parts (in my example a, b, and c).
Note that SUB_ASSIGN reports a programming error, which you should guard against :
when assigning a.b.c, at least a.b must exist and be not null.
A code pattern such as this one should do the trick:
// the ?? operator returns true when an expression is defined and not null
if (! a.b ??) {
a.b = {};
}
...
a.b.c = 0;
or, shorter, if applicable :
if (! a.b ??) {
a.b = {c:0};
}
The relevant documentation for '??' can be found in the official API reference documentation
As your code does not include such a statement anyway, I suppose that the actual error does not lie in your reduce call, but inside your callback function (usr:myfunc).
Moreover, to ease further debugging :
the error report you gave does not seem to contain the full stack trace up to the callback of the 'reduce' call. This might very well be a missing/not-yet-implemented feature of the zetapush API (which you could request...).