Search in sources :

Example 1 with IFactPattern

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);
}
Also used : FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) EntryPointDescr(org.drools.compiler.lang.descr.EntryPointDescr) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromDescr(org.drools.compiler.lang.descr.FromDescr) ExpressionText(org.drools.workbench.models.datamodel.rule.ExpressionText) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) CollectDescr(org.drools.compiler.lang.descr.CollectDescr) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ArrayList(java.util.ArrayList) StringUtils.splitArgumentsList(org.drools.core.util.StringUtils.splitArgumentsList) List(java.util.List) ActionFieldList(org.drools.workbench.models.datamodel.rule.ActionFieldList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModelPersistenceHelper.findMethodInfo(org.drools.workbench.models.commons.backend.rule.RuleModelPersistenceHelper.findMethodInfo)

Example 2 with IFactPattern

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();
}
Also used : LHSGeneratorContextFactory(org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContextFactory) RHSGeneratorContextFactory(org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContextFactory) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 3 with IFactPattern

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("&nbsp;&nbsp;&nbsp;&nbsp;"));
    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);
    }
}
Also used : Command(com.google.gwt.user.client.Command) DirtyableVerticalPane(org.uberfire.ext.widgets.common.client.common.DirtyableVerticalPane) HTML(com.google.gwt.user.client.ui.HTML) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern)

Example 4 with IFactPattern

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;
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) OrDescr(org.drools.compiler.lang.descr.OrDescr) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern)

Example 5 with IFactPattern

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();
}
Also used : LHSGeneratorContextFactory(org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContextFactory) RHSGeneratorContext(org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContext) RHSGeneratorContextFactory(org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContextFactory) LHSGeneratorContext(org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContext) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Aggregations

IFactPattern (org.drools.workbench.models.datamodel.rule.IFactPattern)8 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)4 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)3 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)3 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)3 LHSGeneratorContextFactory (org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContextFactory)2 RHSGeneratorContextFactory (org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContextFactory)2 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)2 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)2 Command (com.google.gwt.user.client.Command)1 HTML (com.google.gwt.user.client.ui.HTML)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)1 CollectDescr (org.drools.compiler.lang.descr.CollectDescr)1 EntryPointDescr (org.drools.compiler.lang.descr.EntryPointDescr)1 ExistsDescr (org.drools.compiler.lang.descr.ExistsDescr)1