Search in sources :

Example 1 with XpathConstraint

use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.

the class PatternBuilder method buildXPathDescr.

private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ExpressionDescr descr, DumperContext mvelCtx) {
    String expression = descr.getExpression();
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
        return null;
    }
    XpathConstraint xpathConstraint = new XpathConstraint();
    ObjectType objectType = pattern.getObjectType();
    if (objectType.isTemplate()) {
        throw new UnsupportedOperationException("xpath is not supported with fact templates");
    }
    Class<?> patternClass = ((ClassObjectType) objectType).getClassType();
    List<Class<?>> backReferenceClasses = new ArrayList<>();
    backReferenceClasses.add(patternClass);
    XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
    pattern.setBackRefDeclarations(backRef);
    ObjectType originalType = pattern.getObjectType();
    ObjectType currentObjectType = originalType;
    mvelCtx.setInXpath(true);
    try {
        // then the xpathoffset must be adjusted by -1, as pattern is not actually added to the rete network
        for (XpathAnalysis.XpathPart part : xpathAnalysis) {
            XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
            // make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().wireObjectType(currentObjectType, xpathChunk);
            if (xpathChunk == null) {
                registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
                pattern.setObjectType(originalType);
                return null;
            }
            if (part.getInlineCast() != null) {
                try {
                    patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
                } catch (ClassNotFoundException e) {
                    registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
                    return null;
                }
                part.addInlineCastConstraint(patternClass);
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
                xpathChunk.setReturnedType(currentObjectType);
            } else {
                patternClass = xpathChunk.getReturnedClass();
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
            }
            context.increaseXpathChuckNr();
            pattern.setObjectType(currentObjectType);
            backReferenceClasses.add(0, patternClass);
            backRef.reset();
            for (String constraint : part.getConstraints()) {
                ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
                if (result == null) {
                    continue;
                }
                // preserving this, as the recursive build() resets it
                int chunkNbr = context.getXpathChuckNr();
                for (Constraint c : build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx)) {
                    xpathChunk.addConstraint(c);
                }
                context.setXpathChuckNr(chunkNbr);
            }
        }
        xpathConstraint.setXpathStartDeclaration(xpathStartDeclaration);
        if (descr instanceof BindingDescr) {
            Declaration pathDeclr = pattern.addDeclaration(((BindingDescr) descr).getVariable());
            pathDeclr.setxPathOffset(context.getXpathChuckNr());
            xpathConstraint.setDeclaration(pathDeclr);
        }
    } finally {
        mvelCtx.setInXpath(false);
        pattern.setBackRefDeclarations(null);
        pattern.setObjectType(originalType);
        context.resetXpathChuckNr();
    }
    return xpathConstraint;
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BindingDescr(org.drools.drl.ast.descr.BindingDescr) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) ArrayList(java.util.ArrayList) XpathBackReference(org.drools.core.rule.XpathBackReference) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 2 with XpathConstraint

use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.

the class PatternBuilder method buildXPathDescr.

private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
    String expression = ((ExpressionDescr) descr).getExpression();
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
        return null;
    }
    XpathConstraint xpathConstraint = new XpathConstraint();
    ObjectType objectType = pattern.getObjectType();
    Class<?> patternClass = objectType.getClassType();
    List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
    backReferenceClasses.add(patternClass);
    XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
    pattern.setBackRefDeclarations(backRef);
    ObjectType originalType = pattern.getObjectType();
    ObjectType currentObjectType = originalType;
    mvelCtx.setInXpath(true);
    try {
        for (XpathAnalysis.XpathPart part : xpathAnalysis) {
            XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
            // make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
            if (xpathChunk == null) {
                registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
                pattern.setObjectType(originalType);
                return null;
            }
            if (part.getInlineCast() != null) {
                try {
                    patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
                } catch (ClassNotFoundException e) {
                    registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
                    return null;
                }
                part.addInlineCastConstraint(patternClass);
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
                xpathChunk.setReturnedType(currentObjectType);
            } else {
                patternClass = xpathChunk.getReturnedClass();
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
            }
            pattern.setObjectType(currentObjectType);
            backReferenceClasses.add(0, patternClass);
            backRef.reset();
            for (String constraint : part.getConstraints()) {
                ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
                if (result == null) {
                    continue;
                }
                for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
                    xpathChunk.addConstraint(c);
                }
            }
        }
    } finally {
        mvelCtx.setInXpath(false);
        pattern.setBackRefDeclarations(null);
        pattern.setObjectType(originalType);
    }
    xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
    if (descr instanceof BindingDescr) {
        xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
    }
    return xpathConstraint;
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) ArrayList(java.util.ArrayList) XpathBackReference(org.drools.core.rule.XpathBackReference) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ExpressionDescr(org.drools.compiler.lang.descr.ExpressionDescr) XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 3 with XpathConstraint

use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.

the class PatternBuilder method buildXpathConstraints.

private void buildXpathConstraints(BuildContext context, BuildUtils utils, Pattern pattern, Constraints constraints) {
    if (!constraints.xpathConstraints.isEmpty()) {
        buildTupleSource(context, utils);
        if (constraints.xpathConstraints.size() == 1 && constraints.xpathConstraints.get(0).getXpathStartDeclaration() != null) {
            context.setObjectSource(null);
            context.decrementCurrentPatternOffset();
        } else {
            buildJoinNode(context, utils);
        }
        ReteooComponentBuilder builder = utils.getBuilderFor(XpathConstraint.class);
        for (XpathConstraint xpathConstraint : constraints.xpathConstraints) {
            for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
                context.setAlphaConstraints(chunk.getAlphaConstraints());
                context.setBetaconstraints(chunk.getBetaConstraints());
                context.setXpathConstraints(chunk.getXpathConstraints());
                builder.build(context, utils, chunk.asFrom());
            }
            Declaration declaration = xpathConstraint.getDeclaration();
            if (declaration != null) {
                // oopath actually have the binding
                Pattern clonedPattern = new Pattern(pattern.getIndex(), context.getCurrentPatternOffset(), new ClassObjectType(xpathConstraint.getResultClass()), declaration.getIdentifier(), declaration.isInternalFact());
                declaration.setPattern(clonedPattern);
            }
        }
        context.popRuleComponent();
    }
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration)

Example 4 with XpathConstraint

use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.

the class PatternBuilder method buildXpathConstraints.

private void buildXpathConstraints(BuildContext context, BuildUtils utils, Constraints constraints) {
    if (!constraints.xpathConstraints.isEmpty()) {
        buildTupleSource(context, utils, false);
        if (constraints.xpathConstraints.size() == 1 && constraints.xpathConstraints.get(0).getXpathStartDeclaration() != null) {
            context.setObjectSource(null);
        } else {
            buildJoinNode(context, utils);
        }
        ReteooComponentBuilder builder = utils.getBuilderFor(XpathConstraint.class);
        for (XpathConstraint xpathConstraint : constraints.xpathConstraints) {
            for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
                context.setAlphaConstraints(chunk.getAlphaConstraints());
                context.setBetaconstraints(chunk.getBetaConstraints());
                context.setXpathConstraints(chunk.getXpathConstraints());
                builder.build(context, utils, chunk.asFrom());
            }
        }
        context.popRuleComponent();
    }
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint)

Example 5 with XpathConstraint

use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.

the class PatternBuilder method createConstraints.

private Constraints createConstraints(BuildContext context, Pattern pattern) {
    Constraints constraints = new Constraints();
    // check if cross products for identity patterns should be disabled
    checkRemoveIdentities(context, pattern, constraints.betaConstraints);
    // checks if this pattern is nested inside a NOT CE
    final boolean isNegative = isNegative(context);
    for (Constraint constraint : pattern.getConstraints()) {
        switch(constraint.getType()) {
            case ALPHA:
                linkAlphaConstraint((AlphaNodeFieldConstraint) constraint, constraints.alphaConstraints);
                break;
            case BETA:
                linkBetaConstraint((BetaNodeFieldConstraint) constraint, constraints.betaConstraints);
                if (isNegative && context.getRuleBase().getConfiguration().getEventProcessingMode() == EventProcessingOption.STREAM && pattern.getObjectType().isEvent() && constraint.isTemporal()) {
                    checkDelaying(context, constraint);
                }
                break;
            case XPATH:
                constraints.xpathConstraints.add((XpathConstraint) constraint);
                break;
            default:
                throw new RuntimeException("Unknown constraint type: " + constraint.getType() + ". This is a bug. Please contact development team.");
        }
    }
    return constraints;
}
Also used : BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IntervalProviderConstraint(org.drools.core.rule.IntervalProviderConstraint) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint)

Aggregations

XpathConstraint (org.drools.core.rule.constraint.XpathConstraint)6 ClassObjectType (org.drools.core.base.ClassObjectType)3 Constraint (org.drools.core.spi.Constraint)3 ArrayList (java.util.ArrayList)2 XpathPart (org.drools.compiler.rule.builder.XpathAnalysis.XpathPart)2 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)2 Declaration (org.drools.core.rule.Declaration)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 XpathBackReference (org.drools.core.rule.XpathBackReference)2 NegConstraint (org.drools.core.rule.constraint.NegConstraint)2 AcceptsClassObjectType (org.drools.core.spi.AcceptsClassObjectType)2 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)2 ObjectType (org.drools.core.spi.ObjectType)2 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)1 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)1 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)1 ExpressionDescr (org.drools.compiler.lang.descr.ExpressionDescr)1 BetaConstraints (org.drools.core.common.BetaConstraints)1 ReactiveFromNode (org.drools.core.reteoo.ReactiveFromNode)1