Search in sources :

Example 1 with BaseDescr

use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintNotIn.

@Test
public void declareConstraintNotIn() {
    List<Object> values = Arrays.asList("3", "8.5");
    String patternType = "INPUT2";
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintNotIn(patternType, values);
    final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
    assertNotNull(descrs);
    assertEquals(1, descrs.size());
    assertTrue(descrs.get(0) instanceof NotDescr);
    NotDescr notDescr = (NotDescr) descrs.get(0);
    assertEquals(1, notDescr.getDescrs().size());
    assertTrue(notDescr.getDescrs().get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
    assertEquals(patternType, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value in (3, 8.5)";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 2 with BaseDescr

use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintsOr.

@Test
public void declareConstraintsOr() {
    String temperatureField = "TEMPERATURE";
    String humidityField = "HUMIDITY";
    List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.or());
    assertNotNull(lhsBuilder.getDescr());
    assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
    assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof OrDescr);
    OrDescr baseOrDescr = (OrDescr) lhsBuilder.getDescr().getDescrs().get(0);
    final List<BaseDescr> descrs = baseOrDescr.getDescrs();
    assertNotNull(descrs);
    assertEquals(2, descrs.size());
    // First KiePMMLFieldOperatorValue
    assertTrue(descrs.get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    // Second KiePMMLFieldOperatorValue
    assertTrue(descrs.get(1) instanceof PatternDescr);
    patternDescr = (PatternDescr) descrs.get(1);
    assertEquals(humidityField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) BaseDescr(org.drools.drl.ast.descr.BaseDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 3 with BaseDescr

use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintIn.

@Test
public void declareConstraintIn() {
    List<Object> values = Arrays.asList("-5", "0.5", "1", "10");
    String patternType = "INPUT1";
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintIn(patternType, values);
    final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
    assertNotNull(descrs);
    assertEquals(1, descrs.size());
    assertTrue(descrs.get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(patternType, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value in (-5, 0.5, 1, 10)";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 4 with BaseDescr

use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.

the class ExprConstraintDescrVisitor method visit.

private void visit(ConstraintConnectiveDescr descr) {
    switch(descr.getConnective()) {
        case AND:
            solvers.startOperator(OperatorDescrType.AND);
            for (BaseDescr baseDescr : descr.getDescrs()) {
                visit(baseDescr);
            }
            solvers.endOperator();
            break;
        case OR:
            solvers.startOperator(OperatorDescrType.OR);
            for (BaseDescr baseDescr : descr.getDescrs()) {
                visit(baseDescr);
            }
            solvers.endOperator();
            break;
        case XOR:
            // TODO: Generated code -Rikkola-
            break;
        case INC_OR:
            // TODO: Generated code -Rikkola-
            break;
        case INC_AND:
            // TODO: Generated code -Rikkola-
            break;
    }
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr)

Example 5 with BaseDescr

use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.

the class MVELDialect method analyzeExpression.

public static AnalysisResult analyzeExpression(final PackageBuildContext context, final BaseDescr descr, final Object content, final BoundIdentifiers availableIdentifiers, final Map<String, Class<?>> localTypes) {
    AnalysisResult result = null;
    // the following is required for proper error handling
    BaseDescr temp = context.getParentDescr();
    context.setParentDescr(descr);
    try {
        result = MVELExprAnalyzer.analyzeExpression(context, (String) content, availableIdentifiers, localTypes, "drools", KnowledgeHelper.class);
    } catch (final Exception e) {
        AsmUtil.copyErrorLocation(e, descr);
        context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the used declarations.\n" + e.getMessage()));
    } finally {
        // setting it back to original parent descr
        context.setParentDescr(temp);
    }
    return result;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) BaseDescr(org.drools.drl.ast.descr.BaseDescr) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) IOException(java.io.IOException)

Aggregations

BaseDescr (org.drools.drl.ast.descr.BaseDescr)117 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)23 AndDescr (org.drools.drl.ast.descr.AndDescr)18 AtomicExprDescr (org.drools.drl.ast.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)16 PatternDescr (org.drools.drl.ast.descr.PatternDescr)15 AnnotatedBaseDescr (org.drools.drl.ast.descr.AnnotatedBaseDescr)14 BindingDescr (org.drools.drl.ast.descr.BindingDescr)13 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)11 Test (org.junit.Test)10 RelationalExprDescr (org.drools.drl.ast.descr.RelationalExprDescr)9 DeclareDescrBuilder (org.drools.drl.ast.dsl.DeclareDescrBuilder)9 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)8 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)7 OrDescr (org.drools.drl.ast.descr.OrDescr)6 AccumulateDescrBuilder (org.drools.drl.ast.dsl.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.drl.ast.dsl.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.drl.ast.dsl.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.drl.ast.dsl.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.drl.ast.dsl.BehaviorDescrBuilder)6