Search in sources :

Example 66 with InternalFactHandle

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

the class TraitTest method testTMSConsistencyWithNonTraitableBeans.

@Test(timeout = 10000)
public void testTMSConsistencyWithNonTraitableBeans() {
    String s1 = "package org.drools.test;\n" + "import org.drools.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + "  insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + "  age  : int\n" + "  name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + "    $p : Person( )\n" + "then\n" + "    don( $p, Student.class, true );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    TraitFactory.setMode(mode, ksession.getKieBase());
    ksession.fireAllRules();
    FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
    InternalFactHandle h = ((InternalFactHandle) personHandle);
    ObjectTypeConfigurationRegistry reg = h.getEntryPoint().getObjectTypeConfigurationRegistry();
    ObjectTypeConf conf = reg.getObjectTypeConf(h.getEntryPoint().getEntryPoint(), ((InternalFactHandle) personHandle).getObject());
    assertTrue(conf.isTMSEnabled());
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry) Test(org.junit.Test)

Example 67 with InternalFactHandle

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

the class MarshallerTest method testOrderFacts.

@Test
public void testOrderFacts() throws Exception {
    List<InternalFactHandle> list = new ArrayList<InternalFactHandle>();
    List<Integer> ids = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 30, 31, 32, -2147483640, 7, 8, 9, 10, 11, 12, 13, 14, 15, 28, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
    for (Integer i : ids) {
        list.add(new DefaultFactHandle(i.intValue(), i));
    }
    InternalFactHandle first = ProtobufOutputMarshaller.orderFacts(list)[0];
    assertEquals(-2147483640, first.getId());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 68 with InternalFactHandle

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

the class MockObjectSource method updateSink.

public void updateSink(final ObjectSink sink, final PropagationContext context, final InternalWorkingMemory workingMemory) {
    this.updated++;
    for (final Iterator it = this.facts.iterator(); it.hasNext(); ) {
        final InternalFactHandle handle = (InternalFactHandle) it.next();
        sink.assertObject(handle, context, workingMemory);
    }
}
Also used : Iterator(java.util.Iterator) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 69 with InternalFactHandle

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

the class NodeSegmentUnlinkingTest method testLiaNodeLinking.

@Test
public void testLiaNodeLinking() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    SegmentUtilities.createSegmentMemory(liaNode, ksession);
    InternalFactHandle fh1 = (InternalFactHandle) ksession.insert("str1");
    n1.assertObject(fh1, context, ksession);
    LiaNodeMemory liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
    BetaMemory bm1 = (BetaMemory) ksession.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
    // still unlinked
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
    // now linked
    InternalFactHandle fh2 = (InternalFactHandle) ksession.insert("str2");
    liaNode.assertObject(fh2, context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    // test unlink after one retract
    liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, ksession);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
    // check counter, after multiple asserts
    InternalFactHandle fh3 = (InternalFactHandle) ksession.insert("str3");
    InternalFactHandle fh4 = (InternalFactHandle) ksession.insert("str4");
    liaNode.assertObject(fh3, context, ksession);
    liaNode.assertObject(fh4, context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, ksession);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
    liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, ksession);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Test(org.junit.Test)

Example 70 with InternalFactHandle

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

the class RightTupleIndexHashTableIteratorTest method test1.

@Test
public void test1() {
    BetaNodeFieldConstraint constraint0 = getConstraint("d", Operator.EQUAL, "this", Foo.class);
    BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    BetaConstraints betaConstraints = null;
    betaConstraints = new SingleBetaConstraints(constraints, config);
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    KieBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ss = kBase.newKieSession();
    InternalFactHandle fh1 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh2 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh3 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh4 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh5 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh6 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh7 = (InternalFactHandle) ss.insert(new Foo("cream", 3));
    InternalFactHandle fh8 = (InternalFactHandle) ss.insert(new Foo("gorda", 15));
    InternalFactHandle fh9 = (InternalFactHandle) ss.insert(new Foo("beer", 16));
    InternalFactHandle fh10 = (InternalFactHandle) ss.insert(new Foo("mars", 0));
    InternalFactHandle fh11 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh12 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh13 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh1, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh2, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh3, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh4, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh5, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh6, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh7, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh8, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh9, null));
    TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
    // can't create a 0 hashCode, so forcing
    TupleList rightTupleList = new TupleList();
    rightTupleList.add(new RightTupleImpl(fh10, null));
    hashTable.getTable()[0] = rightTupleList;
    rightTupleList = new TupleList();
    rightTupleList.add(new RightTupleImpl(fh11, null));
    rightTupleList.add(new RightTupleImpl(fh12, null));
    rightTupleList.add(new RightTupleImpl(fh13, null));
    ((TupleList) hashTable.getTable()[0]).setNext(rightTupleList);
    Entry[] table = hashTable.getTable();
    List list = new ArrayList();
    for (int i = 0; i < table.length; i++) {
        if (table[i] != null) {
            List entries = new ArrayList();
            entries.add(i);
            Entry entry = table[i];
            while (entry != null) {
                entries.add(entry);
                entry = entry.getNext();
            }
            list.add(entries.toArray());
        }
    }
    assertEquals(5, list.size());
    Object[] entries = (Object[]) list.get(0);
    assertEquals(0, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(1);
    assertEquals(102, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(2);
    assertEquals(103, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(3);
    assertEquals(115, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(4);
    assertEquals(117, entries[0]);
    assertEquals(3, entries.length);
    // System.out.println( entries );
    list = new ArrayList<LeftTupleImpl>();
    Iterator it = betaMemory.getRightTupleMemory().iterator();
    for (RightTuple rightTuple = (RightTuple) it.next(); rightTuple != null; rightTuple = (RightTuple) it.next()) {
        list.add(rightTuple);
    }
    assertEquals(13, list.size());
}
Also used : SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) BetaConstraints(org.drools.core.common.BetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RightTuple(org.drools.core.reteoo.RightTuple) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) KieBase(org.kie.api.KieBase) FieldIndexHashTableFullIterator(org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) TupleList(org.drools.core.util.index.TupleList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) 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