Search in sources :

Example 51 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class DurationTimer method createTrigger.

public Trigger createTrigger(Activation item, InternalWorkingMemory wm) {
    long timestamp;
    if (eventFactHandle != null) {
        Tuple leftTuple = item.getTuple();
        EventFactHandle fh = (EventFactHandle) leftTuple.get(eventFactHandle);
        timestamp = fh.getStartTimestamp();
    } else {
        timestamp = wm.getTimerService().getCurrentTime();
    }
    String[] calendarNames = item.getRule().getCalendars();
    Calendars calendars = wm.getCalendars();
    return createTrigger(timestamp, calendarNames, calendars);
}
Also used : Calendars(org.kie.api.runtime.Calendars) EventFactHandle(org.drools.core.common.EventFactHandle) Tuple(org.drools.core.spi.Tuple)

Example 52 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class WorkingMemoryLogger method extractDeclarations.

/**
 * Creates a string representation of the declarations of an activation.
 * This is a list of name-value-pairs for each of the declarations in the
 * tuple of the activation.  The name is the identifier (=name) of the
 * declaration, and the value is a toString of the value of the
 * parameter, followed by the id of the fact between parentheses.
 *
 * @param match The match from which the declarations should be extracted
 * @return A String represetation of the declarations of the activation.
 */
private String extractDeclarations(Match match) {
    final StringBuilder result = new StringBuilder();
    List<String> declarations = match.getDeclarationIds();
    Map<String, Declaration> declsMap = ((AgendaItem) match).getTerminalNode().getSubRule().getOuterDeclarations();
    for (int i = 0; i < declarations.size(); i++) {
        String declaration = declarations.get(i);
        Declaration decl = declsMap.get(declaration);
        InternalFactHandle handle = ((Tuple) match).get(decl);
        if (!handle.isValid()) {
            continue;
        }
        Object value = decl.getValue(null, handle.getObject());
        result.append(declaration);
        result.append("=");
        if (value == null) {
            // this should never occur
            result.append("null");
        } else {
            result.append(value);
        }
        if (i < declarations.size() - 1) {
            result.append("; ");
        }
    }
    return result.toString();
}
Also used : Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) AgendaItem(org.drools.core.common.AgendaItem) Tuple(org.drools.core.spi.Tuple)

Example 53 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class Scenario method assertEquals.

public void assertEquals(TupleSets<LeftTuple> expected, TupleSets<LeftTuple> actual, boolean testInsert, boolean testDelete, boolean testUpdate) {
    Tuple expectedTuple;
    Tuple actualTuple;
    if (testInsert) {
        if (expected.getInsertFirst() != null) {
            expectedTuple = expected.getInsertFirst();
            actualTuple = actual.getInsertFirst();
            int i = 0;
            for (; expectedTuple != null; expectedTuple = expectedTuple.getStagedNext()) {
                Assert.assertTrue("insert " + i + ":\n" + actualTuple + "\nis not the expected\n" + expectedTuple, equals(expectedTuple, actualTuple));
                actualTuple = actualTuple.getStagedNext();
                i++;
            }
            assertNull("Insert excpected more", actualTuple);
        } else if (actual.getInsertFirst() != null) {
            fail("Expected nothing, but insert existed");
        }
    }
    if (testDelete) {
        if (expected.getDeleteFirst() != null) {
            expectedTuple = expected.getDeleteFirst();
            actualTuple = actual.getDeleteFirst();
            int i = 0;
            for (; expectedTuple != null; expectedTuple = expectedTuple.getStagedNext()) {
                Assert.assertTrue("delete " + i + ":\n" + actualTuple + "\nis not the expected\n" + expectedTuple, equals(expectedTuple, actualTuple));
                actualTuple = actualTuple.getStagedNext();
                i++;
            }
            assertNull("Delete excpected more", actualTuple);
        } else if (actual.getDeleteFirst() != null) {
            fail("Expected nothing, but delete existed");
        }
    }
    if (testUpdate) {
        if (expected.getUpdateFirst() != null) {
            expectedTuple = expected.getUpdateFirst();
            actualTuple = actual.getUpdateFirst();
            int i = 0;
            for (; expectedTuple != null; expectedTuple = expectedTuple.getStagedNext()) {
                Assert.assertTrue("update " + i + ":\n" + actualTuple + "\nis not the expected\n" + expectedTuple, equals(expectedTuple, actualTuple));
                actualTuple = actualTuple.getStagedNext();
                i++;
            }
            assertNull("Update excpected more", actualTuple);
        } else if (actual.getUpdateFirst() != null) {
            fail("Expected nothing, but update existed");
        }
    }
}
Also used : LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple)

Example 54 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class Scenario method equalsRightMemory.

public void equalsRightMemory(List<RightTuple> rightTuples) {
    TupleMemory rtm = bm.getRightTupleMemory();
    int length = 0;
    for (RightTuple expectedRightTuple : rightTuples) {
        FastIterator it = betaNode.getRightIterator(rtm);
        Tuple actualRightTuple = null;
        for (actualRightTuple = BetaNode.getFirstTuple(rtm, it); actualRightTuple != null; actualRightTuple = (RightTuple) it.next(actualRightTuple)) {
            if (expectedRightTuple.equals(actualRightTuple)) {
                length++;
                break;
            }
        }
        if (actualRightTuple == null) {
            fail("Could not find RightTuple: " + expectedRightTuple);
        }
    }
    if (rightTuples.size() != rtm.size()) {
        fail("RightTuple memory size did not match: " + length);
    }
}
Also used : FastIterator(org.drools.core.util.FastIterator) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple)

Aggregations

Tuple (org.drools.core.spi.Tuple)54 InternalFactHandle (org.drools.core.common.InternalFactHandle)17 LeftTuple (org.drools.core.reteoo.LeftTuple)16 RightTuple (org.drools.core.reteoo.RightTuple)14 FastIterator (org.drools.core.util.FastIterator)14 Declaration (org.drools.core.rule.Declaration)9 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 WorkingMemory (org.drools.core.WorkingMemory)7 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)7 Cheese (org.drools.core.test.model.Cheese)7 Test (org.junit.Test)7 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)6 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)6 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 BetaMemory (org.drools.core.reteoo.BetaMemory)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 TupleMemory (org.drools.core.reteoo.TupleMemory)5 MethodVisitor (org.mvel2.asm.MethodVisitor)5 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)4