use of org.drools.core.time.TemporalDependencyMatrix in project drools by kiegroup.
the class ReteooRuleBuilder method addRule.
/**
* Creates the corresponting Rete network for the given <code>Rule</code> and adds it to
* the given rule base.
*
* @param rule
* The rule to add.
* @param kBase
* The rulebase to add the rule to.
*
* @return a List<BaseNode> of terminal nodes for the rule
* @throws InvalidPatternException
*/
public List<TerminalNode> addRule(final RuleImpl rule, final InternalKnowledgeBase kBase) throws InvalidPatternException {
// the list of terminal nodes
final List<TerminalNode> nodes = new ArrayList<TerminalNode>();
// transform rule and gets the array of subrules
final GroupElement[] subrules = rule.getTransformedLhs(kBase.getConfiguration().getComponentFactory().getLogicTransformerFactory().getLogicTransformer(), kBase.getGlobals());
for (int i = 0; i < subrules.length; i++) {
// creates a clean build context for each subrule
final BuildContext context = new BuildContext(kBase);
context.setRule(rule);
// if running in STREAM mode, calculate temporal distance for events
if (EventProcessingOption.STREAM.equals(kBase.getConfiguration().getEventProcessingMode())) {
TemporalDependencyMatrix temporal = this.utils.calculateTemporalDistance(subrules[i]);
context.setTemporalDistance(temporal);
}
if (kBase.getConfiguration().isSequential()) {
context.setTupleMemoryEnabled(false);
context.setObjectTypeNodeMemoryEnabled(false);
} else {
context.setTupleMemoryEnabled(true);
context.setObjectTypeNodeMemoryEnabled(true);
}
// adds subrule
final TerminalNode node = this.addSubRule(context, subrules[i], i, rule);
// adds the terminal node to the list of terminal nodes
nodes.add(node);
}
return nodes;
}
use of org.drools.core.time.TemporalDependencyMatrix in project drools by kiegroup.
the class BuildUtils method calculateTemporalDistance.
/**
* Calculates the temporal distance between all event patterns in the given
* subrule.
*
* @param groupElement the root element of a subrule being added to the rulebase
*/
public TemporalDependencyMatrix calculateTemporalDistance(GroupElement groupElement) {
// find the events
List<Pattern> events = new ArrayList<Pattern>();
selectAllEventPatterns(events, groupElement);
final int size = events.size();
if (size >= 1) {
// create the matrix
Interval[][] source = new Interval[size][];
for (int row = 0; row < size; row++) {
source[row] = new Interval[size];
for (int col = 0; col < size; col++) {
if (row == col) {
source[row][col] = new Interval(0, 0);
} else {
source[row][col] = new Interval(Interval.MIN, Interval.MAX);
}
}
}
Interval[][] result;
if (size > 1) {
List<Declaration> declarations = new ArrayList<>();
int eventIndex = 0;
// populate the matrix
for (Pattern event : events) {
// references to other events are always backward references, so we can build the list as we go
declarations.add(event.getDeclaration());
Map<Declaration, Interval> temporal = new HashMap<>();
gatherTemporalRelationships(event.getConstraints(), temporal);
// intersects default values with the actual constrained intervals
for (Map.Entry<Declaration, Interval> entry : temporal.entrySet()) {
int targetIndex = declarations.indexOf(entry.getKey());
Interval interval = entry.getValue();
source[targetIndex][eventIndex].intersect(interval);
Interval reverse = new Interval(interval.getUpperBound() == Long.MAX_VALUE ? Long.MIN_VALUE : -interval.getUpperBound(), interval.getLowerBound() == Long.MIN_VALUE ? Long.MAX_VALUE : -interval.getLowerBound());
source[eventIndex][targetIndex].intersect(reverse);
}
eventIndex++;
}
result = TimeUtils.calculateTemporalDistance(source);
} else {
result = source;
}
return new TemporalDependencyMatrix(result, events);
}
return null;
}
use of org.drools.core.time.TemporalDependencyMatrix in project drools by kiegroup.
the class ReteooRuleBuilder method addRule.
/**
* Creates the corresponting Rete network for the given <code>Rule</code> and adds it to
* the given rule base.
*
* @param rule
* The rule to add.
* @param kBase
* The rulebase to add the rule to.
*
* @return a List<BaseNode> of terminal nodes for the rule
* @throws InvalidPatternException
*/
public List<TerminalNode> addRule(RuleImpl rule, RuleBase kBase, Collection<InternalWorkingMemory> workingMemories) throws InvalidPatternException {
// the list of terminal nodes
final List<TerminalNode> nodes = new ArrayList<>();
// transform rule and gets the array of subrules
final GroupElement[] subrules = rule.getTransformedLhs(LogicTransformer.getInstance(), kBase.getGlobals());
for (int i = 0; i < subrules.length; i++) {
// creates a clean build context for each subrule
final BuildContext context = new BuildContext(kBase, workingMemories);
context.setRule(rule);
// if running in STREAM mode, calculate temporal distance for events
if (EventProcessingOption.STREAM.equals(kBase.getConfiguration().getEventProcessingMode())) {
TemporalDependencyMatrix temporal = this.utils.calculateTemporalDistance(subrules[i]);
context.setTemporalDistance(temporal);
}
if (kBase.getConfiguration().isSequential()) {
context.setTupleMemoryEnabled(false);
context.setObjectTypeNodeMemoryEnabled(false);
} else {
context.setTupleMemoryEnabled(true);
context.setObjectTypeNodeMemoryEnabled(true);
}
// adds subrule
final TerminalNode node = this.addSubRule(context, subrules[i], i, rule);
// adds the terminal node to the list of terminal nodes
nodes.add(node);
}
return nodes;
}
use of org.drools.core.time.TemporalDependencyMatrix in project drools by kiegroup.
the class BuildUtilsTest method testCalculateTemporalDistance.
/**
* Test method for {@link org.drools.core.reteoo.builder.BuildUtils#calculateTemporalDistance(org.drools.core.rule.GroupElement)}.
*/
@Test
public void testCalculateTemporalDistance() {
// input is here just for "documentation" purposes
Interval[][] input = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 4), new Interval(MIN, MAX), new Interval(MIN, MAX) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(MIN, MAX), new Interval(1, 2), new Interval(MIN, MAX) }, { new Interval(-4, 3), new Interval(MIN, MAX), new Interval(0, 0), new Interval(2, 3), new Interval(MIN, MAX) }, { new Interval(MIN, MAX), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(-10, -1), new Interval(0, 0) } };
Interval[][] expected = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 2), new Interval(-1, 4), new Interval(0, 14) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(-2, 0), new Interval(1, 2), new Interval(2, 12) }, { new Interval(-2, 3), new Interval(0, 2), new Interval(0, 0), new Interval(2, 3), new Interval(3, 13) }, { new Interval(-4, 1), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(-14, 0), new Interval(-12, -2), new Interval(-13, -3), new Interval(-10, -1), new Interval(0, 0) } };
AfterEvaluatorDefinition evals = new AfterEvaluatorDefinition();
ClassObjectType ot = new ClassObjectType(StockTick.class, true);
Pattern a = new Pattern(0, ot, "$a");
Pattern b = new Pattern(1, ot, "$b");
b.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-2,2"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern c = new Pattern(2, ot, "$c");
c.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-3,4"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern d = new Pattern(3, ot, "$d");
d.addConstraint(new EvaluatorConstraint(new Declaration[] { b.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,2"), new SelfReferenceClassFieldReader(StockTick.class)));
d.addConstraint(new EvaluatorConstraint(new Declaration[] { c.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "2,3"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern e = new Pattern(4, ot, "$e");
e.addConstraint(new EvaluatorConstraint(new Declaration[] { d.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,10"), new SelfReferenceClassFieldReader(StockTick.class)));
GroupElement not = new GroupElement(Type.NOT);
not.addChild(e);
GroupElement and = new GroupElement(Type.AND);
and.addChild(a);
and.addChild(b);
and.addChild(c);
and.addChild(d);
and.addChild(not);
TemporalDependencyMatrix matrix = utils.calculateTemporalDistance(and);
// printMatrix( matrix.getMatrix() );
assertEqualsMatrix(expected, matrix.getMatrix());
assertEquals(15, matrix.getExpirationOffset(a));
assertEquals(11, matrix.getExpirationOffset(d));
assertEquals(1, matrix.getExpirationOffset(e));
}
use of org.drools.core.time.TemporalDependencyMatrix in project drools by kiegroup.
the class BuildUtilsTest method testCalculateTemporalDistance.
/**
* Test method for {@link org.drools.core.reteoo.builder.BuildUtils#calculateTemporalDistance(org.drools.core.rule.GroupElement)}.
*/
@Test
public void testCalculateTemporalDistance() {
// input is here just for "documentation" purposes
Interval[][] input = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 4), new Interval(MIN, MAX), new Interval(MIN, MAX) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(MIN, MAX), new Interval(1, 2), new Interval(MIN, MAX) }, { new Interval(-4, 3), new Interval(MIN, MAX), new Interval(0, 0), new Interval(2, 3), new Interval(MIN, MAX) }, { new Interval(MIN, MAX), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(-10, -1), new Interval(0, 0) } };
Interval[][] expected = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 2), new Interval(-1, 4), new Interval(0, 14) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(-2, 0), new Interval(1, 2), new Interval(2, 12) }, { new Interval(-2, 3), new Interval(0, 2), new Interval(0, 0), new Interval(2, 3), new Interval(3, 13) }, { new Interval(-4, 1), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(-14, 0), new Interval(-12, -2), new Interval(-13, -3), new Interval(-10, -1), new Interval(0, 0) } };
AfterEvaluatorDefinition evals = new AfterEvaluatorDefinition();
ClassObjectType ot = new ClassObjectType(StockTick.class, true);
Pattern a = new Pattern(0, ot, "$a");
Pattern b = new Pattern(1, ot, "$b");
b.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-2,2"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern c = new Pattern(2, ot, "$c");
c.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-3,4"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern d = new Pattern(3, ot, "$d");
d.addConstraint(new EvaluatorConstraint(new Declaration[] { b.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,2"), new SelfReferenceClassFieldReader(StockTick.class)));
d.addConstraint(new EvaluatorConstraint(new Declaration[] { c.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "2,3"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern e = new Pattern(4, ot, "$e");
e.addConstraint(new EvaluatorConstraint(new Declaration[] { d.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,10"), new SelfReferenceClassFieldReader(StockTick.class)));
GroupElement not = new GroupElement(Type.NOT);
not.addChild(e);
GroupElement and = new GroupElement(Type.AND);
and.addChild(a);
and.addChild(b);
and.addChild(c);
and.addChild(d);
and.addChild(not);
TemporalDependencyMatrix matrix = utils.calculateTemporalDistance(and);
// printMatrix( matrix.getMatrix() );
assertEqualsMatrix(expected, matrix.getMatrix());
assertEquals(15, matrix.getExpirationOffset(a));
assertEquals(11, matrix.getExpirationOffset(d));
assertEquals(1, matrix.getExpirationOffset(e));
}
Aggregations