use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PropertySpecificTest method testComplexBetaSharedAlphaWithWatchesRemoveR1.
@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR1() {
String rule1 = "$b : B( b == 15) @watch(i) A( a == 10, b == 15 ) @watch(c)";
String rule2 = "$b : B( b == 15) @watch(j) A( a == 10, i == 20 ) @watch(s)";
String rule3 = "$b : B( c == 15) @watch(k) A( a == 10, i == 20, b == 10 ) @watch(j)";
KieBase kbase = getKnowledgeBase(rule1, rule2, rule3);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
kbase.removeRule("org.drools.compiler.integrationtests", "r0");
ObjectTypeNode otn = getObjectTypeNode(kbase, "A");
assertNotNull(otn);
Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();
List<String> sp = getSettableProperties(wm, otn);
AlphaNode alphaNode1 = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "s", "j"), sp), alphaNode1.getInferredMask());
// first share
AlphaNode alphaNode1_1 = (AlphaNode) alphaNode1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("i"), sp), alphaNode1_1.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "s", "j"), sp), alphaNode1_1.getInferredMask());
BetaNode betaNode1 = (BetaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[1];
assertEquals(calculatePositiveMask(classType, list("s"), sp), betaNode1.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "s"), sp), betaNode1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("b", "j"), sp), betaNode1.getLeftInferredMask());
// second split, third alpha
AlphaNode alphaNode1_2 = (AlphaNode) alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("b"), sp), alphaNode1_2.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "i", "j"), sp), alphaNode1_2.getInferredMask());
BetaNode betaNode3 = (BetaNode) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("j"), sp), betaNode3.getRightDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), betaNode3.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("k"), sp), betaNode3.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("c", "k"), sp), betaNode3.getLeftInferredMask());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class PropertySpecificTest method test2DifferentAlphaWatchBeforeSameBeta.
@Test
public void test2DifferentAlphaWatchBeforeSameBeta() {
String rule1 = "B(a == 15) @watch(b) C()";
String rule2 = "B(a == 15) @watch(c) C()";
KieBase kbase = getKnowledgeBase(rule1, rule2);
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode otnB = getObjectTypeNode(kbase, "B");
List<String> sp = getSettableProperties(wm, otnB);
Class classType = ((ClassObjectType) otnB.getObjectType()).getClassType();
AlphaNode alphaNode = (AlphaNode) otnB.getObjectSinkPropagator().getSinks()[0];
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode.getInferredMask());
ObjectTypeNode otnC = getObjectTypeNode(kbase, "C");
BetaNode betaNodeC1 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[0];
BetaNode betaNodeC2 = (BetaNode) otnC.getObjectSinkPropagator().getSinks()[1];
LeftInputAdapterNode lia1 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
assertSame(betaNodeC1, lia1.getSinkPropagator().getSinks()[0]);
LeftInputAdapterNode lia2 = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[1];
assertSame(betaNodeC2, lia2.getSinkPropagator().getSinks()[0]);
assertEquals(EmptyBitMask.get(), betaNodeC1.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC1.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("b"), sp), betaNodeC1.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "b"), sp), betaNodeC1.getLeftInferredMask());
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
kbase.removeRule("org.drools.compiler.integrationtests", "r0");
assertEquals(calculatePositiveMask(classType, list("a"), sp), alphaNode.getDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), alphaNode.getInferredMask());
assertEquals(1, lia2.getSinkPropagator().getSinks().length);
BetaNode betaNodeC = (BetaNode) lia2.getSinkPropagator().getSinks()[0];
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightDeclaredMask());
assertEquals(EmptyBitMask.get(), betaNodeC2.getRightInferredMask());
assertEquals(calculatePositiveMask(classType, list("c"), sp), betaNodeC2.getLeftDeclaredMask());
assertEquals(calculatePositiveMask(classType, list("a", "c"), sp), betaNodeC2.getLeftInferredMask());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class MVELConstraintBuilder method buildCompilationUnit.
public MVELCompilationUnit buildCompilationUnit(final RuleBuildContext context, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final PredicateDescr predicateDescr, final AnalysisResult analysis) {
if (context.isTypesafe() && analysis instanceof MVELAnalysisResult) {
Class<?> returnClass = ((MVELAnalysisResult) analysis).getReturnType();
if (returnClass != Boolean.class && returnClass != Boolean.TYPE) {
context.addError(new DescrBuildError(context.getParentDescr(), predicateDescr, null, "Predicate '" + predicateDescr.getContent() + "' must be a Boolean expression\n" + predicateDescr.positionAsString()));
}
}
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
MVELCompilationUnit unit = null;
try {
Map<String, Class<?>> declIds = context.getDeclarationResolver().getDeclarationClasses(context.getRule());
Pattern p = (Pattern) context.getDeclarationResolver().peekBuildStack();
if (p.getObjectType() instanceof ClassObjectType) {
declIds.put("this", ((ClassObjectType) p.getObjectType()).getClassType());
}
unit = dialect.getMVELCompilationUnit((String) predicateDescr.getContent(), analysis, previousDeclarations, localDeclarations, null, context, "drools", KnowledgeHelper.class, context.isInXpath(), MVELCompilationUnit.Scope.CONSTRAINT);
} catch (final Exception e) {
copyErrorLocation(e, predicateDescr);
context.addError(new DescrBuildError(context.getParentDescr(), predicateDescr, e, "Unable to build expression for 'inline-eval' : " + e.getMessage() + "'" + predicateDescr.getContent() + "'\n" + e.getMessage()));
}
return unit;
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class MVELConstraintBuilder method buildVariableConstraint.
public Constraint buildVariableConstraint(RuleBuildContext context, Pattern pattern, String expression, Declaration[] declarations, String leftValue, OperatorDescr operatorDescr, String rightValue, InternalReadAccessor extractor, Declaration requiredDeclaration, RelationalExprDescr relDescr, Map<String, OperatorDescr> aliases) {
if (!isMvelOperator(operatorDescr.getOperator())) {
if (requiredDeclaration == null) {
return null;
}
EvaluatorDefinition.Target right = getRightTarget(extractor);
EvaluatorDefinition.Target left = (requiredDeclaration.isPatternDeclaration() && !(Date.class.isAssignableFrom(requiredDeclaration.getDeclarationClass()) || Number.class.isAssignableFrom(requiredDeclaration.getDeclarationClass()))) ? EvaluatorDefinition.Target.HANDLE : EvaluatorDefinition.Target.FACT;
final Evaluator evaluator = getEvaluator(context, relDescr, extractor.getValueType(), operatorDescr.getOperator(), relDescr.isNegated(), relDescr.getParametersText(), left, right);
return new EvaluatorConstraint(new Declaration[] { requiredDeclaration }, evaluator, extractor);
}
boolean isUnification = requiredDeclaration != null && requiredDeclaration.getPattern().getObjectType().equals(new ClassObjectType(DroolsQuery.class)) && Operator.EQUAL.getOperatorString().equals(operatorDescr.getOperator());
if (isUnification && leftValue.equals(rightValue)) {
expression = resolveUnificationAmbiguity(expression, declarations, leftValue, rightValue);
}
expression = normalizeMVELVariableExpression(expression, leftValue, rightValue, relDescr);
IndexUtil.ConstraintType constraintType = IndexUtil.ConstraintType.decode(operatorDescr.getOperator());
MVELCompilationUnit compilationUnit = isUnification ? null : buildCompilationUnit(context, pattern, expression, aliases);
EvaluatorWrapper[] operators = getOperators(buildOperators(context, pattern, relDescr, aliases));
return new MvelConstraint(Collections.singletonList(context.getPkg().getName()), expression, declarations, operators, compilationUnit, constraintType, requiredDeclaration, extractor, isUnification);
}
use of org.drools.core.base.ClassObjectType 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;
}
Aggregations