@Test public void testOnChangeCallbackRegisteredForConstraintValueEditor() throws Exception { final SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraint.setFieldName("a"); final ConstraintValueEditor editor = mock(ConstraintValueEditor.class); doReturn(editor).when(factPatternWidget).constraintValueEditor(constraint); final SingleFieldConstraint constraintTwo = new SingleFieldConstraint(); constraintTwo.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraintTwo.setFieldName("b"); final ConstraintValueEditor editorTwo = mock(ConstraintValueEditor.class); doReturn(editorTwo).when(factPatternWidget).constraintValueEditor(constraintTwo); factPatternWidget.createValueEditor(constraint); factPatternWidget.createValueEditor(constraintTwo); verify(editor).init(); verify(editorTwo).init(); verify(editor).setOnValueChangeCommand(commandCaptor.capture()); commandCaptor.getValue().execute(); verify(editorTwo).hideError(); verify(factPatternWidget).setModified(true); verify(editorTwo).refresh(); } }
@Test public void testOnChangeCallbackRegisteredForConstraintValueEditor() throws Exception { final SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraint.setFieldName("a"); final ConstraintValueEditor editor = mock(ConstraintValueEditor.class); doReturn(editor).when(factPatternWidget).constraintValueEditor(constraint); final SingleFieldConstraint constraintTwo = new SingleFieldConstraint(); constraintTwo.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraintTwo.setFieldName("b"); final ConstraintValueEditor editorTwo = mock(ConstraintValueEditor.class); doReturn(editorTwo).when(factPatternWidget).constraintValueEditor(constraintTwo); factPatternWidget.createValueEditor(constraint); factPatternWidget.createValueEditor(constraintTwo); verify(editor).init(); verify(editorTwo).init(); verify(editor).setOnValueChangeCommand(commandCaptor.capture()); commandCaptor.getValue().execute(); verify(editorTwo).hideError(); verify(factPatternWidget).setModified(true); verify(editorTwo).refresh(); } }
protected SingleFieldConstraint whenBRLFactPatternHasAField(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 FactPattern); final FactPattern fp = (FactPattern) brl.getDefinition().get(0); 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; }
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; }
private void fixConstraints(FactPattern fp, SingleFieldConstraint sfc) { final FieldConstraint parent = sfc.getParent(); if (parent == null) { sfc.setFactType(fp.getFactType()); } else if (parent instanceof SingleFieldConstraint) { sfc.setFactType(((SingleFieldConstraint) parent).getFieldType()); } sfc.setFieldName(fixFieldName(sfc.getFieldName())); if (sfc.getConnectives() == null) { return; } for (ConnectiveConstraint cc : sfc.getConnectives()) { cc.setFactType(fp.getFactType()); cc.setFieldName(fixFieldName(cc.getFieldName())); } }
private SingleFieldConstraint visitSingleFieldConstraint(SingleFieldConstraint sfc) { SingleFieldConstraint clone = new SingleFieldConstraint(); clone.setConstraintValueType(sfc.getConstraintValueType()); clone.setExpressionValue((ExpressionFormLine) visit(sfc.getExpressionValue())); clone.setFieldBinding(sfc.getFieldBinding()); clone.setFactType(sfc.getFactType()); clone.setFieldName(sfc.getFieldName()); clone.setFieldType(sfc.getFieldType()); clone.setId(sfc.getId()); clone.setOperator(sfc.getOperator()); clone.setParent((FieldConstraint) visit(sfc.getParent())); for (Map.Entry<String, String> entry : sfc.getParameters().entrySet()) { clone.setParameter(entry.getKey(), entry.getValue()); } clone.setValue(sfc.getValue()); if (sfc.getConnectives() != null) { clone.setConnectives(new ConnectiveConstraint[sfc.getConnectives().length]); for (int i = 0; i < sfc.getConnectives().length; i++) { clone.getConnectives()[i] = (ConnectiveConstraint) visit(sfc.getConnectives()[i]); } } return clone; }
private SingleFieldConstraint visitSingleFieldConstraint(SingleFieldConstraint sfc) { SingleFieldConstraint clone = new SingleFieldConstraint(); clone.setConstraintValueType(sfc.getConstraintValueType()); clone.setExpressionValue((ExpressionFormLine) visit(sfc.getExpressionValue())); clone.setFieldBinding(sfc.getFieldBinding()); clone.setFactType(sfc.getFactType()); clone.setFieldName(sfc.getFieldName()); clone.setFieldType(sfc.getFieldType()); clone.setId(sfc.getId()); clone.setOperator(sfc.getOperator()); clone.setParent((FieldConstraint) visit(sfc.getParent())); for (Map.Entry<String, String> entry : sfc.getParameters().entrySet()) { clone.setParameter(entry.getKey(), entry.getValue()); } clone.setValue(sfc.getValue()); if (sfc.getConnectives() != null) { clone.setConnectives(new ConnectiveConstraint[sfc.getConnectives().length]); for (int i = 0; i < sfc.getConnectives().length; i++) { clone.getConnectives()[i] = (ConnectiveConstraint) visit(sfc.getConnectives()[i]); } } return clone; }
@Override public RuleModel getRuleModel() { final RuleModel ruleModel = new RuleModel(); ruleModel.lhs = new IPattern[1]; final FactPattern factPattern = new FactPattern(); final SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setValue("value"); constraint.setFieldType("fieldType"); constraint.setFactType("factType"); constraint.setFieldName("fieldName"); constraint.setOperator("in"); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE); factPattern.addConstraint(constraint); ruleModel.lhs[0] = factPattern; return ruleModel; } });
@Test public void testEmptyLiteralNonStringFieldConstraints() throws Exception { FactPattern f1 = new FactPattern("Applicant"); f1.setBoundName("$a"); SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraint.setFieldType(DataType.TYPE_NUMERIC_INTEGER); constraint.setFactType("Applicant"); constraint.setOperator("=="); constraint.setFieldName("age"); f1.addConstraint(constraint); model.lhs = new IPattern[]{f1}; assertFalse(validator.isValid()); }
@Test public void testEmptyLiteralNonStringFieldConstraints() throws Exception { FactPattern f1 = new FactPattern("Applicant"); f1.setBoundName("$a"); SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraint.setFieldType(DataType.TYPE_NUMERIC_INTEGER); constraint.setFactType("Applicant"); constraint.setOperator("=="); constraint.setFieldName("age"); f1.addConstraint(constraint); model.lhs = new IPattern[]{f1}; assertFalse(validator.isValid()); }
@Test public void testEmptyLiteralStringFieldConstraints() throws Exception { FactPattern f1 = new FactPattern("Applicant"); f1.setBoundName("$a"); SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraint.setFieldType(DataType.TYPE_STRING); constraint.setFactType("Applicant"); constraint.setOperator("=="); constraint.setFieldName("name"); f1.addConstraint(constraint); model.lhs = new IPattern[]{f1}; assertTrue(validator.isValid()); }
@Test public void testAddRowInvalidColumnCount() throws Exception { TemplateModel m = new TemplateModel(); m.name = "t1"; FactPattern p = new FactPattern("Person"); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType(DataType.TYPE_STRING); con.setFieldName("field1"); con.setOperator("=="); con.setValue("$f1"); con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); p.addConstraint(con); m.addLhsItem(p); try { m.addRow(new String[]{}); Assert.fail("Expected IlegalArgumentException"); } catch (IllegalArgumentException iae) { assertEquals("Invalid numbers of columns: 0 expected: 1", iae.getMessage()); } } }
@Test public void testEmptyLiteralStringFieldConstraints() throws Exception { FactPattern f1 = new FactPattern("Applicant"); f1.setBoundName("$a"); SingleFieldConstraint constraint = new SingleFieldConstraint(); constraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL); constraint.setFieldType(DataType.TYPE_STRING); constraint.setFactType("Applicant"); constraint.setOperator("=="); constraint.setFieldName("name"); f1.addConstraint(constraint); model.lhs = new IPattern[]{f1}; assertTrue(validator.isValid()); }
private static SingleFieldConstraint buildSingleFieldConstraint() { SingleFieldConstraint sfc = new SingleFieldConstraint(); sfc.setExpressionValue( buildExpressionFormLine() ); sfc.setFactType( "FactType" ); sfc.setFieldBinding( "fieldBinding" ); sfc.setFieldName( "fieldName" ); sfc.setFieldType( "FieldType" ); sfc.setId( "id" ); sfc.setOperator( "operator" ); sfc.setParameter( "key", "parameter" ); sfc.setParent( new SingleFieldConstraint( "parentFieldName" ) ); sfc.addNewConnective(); return sfc; }
@Test public void testSimpleBothValues() { TemplateModel m = new TemplateModel(); m.name = "t1"; FactPattern p = new FactPattern("Person"); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType(DataType.TYPE_STRING); con.setFieldName("field1"); con.setOperator("=="); con.setValue("$f1"); con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); p.addConstraint(con); SingleFieldConstraint con2 = new SingleFieldConstraint(); con.setFieldType(DataType.TYPE_STRING); con2.setFieldName("field2"); con2.setOperator("=="); con2.setValue("$f2"); con2.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); p.addConstraint(con2); m.addLhsItem(p); m.addRow(new String[]{"foo", "bar"}); String expected = "rule \"t1_0\"" + "dialect \"mvel\"\n" + "when \n" + "Person( field1 == \"foo\", field2 == \"bar\" )" + "then \n" + "end"; checkMarshall(expected, m); }
public static TemplateModel makeModelWithConditions(final String packageName, final Collection<Import> imports, final String name) { final TemplateModel model = new TemplateModel(); model.getImports().getImports().addAll(imports); model.setPackageName(packageName); model.name = name; final FactPattern p1 = new FactPattern("Applicant"); final SingleFieldConstraint con1 = new SingleFieldConstraint(); con1.setFieldType(DataType.TYPE_NUMERIC_INTEGER); con1.setFactType("Applicant"); con1.setFieldName("age"); con1.setOperator("=="); con1.setValue("f1"); con1.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); p1.addConstraint(con1); model.addLhsItem(p1); final FactPattern p2 = new FactPattern("Mortgage"); final SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setFieldType(DataType.TYPE_NUMERIC_INTEGER); con1.setFactType("Mortgage"); con2.setFieldName("amount"); con2.setOperator("=="); con2.setValue("f2"); con2.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); p2.addConstraint(con2); model.addLhsItem(p2); model.addRow(new String[]{"33", null}); return model; }
@Test public void testSimpleSingleValue() { TemplateModel m = new TemplateModel(); m.name = "t1"; FactPattern p = new FactPattern("Person"); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType(DataType.TYPE_STRING); con.setFieldName("field1"); con.setOperator("=="); con.setValue("$f1"); con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); p.addConstraint(con); m.addLhsItem(p); m.addRow(new String[]{"foo"}); String expected = "rule \"t1_0\"" + "dialect \"mvel\"\n" + "when \n" + "Person( field1 == \"foo\" )" + "then \n" + "end"; checkMarshall(expected, m); }
@Test public void testConnectiveConstraintSecondValue() { TemplateModel m = new TemplateModel(); m.name = "t1"; FactPattern p1 = new FactPattern("Person"); m.addLhsItem(p1); final SingleFieldConstraint X = new SingleFieldConstraint(); X.setFieldName("field1"); X.setFieldType(DataType.TYPE_STRING); X.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); X.setValue("$f1"); X.setOperator("=="); p1.addConstraint(X); ConnectiveConstraint connective = new ConnectiveConstraint(); connective.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE); connective.setFieldType(DataType.TYPE_STRING); connective.setOperator("|| =="); connective.setValue("$f2"); X.setConnectives(new ConnectiveConstraint[1]); X.getConnectives()[0] = connective; String expected = "rule \"t1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == \"bar\" )\n" + "then\n" + "end\n"; m.addRow(new String[]{null, "bar"}); checkMarshall(expected, m); }
@Test public void testConnectiveConstraintFirstValue() { TemplateModel m = new TemplateModel(); m.name = "t1"; FactPattern p1 = new FactPattern("Person"); m.addLhsItem(p1); final SingleFieldConstraint X = new SingleFieldConstraint(); X.setFieldName("field1"); X.setFieldType(DataType.TYPE_STRING); X.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); X.setValue("$f1"); X.setOperator("=="); p1.addConstraint(X); ConnectiveConstraint connective = new ConnectiveConstraint(); connective.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE); connective.setFieldType(DataType.TYPE_STRING); connective.setOperator("|| =="); connective.setValue("$f2"); X.setConnectives(new ConnectiveConstraint[1]); X.getConnectives()[0] = connective; String expected = "rule \"t1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == \"foo\" )\n" + "then\n" + "end\n"; m.addRow(new String[]{"foo", null}); checkMarshall(expected, m); }
private TemplateModel getComplexModel() { final TemplateModel m = new TemplateModel(); m.addAttribute(new RuleAttribute("no-loop", "true")); final FactPattern pat = new FactPattern("Person"); pat.setBoundName("p1"); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFactType("Person"); con.setFieldBinding("f1"); con.setFieldName("age"); con.setOperator("<"); con.setValue("42"); pat.addConstraint(con); m.addLhsItem(pat); final CompositeFactPattern comp = new CompositeFactPattern("not"); comp.addFactPattern(new FactPattern("Cancel")); m.addLhsItem(comp); final ActionUpdateField set = new ActionUpdateField(); set.setVariable("p1"); set.addFieldValue(new ActionFieldValue("status", "rejected", DataType.TYPE_STRING)); m.addRhsItem(set); final ActionRetractFact ret = new ActionRetractFact("p1"); m.addRhsItem(ret); final DSLSentence sen = new DSLSentence(); sen.setDefinition("Send an email to {administrator}"); m.addRhsItem(sen); return m; }