Search in sources :

Example 6 with XpathAnalysis

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

the class XpathAnalysisTest method testAttributeIterate.

@Test
public void testAttributeIterate() {
    final String xpath = "/address/";
    final XpathAnalysis result = XpathAnalysis.analyze(xpath);
    assertEquals("The XPath should be valid.", false, result.hasError());
    assertNull(result.getError());
    final Iterator<XpathAnalysis.XpathPart> iterator = getNonEmptyIterator(result);
    verifyXpathPart(new XpathAnalysis.XpathPart("address", true, false, new ArrayList<String>(), null, -1, 0), iterator.next());
    verifyXpathPart(new XpathAnalysis.XpathPart("", true, false, new ArrayList<String>(), null, -1, 0), iterator.next());
    assertEquals(false, iterator.hasNext());
}
Also used : XpathAnalysis(org.drools.compiler.rule.builder.XpathAnalysis) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with XpathAnalysis

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

the class XpathAnalysisTest method testComplexCast.

@Test
public void testComplexCast() {
    final String xpath = "/address/street#MyStreetType[name.value == \"Elm\"].city#MyCityMoreSpecificType[ value ]";
    final XpathAnalysis result = XpathAnalysis.analyze(xpath);
    assertEquals("The XPath should be valid.", false, result.hasError());
    assertNull(result.getError());
    final Iterator<XpathAnalysis.XpathPart> iterator = getNonEmptyIterator(result);
    verifyXpathPart(new XpathAnalysis.XpathPart("address", true, false, new ArrayList<String>(), null, -1, 0), iterator.next());
    verifyXpathPart(new XpathAnalysis.XpathPart("street", true, false, new ArrayList<String>(Arrays.asList("name.value == \"Elm\"")), "MyStreetType", -1, 0), iterator.next());
    verifyXpathPart(new XpathAnalysis.XpathPart("city", false, false, new ArrayList<String>(Arrays.asList("value")), "MyCityMoreSpecificType", -1, 0), iterator.next());
    assertEquals(false, iterator.hasNext());
}
Also used : XpathAnalysis(org.drools.compiler.rule.builder.XpathAnalysis) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with XpathAnalysis

use of org.drools.compiler.rule.builder.XpathAnalysis 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 9 with XpathAnalysis

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

the class XpathAnalysisTest method testEmptyNonReactiveXPath.

@Test
public void testEmptyNonReactiveXPath() {
    final String xpath = "?/";
    final XpathAnalysis result = XpathAnalysis.analyze(xpath);
    assertEquals("The empty XPath should be valid.", false, result.hasError());
    assertNull(result.getError());
    final Iterator<XpathAnalysis.XpathPart> iterator = getNonEmptyIterator(result);
    verifyXpathPart(new XpathAnalysis.XpathPart("", true, true, new ArrayList<String>(), null, -1, 0), iterator.next());
    assertEquals(false, iterator.hasNext());
}
Also used : XpathAnalysis(org.drools.compiler.rule.builder.XpathAnalysis) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with XpathAnalysis

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

the class XpathAnalysisTest method testBasicCast.

@Test
public void testBasicCast() {
    final String xpath = "/address/street#MyStreetType[name.value == \"Elm\"].city";
    final XpathAnalysis result = XpathAnalysis.analyze(xpath);
    assertEquals("The XPath should be valid.", false, result.hasError());
    assertNull(result.getError());
    final Iterator<XpathAnalysis.XpathPart> iterator = getNonEmptyIterator(result);
    verifyXpathPart(new XpathAnalysis.XpathPart("address", true, false, new ArrayList<String>(), null, -1, 0), iterator.next());
    verifyXpathPart(new XpathAnalysis.XpathPart("street", true, false, new ArrayList<String>(Arrays.asList("name.value == \"Elm\"")), "MyStreetType", -1, 0), iterator.next());
    verifyXpathPart(new XpathAnalysis.XpathPart("city", false, false, new ArrayList<String>(), null, -1, 0), iterator.next());
    assertEquals(false, iterator.hasNext());
}
Also used : XpathAnalysis(org.drools.compiler.rule.builder.XpathAnalysis) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

XpathAnalysis (org.drools.compiler.rule.builder.XpathAnalysis)19 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)16 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)1 FromDescr (org.drools.drl.ast.descr.FromDescr)1 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)1 PatternDescr (org.drools.drl.ast.descr.PatternDescr)1