use of org.drools.workbench.models.guided.dtree.shared.model.parser.messages.AmbiguousRootParserMessage in project drools by kiegroup.
the class GuidedDecisionTreeDRLPersistenceUnmarshallingMessagesTest method testAmbiguousRoot.
@Test
public void testAmbiguousRoot() throws Exception {
final String drl1 = "rule \"test_0\"\n" + "when \n" + " Person( )\n" + "then \n" + "end \n";
final String drl2 = "rule \"test_1\"\n" + "when \n" + " Cheese( )\n" + "then \n" + "end \n";
final GuidedDecisionTree expected = new GuidedDecisionTree();
expected.setTreeName("test");
final TypeNode type = new TypeNodeImpl("Person");
expected.setRoot(type);
addModelField("Person", "this", "Person", DataType.TYPE_THIS);
addModelField("Cheese", "this", "Cheese", DataType.TYPE_THIS);
final GuidedDecisionTree model = getAndTestUnmarshalledModel(drl1 + drl2, "test", 1);
assertEquals(expected.getTreeName(), model.getTreeName());
assertNotNull(model.getRoot());
assertEquals(type.getClassName(), model.getRoot().getClassName());
assertFalse(model.getRoot().isBound());
assertEquals(0, model.getRoot().getChildren().size());
assertEquals("test_1", model.getParserErrors().get(0).getOriginalRuleName());
assertEqualsIgnoreWhitespace(drl2, model.getParserErrors().get(0).getOriginalDrl());
assertNotNull(model.getParserErrors().get(0).getMessages());
assertEquals(1, model.getParserErrors().get(0).getMessages().size());
assertTrue(model.getParserErrors().get(0).getMessages().get(0) instanceof AmbiguousRootParserMessage);
final String drl3 = GuidedDecisionTreeDRLPersistence.getInstance().marshal(model);
assertEqualsIgnoreWhitespace(drl1 + drl2, drl3);
}
use of org.drools.workbench.models.guided.dtree.shared.model.parser.messages.AmbiguousRootParserMessage in project drools-wb by kiegroup.
the class ParserMessagesPopup method getMessage.
private String getMessage(final ParserMessage msg) {
if (msg instanceof AmbiguousRootParserMessage) {
final TypeNode tn = model.getRoot();
final AmbiguousRootParserMessage m = (AmbiguousRootParserMessage) msg;
return GuidedDecisionTreeConstants.INSTANCE.parserMessageAmbiguousRootParserMessage(tn.getClassName(), m.getClassName());
} else if (msg instanceof BindingNotFoundParserMessage) {
final BindingNotFoundParserMessage m = (BindingNotFoundParserMessage) msg;
return GuidedDecisionTreeConstants.INSTANCE.parserMessageBindingNotFoundParserMessage(m.getBinding());
} else if (msg instanceof DataTypeConversionErrorParserMessage) {
final DataTypeConversionErrorParserMessage m = (DataTypeConversionErrorParserMessage) msg;
return GuidedDecisionTreeConstants.INSTANCE.parserMessageDataTypeConversionErrorParserMessage(m.getValue(), m.getDataTypeClassName());
} else if (msg instanceof DataTypeNotFoundParserMessage) {
final DataTypeNotFoundParserMessage m = (DataTypeNotFoundParserMessage) msg;
return GuidedDecisionTreeConstants.INSTANCE.parserMessageDataTypeNotFoundParserMessage(m.getClassName(), m.getFieldName());
} else if (msg instanceof DefaultParserMessage) {
final DefaultParserMessage m = (DefaultParserMessage) msg;
return GuidedDecisionTreeConstants.INSTANCE.parserMessageDefaultParserMessage(m.getMessage());
} else if (msg instanceof InvalidRootParserMessage) {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageInvalidRootParserMessage();
} else if (msg instanceof UnsupportedFieldConstraintParserMessage) {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageUnsupportedFieldConstraintParserMessage();
} else if (msg instanceof UnsupportedFieldConstraintTypeParserMessage) {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageUnsupportedFieldConstraintTypeParserMessage();
} else if (msg instanceof UnsupportedFieldNatureTypeParserMessage) {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageUnsupportedFieldNatureTypeParserMessage();
} else if (msg instanceof UnsupportedIActionParserMessage) {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageUnsupportedIActionParserMessage();
} else if (msg instanceof UnsupportedIPatternParserMessage) {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageUnsupportedIPatternParserMessage();
} else {
return GuidedDecisionTreeConstants.INSTANCE.parserMessageUnknownMessage();
}
}
use of org.drools.workbench.models.guided.dtree.shared.model.parser.messages.AmbiguousRootParserMessage 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;
}
Aggregations