Search in sources :

Example 41 with Pattern

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;
}
Also used : Pattern(org.drools.core.rule.Pattern) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) AbstractCompositeConstraint(org.drools.core.rule.AbstractCompositeConstraint) TemporalDependencyMatrix(org.drools.core.time.TemporalDependencyMatrix) Declaration(org.drools.core.rule.Declaration) HashMap(java.util.HashMap) Map(java.util.Map) Interval(org.drools.core.time.Interval)

Example 42 with Pattern

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);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) DroolsError(org.drools.compiler.compiler.DroolsError) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) WindowDeclaration(org.drools.core.rule.WindowDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) WindowDeclarationDescr(org.drools.compiler.lang.descr.WindowDeclarationDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 43 with Pattern

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"));
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 44 with Pattern

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[]);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 45 with Pattern

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;
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Forall(org.drools.core.rule.Forall) ForallDescr(org.drools.compiler.lang.descr.ForallDescr)

Aggregations

Pattern (org.drools.core.rule.Pattern)86 Declaration (org.drools.core.rule.Declaration)43 ClassObjectType (org.drools.core.base.ClassObjectType)39 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)30 Test (org.junit.Test)30 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)23 GroupElement (org.drools.core.rule.GroupElement)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)13 ObjectType (org.drools.core.spi.ObjectType)13 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)12 WorkingMemory (org.drools.core.WorkingMemory)12 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)12 Consequence (org.drools.core.spi.Consequence)12 IOException (java.io.IOException)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)11 TypeDeclaration (org.drools.core.rule.TypeDeclaration)11 InternalFactHandle (org.drools.core.common.InternalFactHandle)10