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());
}
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());
}
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();
}
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());
}
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;
}
Aggregations