Search in sources :

Example 1 with MVELExprDescr

use of org.drools.compiler.lang.descr.MVELExprDescr in project drools by kiegroup.

the class ExpressionHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final String expression = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
    emptyContentCheck(localName, expression, parser);
    FromDescr parent = (FromDescr) parser.getParent();
    parent.setDataSource(new MVELExprDescr(expression.trim()));
    return null;
}
Also used : Element(org.w3c.dom.Element) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr)

Example 2 with MVELExprDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr) Test(org.junit.Test)

Example 3 with MVELExprDescr

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

Example 4 with MVELExprDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 5 with MVELExprDescr

use of org.drools.compiler.lang.descr.MVELExprDescr 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.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr)

Aggregations

FromDescr (org.drools.compiler.lang.descr.FromDescr)9 MVELExprDescr (org.drools.compiler.lang.descr.MVELExprDescr)9 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)6 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 Test (org.junit.Test)6 InputStreamReader (java.io.InputStreamReader)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)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 Element (org.w3c.dom.Element)1