Search in sources :

Example 16 with TraitProxyImpl

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

the class TraitTest method testTraitdeleteOrder.

@Test
public void testTraitdeleteOrder() {
    String drl = "" + "package org.drools.core.factmodel.traits.test;\n" + "\n" + "import org.drools.core.factmodel.traits.*;\n" + "import org.drools.traits.core.factmodel.*;\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();
    TraitFactoryImpl.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 TraitProxyImpl) {
                String traitName = ((TraitProxyImpl) 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.traits.core.factmodel.Entity) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 17 with TraitProxyImpl

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

the class TraitTest method testTraitProxy.

@Test
public void testTraitProxy() {
    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());
    TraitFactoryImpl.setMode(mode, kb);
    TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        impClass.set(imp, "name", "aaa");
        Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
        Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
        assertNotNull(trait);
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        proxy.getFields().put("field", "xyz");
        // proxy.getFields().put("name", "aaa");
        assertNotNull(proxy);
        TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        assertSame(proxy2, proxy);
        TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, trait2);
        assertNotNull(proxy3);
        assertEquals("xyz", proxy3.getFields().get("field"));
        assertEquals("aaa", proxy3.getFields().get("name"));
        TraitableBean imp2 = (TraitableBean) impClass.newInstance();
        impClass.set(imp2, "name", "aaa");
        TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, trait);
        // proxy4.getFields().put("name", "aaa");
        proxy4.getFields().put("field", "xyz");
        assertEquals(proxy2, proxy4);
    } catch (InstantiationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (LogicalTypeInconsistencyException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 18 with TraitProxyImpl

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

the class TraitTest method testWrapperKeySetAndValues.

@Test
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());
    TraitFactoryImpl.setMode(mode, kb);
    TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
    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();
        TraitProxyImpl proxy = (TraitProxyImpl) 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 : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) 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) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) Comparator(java.util.Comparator) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Collection(java.util.Collection) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) HashSet(java.util.HashSet) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)18 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)13 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)12 Test (org.junit.Test)12 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)9 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 BitSet (java.util.BitSet)7 Entity (org.drools.traits.core.factmodel.Entity)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 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3