use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class PatternBuilder method buildPattern.
private Pattern buildPattern(RuleBuildContext context, PatternDescr patternDescr, ObjectType objectType) {
String patternIdentifier = patternDescr.getIdentifier();
boolean duplicateBindings = patternIdentifier != null && objectType instanceof ClassObjectType && context.getDeclarationResolver().isDuplicated(context.getRule(), patternIdentifier, objectType.getClassName());
Pattern pattern;
if (!StringUtils.isEmpty(patternIdentifier) && !duplicateBindings) {
pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
0, objectType, patternIdentifier, patternDescr.isInternalFact(context));
if (objectType instanceof ClassObjectType) {
// make sure PatternExtractor is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, (AcceptsClassObjectType) pattern.getDeclaration().getExtractor());
}
} else {
pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
0, objectType, null);
}
pattern.setPassive(patternDescr.isPassive(context));
if (ClassObjectType.Match_ObjectType.isAssignableFrom(pattern.getObjectType())) {
PropertyHandler handler = PropertyHandlerFactory.getPropertyHandler(RuleTerminalNodeLeftTuple.class);
if (handler == null) {
PropertyHandlerFactoryFixer.getPropertyHandlerClass().put(RuleTerminalNodeLeftTuple.class, new ActivationPropertyHandler());
}
}
// adding the newly created pattern to the build stack this is necessary in case of local declaration usage
context.getDeclarationResolver().pushOnBuildStack(pattern);
if (duplicateBindings) {
processDuplicateBindings(patternDescr.isUnification(), patternDescr, pattern, patternDescr, "this", patternDescr.getIdentifier(), context);
}
return pattern;
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class MVELAccumulateBuilder method buildExternalFunctions.
private Accumulator[] buildExternalFunctions(final RuleBuildContext context, final AccumulateDescr accumDescr, MVELDialect dialect, Map<String, Declaration> decls, Map<String, Declaration> sourceOuterDeclr, BoundIdentifiers boundIds, boolean readLocalsFromTuple) {
Accumulator[] accumulators;
List<AccumulateFunctionCallDescr> functions = accumDescr.getFunctions();
accumulators = new Accumulator[functions.size()];
// creating the custom array reader
InternalReadAccessor arrayReader = new SelfReferenceClassFieldReader(Object[].class);
int index = 0;
Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
for (AccumulateFunctionCallDescr func : functions) {
// build an external function executor
AccumulateFunction function = context.getConfiguration().getAccumulateFunction(func.getFunction());
if (function == null) {
// might have been imported in the package
function = context.getPkg().getAccumulateFunctions().get(func.getFunction());
}
if (function == null) {
context.addError(new DescrBuildError(accumDescr, context.getRuleDescr(), null, "Unknown accumulate function: '" + func.getFunction() + "' on rule '" + context.getRuleDescr().getName() + "'. All accumulate functions must be registered before building a resource."));
return null;
}
final AnalysisResult analysis = dialect.analyzeExpression(context, accumDescr, func.getParams().length > 0 ? func.getParams()[0] : "\"\"", boundIds);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(func.getParams().length > 0 ? func.getParams()[0] : "\"\"", analysis, getUsedDeclarations(decls, analysis), getUsedDeclarations(sourceOuterDeclr, analysis), null, context, "drools", KnowledgeHelper.class, readLocalsFromTuple, MVELCompilationUnit.Scope.CONSTRAINT);
accumulators[index] = new MVELAccumulatorFunctionExecutor(unit, function);
// if there is a binding, create the binding
if (func.getBind() != null) {
if (context.getDeclarationResolver().isDuplicated(context.getRule(), func.getBind(), function.getResultType().getName())) {
if (!func.isUnification()) {
context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + func.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
} else {
Declaration inner = context.getDeclarationResolver().getDeclaration(func.getBind());
Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), accumDescr.isMultiFunction() ? "this[ " + index + " ] == " + func.getBind() : "this == " + func.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(func.getBind()), accumDescr.isMultiFunction() ? new ArrayElementReader(arrayReader, index, function.getResultType()) : new SelfReferenceClassFieldReader(function.getResultType()), true);
((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
pattern.addConstraint(c);
index++;
}
} else {
Declaration declr = pattern.addDeclaration(func.getBind());
if (accumDescr.isMultiFunction()) {
declr.setReadAccessor(new ArrayElementReader(arrayReader, index, function.getResultType()));
} else {
declr.setReadAccessor(new SelfReferenceClassFieldReader(function.getResultType()));
}
}
}
index++;
}
return accumulators;
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class CollectBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final CollectDescr collectDescr = (CollectDescr) descr;
final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
final Pattern sourcePattern = (Pattern) patternBuilder.build(context, collectDescr.getInputPattern());
if (sourcePattern == null) {
return null;
}
final String className = "collect" + context.getNextId();
collectDescr.setClassMethodName(className);
Pattern resultPattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
return new Collect(sourcePattern, resultPattern);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KnowledgeBuilderTest method testOr.
@Test
public void testOr() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final RuleImpl rule = createRule(new OrDescr(), builder, "update(stilton);");
assertLength(0, builder.getErrors().getErrors());
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement or = (GroupElement) lhs.getChildren().get(0);
assertLength(1, or.getChildren());
final Pattern pattern = (Pattern) or.getChildren().get(0);
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KnowledgeBuilderTest method testExists.
@Test
public void testExists() throws Exception {
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
// Make sure we can't accessa variable bound inside the not node
RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
assertTrue(builder.hasErrors());
builder = new KnowledgeBuilderImpl();
rule = createRule(new ExistsDescr(), builder, "");
assertEquals(0, builder.getErrors().getErrors().length);
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
assertLength(1, exists.getChildren());
final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
Aggregations