use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.
the class PatternBuilder method buildXPathDescr.
private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ExpressionDescr descr, DumperContext mvelCtx) {
String expression = descr.getExpression();
XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
if (xpathAnalysis.hasError()) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
return null;
}
XpathConstraint xpathConstraint = new XpathConstraint();
ObjectType objectType = pattern.getObjectType();
if (objectType.isTemplate()) {
throw new UnsupportedOperationException("xpath is not supported with fact templates");
}
Class<?> patternClass = ((ClassObjectType) objectType).getClassType();
List<Class<?>> backReferenceClasses = new ArrayList<>();
backReferenceClasses.add(patternClass);
XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
pattern.setBackRefDeclarations(backRef);
ObjectType originalType = pattern.getObjectType();
ObjectType currentObjectType = originalType;
mvelCtx.setInXpath(true);
try {
// then the xpathoffset must be adjusted by -1, as pattern is not actually added to the rete network
for (XpathAnalysis.XpathPart part : xpathAnalysis) {
XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
// make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().wireObjectType(currentObjectType, xpathChunk);
if (xpathChunk == null) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
pattern.setObjectType(originalType);
return null;
}
if (part.getInlineCast() != null) {
try {
patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
} catch (ClassNotFoundException e) {
registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
return null;
}
part.addInlineCastConstraint(patternClass);
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
xpathChunk.setReturnedType(currentObjectType);
} else {
patternClass = xpathChunk.getReturnedClass();
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
}
context.increaseXpathChuckNr();
pattern.setObjectType(currentObjectType);
backReferenceClasses.add(0, patternClass);
backRef.reset();
for (String constraint : part.getConstraints()) {
ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
if (result == null) {
continue;
}
// preserving this, as the recursive build() resets it
int chunkNbr = context.getXpathChuckNr();
for (Constraint c : build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx)) {
xpathChunk.addConstraint(c);
}
context.setXpathChuckNr(chunkNbr);
}
}
xpathConstraint.setXpathStartDeclaration(xpathStartDeclaration);
if (descr instanceof BindingDescr) {
Declaration pathDeclr = pattern.addDeclaration(((BindingDescr) descr).getVariable());
pathDeclr.setxPathOffset(context.getXpathChuckNr());
xpathConstraint.setDeclaration(pathDeclr);
}
} finally {
mvelCtx.setInXpath(false);
pattern.setBackRefDeclarations(null);
pattern.setObjectType(originalType);
context.resetXpathChuckNr();
}
return xpathConstraint;
}
use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.
the class PatternBuilder method buildXPathDescr.
private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
String expression = ((ExpressionDescr) descr).getExpression();
XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
if (xpathAnalysis.hasError()) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
return null;
}
XpathConstraint xpathConstraint = new XpathConstraint();
ObjectType objectType = pattern.getObjectType();
Class<?> patternClass = objectType.getClassType();
List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
backReferenceClasses.add(patternClass);
XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
pattern.setBackRefDeclarations(backRef);
ObjectType originalType = pattern.getObjectType();
ObjectType currentObjectType = originalType;
mvelCtx.setInXpath(true);
try {
for (XpathAnalysis.XpathPart part : xpathAnalysis) {
XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
// make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
if (xpathChunk == null) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
pattern.setObjectType(originalType);
return null;
}
if (part.getInlineCast() != null) {
try {
patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
} catch (ClassNotFoundException e) {
registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
return null;
}
part.addInlineCastConstraint(patternClass);
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
xpathChunk.setReturnedType(currentObjectType);
} else {
patternClass = xpathChunk.getReturnedClass();
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
}
pattern.setObjectType(currentObjectType);
backReferenceClasses.add(0, patternClass);
backRef.reset();
for (String constraint : part.getConstraints()) {
ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
if (result == null) {
continue;
}
for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
xpathChunk.addConstraint(c);
}
}
}
} finally {
mvelCtx.setInXpath(false);
pattern.setBackRefDeclarations(null);
pattern.setObjectType(originalType);
}
xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
if (descr instanceof BindingDescr) {
xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
}
return xpathConstraint;
}
use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.
the class PatternBuilder method buildXpathConstraints.
private void buildXpathConstraints(BuildContext context, BuildUtils utils, Pattern pattern, Constraints constraints) {
if (!constraints.xpathConstraints.isEmpty()) {
buildTupleSource(context, utils);
if (constraints.xpathConstraints.size() == 1 && constraints.xpathConstraints.get(0).getXpathStartDeclaration() != null) {
context.setObjectSource(null);
context.decrementCurrentPatternOffset();
} else {
buildJoinNode(context, utils);
}
ReteooComponentBuilder builder = utils.getBuilderFor(XpathConstraint.class);
for (XpathConstraint xpathConstraint : constraints.xpathConstraints) {
for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
context.setAlphaConstraints(chunk.getAlphaConstraints());
context.setBetaconstraints(chunk.getBetaConstraints());
context.setXpathConstraints(chunk.getXpathConstraints());
builder.build(context, utils, chunk.asFrom());
}
Declaration declaration = xpathConstraint.getDeclaration();
if (declaration != null) {
// oopath actually have the binding
Pattern clonedPattern = new Pattern(pattern.getIndex(), context.getCurrentPatternOffset(), new ClassObjectType(xpathConstraint.getResultClass()), declaration.getIdentifier(), declaration.isInternalFact());
declaration.setPattern(clonedPattern);
}
}
context.popRuleComponent();
}
}
use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.
the class PatternBuilder method buildXpathConstraints.
private void buildXpathConstraints(BuildContext context, BuildUtils utils, Constraints constraints) {
if (!constraints.xpathConstraints.isEmpty()) {
buildTupleSource(context, utils, false);
if (constraints.xpathConstraints.size() == 1 && constraints.xpathConstraints.get(0).getXpathStartDeclaration() != null) {
context.setObjectSource(null);
} else {
buildJoinNode(context, utils);
}
ReteooComponentBuilder builder = utils.getBuilderFor(XpathConstraint.class);
for (XpathConstraint xpathConstraint : constraints.xpathConstraints) {
for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
context.setAlphaConstraints(chunk.getAlphaConstraints());
context.setBetaconstraints(chunk.getBetaConstraints());
context.setXpathConstraints(chunk.getXpathConstraints());
builder.build(context, utils, chunk.asFrom());
}
}
context.popRuleComponent();
}
}
use of org.drools.core.rule.constraint.XpathConstraint in project drools by kiegroup.
the class PatternBuilder method createConstraints.
private Constraints createConstraints(BuildContext context, Pattern pattern) {
Constraints constraints = new Constraints();
// check if cross products for identity patterns should be disabled
checkRemoveIdentities(context, pattern, constraints.betaConstraints);
// checks if this pattern is nested inside a NOT CE
final boolean isNegative = isNegative(context);
for (Constraint constraint : pattern.getConstraints()) {
switch(constraint.getType()) {
case ALPHA:
linkAlphaConstraint((AlphaNodeFieldConstraint) constraint, constraints.alphaConstraints);
break;
case BETA:
linkBetaConstraint((BetaNodeFieldConstraint) constraint, constraints.betaConstraints);
if (isNegative && context.getRuleBase().getConfiguration().getEventProcessingMode() == EventProcessingOption.STREAM && pattern.getObjectType().isEvent() && constraint.isTemporal()) {
checkDelaying(context, constraint);
}
break;
case XPATH:
constraints.xpathConstraints.add((XpathConstraint) constraint);
break;
default:
throw new RuntimeException("Unknown constraint type: " + constraint.getType() + ". This is a bug. Please contact development team.");
}
}
return constraints;
}
Aggregations