use of org.drools.core.rule.From in project drools by kiegroup.
the class FromBuilder method build.
/* (non-Javadoc)
* @see org.kie.reteoo.builder.ReteooComponentBuilder#build(org.kie.reteoo.builder.BuildContext, org.kie.reteoo.builder.BuildUtils, org.kie.rule.RuleConditionElement)
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
final From from = (From) rce;
context.pushRuleComponent(from);
@SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
NodeFactory nodeFactory = CoreComponentFactory.get().getNodeFactoryService();
FromNode fromNode = from.isReactive() ? nodeFactory.buildReactiveFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from) : nodeFactory.buildFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
context.setTupleSource(utils.attachNode(context, fromNode));
context.setAlphaConstraints(null);
context.setBetaconstraints(null);
context.popRuleComponent();
}
use of org.drools.core.rule.From in project drools by kiegroup.
the class MVELFromBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
String text = ((FromDescr) descr).getExpression();
Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
if (entryPointId.isPresent()) {
return entryPointId.get();
}
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
boolean typeSafe = context.isTypesafe();
if (!dialect.isStrictMode()) {
context.setTypesafe(false);
}
try {
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
if (analysis == null) {
// something bad happened
return null;
}
Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
int j = 0;
for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
declarations[j++] = decls.get(str);
}
Arrays.sort(declarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
From from = new From(dataProvider);
from.setResultPattern(prefixPattern);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(from, dataProvider);
dataProvider.compile(data, context.getRule());
return from;
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
return null;
} finally {
context.setTypesafe(typeSafe);
}
}
use of org.drools.core.rule.From in project drools by kiegroup.
the class ReactiveFromBuilder method build.
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
final From from = (From) rce;
context.pushRuleComponent(from);
@SuppressWarnings("unchecked") BetaConstraints betaConstraints = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), true);
AlphaNodeFieldConstraint[] alphaNodeFieldConstraints = context.getAlphaConstraints() != null ? context.getAlphaConstraints().toArray(new AlphaNodeFieldConstraint[context.getAlphaConstraints().size()]) : new AlphaNodeFieldConstraint[0];
ReactiveFromNode node = CoreComponentFactory.get().getNodeFactoryService().buildReactiveFromNode(context.getNextNodeId(), from.getDataProvider(), context.getTupleSource(), alphaNodeFieldConstraints, betaConstraints, context.isTupleMemoryEnabled(), context, from);
context.setTupleSource(utils.attachNode(context, node));
context.setAlphaConstraints(null);
context.setBetaconstraints(null);
List<XpathConstraint> xpathConstraints = context.getXpathConstraints();
for (XpathConstraint xpathConstraint : xpathConstraints) {
for (XpathConstraint.XpathChunk chunk : xpathConstraint.getChunks()) {
context.setAlphaConstraints(chunk.getAlphaConstraints());
context.setBetaconstraints(chunk.getBetaConstraints());
context.setXpathConstraints(chunk.getXpathConstraints());
build(context, utils, chunk.asFrom());
}
}
}
use of org.drools.core.rule.From in project drools by kiegroup.
the class MVELFromBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
String text = ((FromDescr) descr).getExpression();
Optional<EntryPointId> entryPointId = context.getEntryPointId(text);
if (entryPointId.isPresent()) {
return entryPointId.get();
}
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
boolean typeSafe = context.isTypesafe();
if (!dialect.isStrictMode()) {
context.setTypesafe(false);
}
try {
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = dialect.analyzeExpression(context, descr, text, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
if (analysis == null) {
// something bad happened
return null;
}
Class<?> returnType = ((MVELAnalysisResult) analysis).getReturnType();
if (prefixPattern != null && !prefixPattern.isCompatibleWithFromReturnType(returnType)) {
context.addError(new DescrBuildError(descr, context.getRuleDescr(), null, "Pattern of type: '" + prefixPattern.getObjectType() + "' on rule '" + context.getRuleDescr().getName() + "' is not compatible with type " + returnType.getCanonicalName() + " returned by source"));
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final Declaration[] declarations = new Declaration[usedIdentifiers.getDeclrClasses().size()];
int j = 0;
for (String str : usedIdentifiers.getDeclrClasses().keySet()) {
declarations[j++] = decls.get(str);
}
Arrays.sort(declarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, declarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.CONSEQUENCE);
MVELDataProvider dataProvider = new MVELDataProvider(unit, context.getDialect().getId());
From from = new From(dataProvider);
from.setResultPattern(prefixPattern);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(from, dataProvider);
dataProvider.compile(data, context.getRule());
return from;
} catch (final Exception e) {
AsmUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to build expression for 'from' : " + e.getMessage() + " '" + text + "'"));
return null;
} finally {
context.setTypesafe(typeSafe);
}
}
Aggregations