@Override SingleFieldConstraint constructSingleFieldConstraint( ConditionCol52 c, List<DTCellValue52> columnData ) { SingleFieldConstraint sfc = new SingleFieldConstraint( c.getFactField() ); sfc.setConstraintValueType( c.getConstraintValueType() ); sfc.setFieldType( c.getFieldType() ); sfc.setOperator( "in" ); List<String> consumedValues = new ArrayList<String>(); StringBuilder value = new StringBuilder(); value.append( "( " ); for ( DTCellValue52 cv : columnData ) { //Skip the "otherwise" cell itself if ( cv.isOtherwise() ) { continue; } //Ensure cell values start and end with quotes String scv = GuidedDTDRLUtilities.convertDTCellValueToString( cv ); if ( scv != null ) { if ( !consumedValues.contains( scv ) ) { value.append( scv ).append( ", " ); } consumedValues.add( scv ); } } value.delete( value.lastIndexOf( "," ), value.length() - 1 ); value.append( ")" ); sfc.setValue( value.toString() ); return sfc; }
@Override FieldConstraint constructSingleFieldConstraint( ConditionCol52 c, List<DTCellValue52> columnData ) { SingleFieldConstraint sfc = new SingleFieldConstraint( c.getFactField() ); sfc.setConstraintValueType( c.getConstraintValueType() ); sfc.setFieldType( c.getFieldType() ); sfc.setOperator( "not in" ); List<String> consumedValues = new ArrayList<String>(); StringBuilder value = new StringBuilder(); value.append( "( " ); for ( DTCellValue52 cv : columnData ) { //Skip the "otherwise" cell itself if ( cv.isOtherwise() ) { continue; } //Ensure cell values start and end with quotes String scv = GuidedDTDRLUtilities.convertDTCellValueToString( cv ); if ( scv != null ) { if ( !consumedValues.contains( scv ) ) { value.append( scv ).append( ", " ); } consumedValues.add( scv ); } } value.delete( value.lastIndexOf( "," ), value.length() - 1 ); value.append( ")" ); sfc.setValue( value.toString() ); return sfc; }
public void onExpressionTypeChanged(ExpressionTypeChangeEvent event) { try { //Change "operator" drop-down as the content depends on data-type constraint.setFieldType(event.getNewType()); inner.setWidget(row, 1 + col, operatorDropDown(constraint, inner, row, 2 + col)); //Change "value" editor to the pen icon as the applicable Widget depends on data-type constraint.setConstraintValueType(SingleFieldConstraint.TYPE_UNDEFINED); constraint.setValue(""); inner.setWidget(row, 2 + col, createValueEditor(constraint)); } catch (Exception e) { e.printStackTrace(); } } });
public void onExpressionTypeChanged(ExpressionTypeChangeEvent event) { try { //Change "operator" drop-down as the content depends on data-type constraint.setFieldType(event.getNewType()); inner.setWidget(row, 1 + col, operatorDropDown(constraint, inner, row, 2 + col)); //Change "value" editor to the pen icon as the applicable Widget depends on data-type constraint.setConstraintValueType(SingleFieldConstraint.TYPE_UNDEFINED); constraint.setValue(""); inner.setWidget(row, 2 + col, createValueEditor(constraint)); } catch (Exception e) { e.printStackTrace(); } } });
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 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; }
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 testSimpleFromCollect() { TemplateModel m = new TemplateModel(); m.name = "r1"; FactPattern fp = new FactPattern("Person"); SingleFieldConstraint sfc = new SingleFieldConstraint("field1"); sfc.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE); sfc.setFieldType(DataType.TYPE_STRING); sfc.setOperator("=="); sfc.setValue("$f1"); fp.addConstraint(sfc); FromCollectCompositeFactPattern fac = new FromCollectCompositeFactPattern(); fac.setRightPattern(fp); fac.setFactPattern(new FactPattern("java.util.List")); m.addLhsItem(fac); String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "java.util.List( ) from collect ( Person( field1 == \"foo\" ) ) \n" + "then\n" + "end"; m.addRow(new String[]{"foo"}); 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); }