Search in sources :

Example 6 with TraitProxy

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

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

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

the class TraitTest method testShedVacancy.

@Test
public void testShedVacancy() {
    String drl = "package org.drools.test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive end " + "declare trait B @propertyReactive end " + "declare trait C extends A,B @propertyReactive end " + "declare trait D extends B @propertyReactive end " + "rule Trait when " + "then " + "    Entity e = new Entity( 'x1' ); " + "    don( e, C.class ); " + "    don( e, D.class ); " + "end " + "rule Mood when " + "  $x : B() " + "then " + "  System.out.println( 'Found B' ); " + "end " + "rule Shed when " + "  $s : String() " + "  $x : Entity() " + "then " + "  delete( $s ); " + "  shed( $x, A.class ); " + "end " + "";
    KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    HierarchyEncoder<String> hier = ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().getTraitRegistry().getHierarchy();
    BitSet a = (BitSet) hier.getCode("org.drools.test.A").clone();
    BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone();
    BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone();
    BitSet d = (BitSet) hier.getCode("org.drools.test.D").clone();
    int n = ksession.fireAllRules();
    assertEquals(2, n);
    System.err.print("---------------------------------------------------------------\n\n\n ");
    int counter = 0;
    for (Object o : ksession.getObjects()) {
        if (o instanceof TraitProxy) {
            TraitProxy tp = (TraitProxy) o;
            if (tp._getTypeCode().equals(c)) {
                assertEquals(1, tp.listAssignedOtnTypeCodes().size());
                assertTrue(tp.listAssignedOtnTypeCodes().contains(b));
                counter++;
            } else if (tp._getTypeCode().equals(d)) {
                assertTrue(tp.listAssignedOtnTypeCodes().isEmpty());
                counter++;
            }
        } else if (o instanceof TraitableBean) {
            TraitableBean tb = (TraitableBean) o;
            System.out.println(tb.getCurrentTypeCode());
            counter++;
        }
    }
    assertEquals(3, counter);
    ksession.insert("go");
    ksession.fireAllRules();
    System.err.print("---------------------------------------------------------------\n\n\n ");
    int counter2 = 0;
    for (Object o : ksession.getObjects()) {
        if (o instanceof TraitProxy) {
            TraitProxy tp = (TraitProxy) o;
            assertEquals(d, tp._getTypeCode());
            assertEquals(1, tp.listAssignedOtnTypeCodes().size());
            assertTrue(tp.listAssignedOtnTypeCodes().contains(b));
            counter2++;
        } else if (o instanceof TraitableBean) {
            TraitableBean tb = (TraitableBean) o;
            assertEquals(d, tb.getCurrentTypeCode());
            counter2++;
        }
    }
    assertEquals(2, counter2);
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Test(org.junit.Test)

Example 9 with TraitProxy

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

the class TraitTest method testWrapperKeySetAndValues.

@Test(timeout = 10000)
public void testWrapperKeySetAndValues() {
    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);
        impClass.set(imp, "name", "john");
        proxy.getFields().put("surname", "xxx");
        proxy.getFields().put("name2", "john");
        proxy.getFields().put("nfield", null);
        Set set = new HashSet();
        set.add("name");
        set.add("surname");
        set.add("age");
        set.add("school");
        set.add("name2");
        set.add("nfield");
        assertEquals(6, proxy.getFields().keySet().size());
        assertEquals(set, proxy.getFields().keySet());
        Collection col1 = proxy.getFields().values();
        Collection col2 = Arrays.asList("john", null, 0, "xxx", "john", null);
        Comparator comp = new Comparator() {

            public int compare(Object o1, Object o2) {
                if (o1 == null && o2 != null) {
                    return 1;
                }
                if (o1 != null && o2 == null) {
                    return -1;
                }
                if (o1 == null && o2 == null) {
                    return 0;
                }
                return o1.toString().compareTo(o2.toString());
            }
        };
        Collections.sort((List) col1, comp);
        Collections.sort((List) col2, comp);
        assertEquals(col1, col2);
        assertTrue(proxy.getFields().containsValue(null));
        assertTrue(proxy.getFields().containsValue("john"));
        assertTrue(proxy.getFields().containsValue(0));
        assertTrue(proxy.getFields().containsValue("xxx"));
        assertFalse(proxy.getFields().containsValue("randomString"));
        assertFalse(proxy.getFields().containsValue(-96));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) 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) Comparator(java.util.Comparator) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Collection(java.util.Collection) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with TraitProxy

use of org.drools.core.factmodel.traits.TraitProxy 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)

Aggregations

TraitProxy (org.drools.core.factmodel.traits.TraitProxy)19 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)13 Test (org.junit.Test)12 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 BitSet (java.util.BitSet)7 Entity (org.drools.core.factmodel.traits.Entity)4 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Thing (org.drools.core.factmodel.traits.Thing)3 KieSession (org.kie.api.runtime.KieSession)3