use of org.drools.core.spi.DeclarationScopeResolver 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);
}
}
use of org.drools.core.spi.DeclarationScopeResolver in project drools by kiegroup.
the class QueryElementBuilder method processPositional.
private void processPositional(RuleBuildContext context, QueryImpl query, Declaration[] params, QueryArgument[] arguments, List<Declaration> requiredDeclarations, InternalReadAccessor arrayReader, Pattern pattern, BaseDescr base, String expression, ConstraintConnectiveDescr result) {
int pos = ((ExprConstraintDescr) base).getPosition();
if (pos >= arguments.length) {
context.addError(new DescrBuildError(context.getParentDescr(), base, null, "Unable to parse query '" + query.getName() + "', as postion " + pos + " for expression '" + expression + "' does not exist on query size " + arguments.length));
return;
}
boolean isVariable = isVariable(expression);
DeclarationScopeResolver declarationResolver = context.getDeclarationResolver();
Declaration declr = isVariable ? declarationResolver.getDeclaration(expression) : null;
if (declr != null) {
// it exists, so it's an input
requiredDeclarations.add(declr);
arguments[pos] = new QueryArgument.Declr(declr);
} else if (isVariable && expression.indexOf('.') < 0) {
arguments[pos] = getVariableQueryArgument(arrayReader, params, pos, pattern, expression);
} else {
// it's an expression and thus an input
AnalysisResult analysisResult = analyzeExpression(context, base, expression);
if (analysisResult == null || analysisResult.getIdentifiers().isEmpty()) {
arguments[pos] = getLiteralQueryArgument(context, base, result);
} else {
List<Declaration> declarations = new ArrayList<Declaration>();
for (String identifier : analysisResult.getIdentifiers()) {
Declaration declaration = declarationResolver.getDeclaration(identifier);
if (declaration != null) {
declarations.add(declaration);
}
}
if (declarations.size() == analysisResult.getIdentifiers().size()) {
arguments[pos] = new QueryArgument.Expression(declarations, expression, getParserContext(context));
} else {
arguments[pos] = getLiteralQueryArgument(context, base, result);
}
}
}
}
use of org.drools.core.spi.DeclarationScopeResolver in project drools by kiegroup.
the class LogicTransformer method fixClonedDeclarations.
/**
* During the logic transformation, we eventually clone CEs,
* specially patterns and corresponding declarations. So now
* we need to fix any references to cloned declarations.
*/
protected void fixClonedDeclarations(GroupElement and, Map<String, Class<?>> globals) {
Stack<RuleConditionElement> contextStack = new Stack<RuleConditionElement>();
DeclarationScopeResolver resolver = new DeclarationScopeResolver(globals, contextStack);
contextStack.push(and);
processElement(resolver, contextStack, and);
contextStack.pop();
}
Aggregations