Search in sources :

Example 71 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class ScoringStrategiesTest method executeAndFetchScore.

/* Internal functions */
private double executeAndFetchScore(String sheetName) throws Exception {
    ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    InputStream inputStream = PMMLDocumentTest.class.getResourceAsStream("/scoremodel_scoring_strategies.xls");
    boolean compileResult = scorecardCompiler.compileFromExcel(inputStream, sheetName);
    if (!compileResult) {
        for (ScorecardError error : scorecardCompiler.getScorecardParseErrors()) {
            System.err.println("Scorecard Compiler Error :" + error.getErrorLocation() + "->" + error.getErrorMessage());
        }
        return -999999;
    }
    String drl = scorecardCompiler.getDRL();
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("scoremodel_scoring_strategies.drl").setResourceType(ResourceType.DRL));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    Results res = kieBuilder.buildAll().getResults();
    if (res.hasMessages(Message.Level.ERROR)) {
        System.out.println(res.getMessages());
    }
    assertEquals(0, res.getMessages(Message.Level.ERROR).size());
    KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    KieBase kbase = kieContainer.getKieBase();
    StatelessKieSession session = kbase.newStatelessKieSession();
    FactType scorecardType = kbase.getFactType("org.drools.scorecards.example", "SampleScore");
    Object scorecard = scorecardType.newInstance();
    scorecardType.set(scorecard, "age", 10);
    session.execute(scorecard);
    return (Double) scorecardType.get(scorecard, "scorecard__calculatedScore");
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InputStream(java.io.InputStream) KieServices(org.kie.api.KieServices) FactType(org.kie.api.definition.type.FactType) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 72 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class NonStringCompareTest method testScenario.

private void testScenario(final String factFieldValueForDrl, final String factFieldValueForTest) throws IllegalAccessException, InstantiationException {
    final KieBuilder kbuilder = build(factFieldValueForDrl);
    Assertions.assertThat(kbuilder.getResults().getMessages(Level.ERROR)).isEmpty();
    final KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder);
    final KieSession ksession = kbase.newKieSession();
    try {
        final FactType type = kbase.getFactType(TestConstants.PACKAGE_REGRESSION, "Fact");
        final Object fact = type.newInstance();
        type.set(fact, "field", factFieldValueForTest);
        ksession.insert(fact);
        final int count = ksession.fireAllRules();
        Assertions.assertThat(count).isEqualTo(1);
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType)

Example 73 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class NotInFusionTest method createNotEvent.

private Object createNotEvent(KieSession ksession, String property) throws Exception {
    FactType type = ksession.getKieBase().getFactType("org.drools.testcoverage.regression", "NotEvent");
    Object instance = type.newInstance();
    type.set(instance, "property", property);
    return instance;
}
Also used : FactType(org.kie.api.definition.type.FactType)

Example 74 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class QueryTest method checkRecursiveQuery.

private void checkRecursiveQuery(String query) throws InstantiationException, IllegalAccessException {
    String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + query + "declare Location\n" + "    thing : String\n" + "    location : String\n" + "end\n" + "// rule values at A11, header at A6\n" + "rule \"testPullQueryRule\" when\n" + "    String(this == \"pull\")\n" + "    Person($l : likes)\n" + "    ?isContainedIn($l, \"office\";)\n" + "then\n" + "end\n" + "\n" + "// rule values at A12, header at A6\n" + "rule \"testPushQueryRule\" when\n" + "    String(this == \"push\")\n" + "    Person($l : likes)\n" + "    isContainedIn($l, \"office\";)\n" + "then\n" + "end";
    KieSession ksession = getKieSession(str);
    FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final Person peter = new Person("Peter");
    peter.setLikes("steak");
    final Object steakLocation = locationType.newInstance();
    locationType.set(steakLocation, "thing", "steak");
    locationType.set(steakLocation, "location", "table");
    final Object tableLocation = locationType.newInstance();
    locationType.set(tableLocation, "thing", "table");
    locationType.set(tableLocation, "location", "office");
    ksession.insert(peter);
    final FactHandle steakHandle = ksession.insert(steakLocation);
    final FactHandle tableHandle = ksession.insert(tableLocation);
    ksession.insert("pull");
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
    listener.clear();
    // when location is changed of what Peter likes, pull query should
    // ignore it
    final Object steakLocation2 = locationType.newInstance();
    locationType.set(steakLocation2, "thing", "steak");
    locationType.set(steakLocation2, "location", "desk");
    final Object deskLocation = locationType.newInstance();
    locationType.set(deskLocation, "thing", "desk");
    locationType.set(deskLocation, "location", "office");
    ksession.insert(steakLocation2);
    ksession.insert(deskLocation);
    ksession.delete(steakHandle);
    ksession.delete(tableHandle);
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
    listener.clear();
    final Person paul = new Person("Paul");
    paul.setLikes("steak");
    ksession.insert(paul);
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
}
Also used : TrackingAgendaEventListener(org.drools.modelcompiler.util.TrackingAgendaEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) FactType(org.kie.api.definition.type.FactType)

Example 75 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class AdapterTest method testCustomInputAdapter.

@Test
public void testCustomInputAdapter() {
    String source = PMML4Helper.pmmlDefaultPackageName().replace(".", "/") + "/" + "mock_cold_adapter.xml";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newClassPathResource(source).setResourceType(ResourceType.PMML));
    Results res = ks.newKieBuilder(kfs).buildAll().getResults();
    if (res.hasMessages(Message.Level.ERROR)) {
        System.out.println(res.getMessages(Message.Level.ERROR));
    }
    assertEquals(0, res.getMessages(Message.Level.ERROR).size());
    KieBase kieBase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    FactType ft = kieBase.getFactType("test", "MyAdapter");
    assertTrue(ft != null);
    assertTrue(ft.getFactClass().isInterface());
    FactType fto = kieBase.getFactType("test", "MyOutAdapter");
    assertTrue(fto != null);
    assertTrue(fto.getFactClass().isInterface());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Aggregations

FactType (org.kie.api.definition.type.FactType)208 Test (org.junit.Test)193 KieSession (org.kie.api.runtime.KieSession)114 KieBase (org.kie.api.KieBase)63 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)45 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)31 ArrayList (java.util.ArrayList)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)27 KieServices (org.kie.api.KieServices)22 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)18 Resource (org.kie.api.io.Resource)17 KieBuilder (org.kie.api.builder.KieBuilder)16 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)16 KieFileSystem (org.kie.api.builder.KieFileSystem)15 KieContainer (org.kie.api.runtime.KieContainer)15 List (java.util.List)10 Results (org.kie.api.builder.Results)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)9 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9