use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testFreeFormLine.
@Test
public void testFreeFormLine() {
RuleModel m = new RuleModel();
m.name = "with composite";
m.lhs = new IPattern[1];
m.rhs = new IAction[1];
FreeFormLine fl = new FreeFormLine();
fl.setText("Person()");
m.lhs[0] = fl;
FreeFormLine fr = new FreeFormLine();
fr.setText("fun()");
m.rhs[0] = fr;
String drl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertNotNull(drl);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel m_ = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertEquals(1, m_.lhs.length);
assertEquals(1, m_.rhs.length);
assertEquals("Person", ((FactPattern) m_.lhs[0]).getFactType());
assertEquals("fun()", ((FreeFormLine) m_.rhs[0]).getText());
}
use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeConstraintsRoundTrip.
@Test
public void testCompositeConstraintsRoundTrip() throws Exception {
RuleModel m = new RuleModel();
m.name = "with composite";
FactPattern p1 = new FactPattern("Person");
p1.setBoundName("p1");
m.addLhsItem(p1);
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
p.addConstraint(comp);
final SingleFieldConstraint X = new SingleFieldConstraint();
X.setFieldName("goo");
X.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
X.setValue("foo");
X.setOperator("==");
X.setConnectives(new ConnectiveConstraint[1]);
X.getConnectives()[0] = new ConnectiveConstraint();
X.getConnectives()[0].setConstraintValueType(ConnectiveConstraint.TYPE_LITERAL);
X.getConnectives()[0].setOperator("|| ==");
X.getConnectives()[0].setValue("bar");
comp.addConstraint(X);
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.setFieldName("goo2");
Y.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Y.setValue("foo");
Y.setOperator("==");
comp.addConstraint(Y);
CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
comp2.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
final SingleFieldConstraint Q1 = new SingleFieldConstraint();
Q1.setFieldName("goo");
Q1.setOperator("==");
Q1.setValue("whee");
Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q1);
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.setFieldName("gabba");
Q2.setOperator("==");
Q2.setValue("whee");
Q2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q2);
// now nest it
comp.addConstraint(comp2);
final SingleFieldConstraint Z = new SingleFieldConstraint();
Z.setFieldName("goo3");
Z.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Z.setValue("foo");
Z.setOperator("==");
p.addConstraint(Z);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String drl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m2);
assertEquals("with composite", m2.name);
assertEquals(m2.lhs.length, m.lhs.length);
assertEquals(m2.rhs.length, m.rhs.length);
}
use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testFreeFormatDRLAction.
@Test
public void testFreeFormatDRLAction() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.setName( \"Michael\" );\n" + "Here's something typed by the user as free-format DRL\n" + "$a.setAge( 40 );\n" + "end\n";
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m);
// LHS
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FreeFormLine);
final FreeFormLine ffl1 = (FreeFormLine) m.lhs[0];
assertEquals("$a : Applicant( )", ffl1.getText());
// RHS
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof FreeFormLine);
final FreeFormLine ffl2 = (FreeFormLine) m.rhs[0];
assertEquals("$a.setName( \"Michael\" );\n" + "Here's something typed by the user as free-format DRL\n" + "$a.setAge( 40 );", ffl2.getText());
}
use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionCallMethod.
@Test
public void testActionCallMethod() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.addName( \"Michael\" );\n" + "end\n";
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m);
// LHS
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern p = (FactPattern) m.lhs[0];
assertEquals("$a", p.getBoundName());
assertEquals("Applicant", p.getFactType());
// RHS
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
final ActionCallMethod a = (ActionCallMethod) m.rhs[0];
assertEquals("$a", a.getVariable());
assertEquals("addName", a.getMethodName());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue fv = a.getFieldValue(0);
assertEquals("Michael", fv.getValue());
}
use of org.kie.soup.project.datamodel.oracle.PackageDataModelOracle in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testBasics2.
@Test
public void testBasics2() {
final RuleModelPersistence p = RuleModelDRLPersistenceImpl.getInstance();
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 ActionInsertFact("Report"));
ActionGlobalCollectionAdd ag = new ActionGlobalCollectionAdd();
ag.setFactName("x");
ag.setGlobalName("g");
m.addRhsItem(ag);
m.name = "my rule";
final String drl = p.marshal(m);
assertTrue(drl.indexOf("Person( )") > -1);
assertTrue(drl.indexOf("Accident( )") > -1);
assertTrue(drl.indexOf("no-loop true") > -1);
assertTrue(drl.indexOf("org.kie") == -1);
assertTrue(drl.indexOf("g.add( x );") > -1);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel rm_ = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertEquals(2, rm_.rhs.length);
}
Aggregations