Search in sources :

Example 56 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class ExtendsTest method testDeclareInheritance.

@Test
public void testDeclareInheritance() throws Exception {
    String s1 = "package org.drools;\n" + "import org.drools.compiler.integrationtests.ExtendsTest.*;\n" + "\n" + "declare A \n" + " @role( event )" + " @typesafe( false )\n" + "end\n" + "" + "declare C @role( event ) @typesafe( false ) end \n" + "" + "rule R \n" + "when " + "   $x : C( this.x == 1 ) \n" + "then\n" + "   System.out.println( $x ); \n" + "end\n" + "";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    FactHandle h = knowledgeSession.insert(new X());
    assertTrue(((InternalFactHandle) h).isEvent());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 57 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class Misc2Test method testCustomDynamicSalience.

@Test
public void testCustomDynamicSalience() {
    String drl = "package org.drools.test; " + "import " + Person.class.getName() + "; " + "global java.util.List list; " + "rule A " + "when " + "     $person : Person( name == 'a' ) " + "then" + "     list.add( $person.getAge() ); " + "end " + "rule B " + "when " + "     $person : Person( name == 'b' ) " + "then" + "     list.add( $person.getAge() ); " + "end " + "";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession session = helper.build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    session.setGlobal("list", list);
    for (Rule r : session.getKieBase().getKiePackage("org.drools.test").getRules()) {
        ((RuleImpl) r).setSalience(new Salience() {

            @Override
            public int getValue(KnowledgeHelper khelper, Rule rule, WorkingMemory workingMemory) {
                if (khelper == null) {
                    return 0;
                }
                InternalFactHandle h = (InternalFactHandle) khelper.getMatch().getFactHandles().get(0);
                return ((Person) h.getObject()).getAge();
            }

            @Override
            public int getValue() {
                throw new IllegalStateException("Should not have been called...");
            }

            @Override
            public boolean isDynamic() {
                return true;
            }
        });
    }
    session.insert(new Person("a", 1));
    session.insert(new Person("a", 5));
    session.insert(new Person("a", 3));
    session.insert(new Person("b", 4));
    session.insert(new Person("b", 2));
    session.insert(new Person("b", 6));
    session.fireAllRules();
    assertEquals(Arrays.asList(6, 5, 4, 3, 2, 1), list);
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Salience(org.drools.core.spi.Salience) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 58 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class DynamicRulesTest method testDynamicRulesAddRemove.

@Test(timeout = 10000)
public void testDynamicRulesAddRemove() {
    try {
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_DynamicRulesTom.drl");
        KieSession session = createKnowledgeSession(kbase);
        List<?> results = new ArrayList<Object>();
        session.setGlobal("results", results);
        InternalFactHandle h1 = (InternalFactHandle) session.insert(new Person("tom", 1));
        InternalFactHandle h2 = (InternalFactHandle) session.insert(new Person("fred", 2));
        InternalFactHandle h3 = (InternalFactHandle) session.insert(new Person("harry", 3));
        InternalFactHandle h4 = (InternalFactHandle) session.insert(new Person("fred", 4));
        InternalFactHandle h5 = (InternalFactHandle) session.insert(new Person("ed", 5));
        InternalFactHandle h6 = (InternalFactHandle) session.insert(new Person("tom", 6));
        InternalFactHandle h7 = (InternalFactHandle) session.insert(new Person("sreeni", 7));
        InternalFactHandle h8 = (InternalFactHandle) session.insert(new Person("jill", 8));
        InternalFactHandle h9 = (InternalFactHandle) session.insert(new Person("ed", 9));
        InternalFactHandle h10 = (InternalFactHandle) session.insert(new Person("tom", 10));
        session.fireAllRules();
        assertEquals(3, results.size());
        assertTrue(results.contains(h1.getObject()));
        assertTrue(results.contains(h6.getObject()));
        assertTrue(results.contains(h10.getObject()));
        results.clear();
        kbase.addPackages(loadKnowledgePackages("test_DynamicRulesFred.drl"));
        session.fireAllRules();
        assertEquals(2, results.size());
        assertTrue(results.contains(h2.getObject()));
        assertTrue(results.contains(h4.getObject()));
        results.clear();
        kbase.removeKiePackage("tom");
        kbase.addPackages(loadKnowledgePackages("test_DynamicRulesEd.drl"));
        session.fireAllRules();
        assertEquals(2, results.size());
        assertTrue(results.contains(h5.getObject()));
        assertTrue(results.contains(h9.getObject()));
        results.clear();
        ((Person) h3.getObject()).setName("ed");
        session.update(h3, h3.getObject());
        session.fireAllRules();
        assertEquals(1, results.size());
        assertTrue(results.contains(h3.getObject()));
    } catch (Exception e) {
        e.printStackTrace();
        fail("Should not raise any exception: " + e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 59 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class DefeasibilityTest method testWMStatusOnNegativeDefeat.

@Test(timeout = 10000)
public void testWMStatusOnNegativeDefeat() {
    String droolsSource = "package org.drools.tms.test; " + "global java.util.List posList;" + "global java.util.List negList;" + "declare Bar value : int @key end " + "rule Top " + "@Defeasible " + "@Defeats( 'Sub' ) " + "when " + "   $i : Integer( this < 10 ) " + "then " + "   insertLogical( new Bar( $i ) ); " + "end " + "rule Sub " + "@Defeasible " + "when " + "   $i : Integer() " + "then " + "   insertLogical( new Bar( $i ), $i > 10 ? 'pos' : 'neg' ); " + "end " + "rule Sup " + "@Defeasible " + "@Defeats( 'Sub' ) " + "when " + "   $i : Integer( this > 10 ) " + "then " + "   insertLogical( new Bar( $i ), 'neg' ); " + "end " + "rule React_Pos " + "when " + "   $b : Bar() " + "then " + "   posList.add( $b ); " + "   System.out.println( ' ++++ ' + $b ); " + "end " + "rule React_Neg " + "when " + "   $b : Bar( _.neg )" + "then " + "   negList.add( $b ); " + "   System.out.println( ' ---- ' + $b ); " + "end " + "";
    KieSession session = getSessionFromString(droolsSource);
    List posList = new ArrayList();
    List negList = new ArrayList();
    session.setGlobal("posList", posList);
    session.setGlobal("negList", negList);
    session.insert(20);
    session.insert(5);
    session.fireAllRules();
    assertEquals(1, posList.size());
    assertEquals(1, negList.size());
    Object posBar = posList.get(0);
    InternalFactHandle posHandle = (InternalFactHandle) session.getFactHandle(posBar);
    DefeasibleBeliefSet dbs = (DefeasibleBeliefSet) posHandle.getEqualityKey().getBeliefSet();
    assertEquals(1, dbs.size());
    assertFalse(dbs.isNegated());
    assertTrue(dbs.isDecided());
    assertTrue(dbs.isPositive());
    assertSame(posHandle, dbs.getFactHandle());
    assertFalse(posHandle.isNegated());
    assertTrue(dbs.isDefeasiblyPosProveable());
    assertTrue(session.getObjects().contains(posBar));
    Object negBar = negList.get(0);
    InternalFactHandle negHandle = (InternalFactHandle) getNegativeHandles(session).get(0);
    dbs = (DefeasibleBeliefSet) negHandle.getEqualityKey().getBeliefSet();
    assertEquals(1, dbs.size());
    assertFalse(dbs.isPositive());
    assertTrue(dbs.isDecided());
    assertTrue(dbs.isNegated());
    assertSame(negHandle, dbs.getFactHandle());
    assertTrue(negHandle.isNegated());
    assertTrue(dbs.isDefeasiblyNegProveable());
    assertTrue(session.getObjects().contains(negBar));
}
Also used : DefeasibleBeliefSet(org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 60 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class CepEspTest method testTimeRelationalOperators.

@Test(timeout = 10000)
public void testTimeRelationalOperators() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_TimeRelationalOperators.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final PseudoClockScheduler clock = (PseudoClockScheduler) wm.getSessionClock();
    clock.setStartupTime(1000);
    final List results_coincides = new ArrayList();
    final List results_before = new ArrayList();
    final List results_after = new ArrayList();
    final List results_meets = new ArrayList();
    final List results_met_by = new ArrayList();
    final List results_overlaps = new ArrayList();
    final List results_overlapped_by = new ArrayList();
    final List results_during = new ArrayList();
    final List results_includes = new ArrayList();
    final List results_starts = new ArrayList();
    final List results_started_by = new ArrayList();
    final List results_finishes = new ArrayList();
    final List results_finished_by = new ArrayList();
    wm.setGlobal("results_coincides", results_coincides);
    wm.setGlobal("results_before", results_before);
    wm.setGlobal("results_after", results_after);
    wm.setGlobal("results_meets", results_meets);
    wm.setGlobal("results_met_by", results_met_by);
    wm.setGlobal("results_overlaps", results_overlaps);
    wm.setGlobal("results_overlapped_by", results_overlapped_by);
    wm.setGlobal("results_during", results_during);
    wm.setGlobal("results_includes", results_includes);
    wm.setGlobal("results_starts", results_starts);
    wm.setGlobal("results_started_by", results_started_by);
    wm.setGlobal("results_finishes", results_finishes);
    wm.setGlobal("results_finished_by", results_finished_by);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
    StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
    StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) wm.insert(tick3);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) wm.insert(tick4);
    InternalFactHandle handle5 = (InternalFactHandle) wm.insert(tick5);
    clock.advanceTime(1, TimeUnit.MILLISECONDS);
    InternalFactHandle handle6 = (InternalFactHandle) wm.insert(tick6);
    InternalFactHandle handle7 = (InternalFactHandle) wm.insert(tick7);
    clock.advanceTime(2, TimeUnit.MILLISECONDS);
    InternalFactHandle handle8 = (InternalFactHandle) wm.insert(tick8);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertNotNull(handle5);
    assertNotNull(handle6);
    assertNotNull(handle7);
    assertNotNull(handle8);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    assertTrue(handle6.isEvent());
    assertTrue(handle7.isEvent());
    assertTrue(handle8.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(1, results_coincides.size());
    assertEquals(tick5, results_coincides.get(0));
    assertEquals(1, results_before.size());
    assertEquals(tick2, results_before.get(0));
    assertEquals(1, results_after.size());
    assertEquals(tick3, results_after.get(0));
    assertEquals(1, results_meets.size());
    assertEquals(tick3, results_meets.get(0));
    assertEquals(1, results_met_by.size());
    assertEquals(tick2, results_met_by.get(0));
    assertEquals(1, results_met_by.size());
    assertEquals(tick2, results_met_by.get(0));
    assertEquals(1, results_overlaps.size());
    assertEquals(tick4, results_overlaps.get(0));
    assertEquals(1, results_overlapped_by.size());
    assertEquals(tick8, results_overlapped_by.get(0));
    assertEquals(1, results_during.size());
    assertEquals(tick6, results_during.get(0));
    assertEquals(1, results_includes.size());
    assertEquals(tick4, results_includes.get(0));
    assertEquals(1, results_starts.size());
    assertEquals(tick6, results_starts.get(0));
    assertEquals(1, results_started_by.size());
    assertEquals(tick7, results_started_by.get(0));
    assertEquals(1, results_finishes.size());
    assertEquals(tick8, results_finishes.get(0));
    assertEquals(1, results_finished_by.size());
    assertEquals(tick7, results_finished_by.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14