Search in sources :

Example 6 with Entity

use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.

the class TraitTest method testWrapperEmpty.

@Test(timeout = 10000)
public void testWrapperEmpty() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactory.setMode(mode, kb);
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = studentClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "name", "john");
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "name", null);
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "age", 32);
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "age", null);
        assertFalse(wrapper.isEmpty());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        TraitableBean ind = new Entity();
        FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = RoleClass.getFactClass();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait2);
        Map<String, Object> wrapper2 = proxy2.getFields();
        assertTrue(wrapper2.isEmpty());
        proxy2.getFields().put("name", "john");
        assertFalse(wrapper2.isEmpty());
        proxy2.getFields().put("name", null);
        assertFalse(wrapper2.isEmpty());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Entity(org.drools.core.factmodel.traits.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with Entity

use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.

the class TraitTest method testAlphaNodeSharing.

@Test
public void testAlphaNodeSharing() {
    String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + "    name : String\n" + "end\n" + "rule Init " + "when " + "then " + "    don( new Entity(), Person.class ); " + "end\n" + "rule One when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "\n";
    final KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession kSession = kbase.newKieSession();
    assertEquals(3, kSession.fireAllRules());
    NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
    ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
    assertNotNull(otn);
    assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 8 with Entity

use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.

the class TraitTest method testTupleIntegrityOnModification.

@Test
public void testTupleIntegrityOnModification() {
    String drl = "package test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive value : int end " + "rule Trait when " + "    $core: Entity( ) " + "then " + "    A o = don( $core, A.class ); " + "    System.out.println( 'Found ! ' + o ); " + "end " + "rule Test when " + "   $x: A( value == 0 ) " + "then " + "   list.add( 0 ); " + "end " + "rule Check when " + "   $x: A( value == 42 ) " + "then " + "   list.add( 42 ); " + "end " + "rule Mood when " + "  $x : A( value != 42 ) " + "then " + "  modify ( $x ) { setValue( 42 ); } " + "end ";
    KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Entity());
    ksession.fireAllRules();
    for (final Object o : ksession.getObjects(new ObjectFilter() {

        @Override
        public boolean accept(Object object) {
            return object.getClass().getName().contains("test.A");
        }
    })) {
        InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o);
        LeftTuple first = handle.getFirstLeftTuple();
        assertTrue(first instanceof RuleTerminalNodeLeftTuple);
        assertEquals("Check", ((RuleTerminalNodeLeftTuple) first).getRule().getName());
    }
    assertEquals(Arrays.asList(0, 42), list);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) KieBase(org.kie.api.KieBase) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) ObjectFilter(org.drools.core.ObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Test(org.junit.Test)

Example 9 with Entity

use of org.drools.core.factmodel.traits.Entity 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 10 with Entity

use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.

the class StandaloneTest method testThing.

@Test
public void testThing() throws LogicalTypeInconsistencyException {
    // Entity is @Traitable and implements TraitableBean natively
    // Thing is a Trait
    // --> just call getProxy
    Entity core = new Entity("x");
    Thing thing = factory.don(core, Thing.class);
    assertNotNull(thing);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) Thing(org.drools.core.factmodel.traits.Thing) Test(org.junit.Test)

Aggregations

Entity (org.drools.core.factmodel.traits.Entity)12 Test (org.junit.Test)12 KieSession (org.kie.api.runtime.KieSession)6 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)6 HashMap (java.util.HashMap)4 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)4 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)4 KieBase (org.kie.api.KieBase)4 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)4 ArrayList (java.util.ArrayList)3 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)3 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)3 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)3 ClassPathResource (org.drools.core.io.impl.ClassPathResource)3 FactType (org.kie.api.definition.type.FactType)3 Resource (org.kie.api.io.Resource)3 List (java.util.List)2 Map (java.util.Map)2