Search in sources :

Example 11 with FieldValue

use of org.drools.core.spi.FieldValue in project drools by kiegroup.

the class ReturnValueRestriction method isAllowed.

public boolean isAllowed(final InternalReadAccessor extractor, final InternalFactHandle handle, final InternalWorkingMemory workingMemory, final ContextEntry context) {
    try {
        ReturnValueContextEntry ctx = (ReturnValueContextEntry) context;
        FieldValue value = this.expression.evaluate(handle, null, this.previousDeclarations, this.localDeclarations, workingMemory, ctx.dialectContext);
        return this.evaluator.evaluate(workingMemory, this.readAccessor, handle, value);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FieldValue(org.drools.core.spi.FieldValue) IOException(java.io.IOException)

Example 12 with FieldValue

use of org.drools.core.spi.FieldValue in project drools by kiegroup.

the class FieldFactoryTest method testBigInteger.

@Test
public void testBigInteger() {
    final FieldValue val = FieldFactory.getInstance().getFieldValue("424242", ValueType.BIG_INTEGER_TYPE);
    assertEquals(BigInteger.class, val.getValue().getClass());
    assertTrue(val.getValue().equals(new BigInteger("424242")));
}
Also used : BigInteger(java.math.BigInteger) FieldValue(org.drools.core.spi.FieldValue) Test(org.junit.Test)

Example 13 with FieldValue

use of org.drools.core.spi.FieldValue in project drools by kiegroup.

the class PatternBuilder method buildConstraintForPattern.

protected Constraint buildConstraintForPattern(final RuleBuildContext context, final Pattern pattern, final RelationalExprDescr relDescr, String expr, String value1, String value2, boolean isConstant, Map<String, OperatorDescr> aliases) {
    InternalReadAccessor extractor = getFieldReadAccessor(context, relDescr, pattern, value1, null, true);
    if (extractor == null) {
        return null;
    }
    int dotPos = value1.indexOf('.');
    if (dotPos > 0) {
        String part0 = value1.substring(0, dotPos).trim();
        if ("this".equals(part0.trim())) {
            value1 = value1.substring(dotPos + 1);
        } else if (pattern.getDeclaration() != null && part0.equals(pattern.getDeclaration().getIdentifier())) {
            value1 = value1.substring(dotPos + 1);
            expr = expr.substring(dotPos + 1);
        }
    }
    ValueType vtype = extractor.getValueType();
    String operator = relDescr.getOperator().trim();
    LiteralRestrictionDescr restrictionDescr = buildLiteralRestrictionDescr(context, relDescr, value2, operator, isConstant);
    if (restrictionDescr != null) {
        FieldValue field = getFieldValue(context, vtype, restrictionDescr.getText().trim());
        if (field != null) {
            Constraint constraint = getConstraintBuilder(context).buildLiteralConstraint(context, pattern, vtype, field, expr, value1, operator, value2, extractor, restrictionDescr, aliases);
            if (constraint != null) {
                return constraint;
            }
        }
    }
    value2 = context.getDeclarationResolver().normalizeValueForUnit(value2);
    Declaration declr = null;
    if (value2.indexOf('(') < 0 && value2.indexOf('.') < 0 && value2.indexOf('[') < 0) {
        declr = context.getDeclarationResolver().getDeclaration(value2);
        if (declr == null) {
            // trying to create implicit declaration
            final Pattern thisPattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
            declr = createDeclarationObject(context, value2, thisPattern);
        }
    }
    Declaration[] declarations = null;
    if (declr == null) {
        String[] parts = value2.split("\\.");
        if (parts.length == 2) {
            if ("this".equals(parts[0].trim())) {
                declr = createDeclarationObject(context, parts[1].trim(), (Pattern) context.getDeclarationResolver().peekBuildStack());
                value2 = parts[1].trim();
            } else {
                declr = context.getDeclarationResolver().getDeclaration(parts[0].trim());
                // if a declaration exists, then it may be a variable direct property access
                if (declr != null) {
                    if (declr.isPatternDeclaration()) {
                        // TODO: no need to extract inner declaration when using mvel constraint
                        declarations = new Declaration[] { declr };
                        declr = createDeclarationObject(context, parts[1].trim(), declr.getPattern());
                        value2 = parts[1].trim();
                    } else {
                        // we will later fallback to regular predicates, so don't raise error
                        return null;
                    }
                }
            }
        }
    }
    if (declarations == null) {
        if (declr != null) {
            declarations = new Declaration[] { declr };
        } else {
            declarations = getDeclarationsForReturnValue(context, relDescr, operator, value2);
            if (declarations == null) {
                return null;
            }
        }
    }
    return getConstraintBuilder(context).buildVariableConstraint(context, pattern, expr, declarations, value1, relDescr.getOperatorDescr(), value2, extractor, declr, relDescr, aliases);
}
Also used : Pattern(org.drools.core.rule.Pattern) ValueType(org.drools.core.base.ValueType) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr)

Example 14 with FieldValue

use of org.drools.core.spi.FieldValue in project drools by kiegroup.

the class PatternBuilder method normalizeExpression.

private String normalizeExpression(RuleBuildContext context, Pattern pattern, RelationalExprDescr subDescr, String subExpr) {
    String leftValue = findLeftExpressionValue(subDescr);
    String operator = subDescr.getOperator();
    if (isDateType(context, pattern, leftValue)) {
        String rightValue = findRightExpressionValue(subDescr);
        FieldValue fieldValue = getFieldValue(context, ValueType.DATE_TYPE, rightValue);
        if (fieldValue != null) {
            subExpr = subExpr.replace(rightValue, getNormalizeDate(fieldValue));
        }
        return subExpr;
    }
    if (operator.equals("str")) {
        String rightValue = findRightExpressionValue(subDescr);
        return normalizeStringOperator(leftValue, rightValue, new LiteralRestrictionDescr(operator, subDescr.isNegated(), subDescr.getParameters(), rightValue, LiteralRestrictionDescr.TYPE_STRING));
    }
    // resolve ambiguity between mvel's "empty" keyword and constraints like: List(empty == ...)
    return normalizeEmptyKeyword(subExpr, operator);
}
Also used : FieldValue(org.drools.core.spi.FieldValue) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr)

Example 15 with FieldValue

use of org.drools.core.spi.FieldValue in project drools by kiegroup.

the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.

@Test
public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase);
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
    // With Memory
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
    buildContext);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
    // check sink is empty
    assertLength(0, sink.getAsserted());
    // object should assert as it passes text
    alphaNode.assertObject(f0, context, ksession);
    assertEquals(1, sink.getAsserted().size());
    Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
    final Cheese stilton = new Cheese("stilton", 6);
    final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
    // object should NOT assert as it does not pass test
    alphaNode.assertObject(f1, context, ksession);
    assertLength(1, sink.getAsserted());
    list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

FieldValue (org.drools.core.spi.FieldValue)26 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)9 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)9 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)9 Test (org.junit.Test)9 IOException (java.io.IOException)6 ClassObjectType (org.drools.core.base.ClassObjectType)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)6 ClassFieldReader (org.drools.core.base.ClassFieldReader)5 WorkingMemory (org.drools.core.WorkingMemory)4 IndexableConstraint (org.drools.core.rule.IndexableConstraint)4 Pattern (org.drools.core.rule.Pattern)4 Cheese (org.drools.core.test.model.Cheese)4 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 ArrayList (java.util.ArrayList)3 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)3