use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern 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);
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeFactPatternWithFrom.
@Test
public void testCompositeFactPatternWithFrom() {
final RuleModel m = new RuleModel();
m.name = "model";
final FactPattern fp1 = new FactPattern("Data");
fp1.setBoundName("$d");
m.addLhsItem(fp1);
final FactPattern fp2 = new FactPattern("Person");
final FromCompositeFactPattern ffp1 = new FromCompositeFactPattern();
ffp1.setExpression(new ExpressionFormLine(new ExpressionVariable(fp1.getBoundName(), fp1.getFactType())));
ffp1.setFactPattern(fp2);
m.addLhsItem(ffp1);
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
final String expected = "rule \"model\"\n" + "dialect \"mvel\"\n" + "when\n" + "$d : Data( )\n" + "(Person( ) from $d)\n" + "\n" + "then\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeFactPatternWithFromWithDSL.
@Test
public void testCompositeFactPatternWithFromWithDSL() {
final RuleModel m = new RuleModel();
m.name = "model";
final DSLSentence sen = new DSLSentence();
sen.setDefinition("A DSL phrase");
m.addLhsItem(sen);
final FactPattern fp1 = new FactPattern("Data");
fp1.setBoundName("$d");
m.addLhsItem(fp1);
final CompositeFactPattern cp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_NOT);
final FactPattern fp2 = new FactPattern("Person");
final FromCompositeFactPattern ffp1 = new FromCompositeFactPattern();
ffp1.setExpression(new ExpressionFormLine(new ExpressionVariable(fp1.getBoundName(), fp1.getFactType())));
ffp1.setFactPattern(fp2);
cp.addFactPattern(ffp1);
m.addLhsItem(cp);
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
final String expected = "rule \"model\"\n" + "dialect \"mvel\"\n" + "when\n" + "A DSL phrase\n" + ">$d : Data( )\n" + ">not ( Person( ) from $d\n" + ")\n" + "then\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class BaseBRLTest method whenThereIsABRLFromCompositeFactPattern.
protected BRLConditionColumn whenThereIsABRLFromCompositeFactPattern(final String factType, final String binding) {
final FromCompositeFactPattern fcfp = new FromCompositeFactPattern();
final FactPattern fp = new FactPattern(factType);
fp.setBoundName(binding);
fcfp.setFactPattern(fp);
final BRLConditionColumn brl = new BRLConditionColumn();
brl.getDefinition().add(fcfp);
dtable.getConditions().add(brl);
return brl;
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitFromCompositeFactPattern.
private FromCompositeFactPattern visitFromCompositeFactPattern(FromCompositeFactPattern pattern) {
FromCompositeFactPattern clone = new FromCompositeFactPattern();
clone.setExpression((ExpressionFormLine) visit(pattern.getExpression()));
clone.setFactPattern((FactPattern) visit(pattern.getFactPattern()));
return clone;
}
Aggregations