Search in sources :

Example 51 with RuleUnitExecutor

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());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 52 with RuleUnitExecutor

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));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 53 with RuleUnitExecutor

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));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 54 with RuleUnitExecutor

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;
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) KieRepository(org.kie.api.builder.KieRepository) Resource(org.kie.api.io.Resource) FileNotFoundException(java.io.FileNotFoundException) KieHelper(org.kie.internal.utils.KieHelper) KieServices(org.kie.api.KieServices) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieContainer(org.kie.api.runtime.KieContainer) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 55 with RuleUnitExecutor

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;
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) PMML4Data(org.kie.api.pmml.PMML4Data)

Aggregations

RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)64 Test (org.junit.Test)61 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)53 KieBase (org.kie.api.KieBase)39 KieHelper (org.kie.internal.utils.KieHelper)38 Person (org.drools.compiler.Person)33 PMML4Result (org.kie.api.pmml.PMML4Result)17 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)17 RuleUnit (org.kie.api.runtime.rule.RuleUnit)17 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)16 ArrayList (java.util.ArrayList)14 KieContainer (org.kie.api.runtime.KieContainer)9 KieServices (org.kie.api.KieServices)7 FactHandle (org.kie.api.runtime.rule.FactHandle)6 SegmentExecution (org.kie.pmml.pmml_4_2.model.mining.SegmentExecution)6 DataSource (org.kie.api.runtime.rule.DataSource)5 Arrays.asList (java.util.Arrays.asList)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 LongAddress (org.drools.compiler.LongAddress)4