Search in sources :

Example 1 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testFieldConstraintLessThanOrEqualTo.

@Test
public void testFieldConstraintLessThanOrEqualTo() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age <= 22 )\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Applicant", fp.getFactType());
    assertEquals(1, fp.getNumberOfConstraints());
    FieldConstraint fc = fp.getConstraint(0);
    assertNotNull(fc);
    assertTrue(fc instanceof SingleFieldConstraint);
    SingleFieldConstraint sfc = (SingleFieldConstraint) fc;
    assertEquals("<=", sfc.getOperator());
    assertEquals("22", sfc.getValue());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) Test(org.junit.Test)

Example 2 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testFromAccumulate.

@Test
public void testFromAccumulate() {
    String drl = "package org.test;\n" + "import java.lang.Number;\n" + "rule \"rule1\"\n" + "when\n" + "  total : Number( intValue > 0 ) from accumulate ( Applicant( age < 30 ), count() )\n" + "then\n" + "end";
    addModelField("java.lang.Number", "intValue", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
    addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof FromAccumulateCompositeFactPattern);
    FromAccumulateCompositeFactPattern pattern = (FromAccumulateCompositeFactPattern) m.lhs[0];
    assertNotNull(pattern.getFactPattern());
    FactPattern factPattern = pattern.getFactPattern();
    assertEquals("total", factPattern.getBoundName());
    assertNotNull(factPattern.getConstraintList());
    assertEquals(1, factPattern.getConstraintList().getNumberOfConstraints());
    FieldConstraint constraint = factPattern.getConstraintList().getConstraint(0);
    assertTrue(constraint instanceof SingleFieldConstraint);
    SingleFieldConstraint fieldConstraint = (SingleFieldConstraint) constraint;
    assertEquals("Number", fieldConstraint.getFactType());
    assertEquals("intValue", fieldConstraint.getFieldName());
    assertEquals("Integer", fieldConstraint.getFieldType());
    assertEquals(">", fieldConstraint.getOperator());
    assertEquals("0", fieldConstraint.getValue());
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) Test(org.junit.Test)

Example 3 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method marshalRule.

protected String marshalRule(final RuleModel model) {
    boolean isDSLEnhanced = model.hasDSLSentences();
    bindingsPatterns = new HashMap<String, IFactPattern>();
    bindingsFields = new HashMap<String, FieldConstraint>();
    fixActionInsertFactBindings(model.rhs);
    StringBuilder buf = new StringBuilder();
    // Build rule
    this.marshalPackageHeader(model, buf);
    this.marshalRuleHeader(model, buf);
    this.marshalMetadata(buf, model);
    this.marshalAttributes(buf, model);
    buf.append("\twhen\n");
    this.marshalLHS(buf, model, isDSLEnhanced, new LHSGeneratorContextFactory());
    buf.append("\tthen\n");
    this.marshalRHS(buf, model, isDSLEnhanced, new RHSGeneratorContextFactory());
    this.marshalFooter(buf);
    return buf.toString();
}
Also used : LHSGeneratorContextFactory(org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContextFactory) RHSGeneratorContextFactory(org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContextFactory) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 4 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.

the class BRLRuleModelTest method testDecisionTableColumnsWithLHSBoundFields.

@Test
public void testDecisionTableColumnsWithLHSBoundFields() {
    GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$p1");
    ConditionCol52 c1 = new ConditionCol52();
    c1.setFactField("name");
    c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    c1.setBinding("$c1");
    p1.getChildColumns().add(c1);
    dt.getConditions().add(p1);
    BRLConditionColumn brlCondition = new BRLConditionColumn();
    FactPattern fp = new FactPattern("Driver");
    fp.setBoundName("$brl1");
    SingleFieldConstraint sfc1 = new SingleFieldConstraint();
    sfc1.setFieldBinding("$sfc1");
    sfc1.setOperator("==");
    sfc1.setFactType("Driver");
    sfc1.setFieldName("name");
    sfc1.setFieldType(DataType.TYPE_STRING);
    fp.addConstraint(sfc1);
    brlCondition.getDefinition().add(fp);
    dt.getConditions().add(brlCondition);
    ActionInsertFactCol52 ins = new ActionInsertFactCol52();
    ins.setBoundName("$ins");
    ins.setFactField("rating");
    ins.setFactType("Person");
    ins.setType(DataType.TYPE_STRING);
    dt.getActionCols().add(ins);
    BRLRuleModel model = new BRLRuleModel(dt);
    FieldConstraint fcr1 = model.getLHSBoundField("$sfc1");
    assertNotNull(fcr1);
    assertTrue(fcr1 instanceof SingleFieldConstraint);
    SingleFieldConstraint fcr1sfc = (SingleFieldConstraint) fcr1;
    assertEquals("name", fcr1sfc.getFieldName());
    assertEquals(DataType.TYPE_STRING, fcr1sfc.getFieldType());
}
Also used : BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ConditionCol52(org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) Test(org.junit.Test)

Example 5 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.

the class GuidedDecisionTreeModelUnmarshallingVisitor method visit.

private List<Node> visit(final IPattern p, final GuidedDecisionTree model, final PackageDataModelOracle dmo, final List<ParserMessage> messages) {
    final List<Node> nodes = new ArrayList<Node>();
    if (!(p instanceof FactPattern)) {
        messages.add(new UnsupportedIPatternParserMessage());
        return nodes;
    }
    final FactPattern fp = (FactPattern) p;
    if (fp.isNegated()) {
        messages.add(new UnsupportedIPatternParserMessage());
        return nodes;
    }
    if (fp.getWindow().getOperator() != null) {
        messages.add(new UnsupportedIPatternParserMessage());
        return nodes;
    }
    final TypeNode node = new TypeNodeImpl(fp.getFactType());
    if (fp.isBound()) {
        node.setBinding(fp.getBoundName());
    }
    nodes.add(node);
    for (FieldConstraint fc : fp.getFieldConstraints()) {
        nodes.addAll(visit(fc, model, dmo, messages));
    }
    return nodes;
}
Also used : UnsupportedIPatternParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedIPatternParserMessage) TypeNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.TypeNodeImpl) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ArrayList(java.util.ArrayList) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Aggregations

FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)23 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)23 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)18 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)10 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)7 ConnectiveConstraint (org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)3 IFactPattern (org.drools.workbench.models.datamodel.rule.IFactPattern)3 ConditionCol52 (org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52)3 FlexTable (com.google.gwt.user.client.ui.FlexTable)2 Image (com.google.gwt.user.client.ui.Image)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LHSGeneratorContextFactory (org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContextFactory)2 RHSGeneratorContextFactory (org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContextFactory)2