use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testGuardedUnit.
@Test
public void testGuardedUnit() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(BoxOfficeUnit.class) + ";\n" + "import " + BoxOffice.class.getCanonicalName() + "\n" + "import " + TicketIssuerUnit.class.getCanonicalName() + "\n" + "\n" + "rule BoxOfficeIsOpen when\n" + " $box: /boxOffices[ open ]\n" + "then\n" + " drools.guard( TicketIssuerUnit.class );" + "end";
String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(TicketIssuerUnit.class) + ";\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultTicket.class.getCanonicalName() + "\n" + "rule IssueAdultTicket when\n" + " $p: /persons[ age >= 18 ]\n" + "then\n" + " tickets.insert(new AdultTicket($p));\n" + "end\n" + "rule RegisterAdultTicket when\n" + " $t: /tickets\n" + "then\n" + " results.add( $t.getPerson().getName() );\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Person> persons = executor.newDataSource("persons");
DataSource<BoxOffice> boxOffices = executor.newDataSource("boxOffices");
DataSource<AdultTicket> tickets = executor.newDataSource("tickets");
List<String> list = new ArrayList<>();
executor.bindVariable("results", list);
// two open box offices
BoxOffice office1 = new BoxOffice(true);
FactHandle officeFH1 = boxOffices.insert(office1);
BoxOffice office2 = new BoxOffice(true);
FactHandle officeFH2 = boxOffices.insert(office2);
persons.insert(new Person("Mario", 40));
// fire BoxOfficeIsOpen -> run TicketIssuerUnit -> fire RegisterAdultTicket
executor.run(BoxOfficeUnit.class);
assertEquals(1, list.size());
assertEquals("Mario", list.get(0));
list.clear();
persons.insert(new Person("Matteo", 30));
// fire RegisterAdultTicket
executor.run(BoxOfficeUnit.class);
assertEquals(1, list.size());
assertEquals("Matteo", list.get(0));
list.clear();
// close one box office, the other is still open
office1.setOpen(false);
boxOffices.update(officeFH1, office1);
persons.insert(new Person("Mark", 35));
executor.run(BoxOfficeUnit.class);
assertEquals(1, list.size());
assertEquals("Mark", list.get(0));
list.clear();
// all box offices, are now closed
office2.setOpen(false);
// guarding rule no longer true
boxOffices.update(officeFH2, office2);
persons.insert(new Person("Edson", 35));
// no fire
executor.run(BoxOfficeUnit.class);
assertEquals(0, list.size());
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testWithOOPathOnArray.
@Test
public void testWithOOPathOnArray() throws Exception {
// DROOLS-1646
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithArray.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + " $p : /persons[age >= adultAge]\n" + "then\n" + " System.out.println($p.getName() + \" is adult\");\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
RuleUnit adultUnit = new AdultUnitWithArray(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
assertEquals(2, executor.run(adultUnit));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class RuleUnitTest method testVarResolution.
@Test
public void testVarResolution() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " $p : /persons[age >= adultAge]\n" + "then\n" + " System.out.println($p.getName() + \" is adult and greater than \" + adultAge);\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
executor.bindVariable("adultAge", 18);
assertEquals(2, executor.run(AdultUnit.class));
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class DroolsAbstractPMMLTest method createExecutor.
protected RuleUnitExecutor createExecutor(String sourceName) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
ReleaseId releaseId = new ReleaseIdImpl("org.kie:pmmlTest:1.0-SNAPSHOT");
((KieRepositoryImpl) kr).setDefaultGAV(releaseId);
Resource res = ResourceFactory.newClassPathResource(sourceName);
kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
assertNotNull(kbase);
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
KieContainer kc = ((KnowledgeBaseImpl) ((InternalRuleUnitExecutor) executor).getKieSession().getKieBase()).getKieContainer();
InternalKieModule ikm = (InternalKieModule) kr.getKieModule(releaseId);
try (FileOutputStream fos = new FileOutputStream("/tmp/outputModule.jar")) {
fos.write(ikm.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
kc.getKieBaseNames().forEach(n -> {
System.out.println(n);
});
data = executor.newDataSource("request");
resultData = executor.newDataSource("results");
pmmlData = executor.newDataSource("pmmlData");
return executor;
}
use of org.kie.api.runtime.rule.RuleUnitExecutor in project drools by kiegroup.
the class PMMLExecutor method run.
private PMML4Result run(PMMLRequestData requestData, Optional<PMML4Data> data) {
KieRuntimeLogger logger = null;
RuleUnitExecutor ruleUnitExecutor = RuleUnitExecutor.create().bind(kieBase);
if (runWithLogging) {
if (loggingFileName != null) {
logger = ((InternalRuleUnitExecutor) ruleUnitExecutor).addFileLogger(loggingFileName);
} else {
logger = ((InternalRuleUnitExecutor) ruleUnitExecutor).addConsoleLogger();
}
}
DataSource<PMMLRequestData> requestDataSource = ruleUnitExecutor.newDataSource("request");
;
DataSource<PMML4Result> resultDataSource = ruleUnitExecutor.newDataSource("results");
DataSource<PMML4Data> pmmlDataSource = ruleUnitExecutor.newDataSource("pmmlData");
requestDataSource.insert(requestData);
PMML4Result resultHandler = new PMML4Result();
resultDataSource.insert(resultHandler);
if (data.isPresent()) {
pmmlDataSource.insert(data.get());
}
try {
ruleUnitExecutor.run(startingRuleUnit("RuleUnitIndicator", requestData.getModelName(), "org.drools.scorecards.example"));
} finally {
if (logger != null) {
logger.close();
}
}
return resultHandler;
}
Aggregations