use of org.drools.workbench.models.datamodel.rule.RuleModel 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.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes2.
@Test
public void testMethodCallCheckParameterDataTypes2() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " t : MyType( )\n" + " then\n" + " t.doSomething( 1 * 2 );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
methodInformation.put("org.mortgages.MyType", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("doSomething", mc.getMethodName());
assertEquals("t", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(1, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("1 * 2", f1.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
assertEquals(FieldNatureType.TYPE_FORMULA, f1.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsVariableSamePackage.
@Test
public void testRHSInsertFactWithFieldAsVariableSamePackage() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
String drl = "package org.test\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\n" + "insert( fact0 );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Person", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Person", sfp.getFactType());
assertEquals("field1", sfp.getFieldName());
assertEquals("==", sfp.getOperator());
assertEquals("44", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals("$f", sfp.getFieldBinding());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
ActionInsertFact ap = (ActionInsertFact) a;
assertEquals("Person", ap.getFactType());
assertEquals("fact0", ap.getBoundName());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("field1", afv.getField());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv.getNature());
assertEquals("=$f", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testEnumeration.
@Test
public void testEnumeration() throws Exception {
// https://bugzilla.redhat.com/show_bug.cgi?id=1047879
String drl = "import org.drools.workbench.models.commons.backend.rule.TestEnum;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "OuterClassWithEnums( outerField == TestEnum.VALUE1 )\n" + "then\n" + "end";
addModelField("OuterClassWithEnums", "outerField", TestEnum.class.getSimpleName(), DataType.TYPE_COMPARABLE);
addJavaEnumDefinition("OuterClassWithEnums", "outerField", new String[] { "TestEnum.VALUE1=TestEnum.VALUE1", "TestEnum.VALUE2=TestEnum.VALUE2" });
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
FactPattern pattern = (FactPattern) m.lhs[0];
assertEquals(1, pattern.getNumberOfConstraints());
assertTrue(pattern.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint constraint = (SingleFieldConstraint) pattern.getConstraint(0);
assertEquals("OuterClassWithEnums", constraint.getFactType());
assertEquals("outerField", constraint.getFieldName());
assertEquals(DataType.TYPE_COMPARABLE, constraint.getFieldType());
assertEquals("==", constraint.getOperator());
assertEquals("TestEnum.VALUE1", constraint.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, constraint.getConstraintValueType());
final String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertEqualsIgnoreWhitespace(drl, drl2);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method getRuleModel.
private RuleModel getRuleModel(final ExpandedDRLInfo expandedDRLInfo, final PackageDataModelOracle dmo, final Collection<RuleModelIActionPersistenceExtension> extensions) throws RuleModelDRLPersistenceException {
// De-serialize model
RuleDescr ruleDescr = parseDrl(expandedDRLInfo);
RuleModel model = new RuleModel();
model.name = ruleDescr.getName();
model.parentName = ruleDescr.getParentName();
for (AnnotationDescr annotation : ruleDescr.getAnnotations()) {
model.addMetadata(new RuleMetadata(annotation.getName(), annotation.getValuesAsString()));
}
// De-serialize Package name
final String packageName = PackageNameParser.parsePackageName(expandedDRLInfo.plainDrl);
model.setPackageName(packageName);
// De-serialize imports
final Imports imports = ImportsParser.parseImports(expandedDRLInfo.plainDrl);
for (Import item : imports.getImports()) {
model.getImports().addImport(item);
}
boolean isJavaDialect = parseAttributes(model, ruleDescr.getAttributes());
Map<String, String> boundParams = parseLhs(model, ruleDescr.getLhs(), isJavaDialect, expandedDRLInfo, dmo);
parseRhs(model, expandedDRLInfo.consequence != null ? expandedDRLInfo.consequence : (String) ruleDescr.getConsequence(), isJavaDialect, boundParams, expandedDRLInfo, dmo, extensions);
return model;
}
Aggregations