use of org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd in project drools by kiegroup.
the class RuleTemplateModelXMLPersistenceTest method testBasics.
@Test
public void testBasics() {
final RuleTemplateModelPersistence p = RuleTemplateModelXMLPersistenceImpl.getInstance();
final TemplateModel m = new TemplateModel();
m.addLhsItem(new FactPattern("Person"));
m.addLhsItem(new FactPattern("Accident"));
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addRhsItem(new ActionInsertFact("Report"));
ActionGlobalCollectionAdd ag = new ActionGlobalCollectionAdd();
ag.setFactName("x");
ag.setGlobalName("g");
m.addRhsItem(ag);
m.name = "my rule";
final String xml = p.marshal(m);
System.out.println(xml);
assertTrue(xml.indexOf("Person") > -1);
assertTrue(xml.indexOf("Accident") > -1);
assertTrue(xml.indexOf("no-loop") > -1);
assertTrue(xml.indexOf("org.kie") == -1);
assertTrue(xml.indexOf("addToGlobal") > -1);
RuleModel rm_ = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
assertEquals(2, rm_.rhs.length);
}
use of org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testAddToGlobalCollection.
@Test
public void testAddToGlobalCollection() {
String global = "global java.util.ArrayList list";
String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "list.add( $a );\n" + "end\n";
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Arrays.asList(global), mock(PackageDataModelOracle.class));
assertNotNull(m);
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
// LHS
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern p = (FactPattern) m.lhs[0];
assertEquals("$a", p.getBoundName());
assertEquals("Applicant", p.getFactType());
// RHS
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionGlobalCollectionAdd);
final ActionGlobalCollectionAdd a = (ActionGlobalCollectionAdd) m.rhs[0];
assertEquals("list", a.getGlobalName());
assertEquals("$a", a.getFactName());
}
use of org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testAddGlobal.
@Test
public void testAddGlobal() {
String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" + "\t\tAccident( )\n\tthen\n\t\tinsert( new Report() );\n\t\tresults.add(f);\nend\n";
final RuleModel m = new RuleModel();
m.addLhsItem(new FactPattern("Person"));
m.addLhsItem(new FactPattern("Accident"));
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addRhsItem(new ActionInsertFact("Report"));
ActionGlobalCollectionAdd add = new ActionGlobalCollectionAdd();
add.setGlobalName("results");
add.setFactName("f");
m.addRhsItem(add);
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd in project drools-wb by kiegroup.
the class RuleModellerWidgetFactory method getWidget.
public RuleModellerWidget getWidget(RuleModeller ruleModeller, EventBus eventBus, IAction action, Boolean readOnly) {
if (action instanceof ActionCallMethod) {
return new ActionCallMethodWidget(ruleModeller, eventBus, (ActionCallMethod) action, readOnly);
}
if (action instanceof ActionSetField) {
return new ActionSetFieldWidget(ruleModeller, eventBus, (ActionSetField) action, readOnly);
}
if (action instanceof ActionInsertFact) {
return new ActionInsertFactWidget(ruleModeller, eventBus, (ActionInsertFact) action, readOnly);
}
if (action instanceof ActionRetractFact) {
return new ActionRetractFactWidget(ruleModeller, eventBus, (ActionRetractFact) action, readOnly);
}
if (action instanceof DSLSentence) {
RuleModellerWidget w = new DSLSentenceWidget(ruleModeller, eventBus, (DSLSentence) action, readOnly);
return w;
}
if (action instanceof FreeFormLine) {
return new FreeFormLineWidget(ruleModeller, eventBus, (FreeFormLine) action, readOnly);
}
if (action instanceof ActionGlobalCollectionAdd) {
return new GlobalCollectionAddWidget(ruleModeller, eventBus, (ActionGlobalCollectionAdd) action, readOnly);
}
// All hardcoded action widgets have been checked, perform a plugin lookup
List<RuleModellerActionPlugin> matchingActionPlugins = actionPlugins.stream().filter(p -> p.accept(action)).collect(Collectors.toList());
if (matchingActionPlugins.size() > 1) {
throw new IllegalStateException("Ambigious " + RuleModellerActionPlugin.class.getName() + " implementations for action " + action);
}
if (matchingActionPlugins.size() == 1) {
RuleModellerActionPlugin actionPlugin = matchingActionPlugins.get(0);
RuleModellerWidget ruleModellerWidget = actionPlugin.createWidget(ruleModeller, eventBus, action, readOnly);
return ruleModellerWidget;
}
// NON-NLS
throw new RuntimeException("I don't know what type of action is: " + action);
}
use of org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd in project drools-wb by kiegroup.
the class RuleModellerActionSelectorPopup method addGlobalCollections.
// Add global collections
void addGlobalCollections() {
List<String> vars = model.getLHSBoundFacts();
if (vars.size() == 0) {
return;
}
if (oracle.getGlobalCollections().length == 0) {
return;
}
choices.addItem(SECTION_SEPARATOR);
for (String bf : vars) {
for (int i = 0; i < oracle.getGlobalCollections().length; i++) {
final String glob = oracle.getGlobalCollections()[i];
final String var = bf;
choices.addItem(GuidedRuleEditorResources.CONSTANTS.Append0ToList1(var, glob), "GLOBCOL" + glob + var);
cmds.put("GLOBCOL" + glob + var, new Command() {
public void execute() {
ActionGlobalCollectionAdd gca = new ActionGlobalCollectionAdd();
gca.setGlobalName(glob);
gca.setFactName(var);
model.addRhsItem(gca, Integer.parseInt(positionCbo.getValue(positionCbo.getSelectedIndex())));
hide();
}
});
}
}
}
Aggregations