Search in sources :

Example 26 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class NeuralNetworkTest method testOverride.

@Test
public void testOverride() throws Exception {
    setKSession(getModelSession(source3, VERBOSE));
    setKbase(getKSession().getKieBase());
    getKSession().fireAllRules();
    getKSession().getEntryPoint("in_Feat1").insert(2.2);
    getKSession().fireAllRules();
    getKSession().getEntryPoint("in_Feat2").insert(5);
    getKSession().fireAllRules();
    // System.err.println(reportWMObjects(getKSession()));
    FactType out1 = getKbase().getFactType("org.kie.pmml.pmml_4_2.test", "Out1");
    FactType out2 = getKbase().getFactType("org.kie.pmml.pmml_4_2.test", "Out2");
    FactType nump = getKbase().getFactType("org.kie.pmml.pmml_4_2.test", "Feat2");
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out1.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out2.getFactClass())).size());
    assertEquals(2, getKSession().getObjects(new ClassObjectFilter(nump.getFactClass())).size());
    getKSession().getEntryPoint("in_Feat1").insert(2.5);
    getKSession().getEntryPoint("in_Feat2").insert(6);
    getKSession().fireAllRules();
    // System.err.println(reportWMObjects(getKSession()));
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out1.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out2.getFactClass())).size());
    assertEquals(2, getKSession().getObjects(new ClassObjectFilter(nump.getFactClass())).size());
    checkGeneratedRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 27 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class TraitTest method testPreserveAllSetBitMask.

@Test
public void testPreserveAllSetBitMask() {
    // DROOLS-1699
    String drl = "package t.x;\n" + "" + "import " + Entity.class.getName() + "; " + "" + "declare trait MyThing end\n" + "" + "declare trait RootThing extends MyThing " + " objProp : java.util.List = new java.util.ArrayList() " + "end " + "" + "declare trait F extends RootThing end\n" + "" + "declare trait D extends RootThing end\n" + "" + "declare trait E extends D end\n" + "" + "rule Init when\n" + "then " + " Entity e1 = new Entity( \"X\" ); " + " insert( e1 ); " + " Entity e2 = new Entity( \"Y\" ); " + " insert( e2 ); " + " " + " D d1 = don( e1, D.class, true ); " + " F f2 = don( e2, F.class, true ); " + " " + " modify ( d1 ) { getObjProp().add( f2.getCore() ); } " + " modify ( f2.getCore() ) {} " + "end " + "" + "rule Rec no-loop when\n" + " MyThing( $x_0 := core, this isA D.class, $p : this#RootThing.objProp ) " + " exists MyThing( $x_1 := core , core memberOf $p, this isA F.class ) " + "then " + " System.out.println( \"Recognized \" + $x_0 + \" as an instance of E by rule \" + drools.getRule().getName() ); " + " don( $x_0, E.class, true ); " + "end " + "" + "rule Shed_2 when\n" + " $s : String( this == \"go2\") " + " $x : E( $objs : objProp ) " + " $y : F( $z : core memberOf $objs ) " + "then " + " retract( $s ); " + " System.out.println( \"SUCCESS : E has been recognized, removing Y \" ); " + " modify ( $x ) { getObjProp().remove( $z ); } " + " modify ( $y ) {} " + "end ";
    KieHelper helper = new KieHelper();
    KieBase kieBase = helper.addContent(drl, ResourceType.DRL).getKieContainer().getKieBase();
    KieSession kSession = kieBase.newKieSession();
    kSession.fireAllRules();
    kSession.insert("go2");
    kSession.fireAllRules();
    for (Object o : kSession.getObjects(new ClassObjectFilter(Entity.class))) {
        Entity e = (Entity) o;
        if (e.getId().equals("X")) {
            assertTrue(e.hasTrait("t.x.D"));
            assertFalse(e.hasTrait("t.x.E"));
            assertFalse(e.hasTrait("t.x.F"));
            assertEquals(0, ((List) e._getDynamicProperties().get("objProp")).size());
        } else if (e.getId().equals("Y")) {
            assertTrue(e.hasTrait("t.x.F"));
            assertFalse(e.hasTrait("t.x.D"));
            assertFalse(e.hasTrait("t.x.E"));
        } else {
            fail("Unrecognized entity in WM");
        }
    }
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 28 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class ExtendsTest method testExtends.

@Test
public void testExtends() throws Exception {
    // Test Base Fact Type
    KieSession ksession = genSession("test_Extends.drl");
    FactType person = ksession.getKieBase().getFactType("defaultpkg", "Person");
    FactType eqPair = ksession.getKieBase().getFactType("defaultpkg", "EqualityPair");
    Object p = person.newInstance();
    assertNotNull(p);
    ksession.insert("Populate");
    ksession.fireAllRules();
    // A Rule will generate 3 Persons, one with default constructor, two with field constructor
    // and 3 Students, which extends Person. One with default, one with field constructor
    assertEquals(6, ksession.getObjects(new ClassObjectFilter(person.getFactClass())).size());
    assertEquals(1 + 4 + 1 + 4, ksession.getObjects(new ClassObjectFilter(eqPair.getFactClass())).size());
    ksession.dispose();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 29 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class DroolsAbstractPMMLTest method getDoubleFieldValue.

public Double getDoubleFieldValue(FactType type) {
    Class<?> klass = type.getFactClass();
    Iterator iter = getKSession().getObjects(new ClassObjectFilter(klass)).iterator();
    Object obj = iter.next();
    return (Double) type.get(obj, "value");
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Iterator(java.util.Iterator)

Example 30 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class DroolsAbstractPMMLTest method checkFirstDataFieldOfTypeStatus.

protected void checkFirstDataFieldOfTypeStatus(FactType type, boolean valid, boolean missing, String ctx, Object... target) {
    Class<?> klass = type.getFactClass();
    Iterator iter = getKSession().getObjects(new ClassObjectFilter(klass)).iterator();
    assertTrue(iter.hasNext());
    Object obj = iter.next();
    if (ctx == null) {
        while (type.get(obj, "context") != null && iter.hasNext()) obj = iter.next();
    } else {
        while ((!ctx.equals(type.get(obj, "context"))) && iter.hasNext()) obj = iter.next();
    }
    Object tgt = type.get(obj, "value");
    if (tgt instanceof Double) {
        assert (target[0] instanceof Double);
        assertEquals((Double) target[0], (Double) tgt, 1e-6);
    } else {
        assertEquals(target[0], tgt);
    }
    assertEquals(valid, type.get(obj, "valid"));
    assertEquals(missing, type.get(obj, "missing"));
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Iterator(java.util.Iterator)

Aggregations

ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)51 Test (org.junit.Test)44 KieSession (org.kie.api.runtime.KieSession)24 FactType (org.kie.api.definition.type.FactType)16 Verifier (org.drools.verifier.Verifier)12 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)12 Subsumption (org.drools.verifier.report.components.Subsumption)11 FactHandle (org.kie.api.runtime.rule.FactHandle)11 KieBase (org.kie.api.KieBase)10 ArrayList (java.util.ArrayList)9 Collection (java.util.Collection)9 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)9 KieServices (org.kie.api.KieServices)9 Iterator (java.util.Iterator)7 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)7 Map (java.util.Map)5 KieBuilder (org.kie.api.builder.KieBuilder)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5 KieContainer (org.kie.api.runtime.KieContainer)5 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)5