Search in sources :

Example 11 with FromDescr

use of org.drools.drl.ast.descr.FromDescr in project drools by kiegroup.

the class PatternBuilder method lookupObjectType.

private Declaration lookupObjectType(RuleBuildContext context, PatternDescr patternDescr) {
    List<? extends BaseDescr> descrs = patternDescr.getConstraint().getDescrs();
    if (descrs.size() != 1 || !(descrs.get(0) instanceof ExprConstraintDescr)) {
        return null;
    }
    ExprConstraintDescr descr = (ExprConstraintDescr) descrs.get(0);
    String expr = descr.getExpression();
    if (expr.charAt(0) != '/') {
        return null;
    }
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expr);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expr + "': " + xpathAnalysis.getError());
        return null;
    }
    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()) {
            String xpathExpr = (patternDescr.getIdentifier() == null ? "" : patternDescr.getIdentifier() + " : ") + expr.substring(xpathAnalysis.getPart(1).getStart());
            patternDescr.addConstraint(new ExprConstraintDescr(xpathExpr));
            patternDescr.setIdentifier("$void$");
        }
    } else {
        Declaration declr = resolver.getDeclaration(identifier);
        if (declr == null) {
            registerDescrBuildError(context, patternDescr, "The identifier '" + identifier + "' is not in scope");
            return null;
        }
        patternDescr.setObjectType(declr.getExtractor().getExtractToClassName());
        expr = (patternDescr.getIdentifier() != null ? patternDescr.getIdentifier() + (patternDescr.isUnification() ? " := " : " : ") : "") + expr.substring(identifier.length() + 1);
        descr.setExpression(expr);
        return declr;
    }
    return null;
}
Also used : XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) DeclarationScopeResolver(org.drools.core.spi.DeclarationScopeResolver) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 12 with FromDescr

use of org.drools.drl.ast.descr.FromDescr in project drools by kiegroup.

the class PatternBuilder method isPassThroughPattern.

private static boolean isPassThroughPattern(PatternDescr pattern, String identifier) {
    // We should add properties in the 2nd pattern to create a correct property reactivity mask.
    if (pattern.getSource() instanceof FromDescr) {
        FromDescr from = (FromDescr) pattern.getSource();
        String expr = from.getExpression().trim();
        if (identifier.equals(expr)) {
            return true;
        }
    }
    return false;
}
Also used : FromDescr(org.drools.drl.ast.descr.FromDescr)

Example 13 with FromDescr

use of org.drools.drl.ast.descr.FromDescr in project drools by kiegroup.

the class PatternUtil method normalizeOOPathPattern.

public static PatternDescr normalizeOOPathPattern(PatternDescr pattern, RuleContext context) {
    String oopathExpr = pattern.getDescrs().get(0).getText();
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(oopathExpr);
    XpathAnalysis.XpathPart firstPart = xpathAnalysis.getPart(0);
    PatternDescr normalizedPattern = new PatternDescr();
    normalizedPattern.setObjectType(findPatternType(firstPart, context));
    firstPart.getConstraints().stream().map(ExprConstraintDescr::new).forEach(normalizedPattern::addConstraint);
    if (xpathAnalysis.getParts().size() == 1) {
        normalizedPattern.setIdentifier(pattern.getIdentifier());
    } else {
        StringBuilder sb = new StringBuilder();
        if (pattern.getIdentifier() != null) {
            sb.append(pattern.getIdentifier()).append(": ");
        }
        for (int i = 1; i < xpathAnalysis.getParts().size(); i++) {
            sb.append("/").append(xpathAnalysis.getPart(i));
        }
        normalizedPattern.addConstraint(new ExprConstraintDescr(sb.toString()));
    }
    FromDescr source = new FromDescr();
    source.setDataSource(new MVELExprDescr(firstPart.getField()));
    normalizedPattern.setSource(source);
    return normalizedPattern;
}
Also used : XpathAnalysis(org.drools.compiler.rule.builder.XpathAnalysis) PatternDescr(org.drools.drl.ast.descr.PatternDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 14 with FromDescr

use of org.drools.drl.ast.descr.FromDescr in project drools by kiegroup.

the class SourceDescrBuilderImpl method expression.

public P expression(String expression) {
    FromDescr from = new FromDescr();
    from.setDataSource(new MVELExprDescr(expression));
    from.setResource(descr.getResource());
    descr.setSource(from);
    return parent;
}
Also used : FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr)

Example 15 with FromDescr

use of org.drools.drl.ast.descr.FromDescr in project drools by kiegroup.

the class AccumulateInline method parseAccumulatePattern.

void parseAccumulatePattern() {
    PatternDescr pattern = accumulateDescr.getInputPattern();
    if (pattern == null || pattern.getSource() == null) {
        return;
    }
    PatternSourceDescr sourceDescr = pattern.getSource();
    if (sourceDescr instanceof FromDescr) {
        DeclarativeInvokerDescr invokerDescr = ((FromDescr) sourceDescr).getDataSource();
        String mvelBlock = addCurlyBracesToBlock(addSemicolon(invokerDescr.getText()));
        CompiledBlockResult fromCodeCompilationResult = mvelCompiler.compileStatement(mvelBlock);
        BlockStmt fromBlock = fromCodeCompilationResult.statementResults();
        for (Statement stmt : fromBlock.getStatements()) {
            stmt.findAll(NameExpr.class).stream().map(Node::toString).filter(context::hasDeclaration).forEach(usedExternalDeclarations::add);
        }
    }
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) PatternSourceDescr(org.drools.drl.ast.descr.PatternSourceDescr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) FromDescr(org.drools.drl.ast.descr.FromDescr) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult) DeclarativeInvokerDescr(org.drools.drl.ast.descr.DeclarativeInvokerDescr)

Aggregations

FromDescr (org.drools.drl.ast.descr.FromDescr)20 PatternDescr (org.drools.drl.ast.descr.PatternDescr)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)11 Test (org.junit.Test)11 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)10 PackageDescr (org.drools.drl.ast.descr.PackageDescr)6 Declaration (org.drools.core.rule.Declaration)2 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)2 NotDescr (org.drools.drl.ast.descr.NotDescr)2 Element (org.w3c.dom.Element)2 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)1 Node (com.github.javaparser.ast.Node)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 Statement (com.github.javaparser.ast.stmt.Statement)1 InputStreamReader (java.io.InputStreamReader)1 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 XpathAnalysis (org.drools.compiler.rule.builder.XpathAnalysis)1