You'll have to something like
FactType factType = kieContainer.getKieBase().getFactType(
packagename, "NumberCompare" );
Now you may
Object nc = factType.newInstance();
factType.set( nc, "numberone", Integer.valueOf( 42 ) );
...
kieSession.insert( nc );
Of course, declaring and instantiating a POJO causes much less
headscratching, so why don't you do that?
-W
On 08/12/2013, anjana.ackroyd <anjana.ackroyd(a)gmail.com> wrote:
Hi,
I am new to drools and trying to figure out how to get this simple example
working.
I have intellij IDE .
I am trying to compare two numbers using drools 6.x and dont know how to
insert numberone and numbertwo after I get my drl file into my file system
Here is my code
RuleRunner.java
package com.cambiahealth.enterprise.service.droolsrule;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.Message.Level;
import org.kie.api.io.KieResources;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class RuleRunner
{
private KieServices kieServices;
private KieContainer kieContainer;
private KieSession kieSession;
private KieResources kieResources;
private KieFileSystem kieFileSystem;
private KieRepository kieRepository;
public RuleRunner()
{
this.kieServices = KieServices.Factory.get();
this.kieResources = kieServices.getResources();
this.kieFileSystem = kieServices.newKieFileSystem();
this.kieRepository = kieServices.getRepository();
}
public void addRuleFile(String packagename, String rulefile)
{
Resource resource = kieResources.newClassPathResource(rulefile);
packagename = packagename.replace(".","/");
String resourcepath =
"src/main/resources/"+packagename+"/"+rulefile;
kieFileSystem.write(resourcepath, resource);
}
public KieSession buildKnowledgeSession()
{
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
if (kb.getResults().hasMessages(Level.ERROR))
{
throw new RuntimeException("Build Errors:\n" +
kb.getResults().toString());
}
kieContainer =
kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
kieSession = this.kieContainer.newKieSession();
return kieSession;
}
public KieSession fireAllRules()
{
kieSession.fireAllRules();
return kieSession;
}
public void dispose()
{
this.kieSession.dispose();
}
}
My numbercompare.drl
declare NumberCompare
message: String
numberone: Integer
numbertwo: Integer
end
rule "numberone is equals to numbertwo"
when
n: NumberCompare(numberone == numbertwo);
then
n.setMessage("numberone is equals to numbertwo");
end
rule "numberone is greater than numbertwo"
when
n: NumberCompare(numberone > numbertwo);
then
n.setMessage("numberone is greater than numbertwo");
end
rule "numberone is less than numbertwo"
when
n: NumberCompare(numberone < numbertwo);
then
n.setMessage("numberone is less than numbertwo");
end
My DroolsService.java where I want to pass in values 1 and 2 to the drl
file
runner.addRuleFile("drools", "compareNumberRule.drl");
KieSession kieSession = runner.buildKnowledgeSession();
//How do I pass 1 as numberone and 2 as numbertwo values ????? before
firerules
runner.fireAllRules();
runner.dispose();
return number.getMessage(); //To change body of implemented
methods
use File | Settings | File Templates.
}
}
--
View this message in context:
http://drools.46999.n3.nabble.com/How-to-insert-values-into-drl-tp4027151...
Sent from the Drools: User forum mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users