Search in sources :

Example 1 with TraitField

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

the class LogicalTraitTest method testShadowAliasTraitOnClassLogicalRetract.

@Test
public void testShadowAliasTraitOnClassLogicalRetract() {
    String drl = "package org.drools.test; \n" + "import org.drools.core.factmodel.traits.*; \n" + "import org.drools.core.factmodel.traits.Trait; \n" + "" + "global java.util.List list; \n" + "" + "declare trait X \n" + "  fld : T \n" + "  fld2 : Q \n" + "end \n" + "" + "declare trait W \n" + "  fld : T \n" + "end \n" + "declare trait V \n" + "  fld2 : Q \n" + "end \n" + "" + "declare Y \n" + "@Traitable( logical = true ) \n" + "  fld : K \n" + "  fld2 : Object \n" + "end \n" + "" + "declare trait T @Trait( logical=true ) end \n" + "declare trait Q @Trait( logical=true ) end \n" + "declare K @Traitable() end \n" + "" + "rule Don \n" + "when \n" + "  $s : String( this == \"go\" ) \n" + "then \n" + "  K k = new K(); \n" + "  Y y = new Y( k, null ); \n" + "  insert( y ); \n" + "  insert( k ); \n" + "" + "  don( k, Q.class ); \n" + "" + "  don( y, X.class, true ); \n" + "  don( y, W.class ); \n" + "  don( y, V.class ); \n" + "end \n" + "" + "rule Check \n" + "when \n" + "  $x : X( this isA V, fld isA T ) \n" + "then \n" + "  shed( $x, V.class ); \n" + "  list.add( \"ok\" );" + "end \n" + "" + "rule Check2 \n" + "salience 10 \n" + "when \n" + "  String( this == \"go2\" ) \n" + "  Q() \n" + "  not X() not V() " + "then \n" + "  list.add( \"ok2\" );" + "end \n" + "" + "rule Check3 \n" + "salience 5 \n" + "when \n" + "  String( this == \"go2\" ) \n" + "  K( this isA Q ) \n" + "  not X() not V() " + "then \n" + "  list.add( \"ok3\" );" + "end \n" + "" + "";
    KnowledgeBuilder kbuilderImpl = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilderImpl.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilderImpl.hasErrors()) {
        fail(kbuilderImpl.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilderImpl.getKnowledgePackages());
    TraitFactory.setMode(mode, kbase);
    KieSession ks = kbase.newKieSession();
    ArrayList list = new ArrayList();
    ks.setGlobal("list", list);
    FactHandle handle = ks.insert("go");
    ks.fireAllRules();
    assertEquals(Arrays.asList("ok"), list);
    for (Object o : ks.getObjects()) {
        System.out.println(o + " >> " + ((InternalFactHandle) ks.getFactHandle(o)).getEqualityKey());
    }
    ks.retract(handle);
    ks.fireAllRules();
    for (Object o : ks.getObjects(new ClassObjectFilter(ks.getKieBase().getFactType("org.drools.test", "Y").getFactClass()))) {
        assertTrue(o instanceof TraitableBean);
        TraitableBean tb = (TraitableBean) o;
        TraitField fld = tb._getFieldTMS().getRegisteredTraitField("fld");
        Set<Class<?>> types = fld.getRangeTypes();
        assertEquals(2, types.size());
        TraitField fld2 = tb._getFieldTMS().getRegisteredTraitField("fld2");
        Set<Class<?>> types2 = fld2.getRangeTypes();
        assertEquals(1, types2.size());
    }
    try {
        ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ks.setGlobal("list", list);
    ks.insert("go2");
    ks.fireAllRules();
    System.out.println(list);
    assertEquals(Arrays.asList("ok", "ok2", "ok3"), list);
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) TraitField(org.drools.core.factmodel.traits.TraitField) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 TraitField (org.drools.core.factmodel.traits.TraitField)1 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 Test (org.junit.Test)1 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)1 KieSession (org.kie.api.runtime.KieSession)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1