Search in sources :

Example 1 with MVELExprDescr

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

the class XmlPackageReaderTest method testParseFrom.

@Test
public void testParseFrom() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFrom.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    PatternDescr patterndescr = (PatternDescr) obj.getLhs().getDescrs().get(0);
    FromDescr from = (FromDescr) patterndescr.getSource();
    MVELExprDescr accessordescriptor = (MVELExprDescr) from.getDataSource();
    assertEquals("cheesery.getCheeses(i+4)", accessordescriptor.getExpression());
    assertEquals(patterndescr.getObjectType(), "Cheese");
    assertEquals(patterndescr.getIdentifier(), "cheese");
}
Also used : InputStreamReader(java.io.InputStreamReader) 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) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 2 with MVELExprDescr

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

the class RuleParserTest method testSimpleMethodCallWithFrom.

@Test
public void testSimpleMethodCallWithFrom() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_SimpleMethodCallWithFrom.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final FromDescr from = (FromDescr) pattern.getSource();
    final MVELExprDescr method = (MVELExprDescr) from.getDataSource();
    assertEquals("something.doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", method.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 3 with MVELExprDescr

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

the class RuleParserTest method testSimpleFunctionCallWithFrom.

@Test
public void testSimpleFunctionCallWithFrom() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_SimpleFunctionCallWithFrom.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final FromDescr from = (FromDescr) pattern.getSource();
    final MVELExprDescr func = (MVELExprDescr) from.getDataSource();
    assertEquals("doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", func.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 4 with MVELExprDescr

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

the class RuleParserTest method testSimpleAccessorAndArgWithFrom.

@Test
public void testSimpleAccessorAndArgWithFrom() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_SimpleAccessorArgWithFrom.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[\"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 5 with MVELExprDescr

use of org.drools.drl.ast.descr.MVELExprDescr 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)

Aggregations

FromDescr (org.drools.drl.ast.descr.FromDescr)10 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)10 PatternDescr (org.drools.drl.ast.descr.PatternDescr)7 RuleDescr (org.drools.drl.ast.descr.RuleDescr)6 Test (org.junit.Test)6 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)2 InputStreamReader (java.io.InputStreamReader)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 XpathAnalysis (org.drools.compiler.rule.builder.XpathAnalysis)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 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1 Element (org.w3c.dom.Element)1