Search in sources :

Example 1 with XpathPart

use of org.drools.compiler.rule.builder.XpathAnalysis.XpathPart in project drools by kiegroup.

the class PatternBuilder method lookupObjectType.

private void lookupObjectType(RuleBuildContext context, PatternDescr patternDescr) {
    List<? extends BaseDescr> descrs = patternDescr.getConstraint().getDescrs();
    if (descrs.size() != 1 || !(descrs.get(0) instanceof ExprConstraintDescr)) {
        return;
    }
    ExprConstraintDescr descr = (ExprConstraintDescr) descrs.get(0);
    String expr = descr.getExpression();
    if (expr.charAt(0) != '/') {
        return;
    }
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expr);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expr + "': " + xpathAnalysis.getError());
        return;
    }
    XpathPart firstXpathChunk = xpathAnalysis.getPart(0);
    String identifier = firstXpathChunk.getField();
    DeclarationScopeResolver resolver = context.getDeclarationResolver();
    if (resolver.hasDataSource(identifier)) {
        patternDescr.setObjectType(findObjectType(context, firstXpathChunk, identifier));
        FromDescr fromDescr = new FromDescr();
        fromDescr.setDataSource(new MVELExprDescr(identifier));
        patternDescr.setSource(fromDescr);
        patternDescr.removeAllConstraint();
        firstXpathChunk.getConstraints().forEach(s -> patternDescr.addConstraint(new ExprConstraintDescr(s)));
        if (!xpathAnalysis.isSinglePart()) {
            patternDescr.addConstraint(new ExprConstraintDescr(patternDescr.getIdentifier() + " : " + expr.substring(xpathAnalysis.getPart(1).getStart())));
            patternDescr.setIdentifier("$void$");
        }
    } else {
        Declaration declr = resolver.getDeclaration(identifier);
        patternDescr.setXpathStartDeclaration(declr);
        patternDescr.setObjectType(declr.getExtractor().getExtractToClassName());
        expr = patternDescr.getIdentifier() + (patternDescr.isUnification() ? " := " : " : ") + expr.substring(identifier.length() + 1);
        descr.setExpression(expr);
    }
}
Also used : XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) DeclarationScopeResolver(org.drools.core.spi.DeclarationScopeResolver) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Aggregations

ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)1 FromDescr (org.drools.compiler.lang.descr.FromDescr)1 MVELExprDescr (org.drools.compiler.lang.descr.MVELExprDescr)1 XpathPart (org.drools.compiler.rule.builder.XpathAnalysis.XpathPart)1 Declaration (org.drools.core.rule.Declaration)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 DeclarationScopeResolver (org.drools.core.spi.DeclarationScopeResolver)1