use of org.drools.workbench.models.datamodel.rule.ExpressionField 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.ExpressionField in project drools by kiegroup.
the class RuleModelTest method testAllVariableBindings2.
@Test
public void testAllVariableBindings2() {
final RuleModel model = new RuleModel();
model.lhs = new IPattern[1];
final FactPattern fp = new FactPattern("Car");
model.lhs[0] = fp;
fp.setBoundName("$c");
SingleFieldConstraint sfc = new SingleFieldConstraintEBLeftSide("make");
sfc.getExpressionValue().appendPart(new ExpressionField("make", "java.lang.String", "String"));
sfc.setFieldBinding("$m");
fp.addConstraint(sfc);
List<String> vars = model.getAllVariables();
assertEquals(2, vars.size());
assertEquals("$c", vars.get(0));
assertEquals("$m", vars.get(1));
}
use of org.drools.workbench.models.datamodel.rule.ExpressionField in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFromBoundVariable.
@Test
public void testFromBoundVariable() {
String drl = "import java.lang.Number;\n" + "import org.drools.workbench.models.commons.backend.rule.Counter;\n" + "rule \"rule1\"\n" + "when\n" + "cc : Counter()\n" + "Number() from cc.number\n" + "then\n" + "end";
addModelField("org.drools.workbench.models.commons.backend.rule.Counter", "number", "java.lang.Number", DataType.TYPE_NUMERIC);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(2, m.lhs.length);
IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
FactPattern fp0 = (FactPattern) p0;
assertEquals("Counter", fp0.getFactType());
assertEquals("cc", fp0.getBoundName());
assertEquals(0, fp0.getNumberOfConstraints());
IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCompositeFactPattern);
FromCompositeFactPattern fcfp1 = (FromCompositeFactPattern) p1;
FactPattern fp1 = fcfp1.getFactPattern();
ExpressionFormLine efl1 = fcfp1.getExpression();
assertNotNull(fp1);
assertNotNull(efl1);
assertEquals("Number", fp1.getFactType());
assertEquals(0, fp1.getNumberOfConstraints());
assertEquals(2, efl1.getParts().size());
assertTrue(efl1.getParts().get(0) instanceof ExpressionVariable);
assertTrue(efl1.getParts().get(1) instanceof ExpressionField);
ExpressionVariable eflv1 = (ExpressionVariable) efl1.getParts().get(0);
assertEquals("cc", eflv1.getName());
assertEquals("Counter", eflv1.getClassType());
assertEquals(DataType.TYPE_NUMERIC, eflv1.getGenericType());
ExpressionField eflf1 = (ExpressionField) efl1.getParts().get(1);
assertEquals("number", eflf1.getName());
assertEquals("java.lang.Number", eflf1.getClassType());
assertEquals(DataType.TYPE_NUMERIC, eflf1.getGenericType());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionField in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressionsWithAnotherExpression.
@Test
public void testNestedFieldExpressionsWithAnotherExpression() {
String drl = "rule rule1\n" + "when\n" + "p : Person( address.postalCode == p.address.postalCode )\n" + "then\n" + "end";
addModelField("org.test.Person", "address", "org.test.Address", "Address");
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Address", "postalCode", "java.lang.Integer", "Integer");
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("postalCode", ebLeftSide.getFieldName());
assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
assertEquals("==", ebLeftSide.getOperator());
assertEquals("", ebLeftSide.getValue());
assertEquals(3, ebLeftSide.getExpressionValue().getParts().size());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(0) instanceof ExpressionVariable);
ExpressionVariable expressionVariable = (ExpressionVariable) ebLeftSide.getExpressionValue().getParts().get(0);
assertEquals("p", expressionVariable.getName());
assertEquals("org.test.Person", expressionVariable.getClassType());
assertEquals(DataType.TYPE_THIS, expressionVariable.getGenericType());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(1) instanceof ExpressionField);
ExpressionField ef1 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(1);
assertEquals("address", ef1.getName());
assertEquals("org.test.Address", ef1.getClassType());
assertEquals("Address", ef1.getGenericType());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(2) instanceof ExpressionField);
ExpressionField ef2 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(2);
assertEquals("postalCode", ef2.getName());
assertEquals("java.lang.Integer", ef2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, ef2.getGenericType());
assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
assertEquals("Person", expressionUnboundFact.getName());
assertEquals("Person", expressionUnboundFact.getClassType());
assertEquals("Person", 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("address", expressionField1.getName());
assertEquals("org.test.Address", expressionField1.getClassType());
assertEquals("Address", 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("postalCode", expressionField2.getName());
assertEquals("java.lang.Integer", expressionField2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, expressionField2.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
assertNull(expressionField2.getNext());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionField in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parsePatternSource.
private IFactPattern parsePatternSource(final RuleModel m, final PatternDescr pattern, final PatternSourceDescr patternSource, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
if (pattern.getIdentifier() != null) {
boundParams.put(pattern.getIdentifier(), pattern.getObjectType());
}
if (patternSource instanceof AccumulateDescr) {
AccumulateDescr accumulate = (AccumulateDescr) patternSource;
FromAccumulateCompositeFactPattern fac = new FromAccumulateCompositeFactPattern();
fac.setSourcePattern(parseBaseDescr(m, accumulate.getInput(), isJavaDialect, boundParams, dmo));
fac.setInitCode(accumulate.getInitCode());
fac.setActionCode(accumulate.getActionCode());
fac.setReverseCode(accumulate.getReverseCode());
fac.setResultCode(accumulate.getResultCode());
FactPattern factPattern = new FactPattern(pattern.getObjectType());
factPattern.setBoundName(pattern.getIdentifier());
parseConstraint(m, factPattern, pattern.getConstraint(), isJavaDialect, boundParams, dmo);
fac.setFactPattern(factPattern);
for (AccumulateDescr.AccumulateFunctionCallDescr func : accumulate.getFunctions()) {
String funcName = func.getFunction();
boolean first = true;
StringBuilder sb = new StringBuilder();
for (String param : func.getParams()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(param);
}
fac.setFunction(funcName + "(" + sb + ")");
break;
}
return fac;
} else if (patternSource instanceof CollectDescr) {
CollectDescr collect = (CollectDescr) patternSource;
FromCollectCompositeFactPattern fac = new FromCollectCompositeFactPattern();
fac.setRightPattern(parseBaseDescr(m, collect.getInputPattern(), isJavaDialect, boundParams, dmo));
fac.setFactPattern(getFactPattern(m, pattern, isJavaDialect, boundParams, dmo));
return fac;
} else if (patternSource instanceof EntryPointDescr) {
EntryPointDescr entryPoint = (EntryPointDescr) patternSource;
FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
fep.setEntryPointName(entryPoint.getText());
fep.setFactPattern(getFactPattern(m, pattern, isJavaDialect, boundParams, dmo));
return fep;
} else if (patternSource instanceof FromDescr) {
FromDescr from = (FromDescr) patternSource;
FromCompositeFactPattern fcfp = new FromCompositeFactPattern();
FactPattern factPattern = new FactPattern(pattern.getObjectType());
factPattern.setBoundName(pattern.getIdentifier());
parseConstraint(m, factPattern, pattern.getConstraint(), isJavaDialect, boundParams, dmo);
fcfp.setFactPattern(factPattern);
ExpressionFormLine expression = new ExpressionFormLine();
fcfp.setExpression(expression);
String dataSource = from.getDataSource().toString();
String[] splitSource = dataSource.split("\\.");
ModelField[] fields = null;
for (int i = 0; i < splitSource.length; i++) {
String sourcePart = splitSource[i];
if (i == 0) {
String type = boundParams.get(sourcePart);
expression.appendPart(new ExpressionVariable(sourcePart, type, DataType.TYPE_NUMERIC));
fields = findFields(m, dmo, type);
} else {
ModelField modelField = null;
if (fields != null) {
for (ModelField field : fields) {
if (field.getName().equals(sourcePart)) {
modelField = field;
break;
}
}
}
if (modelField == null) {
final String previousClassName = expression.getClassType();
final List<MethodInfo> mis = dmo.getModuleMethodInformation().get(previousClassName);
boolean isMethod = false;
if (mis != null) {
for (MethodInfo mi : mis) {
if (mi.getName().equals(sourcePart)) {
expression.appendPart(new ExpressionMethod(mi.getName(), mi.getReturnClassType(), mi.getGenericType(), mi.getParametricReturnType()));
isMethod = true;
break;
}
}
}
if (isMethod == false) {
expression.appendPart(new ExpressionText(sourcePart));
}
} else {
expression.appendPart(new ExpressionField(sourcePart, modelField.getClassName(), modelField.getType()));
fields = findFields(m, dmo, modelField.getClassName());
}
}
}
return fcfp;
}
throw new RuntimeException("Unknown pattern source " + patternSource);
}
Aggregations