Search in sources :

Example 31 with LeftTupleImpl

use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testReload.

@Test
public void testReload() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addGlobal("map", Map.class);
    final KieSession workingMemory = kBase.newKieSession();
    final HashMap map = new HashMap();
    workingMemory.setGlobal("map", map);
    final LeftTupleImpl tuple = new MockTuple(new HashMap());
    tuple.setLeftTupleSink(new RuleTerminalNode(1, new MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
    final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
    DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
    pkg.removeRule(rule);
    // Make sure the compiled classes are also removed
    assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
    builder.addPackage(packageDescr);
    pkg = builder.getPackage(packageDescr.getName());
    rule = pkg.getRule("rule-1");
    knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(2), map.get("value"));
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.drl.ast.descr.AndDescr) DefaultKnowledgeHelper(org.drools.kiesession.consequence.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) MockBetaNode(org.drools.mvel.MockBetaNode) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 32 with LeftTupleImpl

use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.

the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable.

@Test
public void testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable() {
    // Construct a table with one row, containing one list, containing three entries.
    int numEntries = 3;
    TupleList[] table = new TupleList[3];
    TupleList rtList = new TupleList();
    table[0] = rtList;
    for (int i = 0; i < numEntries; i++) {
        LeftTupleImpl leftTuple = new LeftTupleImpl();
        leftTuple.setMemory(rtList);
        rtList.add(leftTuple);
    }
    rtList = new TupleList();
    table[2] = rtList;
    for (int i = 0; i < numEntries; i++) {
        LeftTupleImpl leftTuple = new LeftTupleImpl();
        leftTuple.setMemory(rtList);
        rtList.add(leftTuple);
    }
    rtList = new TupleList();
    table[2].setNext(rtList);
    for (int i = 0; i < numEntries; i++) {
        LeftTupleImpl leftTuple = new LeftTupleImpl();
        leftTuple.setMemory(rtList);
        rtList.add(leftTuple);
    }
    // test fast
    TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
    List<LeftTupleImpl> list = new ArrayList<LeftTupleImpl>();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter.next(null); leftTuple != null; leftTuple = (LeftTupleImpl) iter.next(leftTuple)) {
        // ensure no duplicate
        assertFalse(contains(list, leftTuple));
        list.add(leftTuple);
    }
    assertEquals(numEntries * 3, list.size());
    // test normal
    TupleIndexHashTable lthTable = new TupleIndexHashTable();
    lthTable.init(table, 3, numEntries * 3);
    TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(lthTable);
    list = new ArrayList<LeftTupleImpl>();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter2.next(); leftTuple != null; leftTuple = (LeftTupleImpl) iter2.next()) {
        // ensure no duplicate
        assertFalse(contains(list, leftTuple));
        list.add(leftTuple);
    }
    assertEquals(numEntries * 3, list.size());
}
Also used : TupleList(org.drools.core.util.index.TupleList) ArrayList(java.util.ArrayList) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) Test(org.junit.Test)

Example 33 with LeftTupleImpl

use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.

the class BaseLeftTuplesBuilder method insert.

public T insert(Object... objects) {
    this.testStagedInsert = true;
    if (objects == null) {
        objects = new Object[0];
    }
    for (int i = 0; i < objects.length; i++) {
        if (!(objects[i] instanceof Pair)) {
            Object o1 = objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
            LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
            leftTuples.addInsert(leftTuple);
        } else {
            Pair p = (Pair) objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
            LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
            InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
            LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
            leftTuples.addInsert(leftTuple2);
        }
    }
    return (T) this;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 34 with LeftTupleImpl

use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.

the class BaseLeftTuplesBuilder method update.

public T update(Object... objects) {
    this.testStagedUpdate = true;
    if (objects == null) {
        objects = new Object[0];
    }
    for (int i = 0; i < objects.length; i++) {
        if (!(objects[i] instanceof Pair)) {
            Object o1 = objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
            LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
            leftTuples.addUpdate(leftTuple);
        } else {
            Pair p = (Pair) objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
            LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
            InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
            LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
            leftTuples.addUpdate(leftTuple2);
        }
    }
    return (T) this;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 35 with LeftTupleImpl

use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.

the class RightTupleListTest method testEmptyIterator.

@Test
public void testEmptyIterator() {
    final TupleList map = new TupleList();
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    assertNull(map.getFirst(new LeftTupleImpl(h1, null, true)));
}
Also used : TupleList(org.drools.core.util.index.TupleList) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Aggregations

LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)37 Test (org.junit.Test)31 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 Declaration (org.drools.core.rule.Declaration)17 ClassObjectType (org.drools.core.base.ClassObjectType)16 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 Pattern (org.drools.core.rule.Pattern)15 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)12 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)12 HashMap (java.util.HashMap)10 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)10 Cheese (org.drools.core.test.model.Cheese)10 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)8 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)8 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)8 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)8