Search in sources :

Example 1 with GuidedDecisionTreeParserError

use of org.drools.workbench.models.guided.dtree.shared.model.parser.GuidedDecisionTreeParserError in project drools by kiegroup.

the class GuidedDecisionTreeModelMarshallingVisitor method visit.

public String visit(final GuidedDecisionTree model) {
    if (model == null) {
        return "";
    }
    // Append the DRL generated from the model
    if (model.getRoot() != null) {
        baseRuleName = model.getTreeName();
        final List<Node> path = new ArrayList<Node>();
        visit(path, model.getRoot());
    }
    // Append the DRL stored as a result of parsing errors
    for (GuidedDecisionTreeParserError error : model.getParserErrors()) {
        rules.append(error.getOriginalDrl()).append("\n");
    }
    return rules.toString();
}
Also used : GuidedDecisionTreeParserError(org.drools.workbench.models.guided.dtree.shared.model.parser.GuidedDecisionTreeParserError) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ArrayList(java.util.ArrayList)

Example 2 with GuidedDecisionTreeParserError

use of org.drools.workbench.models.guided.dtree.shared.model.parser.GuidedDecisionTreeParserError in project drools by kiegroup.

the class GuidedDecisionTreeModelUnmarshallingVisitor method visit.

private GuidedDecisionTree visit(final String drl, final String baseFileName, final List<String> globals, final PackageDataModelOracle dmo) {
    PortablePreconditions.checkNotNull("drl", drl);
    PortablePreconditions.checkNotNull("baseFileName", baseFileName);
    PortablePreconditions.checkNotNull("globals", globals);
    PortablePreconditions.checkNotNull("dmo", dmo);
    final GuidedDecisionTree model = new GuidedDecisionTree();
    model.setTreeName(baseFileName);
    // De-serialize Package name
    final String packageName = PackageNameParser.parsePackageName(drl);
    model.setPackageName(packageName);
    // De-serialize imports
    final Imports imports = ImportsParser.parseImports(drl);
    for (Import item : imports.getImports()) {
        model.getImports().addImport(item);
    }
    // Split DRL into separate rules
    final List<String> rules = new ArrayList<String>();
    final Pattern pattern = Pattern.compile("\\s?rule\\s(.+?)\\send\\s?", Pattern.DOTALL);
    final Matcher matcher = pattern.matcher(drl);
    while (matcher.find()) {
        rules.add(matcher.group());
    }
    // Build a linear Path of Nodes for each rule
    final List<GuidedDecisionTreeParserExtendedError> rulesParserContent = new ArrayList<GuidedDecisionTreeParserExtendedError>();
    for (String rule : rules) {
        final GuidedDecisionTreeParserExtendedError ruleParserContent = new GuidedDecisionTreeParserExtendedError();
        rulesParserContent.add(ruleParserContent);
        try {
            final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal(rule, globals, dmo);
            ruleParserContent.setOriginalRuleName(rm.name);
            ruleParserContent.setOriginalDrl(rule);
            for (IPattern p : rm.lhs) {
                ruleParserContent.getNodes().addAll(visit(p, model, dmo, ruleParserContent.getMessages()));
            }
            for (IAction a : rm.rhs) {
                ruleParserContent.getNodes().addAll(visit(a, getTypesOnPath(ruleParserContent.getNodes()), model, dmo, ruleParserContent.getMessages()));
            }
        } catch (Exception e) {
            ruleParserContent.getMessages().add(new DefaultParserMessage(e.getMessage()));
        }
    }
    // Combine Paths into a single tree.
    for (GuidedDecisionTreeParserExtendedError ruleParserContent : rulesParserContent) {
        Node activeModelNode = null;
        boolean error = !ruleParserContent.getMessages().isEmpty();
        for (int index = 0; !error && index < ruleParserContent.getNodes().size(); index++) {
            final Node node = ruleParserContent.getNodes().get(index);
            switch(index) {
                case 0:
                    if (!(node instanceof TypeNode)) {
                        ruleParserContent.getMessages().add(new InvalidRootParserMessage());
                        error = true;
                        break;
                    }
                    final TypeNode root = (TypeNode) node;
                    if (model.getRoot() == null) {
                        model.setRoot(root);
                    } else if (!root.equals(model.getRoot())) {
                        ruleParserContent.getMessages().add(new AmbiguousRootParserMessage(root.getClassName()));
                        error = true;
                        break;
                    }
                    activeModelNode = model.getRoot();
                    break;
                default:
                    if (!activeModelNode.getChildren().contains(node)) {
                        // If the active node in the Model doesn't contain the child add it as a new child
                        activeModelNode.addChild(node);
                        activeModelNode = node;
                    } else {
                        // Otherwise swap out the Node in the Path for the existing one in the Model
                        activeModelNode = activeModelNode.getChildren().get(activeModelNode.getChildren().indexOf(node));
                    }
            }
        }
        if (!ruleParserContent.getMessages().isEmpty()) {
            model.getParserErrors().add(new GuidedDecisionTreeParserError(ruleParserContent.getOriginalRuleName(), ruleParserContent.getOriginalDrl(), ruleParserContent.getMessages()));
        }
    }
    return model;
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) Pattern(java.util.regex.Pattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) IPattern(org.drools.workbench.models.datamodel.rule.IPattern) Import(org.kie.soup.project.datamodel.imports.Import) IAction(org.drools.workbench.models.datamodel.rule.IAction) Matcher(java.util.regex.Matcher) InvalidRootParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.InvalidRootParserMessage) GuidedDecisionTreeParserError(org.drools.workbench.models.guided.dtree.shared.model.parser.GuidedDecisionTreeParserError) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ArrayList(java.util.ArrayList) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) DefaultParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.DefaultParserMessage) Imports(org.kie.soup.project.datamodel.imports.Imports) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) AmbiguousRootParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.AmbiguousRootParserMessage)

Aggregations

ArrayList (java.util.ArrayList)2 ActionInsertNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode)2 ActionRetractNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode)2 ActionUpdateNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode)2 ConstraintNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode)2 Node (org.drools.workbench.models.guided.dtree.shared.model.nodes.Node)2 TypeNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode)2 GuidedDecisionTreeParserError (org.drools.workbench.models.guided.dtree.shared.model.parser.GuidedDecisionTreeParserError)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)1 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)1 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)1 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)1 IAction (org.drools.workbench.models.datamodel.rule.IAction)1 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)1 GuidedDecisionTree (org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree)1 AmbiguousRootParserMessage (org.drools.workbench.models.guided.dtree.shared.model.parser.messages.AmbiguousRootParserMessage)1