Search in sources :

Example 51 with InternalFactHandle

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

the class SimpleBeliefSystem method getObjectTypeConf.

private ObjectTypeConf getObjectTypeConf(BeliefSet beliefSet) {
    InternalFactHandle fh = beliefSet.getFactHandle();
    ObjectTypeConfigurationRegistry reg;
    ObjectTypeConf typeConf;
    reg = ep.getObjectTypeConfigurationRegistry();
    typeConf = reg.getObjectTypeConf(ep.getEntryPoint(), fh.getObject());
    return typeConf;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) InternalFactHandle(org.drools.core.common.InternalFactHandle) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry)

Example 52 with InternalFactHandle

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

the class SimpleBeliefSystem method delete.

@Override
public void delete(SimpleMode mode, RuleImpl rule, Activation activation, Object payload, BeliefSet<SimpleMode> beliefSet, PropagationContext context) {
    beliefSet.remove(mode);
    InternalFactHandle bfh = beliefSet.getFactHandle();
    if (beliefSet.isEmpty() && bfh.getEqualityKey() != null && bfh.getEqualityKey().getStatus() == EqualityKey.JUSTIFIED) {
        ep.delete(bfh, bfh.getObject(), getObjectTypeConf(beliefSet), context.getRuleOrigin(), null, activation != null ? activation.getTuple().getTupleSink() : null);
    } else if (!beliefSet.isEmpty() && bfh.getObject() == payload && payload != bfh.getObject()) {
        // prime has changed, to update new object
        // Equality might have changed on the object, so remove (which uses the handle id) and add back in
        bfh.getEntryPoint().getObjectStore().updateHandle(bfh, beliefSet.getFirst().getObject().getObject());
        bfh.getEntryPoint().update(bfh, bfh.getObject(), allSetButTraitBitMask(), Object.class, null);
    }
    if (beliefSet.isEmpty() && bfh.getEqualityKey() != null) {
        // if the beliefSet is empty, we must null the logical handle
        EqualityKey key = bfh.getEqualityKey();
        key.setLogicalFactHandle(null);
        key.setBeliefSet(null);
        if (key.getStatus() == EqualityKey.JUSTIFIED) {
            // if it's stated, there will be other handles, so leave it in the TMS
            tms.remove(key);
        }
    }
}
Also used : EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 53 with InternalFactHandle

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

the class SetVariableCommandFromLastReturn method execute.

public Object execute(Context context) {
    Context targetCtx;
    if (this.contextName == null) {
        targetCtx = context;
    } else {
        targetCtx = ((RegistryContext) context).getContextManager().getContext(this.contextName);
    }
    GetDefaultValue sim = (GetDefaultValue) context.get("simulator");
    Object o = sim.getObject();
    // for FactHandle's we store the handle on a map and the actual object as
    if (o instanceof FactHandle) {
        Map<String, FactHandle> handles = (Map<String, FactHandle>) targetCtx.get("h");
        if (handles == null) {
            handles = new HashMap<String, FactHandle>();
            targetCtx.set("h", handles);
        }
        handles.put(identifier, (FactHandle) o);
        o = ((InternalFactHandle) o).getObject();
    }
    targetCtx.set(identifier, o);
    return o;
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) RegistryContext(org.drools.core.command.impl.RegistryContext) Map(java.util.Map) HashMap(java.util.HashMap)

Example 54 with InternalFactHandle

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

the class GetFactHandleCommand method execute.

public FactHandle execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    InternalFactHandle factHandle = (InternalFactHandle) ksession.getFactHandle(object);
    if (factHandle != null) {
        InternalFactHandle handle = factHandle.clone();
        if (disconnected) {
            handle.disconnect();
        }
        return handle;
    }
    return null;
}
Also used : KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 55 with InternalFactHandle

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

the class IndexingTest method testFullFastIteratorResume.

@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, int $age ) \n";
    str += "    not $p2 : Person( $name := name, age > $age ) \n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
            node = n;
            break;
        }
    }
    StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
    System.out.println(bm.getLeftTupleMemory().getClass());
    System.out.println(bm.getRightTupleMemory().getClass());
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("inserted", new Integer(0));
    map.put("deleted", new Integer(0));
    map.put("updated", new Integer(0));
    wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

        @Override
        public void rowInserted(Row row) {
        }

        @Override
        public void rowDeleted(Row row) {
        }

        @Override
        public void rowUpdated(Row row) {
        }
    });
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = new Person("x0", 100);
    InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
    peeps.put(p.getName(), fh);
    for (int i = 1; i < 100; i++) {
        p = new Person("x" + i, 101);
        fh = (InternalFactHandle) wm.insert(p);
        wm.fireAllRules();
        peeps.put(p.getName(), fh);
    }
    List<RightTuple> list = new ArrayList<RightTuple>(100);
    FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
    for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
        list.add(rt);
    }
    assertEquals(100, list.size());
    // check we can resume from each entry in the list above.
    for (int i = 0; i < 100; i++) {
        RightTuple rightTuple = list.get(i);
        // resumes from the current rightTuple
        it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
        int j = i + 1;
        for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
            assertSame(list.get(j), rt);
            j++;
        }
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) RightTuple(org.drools.core.reteoo.RightTuple) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) 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