Search in sources :

Example 21 with DefaultFactHandle

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

the class XStreamXMLTest method testGetFactHandlesExecutionResults.

@Test
public void testGetFactHandlesExecutionResults() {
    final Message msg = new Message("Hello World!");
    final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
    final Message msg2 = new Message("Hello World again!");
    final FactHandle msgHandle2 = new DefaultFactHandle(2, null, 10, 10, 20, msg2);
    List<FactHandle> factHandleList = new ArrayList<FactHandle>();
    factHandleList.add(msgHandle);
    factHandleList.add(msgHandle2);
    HashMap<String, Object> factHandles = new LinkedHashMap<String, Object>();
    ExecutionResultImpl executionResult = new ExecutionResultImpl();
    executionResult.setFactHandles(factHandles);
    HashMap<String, Object> results = new LinkedHashMap<String, Object>();
    results.put("facts", factHandleList);
    executionResult.setResults(results);
    String xmlString = xstream.toXML(executionResult);
    Assert.assertEquals("<execution-results>\n" + "  <result identifier=\"facts\">\n" + "    <list>\n" + "      <fact-handle external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "      <fact-handle external-form=\"0:2:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "    </list>\n" + "  </result>\n" + "</execution-results>", xmlString);
    ExecutionResultImpl executionResult2 = (ExecutionResultImpl) xstream.fromXML(xmlString);
    Assert.assertEquals(executionResult.getFactHandles().size(), executionResult2.getFactHandles().size());
    Assert.assertEquals(executionResult.getResults().size(), executionResult2.getResults().size());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 22 with DefaultFactHandle

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

the class XStreamXMLTest method testExecutionResults.

@Test
public void testExecutionResults() {
    final Message msg = new Message("Hello World!");
    final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
    final Message msg2 = new Message("Hello World again!");
    final FactHandle msgHandle2 = new DefaultFactHandle(2, null, 10, 10, 20, msg2);
    HashMap<String, Object> factHandles = new LinkedHashMap<String, Object>();
    factHandles.put("first", msgHandle);
    factHandles.put("second", msgHandle2);
    ExecutionResultImpl executionResult = new ExecutionResultImpl();
    executionResult.setFactHandles(factHandles);
    HashMap<String, Object> results = new LinkedHashMap<String, Object>();
    results.put("message1", msg);
    results.put("message2", msg2);
    executionResult.setResults(results);
    String xmlString = xstream.toXML(executionResult);
    Assert.assertEquals("<execution-results>\n" + "  <result identifier=\"message1\">\n" + "    <org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "      <msg>Hello World!</msg>\n" + "    </org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "  </result>\n" + "  <result identifier=\"message2\">\n" + "    <org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "      <msg>Hello World again!</msg>\n" + "    </org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + "  </result>\n" + "  <fact-handle identifier=\"first\" external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "  <fact-handle identifier=\"second\" external-form=\"0:2:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + "</execution-results>", xmlString);
    ExecutionResultImpl executionResult2 = (ExecutionResultImpl) xstream.fromXML(xmlString);
    Assert.assertEquals(executionResult.getFactHandles().size(), executionResult2.getFactHandles().size());
    Assert.assertEquals(executionResult.getResults().size(), executionResult2.getResults().size());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 23 with DefaultFactHandle

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

the class FieldIndexEntryTest method testTwoEntries.

@Test
public void testTwoEntries() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    final Cheese stilton2 = new Cheese("stilton", 59);
    final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h2RightTuple);
    assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // remove second
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // check index type does not change, as this fact is removed
    stilton1.setType("cheddar");
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 24 with DefaultFactHandle

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

the class ReteooFactHandleFactory method newFactHandle.

/* (non-Javadoc)
     * @see org.kie.reteoo.FactHandleFactory#newFactHandle(long)
     */
public InternalFactHandle newFactHandle(final int id, final Object object, final long recency, final ObjectTypeConf conf, final InternalWorkingMemory workingMemory, final WorkingMemoryEntryPoint wmEntryPoint) {
    if (conf != null && conf.isEvent()) {
        TypeDeclaration type = conf.getTypeDeclaration();
        long timestamp;
        if (type != null && type.getTimestampExtractor() != null) {
            timestamp = type.getTimestampExtractor().getLongValue(workingMemory, object);
        } else {
            timestamp = workingMemory.getTimerService().getCurrentTime();
        }
        long duration = 0;
        if (type != null && type.getDurationExtractor() != null) {
            duration = type.getDurationExtractor().getLongValue(workingMemory, object);
        }
        return new EventFactHandle(id, object, recency, timestamp, duration, wmEntryPoint != null ? wmEntryPoint : workingMemory, conf != null && conf.isTrait());
    } else {
        return new DefaultFactHandle(id, object, recency, wmEntryPoint != null ? wmEntryPoint : workingMemory, conf != null && conf.isTrait());
    }
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 25 with DefaultFactHandle

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

the class StatefulSessionTest method testDisconnectedFactHandle.

@Test
public void testDisconnectedFactHandle() {
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration);
    KieSession ksession = kbase.newKieSession();
    final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
    final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
    FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
    assertEquals("hello", ksession.getObject(key));
    key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
    assertEquals("goodbye", ksession.getObject(key));
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

DefaultFactHandle (org.drools.core.common.DefaultFactHandle)92 Test (org.junit.Test)78 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 FactHandle (org.kie.api.runtime.rule.FactHandle)29 Cheese (org.drools.core.test.model.Cheese)24 ArrayList (java.util.ArrayList)23 ClassObjectType (org.drools.core.base.ClassObjectType)22 KieSession (org.kie.api.runtime.KieSession)19 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)18 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)16 Declaration (org.drools.core.rule.Declaration)15 RightTuple (org.drools.core.reteoo.RightTuple)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 Pattern (org.drools.core.rule.Pattern)12 Tuple (org.drools.core.spi.Tuple)12 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)12 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)11