use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls2.
@Test
public void testFunctionCalls2() {
String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.lang.String;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " s : String()\n" + "then\n" + " s.indexOf( 0 );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_STRING), "int", null, DataType.TYPE_STRING));
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "int", null, DataType.TYPE_STRING));
methodInformation.put("java.lang.String", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
assertEquals(1, actionCallMethod1.getState());
assertEquals("indexOf", actionCallMethod1.getMethodName());
assertEquals("s", actionCallMethod1.getVariable());
assertEquals(1, actionCallMethod1.getFieldValues().length);
assertEquals("indexOf", actionCallMethod1.getFieldValues()[0].getField());
assertEquals("0", actionCallMethod1.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod1.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod1.getFieldValues()[0].getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNestedMethodOneParameter.
@Test
public void testLHSNestedMethodOneParameter() {
String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.util.List;\n" + "import org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass;\n" + "import org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + " $foo : MyListContainerClass()\n" + " $bar : MyStringContainerClass( myString == $foo.myList.get(1))\n" + "then\n" + "end\n";
addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass", "this", "org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass", DataType.TYPE_THIS);
addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass", "myList", "java.util.List", "java.util.List");
addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass", "this", "org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass", DataType.TYPE_THIS);
addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass", "myString", "java.lang.String", DataType.TYPE_STRING);
addMethodInformation("java.util.List", "get", new ArrayList<String>() {
{
add("Integer");
}
}, "java.lang.Object", null, "java.lang.Object");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFactPatternWithBinding.
@Test
public void testFactPatternWithBinding() {
String drl = "rule \"rule1\"\n" + "when\n" + "$a : Applicant()\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("$a", fp.getBoundName());
assertEquals(0, fp.getNumberOfConstraints());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraintsOnlyLeafOperator.
@Test
public void testNestedFieldConstraintsOnlyLeafOperator() {
String drl = "rule \"rule1\"\n" + "when\n" + "ParentType( parentChildField.childField == \"hello\" )\n" + "then\n" + "end";
addModelField("org.test.ParentType", "parentChildField", "org.test.ChildType", "ChildType");
addModelField("org.test.ChildType", "childField", "java.lang.String", "String");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
assertEquals("childField", ebLeftSide.getFieldName());
assertEquals("java.lang.String", ebLeftSide.getFieldType());
assertEquals("==", ebLeftSide.getOperator());
assertEquals("hello", ebLeftSide.getValue());
assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
assertEquals("ParentType", expressionUnboundFact.getName());
assertEquals("ParentType", expressionUnboundFact.getClassType());
assertEquals("ParentType", expressionUnboundFact.getGenericType());
assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
assertNull(expressionUnboundFact.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
assertEquals("parentChildField", expressionField1.getName());
assertEquals("org.test.ChildType", expressionField1.getClassType());
assertEquals("ChildType", expressionField1.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
assertEquals("childField", expressionField2.getName());
assertEquals("java.lang.String", expressionField2.getClassType());
assertEquals("String", expressionField2.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
assertNull(expressionField2.getNext());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS_WithKeyword_then.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1173842
void testDSLExpansionLHS_WithKeyword_then() {
String expected_dslr = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "- age more then 55\n" + "then\n" + "end\n";
String expected_drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "Applicant( age > 55 )\n" + "then\n" + "end\n";
final String dslDefinition1 = "There is an Applicant";
final String dslFile1 = "[when]" + dslDefinition1 + "=Applicant( )";
final String dslDefinition2 = "- age more then {age}";
final String dslFile2 = "[when]" + dslDefinition2 + "=age > {age}";
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(expected_dslr, Collections.emptyList(), dmo, dslFile1, dslFile2);
assertNotNull(m);
assertEquals(2, m.lhs.length);
assertTrue(m.lhs[0] instanceof DSLSentence);
assertTrue(m.lhs[1] instanceof DSLSentence);
DSLSentence dslSentence1 = (DSLSentence) m.lhs[0];
assertEquals(dslDefinition1, dslSentence1.getDefinition());
assertEquals(0, dslSentence1.getValues().size());
DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
assertEquals(dslDefinition2, dslSentence2.getDefinition());
assertEquals(1, dslSentence2.getValues().size());
DSLVariableValue dslVariableValue = dslSentence2.getValues().get(0);
assertEquals("55", dslVariableValue.getValue());
// Check round-trip
assertEqualsIgnoreWhitespace(expected_dslr, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
// Check DSL expansion (as BZ stated runtime was flawed as well)
final Expander expander = new DefaultExpander();
final List<DSLMappingFile> dsls = new ArrayList<>();
try {
final DSLTokenizedMappingFile dslTokenizer1 = new DSLTokenizedMappingFile();
if (dslTokenizer1.parseAndLoad(new StringReader(dslFile1))) {
dsls.add(dslTokenizer1);
} else {
fail();
}
final DSLTokenizedMappingFile dslTokenizer2 = new DSLTokenizedMappingFile();
if (dslTokenizer2.parseAndLoad(new StringReader(dslFile2))) {
dsls.add(dslTokenizer2);
} else {
fail();
}
} catch (IOException e) {
fail();
}
for (DSLMappingFile dsl : dsls) {
expander.addDSLMapping(dsl.getMapping());
}
final String actual_drl = expander.expand(expected_dslr);
assertEqualsIgnoreWhitespace(expected_drl, actual_drl);
}
Aggregations