use of org.drools.core.rule.Pattern 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<Declaration>();
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<Declaration, Interval>();
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.rule.Pattern in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
private void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
// TODO: process annotations
// process pattern
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
dummy.setResource(packageDescr.getResource());
dummy.addAttribute(new AttributeDescr("dialect", "java"));
RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
if (builder != null) {
final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
if (pattern.getXpathConstraint() != null) {
context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
}
window.setPattern(pattern);
} else {
throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
}
if (!context.getErrors().isEmpty()) {
for (DroolsError error : context.getErrors()) {
addBuilderResult(error);
}
} else {
pkgRegistry.getPackage().addWindowDeclaration(window);
}
}
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class AnnotationsTest method testNestedAnnotations.
@Test
public void testNestedAnnotations() {
String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Outer( value = @Inner( text = \"world\" ) ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
Map<String, AnnotationDefinition> defs = p.getAnnotations();
assertEquals(1, defs.size());
AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
assertNotNull(outer);
Object val = outer.getPropertyValue("value");
assertNotNull(val);
assertTrue(val instanceof AnnotationDefinition);
AnnotationDefinition inner = (AnnotationDefinition) val;
assertEquals("world", inner.getPropertyValue("text"));
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class AnnotationsTest method testNestedAnnotationsWithMultiplicity.
@Test
public void testNestedAnnotationsWithMultiplicity() {
String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + " String() @Outer( values = { @Inner( text = \"hello\" ), @Inner( text = \"world\" ) } ) " + "then " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
assertEquals(0, helper.verify().getMessages().size());
Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
Map<String, AnnotationDefinition> defs = p.getAnnotations();
assertEquals(1, defs.size());
AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
assertNotNull(outer);
Object val = outer.getPropertyValue("values");
assertNotNull(val);
assertTrue(val instanceof AnnotationDefinition[]);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class ForallBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final ForallDescr forallDescr = (ForallDescr) descr;
final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
final Pattern basePattern = (Pattern) patternBuilder.build(context, forallDescr.getBasePattern());
if (basePattern == null) {
return null;
}
final Forall forall = new Forall(basePattern);
// adding the newly created forall CE to the build stack
// this is necessary in case of local declaration usage
context.getDeclarationResolver().pushOnBuildStack(forall);
for (BaseDescr baseDescr : forallDescr.getRemainingPatterns()) {
final Pattern anotherPattern = (Pattern) patternBuilder.build(context, (PatternDescr) baseDescr);
forall.addRemainingPattern(anotherPattern);
}
if (forallDescr.getDescrs().size() == 1) {
// An optimization for unlinking, where we allow unlinking if the resulting 'not' node has no constraints
// we need to record this here, due to getRemainingPatterns injecting "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
// which we wish to ignore
PatternDescr p = (PatternDescr) forallDescr.getDescrs().get(0);
if (p.getConstraint().getDescrs().isEmpty()) {
forall.setEmptyBetaConstraints(true);
}
}
// poping the forall
context.getDeclarationResolver().popBuildStack();
return forall;
}
Aggregations