Search in sources :

Example 16 with FromDescr

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

the class MVELFromBuilder method build.

public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
    String text = ((FromDescr) descr).getExpression();
    Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
    if (entryPointId.isPresent()) {
        return entryPointId.get();
    }
    // This builder is re-usable in other dialects, so specify by name
    MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
    boolean typeSafe = context.isTypesafe();
    if (!dialect.isStrictMode()) {
        context.setTypesafe(false);
    }
    try {
        Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
        AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
        if (analysis == null) {
            // something bad happened
            return null;
        }
        Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
        if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
            context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
            return null;
        }
        final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
        final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
        int j = 0;
        for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
            declarations[j++] = decls.get(str);
        }
        Arrays.sort(declarations, SortDeclarations.instance);
        MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
        MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
        From from = new From(dataProvider);
        from.setResultPattern(prefixPattern);
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
        data.addCompileable(from, dataProvider);
        dataProvider.compile(data, context.getRule());
        return from;
    } catch (final Exception e) {
        AsmUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
        return null;
    } finally {
        context.setTypesafe(typeSafe);
    }
}
Also used : MVELDataProvider(org.drools.mvel.dataproviders.MVELDataProvider) MVELCompilationUnit(org.drools.mvel.expr.MVELCompilationUnit) FromDescr(org.drools.drl.ast.descr.FromDescr) From(org.drools.core.rule.From) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) MVELDialectRuntimeData(org.drools.mvel.MVELDialectRuntimeData) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) EntryPointId(org.drools.core.rule.EntryPointId) Declaration(org.drools.core.rule.Declaration)

Example 17 with FromDescr

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

the class RuleParserTest method testFromWithInlineListMethod.

@Test
public void testFromWithInlineListMethod() throws Exception {
    String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3].sublist(1, 2) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("XYZ", rule.getName());
    assertFalse(parser.hasErrors());
    PatternDescr number = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("[1, 2, 3].sublist(1, 2)", ((FromDescr) number.getSource()).getDataSource().toString());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 18 with FromDescr

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

the class RuleParserTest method testSimpleAccessorWithFrom.

@Test
public void testSimpleAccessorWithFrom() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_SimpleAccessorWithFrom.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final FromDescr from = (FromDescr) pattern.getSource();
    final MVELExprDescr accessor = (MVELExprDescr) from.getDataSource();
    assertEquals("something.doIt", accessor.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) Test(org.junit.Test)

Example 19 with FromDescr

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

the class RuleParserTest method testComplexChainedAcessor.

@Test
public void testComplexChainedAcessor() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_ComplexChainedCallWithFrom.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final FromDescr from = (FromDescr) pattern.getSource();
    final MVELExprDescr accessor = (MVELExprDescr) from.getDataSource();
    assertEquals("doIt1( foo,bar,42,\"hello\",[ a : \"b\"], [a, \"b\", 42] ).doIt2(bar, [a, \"b\", 42]).field[\"key\"]", accessor.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) Test(org.junit.Test)

Example 20 with FromDescr

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

the class RuleParserTest method testFromComplexAcessor.

@Test
public void testFromComplexAcessor() throws Exception {
    String source = "rule \"Invalid customer id\" ruleflow-group \"validate\" lock-on-active true \n" + " when \n" + "     o: Order( ) \n" + "     not( Customer( ) from customerService.getCustomer(o.getCustomerId()) ) \n" + " then \n" + "     System.err.println(\"Invalid customer id found!\"); \n" + "     o.addError(\"Invalid customer id\"); \n" + "end \n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrorMessages().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("Invalid customer id", rule.getName());
    assertEquals(2, rule.getLhs().getDescrs().size());
    NotDescr not = (NotDescr) rule.getLhs().getDescrs().get(1);
    PatternDescr customer = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Customer", customer.getObjectType());
    assertEquals("customerService.getCustomer(o.getCustomerId())", ((FromDescr) customer.getSource()).getDataSource().getText());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

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