use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testInsertLogical.
@Test
public void testInsertLogical() {
String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" + "\t\tAccident( )\n\tthen\n\t\tinsertLogical( new Report() );\nend\n";
final RuleModel m = new RuleModel();
m.addLhsItem(new FactPattern("Person"));
m.addLhsItem(new FactPattern("Accident"));
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addRhsItem(new ActionInsertLogicalFact("Report"));
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeAndConstraints.
@Test
public void testCompositeAndConstraints() {
RuleModel m = new RuleModel();
m.name = "and composite";
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
p.addConstraint(comp);
final SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFactType("Goober");
sfc1.setFieldName("gooField");
sfc1.setFieldType(DataType.TYPE_STRING);
sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc1.setValue("gooValue");
sfc1.setOperator("==");
comp.addConstraint(sfc1);
final SingleFieldConstraint sfc2 = new SingleFieldConstraint();
sfc2.setFactType("Goober");
sfc2.setFieldName("fooField");
sfc2.setFieldType(DataType.TYPE_OBJECT);
sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc2.setOperator("!= null");
comp.addConstraint(sfc2);
final SingleFieldConstraint sfc3 = new SingleFieldConstraint();
sfc3.setFactType("Bar");
sfc3.setFieldName("barField");
sfc3.setParent(sfc2);
sfc3.setFieldType(DataType.TYPE_STRING);
sfc3.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc3.setValue("barValue");
sfc3.setOperator("==");
comp.addConstraint(sfc3);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String expected = "rule \"and composite\"" + "dialect \"mvel\"\n" + "when\n" + "Goober( gooField == \"gooValue\" && fooField != null && fooField.barField == \"barValue\" )\n" + "then\n" + "insert( new Whee() );\n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testNotSoundsLikeAndNotMatches.
@Test
public void testNotSoundsLikeAndNotMatches() {
final String ruleName = "test not soundslike constraint";
final RuleModel model = new RuleModel();
model.name = ruleName;
final FactPattern pattern = new FactPattern("Person");
final CompositeFieldConstraint notMatchesAndNotSoundsLike = new CompositeFieldConstraint();
notMatchesAndNotSoundsLike.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
pattern.addConstraint(notMatchesAndNotSoundsLike);
final SingleFieldConstraint fieldNotSoundsLikeGoo = new SingleFieldConstraint();
fieldNotSoundsLikeGoo.setFieldType(DataType.TYPE_STRING);
fieldNotSoundsLikeGoo.setFieldName("field");
fieldNotSoundsLikeGoo.setOperator("not soundslike");
fieldNotSoundsLikeGoo.setValue("goo");
fieldNotSoundsLikeGoo.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
notMatchesAndNotSoundsLike.addConstraint(fieldNotSoundsLikeGoo);
final SingleFieldConstraint fieldNotMatchesGoo = new SingleFieldConstraint();
fieldNotMatchesGoo.setFieldType(DataType.TYPE_STRING);
fieldNotMatchesGoo.setFieldName("field");
fieldNotMatchesGoo.setOperator("not matches");
fieldNotMatchesGoo.setValue("goo");
fieldNotMatchesGoo.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
notMatchesAndNotSoundsLike.addConstraint(fieldNotMatchesGoo);
model.addLhsItem(pattern);
String expected = "rule \"" + ruleName + "\"" + "\tdialect \"mvel\"\n when " + "Person( field not soundslike \"goo\" && field not matches \"goo\" )" + "then" + "end";
checkMarshalling(expected, model);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testDSLWithFromAccumulate.
@Test
public void testDSLWithFromAccumulate() {
final RuleModel m = new RuleModel();
m.name = "testDSLWithFromAccumulate";
m.lhs = new IPattern[2];
m.rhs = new IAction[0];
final FromAccumulateCompositeFactPattern from = new FromAccumulateCompositeFactPattern();
final FactPattern person = new FactPattern("Person");
person.setBoundName("$p");
m.lhs[0] = person;
from.setFactPattern(new FactPattern("java.lang.Number"));
from.setSourcePattern(person);
from.setFunction("count($p)");
m.lhs[1] = from;
final RuleModel modelSpy = spy(m);
doReturn(true).when(modelSpy).hasDSLSentences();
final String drl = ruleModelPersistence.marshal(modelSpy);
final RuleModel tempModel = spy(ruleModelPersistence.unmarshalUsingDSL(drl, null, dmo, ""));
doReturn(true).when(tempModel).hasDSLSentences();
final String newDrl = ruleModelPersistence.marshal(tempModel);
final RuleModel newModel = ruleModelPersistence.unmarshalUsingDSL(newDrl, null, dmo, "");
assertTrue(newModel.lhs[0] instanceof FactPattern);
assertTrue(newModel.lhs[1] instanceof FromAccumulateCompositeFactPattern);
assertDSLEscaping(newDrl);
Assertions.assertThat(newDrl).contains(">java.lang.Number( ) from accumulate ( $p : Person( ),\n").contains(">\t\t\tcount($p))");
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testEnumTypeNumeric.
@Test
public void testEnumTypeNumeric() {
// A legacy "Guvnor" enums (i.e pick-list of underlying field data-type)
String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( age == 100 )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n";
final RuleModel m = new RuleModel();
final FactPattern pat = new FactPattern("Cheese");
m.addLhsItem(pat);
final SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldName("age");
con.setOperator("==");
con.setValue("100");
con.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
con.setConstraintValueType(BaseSingleFieldConstraint.TYPE_ENUM);
pat.addConstraint(con);
m.addRhsItem(new ActionInsertFact("Report"));
m.name = "my rule";
checkMarshalling(expected, m);
}
Aggregations