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.