Search in sources :

Example 41 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class RuleTerminalNode method setDeclarations.

public void setDeclarations(Map<String, Declaration> decls) {
    if (rule.getSalience() instanceof MVELSalienceExpression) {
        MVELSalienceExpression expr = (MVELSalienceExpression) rule.getSalience();
        Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
        this.salienceDeclarations = new Declaration[declrs.length];
        int i = 0;
        for (Declaration declr : declrs) {
            this.salienceDeclarations[i++] = decls.get(declr.getIdentifier());
        }
        Arrays.sort(this.salienceDeclarations, SortDeclarations.instance);
    }
    if (rule.getEnabled() instanceof MVELEnabledExpression) {
        MVELEnabledExpression expr = (MVELEnabledExpression) rule.getEnabled();
        Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
        this.enabledDeclarations = new Declaration[declrs.length];
        int i = 0;
        for (Declaration declr : declrs) {
            this.enabledDeclarations[i++] = decls.get(declr.getIdentifier());
        }
        Arrays.sort(this.enabledDeclarations, SortDeclarations.instance);
    }
    if (rule.getTimer() instanceof BaseTimer) {
        this.timerDeclarations = ((BaseTimer) rule.getTimer()).getTimerDeclarations(decls);
    }
}
Also used : MVELEnabledExpression(org.drools.core.base.mvel.MVELEnabledExpression) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) BaseTimer(org.drools.core.time.impl.BaseTimer) Declaration(org.drools.core.rule.Declaration)

Example 42 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class RuleTerminalNodeLeftTuple method getDeclarationIds.

public List<String> getDeclarationIds() {
    Declaration[] declArray = ((org.drools.core.reteoo.RuleTerminalNode) getTupleSink()).getAllDeclarations();
    List<String> declarations = new ArrayList<String>();
    for (Declaration decl : declArray) {
        declarations.add(decl.getIdentifier());
    }
    return Collections.unmodifiableList(declarations);
}
Also used : ArrayList(java.util.ArrayList) Declaration(org.drools.core.rule.Declaration)

Example 43 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class BuildUtils method gatherTemporalRelationships.

private void gatherTemporalRelationships(List<?> constraints, Map<Declaration, Interval> temporal) {
    for (Object obj : constraints) {
        if (obj instanceof IntervalProviderConstraint) {
            IntervalProviderConstraint constr = (IntervalProviderConstraint) obj;
            if (constr.isTemporal()) {
                // if a constraint already exists, calculate the intersection
                Declaration[] decs = constr.getRequiredDeclarations();
                // only calculate relationships to other event patterns
                if (decs.length > 0 && decs[0].isPatternDeclaration() && decs[0].getPattern().getObjectType().isEvent()) {
                    Declaration target = decs[0];
                    Interval interval = temporal.get(target);
                    if (interval == null) {
                        interval = constr.getInterval();
                        temporal.put(target, interval);
                    } else {
                        interval.intersect(constr.getInterval());
                    }
                }
            }
        } else if (obj instanceof AbstractCompositeConstraint) {
            gatherTemporalRelationships(Arrays.asList(((AbstractCompositeConstraint) obj).getBetaConstraints()), temporal);
        }
    }
}
Also used : Declaration(org.drools.core.rule.Declaration) AbstractCompositeConstraint(org.drools.core.rule.AbstractCompositeConstraint) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint) Interval(org.drools.core.time.Interval)

Example 44 with Declaration

use of org.drools.core.rule.Declaration 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 45 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class ExpressionIntervalTimer method createTrigger.

public Trigger createTrigger(long timestamp, Tuple leftTuple, DefaultJobHandle jh, String[] calendarNames, Calendars calendars, Declaration[][] declrs, InternalWorkingMemory wm) {
    long timeSinceLastFire = 0;
    Declaration[] delayDeclarations = declrs[0];
    Declaration[] periodDeclarations = declrs[1];
    Declaration[] startDeclarations = declrs[2];
    Declaration[] endDeclarations = declrs[3];
    Date lastFireTime = null;
    Date createdTime = null;
    long newDelay = 0;
    if (jh != null) {
        IntervalTrigger preTrig = (IntervalTrigger) jh.getTimerJobInstance().getTrigger();
        lastFireTime = preTrig.getLastFireTime();
        createdTime = preTrig.getCreatedTime();
        if (lastFireTime != null) {
            // it is already fired calculate the new delay using the period instead of the delay
            newDelay = evalTimeExpression(this.period, leftTuple, delayDeclarations, wm) - timestamp + lastFireTime.getTime();
        } else {
            newDelay = evalTimeExpression(this.delay, leftTuple, delayDeclarations, wm) - timestamp + createdTime.getTime();
        }
    } else {
        newDelay = evalTimeExpression(this.delay, leftTuple, delayDeclarations, wm);
    }
    if (newDelay < 0) {
        newDelay = 0;
    }
    return new IntervalTrigger(timestamp, evalDateExpression(this.startTime, leftTuple, startDeclarations, wm), evalDateExpression(this.endTime, leftTuple, startDeclarations, wm), this.repeatLimit, newDelay, period != null ? evalTimeExpression(this.period, leftTuple, periodDeclarations, wm) : 0, calendarNames, calendars, createdTime, lastFireTime);
}
Also used : Declaration(org.drools.core.rule.Declaration) Date(java.util.Date)

Aggregations

Declaration (org.drools.core.rule.Declaration)115 Pattern (org.drools.core.rule.Pattern)42 ClassObjectType (org.drools.core.base.ClassObjectType)29 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)27 TypeDeclaration (org.drools.core.rule.TypeDeclaration)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)17 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)17 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)16 Test (org.junit.Test)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)14 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)12 WorkingMemory (org.drools.core.WorkingMemory)11 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)11 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 Consequence (org.drools.core.spi.Consequence)10 Constraint (org.drools.core.spi.Constraint)10 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)9