Trouble with the MVEL Map syntax
by barnesjd
I'm interested in using the
http://mvel.codehaus.org/Inline+List,+Maps+and+Arrays compact syntax
available for Maps in MVEL , but I can't quite get it right. I am
attempting to write a simple function to convert English numbers (i.e.
"one", "two", etc) to the integer value. This is how I would LIKE it to
work:
/function int englishToInt(String englishNumber)
{
return ["one":1, "two":2, "three":3, "four":4,
"five":5].get(englishNumber.toLowerCase());
}/
The closest I've gotten to having it work is this:
/function int englishToInt(String englishNumber)
{
Map m = ["one":1, "two":2, "three":3, "four":4, "five":5];
Object obj = m.get(englishNumber.toLowerCase());
if(obj != null)
return Integer.parseInt(obj.toString());
}/
But I get this rather unhelpful error message:
/
Error importing : 'defaultpkg.EnglishToInt.englishToInt'
[ function englishToIntenglishToInt (line:7): Syntax error on tokens, delete
these tokens
]/
... What tokens?? lol
Thanks,
Joe
--
View this message in context: http://drools.46999.n3.nabble.com/Trouble-with-the-MVEL-Map-syntax-tp3844...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
Problem using BatchExecutionCommand - appears to process same command repeatedly
by Rhodes, Phil
Hey all, I'm working with Drools Expert 5.3.0 Final and have run across
a problem that has me pulling my hair out.
Basically, I'm creating a couple of Commands, adding them to a
BatchExecutionCommand and then executing that
command... but based on the output of the processing, it looks like the
same command was executed repeatedly, instead
of the Commands being iterated.
Here's what my rule file looks like:
package exp
import exp.DriversLicense3Main.Applicant;
import exp.DriversLicense3Main.Application;
rule "Is of valid age"
when
$app: Applicant( age < 18 )
$a : Application()
then
System.out.println( "matched one: " + $app.getName() );
$a.setValid( false );
end
And here's the driver that runs things:
public static void main(String[] args)
{
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
// this will parse and compile in one step
kbuilder.add( ResourceFactory.newClassPathResource(
"DriversLicense3.drl",
DriversLicense3Main.class ),
ResourceType.DRL );
// Check the builder for errors
if ( kbuilder.hasErrors() ) {
System.out.println( kbuilder.getErrors().toString() );
throw new RuntimeException( "Unable to compile
\"DriversLicense3.drl\"." );
}
// get the compiled packages (which are serializable)
final Collection<KnowledgePackage> pkgs = kbuilder
.getKnowledgePackages();
// add the packages to a knowledgebase (deploy the knowledge
packages).
final KnowledgeBase kbase =
KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( pkgs );
StatelessKnowledgeSession ksession =
kbase.newStatelessKnowledgeSession();
Applicant applicant1 = new Applicant( "Phillip", 38 );
Application app1 = new Application();
List l1 = new ArrayList();
l1.add( app1);
l1.add( applicant1);
Applicant applicant2 = new Applicant( "Joe", 17 );
Application app2 = new Application();
List l2 = new ArrayList();
l2.add( app2 );
l2.add( applicant2 );
List<Command> cmds = new ArrayList<Command>();
cmds.add( CommandFactory.newInsertElements( l1, "phil", true,
"phil" ) );
cmds.add( CommandFactory.newInsertElements( l2, "joe", true,
"joe" ) );
BatchExecutionCommand batch =
CommandFactory.newBatchExecution(cmds, "ksession1");
ExecutionResults results = ksession.execute( batch );
System.out.println( ((List<Applicant>)results.getValue( "phil"
)).get(1).getName() + ": " + ((List<Application>)results.getValue(
"phil" )).get(0).isValid() );
System.out.println( ((List<Applicant>)results.getValue( "joe"
)).get(1).getName() + ": " + ((List<Application>)results.getValue( "joe"
)).get(0).isValid() );
System.out.println( "done" );
}
Applicant and Application are just some data classes that look like
this:
public static class Applicant
{
private String name;
private int age;
public Applicant() {}
public Applicant( final String name, final int age )
{
this.name = name;
this.age = age;
}
// getter and setter methods here
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
public static class Application
{
private Date dateApplied;
private boolean valid = true;
// getter and setter methods here
public Date getDateApplied()
{
return dateApplied;
}
public void setDateApplied(Date dateApplied)
{
this.dateApplied = dateApplied;
}
public boolean isValid()
{
return valid;
}
public void setValid(boolean valid)
{
this.valid = valid;
}
}
So when I run this, I would expect to see the application "Joe" match
the rule and have the valid attribute on the Application set to false,
while
the "Phil" one should remain true. But when I run this, I get the out
(from my println command in the rule)
matched one: Joe
matched one: Joe
and when I retrieve the results by name, I find that both Applications
have had the valid field set to false.
Can anybody help point me in the right direction here? Am I doing
something wrong, or is this possibly a bug in Drools, or something else?
Thanks,
Phil
Visit us on the Web at mesirowfinancial.com
This communication may contain privileged and/or confidential information. It is intended solely for the use of the addressee. If you are not the intended recipient, you are strictly prohibited from disclosing, copying, distributing or using any of this information. If you received this communication in error, please contact the sender immediately and destroy the material in its entirety, whether electronic or hard copy. Confidential, proprietary or time-sensitive communications should not be transmitted via the Internet, as there can be no assurance of actual or timely delivery, receipt and/or confidentiality. This is not an offer, or solicitation of any offer to buy or sell any security, investment or other product.
14 years, 3 months
Cant check complex boolean expressions with Collections.disjoint
by aitchnyu
I have a `SubjectTeacherPeriod` object which has a property `strAttributeMap`
which is a map of `String` and `List<String>`. I want to check if
`HardConstraint.strAttributeValue` and
`SubjectTeacherPeriod.strAttributeMap[$attribute]` *intersect*. Here is the
rule:
rule "hardConflictIsIn"
when
$hc : HardConstraint(
$attribute : attribute,
operator == "is in",
$values : strAttributeValue,
$period : period
)
$stp : SubjectTeacherPeriod(
period == $period,
//ERROR IN FOLLOWING LINE
Collections.disjoint(strAttributeMap[$attribute], $values) ==
false
)
then
insertLogical(new IntConstraintOccurrence("hardConflictIsIn",
ConstraintType.NEGATIVE_HARD,1,
$hc,$stp));
end
But the rule (actually two of them) throws the error:
Exception in thread "main" java.lang.IllegalStateException: There are errors
in the scoreDrl's:
Unable to Analyse Expression
Collections.disjoint(strAttributeMap[$attribute], $values) == false:
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast
to java.lang.Class : [Rule name='hardConflictIsIn']
Unable to Analyse Expression
Collections.disjoint(strAttributeMap[$attribute], $values) == true:
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast
to java.lang.Class : [Rule name='hardConflictNotIn']
at
org.drools.planner.config.solver.SolverConfig.buildRuleBase(SolverConfig.java:238)
at
org.drools.planner.config.solver.SolverConfig.buildSolver(SolverConfig.java:170)
at
org.drools.planner.config.XmlSolverConfigurer.buildSolver(XmlSolverConfigurer.java:103)
at
in.co.technovia.timetabler.TimeTableApp.createSolver(TimeTableApp.java:61)
at in.co.technovia.timetabler.TimeTableApp.main(TimeTableApp.java:45)
I got it from an answer from my own question at
http://stackoverflow.com/a/9241089/604511 . Is this a bug in Drools itself?
--
View this message in context: http://drools.46999.n3.nabble.com/Cant-check-complex-boolean-expressions-...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
Re: [rules-users] Drools silently fails when I screw up
by ge0ffrey
barnesjd wrote
>
> Hi everyone. I'm new to Drools (using 5.2). I'm using it in Grails
> (2.0.1) with the drools-gorm plugin (0.5.6) (warning: Grails is also new
> for me). I've noticed that whenever I do something that is perhaps
> syntactically invalid, my rules simply don't fire. Given that I'm new to
> Drools, it would be nice if I could actually know what mistake I made.
>
> Are there some options I can use that will make Drools more vocal about my
> mistakes?
>
> Thanks!
> Joe
>
use knowledgebase.hasErrors()
--
View this message in context: http://drools.46999.n3.nabble.com/Drools-silently-fails-when-I-screw-up-t...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
Doubt when modifying facts on an action
by Zeta
Hi all,
my spreedsheet received 4 facts. I need all of them to check conditions but
not all of them are used in all rules. There are some rules that modify a
fact which is not used in any of the conditions and thus, it is not
"declared" on the left side of the rule.
Let me show you an example:
CONDITION CONDITION ACTION
Customer c Tariff t
name value
customer.setOffer($param)
RULE1 "pepe" 5 "lot of
offers"
RULE2 100 "lot of
offers"
When I try to compile this, I get a compilation error:
[ERROR] | 2012-03-14 17:21:30,238 |
es....ruleengine.impl.RuleEngineUpgradeImpl[RuleEngineUpgradeImpl.java:117]
| Rule Compilation error customer cannot be resolved
customer cannot be resolved
Any suggestion please?
Thanks,
Zoraida.-
--
View this message in context: http://drools.46999.n3.nabble.com/Doubt-when-modifying-facts-on-an-action...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
Knowing which constraints are broken for final best solution in drools-planner
by maraoz
Hi! I'm new to this forum so... my name is Manuel and I'm using drools
planner to solve an exam scheduling problem :)
I need to analyze which constraints are broken while the algorithm is
running and when the solver finishes also. Geoffrey pointed me to the
examples, where that is done.
I copied the getScoreDetailList() idea from that example, and it works
perfectly fine for when the algorithm is running (constraints are found and
I can inspect them). The basic idea is that this method obtains the working
memory and obtains from it the objects of type ConstraintOccurrence.
The thing is, when the solver finishes and I call this method, it returns no
constraints, even though I know by the score that some constraints are still
broken. Am I doing something wrong? Or is this the expected behaviour? It
seems as if working memory erases the ConstraintOccurrences when the solver
finishes, but I don't really know. In that case, how can I obtain the
constraints broken for the final solution?
Hope I made myself clear! Thanks!
--
View this message in context: http://drools.46999.n3.nabble.com/Knowing-which-constraints-are-broken-fo...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months