use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools-wb by kiegroup.
the class GuidedRuleEditorValidatorTest method testEmptyFrom.
@Test
public void testEmptyFrom() throws Exception {
model.lhs = new IPattern[] { new FromCompositeFactPattern() };
assertFalse(validator.isValid());
assertEquals(2, validator.getErrors().size());
assertEquals(MISSING_FACT_PATTERN, validator.getErrors().get(0));
assertEquals(MISSING_RHS_FROM, validator.getErrors().get(1));
verify(constants).AreasMarkedWithRedAreMandatoryPleaseSetAValueBeforeSaving();
verify(constants).WhenUsingFromTheSourceNeedsToBeSet();
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class BaseBRLTest method whenBRLFromCompositeFactPatternHasAField.
protected SingleFieldConstraint whenBRLFromCompositeFactPatternHasAField(final BRLConditionColumn brl, final String fieldName, final String fieldType, final String fieldBinding) {
assertFalse("BRLConditionColumn has not been initialised. Was 'whenThereIsABRLFactPattern' called?", brl.getDefinition().isEmpty());
assertEquals("BRLConditionColumn has not been initialised correctly. Was 'whenThereIsABRLFactPattern' called?", 1, brl.getDefinition().size());
assertTrue("BRLConditionColumn has not been initialised correctly. Was 'whenThereIsABRLFactPattern' called?", brl.getDefinition().get(0) instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fcfp = (FromCompositeFactPattern) brl.getDefinition().get(0);
final FactPattern fp = fcfp.getFactPattern();
final SingleFieldConstraint sfc = new SingleFieldConstraint();
sfc.setFactType(fp.getFactType());
sfc.setFieldName(fieldName);
sfc.setFieldType(fieldType);
sfc.setFieldBinding(fieldBinding);
fp.addConstraint(sfc);
brl.getDefinition().add(fp);
return sfc;
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testDSLWithFrom.
/*
* https://issues.jboss.org/browse/DROOLS-1903
*/
@Test
public void testDSLWithFrom() {
final RuleModel m = new RuleModel();
m.name = "testDSLWithFrom";
m.lhs = new IPattern[2];
m.rhs = new IAction[0];
final FromCollectCompositeFactPattern from = new FromCollectCompositeFactPattern();
final FromCompositeFactPattern rightPattern = new FromCompositeFactPattern();
final FactPattern person = new FactPattern("Person");
person.setBoundName("$person");
m.lhs[0] = person;
rightPattern.setFactPattern(new FactPattern("Person"));
rightPattern.setExpression(new ExpressionFormLine(new ExpressionVariable(person.getBoundName(), person.getFactType())));
from.setRightPattern(rightPattern);
from.setFactPattern(new FactPattern("java.util.List"));
m.lhs[1] = from;
final RuleModel modelSpy = spy(m);
doReturn(true).when(modelSpy).hasDSLSentences();
final String drl = ruleModelPersistence.marshal(modelSpy);
final RuleModel tempModel = spy(ruleModelPersistence.unmarshalUsingDSL(drl, null, dmo, ""));
doReturn(true).when(tempModel).hasDSLSentences();
final String newDrl = ruleModelPersistence.marshal(tempModel);
final RuleModel newModel = ruleModelPersistence.unmarshalUsingDSL(newDrl, null, dmo, "");
assertTrue(newModel.lhs[0] instanceof FactPattern);
assertTrue(newModel.lhs[1] instanceof FromCollectCompositeFactPattern);
assertDSLEscaping(newDrl);
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class RuleModelTest method testBoundFromCompositeFactFinder.
@Test
public void testBoundFromCompositeFactFinder() {
final RuleModel model = new RuleModel();
model.lhs = new IPattern[1];
final FromCompositeFactPattern fcfp = new FromCompositeFactPattern();
final FactPattern x = new FactPattern("Car");
x.setBoundName("x");
final SingleFieldConstraint a = new SingleFieldConstraint("name");
a.setFieldBinding("a");
a.setFieldType("String");
x.addConstraint(a);
fcfp.setFactPattern(x);
model.lhs[0] = fcfp;
assertEquals(x, model.getLHSBoundFact("x"));
assertEquals(1, model.getLHSBoundFacts().size());
assertEquals("x", model.getLHSBoundFacts().get(0));
assertEquals(a, model.getLHSBoundField("a"));
assertEquals("Car", model.getLHSBindingType("x"));
assertEquals("String", model.getLHSBindingType("a"));
assertEquals(x, model.getLHSParentFactPatternForBinding("a"));
assertEquals(2, model.getAllLHSVariables().size());
assertTrue(model.getAllLHSVariables().contains("x"));
assertTrue(model.getAllLHSVariables().contains("a"));
model.rhs = new IAction[1];
final ActionSetField set = new ActionSetField();
set.setVariable("x");
model.rhs[0] = set;
assertTrue(model.isBoundFactUsed("x"));
assertEquals(1, model.lhs.length);
assertFalse(model.removeLhsItem(0));
assertEquals(1, model.lhs.length);
}
use of org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMultipleFromKeywords.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1191737
void testMultipleFromKeywords() throws Exception {
String drl = "package org.test;\n" + "rule \"ToyWithoutName \"\n" + "dialect \"java\"\n" + "when\n" + " $father: Father()\n" + " ($kid: Kid() from $father.kids)\n" + " ($toy: Toy(name == null) from $kid.toys)\n" + "then\n" + " System.out.println(\"blabla\");\n" + "end";
addModelField("org.test.Father", "this", "org.test.Father", DataType.TYPE_THIS);
addModelField("org.test.Father", "kids", "org.test.Kid", DataType.TYPE_COLLECTION);
addModelField("org.test.Kid", "this", "org.test.Kid", DataType.TYPE_THIS);
addModelField("org.test.Kid", "toys", "org.test.Toy", DataType.TYPE_COLLECTION);
addModelField("org.test.Toy", "this", "org.test.Toy", DataType.TYPE_THIS);
addModelField("org.test.Toy", "name", "java.lang.String", DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(3, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("$father", fp0.getBoundName());
assertEquals("Father", fp0.getFactType());
final IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp1 = (FromCompositeFactPattern) p1;
assertEquals("$kid", fp1.getFactPattern().getBoundName());
assertEquals("Kid", fp1.getFactType());
final IPattern p2 = m.lhs[2];
assertTrue(p2 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp2 = (FromCompositeFactPattern) p2;
assertEquals("$toy", fp2.getFactPattern().getBoundName());
assertEquals("Toy", fp2.getFactType());
assertEquals(1, m.rhs.length);
final IAction a = m.rhs[0];
assertTrue(a instanceof FreeFormLine);
final FreeFormLine affl = (FreeFormLine) a;
assertEquals("System.out.println(\"blabla\");", affl.getText());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Aggregations