use of org.drools.workbench.models.datamodel.rule.IFactPattern 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.IFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method marshalRule.
protected String marshalRule(final RuleModel model) {
boolean isDSLEnhanced = model.hasDSLSentences();
bindingsPatterns = new HashMap<String, IFactPattern>();
bindingsFields = new HashMap<String, FieldConstraint>();
fixActionInsertFactBindings(model.rhs);
StringBuilder buf = new StringBuilder();
// Build rule
this.marshalPackageHeader(model, buf);
this.marshalRuleHeader(model, buf);
this.marshalMetadata(buf, model);
this.marshalAttributes(buf, model);
buf.append("\twhen\n");
this.marshalLHS(buf, model, isDSLEnhanced, new LHSGeneratorContextFactory());
buf.append("\tthen\n");
this.marshalRHS(buf, model, isDSLEnhanced, new RHSGeneratorContextFactory());
this.marshalFooter(buf);
return buf.toString();
}
use of org.drools.workbench.models.datamodel.rule.IFactPattern in project drools-wb by kiegroup.
the class CompositeFactPatternWidget method doLayout.
protected void doLayout() {
this.layout.setWidget(0, 0, getCompositeLabel());
this.layout.getFlexCellFormatter().setColSpan(0, 0, 2);
// this.layout.getFlexCellFormatter().setWidth(0, 0, "15%");
this.layout.setWidget(1, 0, new HTML(" "));
if (this.pattern.getPatterns() != null) {
DirtyableVerticalPane vert = new DirtyableVerticalPane();
IFactPattern[] facts = pattern.getPatterns();
for (int i = 0; i < facts.length; i++) {
RuleModellerWidget widget = this.getModeller().getWidgetFactory().getWidget(this.getModeller(), this.getEventBus(), facts[i], this.readOnly);
widget.addOnModifiedCommand(new Command() {
public void execute() {
setModified(true);
}
});
// Wrap widget so the Fact pattern can be deleted
vert.add(wrapLHSWidget(pattern, i, widget));
vert.add(spacerWidget());
}
this.layout.setWidget(1, 1, vert);
}
}
use of org.drools.workbench.models.datamodel.rule.IFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parseExistentialElementDescr.
private CompositeFactPattern parseExistentialElementDescr(final RuleModel m, final ConditionalElementDescr conditionalDescr, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
CompositeFactPattern comp;
if (conditionalDescr instanceof NotDescr) {
comp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_NOT);
} else if (conditionalDescr instanceof OrDescr) {
comp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
} else if (conditionalDescr instanceof ExistsDescr) {
comp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_EXISTS);
} else {
throw new IllegalArgumentException("Unknown conditional descr type: " + conditionalDescr);
}
addPatternToComposite(m, conditionalDescr, comp, isJavaDialect, boundParams, dmo);
IFactPattern[] patterns = comp.getPatterns();
return patterns != null && patterns.length > 0 ? comp : null;
}
use of org.drools.workbench.models.datamodel.rule.IFactPattern in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceImpl method marshalRule.
@Override
protected String marshalRule(final RuleModel model) {
boolean isDSLEnhanced = model.hasDSLSentences();
bindingsPatterns = new HashMap<String, IFactPattern>();
bindingsFields = new HashMap<String, FieldConstraint>();
fixActionInsertFactBindings(model.rhs);
StringBuilder buf = new StringBuilder();
StringBuilder header = new StringBuilder();
LHSGeneratorContextFactory lhsGeneratorContextFactory = new LHSGeneratorContextFactory();
RHSGeneratorContextFactory rhsGeneratorContextFactory = new RHSGeneratorContextFactory();
// Build rule
this.marshalRuleHeader(model, header);
super.marshalMetadata(buf, model);
super.marshalAttributes(buf, model);
buf.append("\twhen\n");
super.marshalLHS(buf, model, isDSLEnhanced, lhsGeneratorContextFactory);
buf.append("\tthen\n");
super.marshalRHS(buf, model, isDSLEnhanced, rhsGeneratorContextFactory);
this.marshalFooter(buf);
for (LHSGeneratorContext gc : lhsGeneratorContextFactory.getGeneratorContexts()) {
header.append("@code{hasLHSOutput" + gc.getDepth() + "_" + gc.getOffset() + " = false}");
header.append("@code{hasLHSNonTemplateOutput" + gc.getDepth() + "_" + gc.getOffset() + " = " + gc.hasNonTemplateOutput() + "}");
}
for (RHSGeneratorContext gc : rhsGeneratorContextFactory.getGeneratorContexts()) {
header.append("@code{hasRHSOutput" + gc.getDepth() + "_" + gc.getOffset() + " = false}");
header.append("@code{hasRHSNonTemplateOutput" + gc.getDepth() + "_" + gc.getOffset() + " = " + gc.hasNonTemplateOutput() + "}");
}
header.append("@code{\n" + " def removeDelimitingQuotes(value) {\n" + " if(value.startsWith('\"') && value.endsWith('\"')) {\n" + " return value.substring(1, value.length() - 1);\n" + " }\n" + " value;\n" + " }\n" + "}");
header.append("@code{\n" + "def capitals(value) {\n" + " value.toUpperCase();\n" + "}}");
header.append("@code{\n" + " def makeValueList(value) {\n" + " if(value.startsWith('\"') && value.endsWith('\"')) {\n" + " value = value.substring(1, value.length() - 1);\n" + " }\n" + " workingValue = value.trim();\n" + " if ( workingValue.startsWith('(') ) {\n" + " workingValue = workingValue.substring( 1 );\n" + " }\n" + " if ( workingValue.endsWith(')') ) {\n" + " workingValue = workingValue.substring( 0," + " workingValue.length() - 1 );\n" + " }\n" + " values = workingValue.split( ',' );\n" + " output = ' (';\n" + " for (v : values ) {\n" + " v = v.trim();\n" + " if ( v.startsWith( '\"' ) ) {\n" + " v = v.substring( 1 );\n" + " }\n" + " if ( v.endsWith( '\"' ) ) {\n" + " v = v.substring( 0,v.length() - 1 );\n" + " }\n" + " output+='\"'+v+'\", ';\n" + " }" + " output=output.substring(0,output.length()-2)+')';\n" + " output;\n" + " }\n" + "}");
return header.append(buf).toString();
}
Aggregations