Search in sources :

Example 1 with Entity

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

the class TraitTest method testTraitdeleteOrder.

@Test(timeout = 10000)
public void testTraitdeleteOrder() {
    String drl = "" + "package org.drools.core.factmodel.traits.test;\n" + "\n" + "import org.drools.core.factmodel.traits.*;\n" + "import java.util.*;\n" + "\n" + "declare trait A end \n" + "declare trait B extends A end \n" + "declare trait C end \n" + "\n" + "rule \"don\"\n" + "when \n" + "  $e : Entity() \n" + "then\n" + "  don( $e, A.class ); \n" + "  don( $e, C.class ); \n" + "  don( $e, B.class ); \n" + "end\n" + "";
    KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession();
    TraitFactory.setMode(mode, ksession.getKieBase());
    FactHandle handle = ksession.insert(new Entity());
    ksession.fireAllRules();
    final ArrayList list = new ArrayList();
    ksession.addEventListener(new RuleRuntimeEventListener() {

        public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent) {
        }

        public void objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent objectUpdatedEvent) {
        }

        public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectRetractedEvent) {
            Object o = objectRetractedEvent.getOldObject();
            if (o instanceof TraitProxy) {
                String traitName = ((TraitProxy) o)._getTraitName();
                list.add(traitName.substring(traitName.lastIndexOf(".") + 1));
            }
        }
    });
    ksession.delete(handle);
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(Arrays.asList("B", "C", "A"), list);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) TraitProxy(org.drools.core.factmodel.traits.TraitProxy) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with Entity

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

the class TraitTest method testWrapperContainsKey.

@Test(timeout = 10000)
public void testWrapperContainsKey() {
    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();
        impClass.set(imp, "name", "john");
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertTrue(wrapper.containsKey("name"));
        assertTrue(wrapper.containsKey("school"));
        assertTrue(wrapper.containsKey("age"));
        assertFalse(wrapper.containsKey("surname"));
        proxy.getFields().put("school", "skol");
        proxy.getFields().put("surname", "xxx");
        assertTrue(wrapper.containsKey("surname"));
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        TraitableBean ind = new Entity();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
        Map virtualFields2 = ind._getDynamicProperties();
        Map wrapper2 = proxy2.getFields();
        assertTrue(wrapper2.containsKey("name"));
        assertTrue(wrapper2.containsKey("school"));
        assertTrue(wrapper2.containsKey("age"));
        assertFalse(wrapper2.containsKey("surname"));
        traitClass.set(proxy2, "name", "john");
        proxy2.getFields().put("school", "skol");
        proxy2.getFields().put("surname", "xxx");
        assertTrue(wrapper2.containsKey("surname"));
        FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = traitClass2.getFactClass();
        TraitableBean ind2 = new Entity();
        TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
        Map<String, Object> wrapper99 = proxy99.getFields();
        assertFalse(wrapper99.containsKey("name"));
        assertFalse(wrapper99.containsKey("school"));
        assertFalse(wrapper99.containsKey("age"));
        assertFalse(wrapper99.containsKey("surname"));
        proxy99.getFields().put("surname", "xxx");
        proxy99.getFields().put("name", "xyz");
        proxy99.getFields().put("school", "skol");
        assertTrue(wrapper99.containsKey("name"));
        assertTrue(wrapper99.containsKey("school"));
        assertFalse(wrapper99.containsKey("age"));
        assertTrue(wrapper99.containsKey("surname"));
        assertEquals(3, proxy99.getFields().size());
        TraitableBean ind0 = new Entity();
        TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind0, trait2);
        Map<String, Object> wrapper100 = proxy100.getFields();
        assertFalse(wrapper100.containsKey("name"));
        assertFalse(wrapper100.containsKey("school"));
        assertFalse(wrapper100.containsKey("age"));
        assertFalse(wrapper100.containsKey("surname"));
        TraitProxy proxy101 = (TraitProxy) tFactory.getProxy(ind0, trait);
        // object gains properties by virtue of another trait
        // so new props are accessible even using the old proxy
        assertTrue(wrapper100.containsKey("name"));
        assertTrue(wrapper100.containsKey("school"));
        assertTrue(wrapper100.containsKey("age"));
        assertFalse(wrapper100.containsKey("surname"));
    } 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) Map(java.util.Map) HashMap(java.util.HashMap) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 3 with Entity

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

the class TraitTest method testWrapperSize.

@Test(timeout = 10000)
public void testWrapperSize() {
    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 traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        impClass.set(imp, "name", "john");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("school", "skol");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("surname", "xxx");
        assertEquals(4, wrapper.size());
        assertEquals(2, virtualFields.size());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        // TraitableBean ind = (TraitableBean) indClass.newInstance();
        TraitableBean ind = new Entity();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
        Map virtualFields2 = ind._getDynamicProperties();
        Map wrapper2 = proxy2.getFields();
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        traitClass.set(proxy2, "name", "john");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("school", "skol");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("surname", "xxx");
        assertEquals(4, wrapper2.size());
        assertEquals(4, virtualFields2.size());
        FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = traitClass2.getFactClass();
        // TraitableBean ind2 = (TraitableBean) indClass.newInstance();
        TraitableBean ind2 = new Entity();
        TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
        proxy99.getFields().put("surname", "xxx");
        proxy99.getFields().put("name", "xyz");
        proxy99.getFields().put("school", "skol");
        assertEquals(3, proxy99.getFields().size());
        TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind2, trait);
        assertEquals(4, proxy100.getFields().size());
    } 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) Map(java.util.Map) HashMap(java.util.HashMap) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with Entity

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

the class TraitTest method testTraitEncoding.

@Test(timeout = 10000)
public void testTraitEncoding() {
    String s1 = "package org.drools.core.factmodel.traits;\n" + "declare trait A end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "declare trait F extends C end\n" + "declare trait G extends D end\n" + "declare trait H extends D end\n" + "declare trait I extends E end\n" + "declare trait J extends F end\n" + "declare trait K extends G, H end\n" + "declare trait L extends G, H end\n" + "declare trait M extends I, J end\n" + "declare trait N extends K, L end\n" + "" + "rule \"donOneThing\"\n" + "when\n" + "    $x : Entity()\n" + "then\n" + "    don( $x, A.class );\n" + "end\n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    String( this == \"y\" ) \n" + "    $x : Entity()\n" + "then\n" + "    don( $x, B.class );\n" + "    don( $x, D.class );\n" + "    don( $x, F.class );\n" + "    don( $x, E.class );\n" + "    don( $x, I.class );\n" + "    don( $x, K.class );\n" + "    don( $x, J.class );\n" + "    don( $x, C.class );\n" + "    don( $x, H.class );\n" + "    don( $x, G.class );\n" + "    don( $x, L.class );\n" + "    don( $x, M.class );\n" + "    don( $x, N.class );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    TraitRegistry tr = ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().getTraitRegistry();
    System.out.println(tr.getHierarchy());
    Entity ent = new Entity("x");
    KieSession ksession = kbase.newKieSession();
    ksession.insert(ent);
    ksession.fireAllRules();
    assertEquals(1, ent.getMostSpecificTraits().size());
    ksession.insert("y");
    ksession.fireAllRules();
    System.out.println(ent.getMostSpecificTraits());
    assertEquals(2, ent.getMostSpecificTraits().size());
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) TraitRegistry(org.drools.core.factmodel.traits.TraitRegistry) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with Entity

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

the class MetadataTest method testDonWithAttributes.

@Test
public void testDonWithAttributes() {
    Entity entity = new Entity("123");
    entity._setDynamicProperties(new HashMap());
    SubKlass klass = SubKlass_.donSubKlass(entity).setTraitFactory(new StandaloneTraitFactory(ProjectClassLoader.createProjectClassLoader())).prop("hello").subProp(32).call();
    assertEquals("hello", klass.getProp());
    assertEquals(32, (int) klass.getSubProp());
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) HashMap(java.util.HashMap) SubKlass(org.drools.core.meta.org.test.SubKlass) StandaloneTraitFactory(org.drools.core.util.StandaloneTraitFactory) 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