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());
}
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());
}
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;
}
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());
}
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());
}
Aggregations