Search in sources :

Example 1 with ObjectFilter

use of org.drools.core.ObjectFilter in project drools by kiegroup.

the class TraitTest method testNodePartitioningByProxiesAfterShed.

@Test
public void testNodePartitioningByProxiesAfterShed() {
    String source = "package t.x  " + "import java.util.*;  " + "import org.drools.core.factmodel.traits.Thing  " + "import org.drools.core.factmodel.traits.Traitable  " + " " + "global java.util.List list;  " + " " + "" + "declare trait A end " + "declare trait B extends A end " + "declare trait C extends B end " + "declare trait D extends A end " + "declare trait E extends C, D end " + "declare trait F extends E end " + "declare trait G extends A end " + "" + "declare Kore " + "   @Traitable " + "end " + "" + "rule Init when " + "then " + "   Kore k = new Kore(); " + "   don( k, C.class );  " + "   don( k, D.class );  " + "   don( k, B.class );  " + "   don( k, A.class );  " + "   don( k, F.class );  " + "   don( k, E.class );  " + "   don( k, G.class );  " + "   shed( k, B.class );  " + "end ";
    for (char c = 'A'; c <= 'G'; c++) {
        String C = "" + c;
        source += "rule Rule" + C + " when " + C + "() then list.add( '" + C + "' ); end ";
    }
    source += "rule RuleAll " + "when  " + "   A() D() G() " + "then  " + "   list.add( 'Z' ); " + "end " + "";
    KieSession ks = getSessionFromString(source);
    TraitFactory.setMode(mode, ks.getKieBase());
    List list = new ArrayList();
    ks.setGlobal("list", list);
    ks.fireAllRules();
    System.out.println(list);
    assertEquals(Arrays.asList('A', 'D', 'G', 'Z'), list);
    for (Object o : ks.getObjects(new ObjectFilter() {

        @Override
        public boolean accept(Object object) {
            return object instanceof TraitableBean;
        }
    })) {
        Set<BitSet> otns = checkOTNPartitioning((TraitableBean) o, ks);
        assertEquals(3, otns.size());
    }
}
Also used : 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) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) ObjectFilter(org.drools.core.ObjectFilter) Test(org.junit.Test)

Example 2 with ObjectFilter

use of org.drools.core.ObjectFilter in project drools by kiegroup.

the class TraitTest method testNodePartitioningByProxies.

@Test
public void testNodePartitioningByProxies() {
    String source = "package t.x  " + "import java.util.*;  " + "import org.drools.core.factmodel.traits.Thing  " + "import org.drools.core.factmodel.traits.Traitable  " + " " + "global java.util.List list;  " + " " + "" + "declare trait A @PropertyReactive end " + "declare trait B extends A @PropertyReactive end " + "declare trait C extends B @PropertyReactive end " + "declare trait D extends A @PropertyReactive end " + "declare trait E extends C, D @PropertyReactive end " + "declare trait F extends E @PropertyReactive end " + "declare trait G extends A @PropertyReactive end " + "" + "declare Kore " + "   @Traitable " + "end " + "" + "rule Init when " + "then " + "   Kore k = new Kore(); " + "   don( k, C.class );  " + "   don( k, D.class );  " + "   don( k, B.class );  " + "   don( k, A.class );  " + "   don( k, F.class );  " + "   don( k, E.class );  " + "   don( k, G.class );  " + "end ";
    for (char c = 'A'; c <= 'G'; c++) {
        String C = "" + c;
        source += "rule Rule" + C + " when " + C + "() then list.add( '" + C + "' ); end ";
    }
    source += "rule RuleAll " + "when  " + "   A() B() C() D() E() F() G() " + "then  " + "   list.add( 'Z' ); " + "end " + "";
    KieSession ks = getSessionFromString(source);
    TraitFactory.setMode(mode, ks.getKieBase());
    List list = new ArrayList();
    ks.setGlobal("list", list);
    ks.fireAllRules();
    System.out.println(list);
    assertEquals(Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'Z'), list);
    for (Object o : ks.getObjects(new ObjectFilter() {

        @Override
        public boolean accept(Object object) {
            return object instanceof TraitableBean;
        }
    })) {
        Set<BitSet> otns = checkOTNPartitioning((TraitableBean) o, ks);
        assertEquals(7, otns.size());
    }
}
Also used : 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) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) ObjectFilter(org.drools.core.ObjectFilter) Test(org.junit.Test)

Example 3 with ObjectFilter

use of org.drools.core.ObjectFilter 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)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 ObjectFilter (org.drools.core.ObjectFilter)3 Test (org.junit.Test)3 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)3 KieSession (org.kie.api.runtime.KieSession)3 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3 BitSet (java.util.BitSet)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 Entity (org.drools.core.factmodel.traits.Entity)1 LeftTuple (org.drools.core.reteoo.LeftTuple)1 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)1 KieBase (org.kie.api.KieBase)1