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; }
public FieldConstraint asFieldConstraint(final RuleModel m, final FactPattern factPattern) { SingleFieldConstraint con = new SingleFieldConstraint(); con.setConstraintValueType(SingleFieldConstraint.TYPE_PREDICATE); con.setValue(expr); return con; } }
private void assertConstraintValue(final SingleFieldConstraint sfc) { if (DataType.TYPE_STRING.equals(sfc.getFieldType())) { if (sfc.getValue() == null) { sfc.setValue(""); } } }
private boolean isParentFactPattern( final SingleFieldConstraint sfc ) { if ( sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_TEMPLATE ) { if ( sfc.getValue().equals( this.baseVariableName ) ) { return true; } } return false; }
@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 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); }
final SingleFieldConstraint isFinished = new SingleFieldConstraint(); isFinished.setFieldBinding("$finished"); isFinished.setFactType("House"); isFinished.setFieldName("finished"); isFinished.setFieldType("java.time.LocalDate"); isFinished.setOperator("!= null"); pattern.addConstraint(isFinished); model.addLhsItem(pattern); final SingleFieldConstraint isFinishedAfter = new SingleFieldConstraint(); isFinishedAfter.setFactType("House"); isFinishedAfter.setFieldName("cost"); isFinishedAfter.setFieldType("java.util.BigDecimal"); isFinishedAfter.setOperator("=="); pattern.addConstraint(isFinishedAfter); model.addLhsItem(pattern);
private void visitSingleFieldConstraint(SingleFieldConstraint sfc) { InterpolationVariable var = new InterpolationVariable(sfc.getValue(), sfc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), sfc.getFieldName(), sfc.getOperator()); if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfc.getConstraintValueType() && !vars.containsKey(var)) { vars.put(var, vars.size()); } //Visit Connection constraints if (sfc.getConnectives() != null) { for (int i = 0; i < sfc.getConnectives().length; i++) { final ConnectiveConstraint cc = sfc.getConnectives()[i]; InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), cc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), cc.getFieldName()); if (BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.containsKey(ccVar)) { vars.put(ccVar, vars.size()); } } } }
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; }
private boolean areOperatorAndValueInvalid(final SingleFieldConstraint singleFieldConstraint) { if (singleFieldConstraint.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_LITERAL) { if (DataType.TYPE_STRING.equals(singleFieldConstraint.getFieldType())) { return false; } } return singleFieldConstraint.getOperator() != null && !singleFieldConstraint.getOperator().equals("== null") && !singleFieldConstraint.getOperator().equals("!= null") && singleFieldConstraint.getValue() == null && singleFieldConstraint.getExpressionValue().isEmpty(); }
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())); } }
@Test public void testAbbreviatedCondition() throws Exception { final String resourcePath = RULES_ROOT + "matchPeopleAbbreviatedCondition.rdrl"; final GuidedEditorContent content = guidedRuleService.loadContent(getPath(resourcePath)); final RuleModel model = content.getModel(); Assertions.assertThat(model.lhs.length).isEqualTo(1); final FactPattern pattern = (FactPattern) model.lhs[0]; final SingleFieldConstraint fieldConstraintOne = (SingleFieldConstraint) pattern.getConstraint(0); Assertions.assertThat(fieldConstraintOne.getConnectives()).hasSize(2); final ConnectiveConstraint fieldConstraintTwo = fieldConstraintOne.getConnectives()[0]; final ConnectiveConstraint fieldConstraintThree = fieldConstraintOne.getConnectives()[1]; Assertions.assertThat(fieldConstraintOne.getFieldName()).isEqualTo("age"); Assertions.assertThat(fieldConstraintOne.getOperator()).isEqualTo(Operator.NOT_EQUAL.getOperatorString()); Assertions.assertThat(fieldConstraintOne.getValue()).isEqualTo("18"); Assertions.assertThat(fieldConstraintTwo.getFieldName()).isEqualTo("age"); Assertions.assertThat(fieldConstraintTwo.getOperator()).isEqualTo("&& " + Operator.LESS.getOperatorString()); Assertions.assertThat(fieldConstraintTwo.getValue()).isEqualTo("25"); Assertions.assertThat(fieldConstraintThree.getFieldName()).isEqualTo("age"); Assertions.assertThat(fieldConstraintThree.getOperator()).isEqualTo("&& " + Operator.GREATER.getOperatorString()); Assertions.assertThat(fieldConstraintThree.getValue()).isEqualTo("15"); final List<ValidationMessage> messages = validateResource(resourcePath); Assertions.assertThat(messages).isEmpty(); }
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; }
private boolean isConstraintComplete(final SingleFieldConstraint constr) { if (constr.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_EXPR_BUILDER_VALUE) { return true; } else if (constr instanceof SingleFieldConstraintEBLeftSide) { return true; } else if (constr.getFieldBinding() != null) { return true; } final String operator = constr.getOperator(); final String fieldType = constr.getFieldType(); final String fieldValue = constr.getValue(); if (operator == null) { return false; } if (operator.equals("== null") || operator.equals("!= null")) { return true; } if (DataType.TYPE_STRING.equals(fieldType)) { return true; } return !(fieldValue == null || fieldValue.isEmpty()); }
private void visitSingleFieldConstraint(final SingleFieldConstraint sfc) { ResourceReference resRef = addResourceReference(getFullyQualifiedClassName(sfc.getFactType()), ResourceType.JAVA); resRef.addPartReference(sfc.getFieldName(), PartType.FIELD); addResourceReference(getFullyQualifiedClassName(sfc.getFieldType()), ResourceType.JAVA); if (sfc.getConnectives() != null) { for (int i = 0; i < sfc.getConnectives().length; i++) { visit(sfc.getConnectives()[i]); } } }
@Test public void testMissingValueWhenOperatorExists() throws Exception { FactPattern pattern = new FactPattern("Person"); SingleFieldConstraint constraint = new SingleFieldConstraint("age"); constraint.setOperator("=="); pattern.addConstraint(constraint); model.lhs = new IPattern[]{pattern}; assertFalse(validator.isValid()); assertEquals(1, validator.getErrors().size()); assertEquals(MISSING_VALUE_WHEN_OPERATOR_IS_SET, validator.getErrors().get(0)); verify(constants).FactType0HasAField1ThatHasAnOperatorSetButNoValuePleaseAddAValueOrRemoveTheOperator("Person", "age"); }
private void visitSingleFieldConstraint( final SingleFieldConstraint sfc ) { if ( sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_TEMPLATE ) { this.peerVariables.add( new ValueHolder( sfc.getFieldName(), sfc.getValue(), ValueHolder.Type.TEMPLATE_KEY ) ); } else { this.peerVariables.add( new ValueHolder( sfc.getFieldName(), sfc.getValue(), ValueHolder.Type.VALUE ) ); } }
@Test public void testFromAccumulateCompositePattern() throws Exception { FactPattern pattern1 = new FactPattern("Person"); SingleFieldConstraint constraint1 = new SingleFieldConstraint("name"); constraint1.setOperator("=="); constraint1.setValue("Toni"); pattern1.addConstraint(constraint1); FactPattern pattern2 = new FactPattern("Address"); SingleFieldConstraint constraint2 = new SingleFieldConstraint("street"); constraint2.setOperator("!="); constraint2.setValue("some street"); pattern2.addConstraint(constraint2); FromAccumulateCompositeFactPattern fromAccumulateCompositeFactPattern = new FromAccumulateCompositeFactPattern(); fromAccumulateCompositeFactPattern.setSourcePattern(pattern1); fromAccumulateCompositeFactPattern.setFactPattern(pattern2); ExpressionFormLine expression = new ExpressionFormLine(); expression.setBinding("person.addresses"); fromAccumulateCompositeFactPattern.setExpression(expression); model.lhs = new IPattern[]{fromAccumulateCompositeFactPattern}; assertTrue(validator.isValid()); }
final String factType = "Person"; final String fieldName = "age"; when(constraint.getFactType()).thenReturn(factType); when(constraint.getFieldName()).thenReturn(fieldName); when(constraint.getOperator()).thenReturn(originalOperator); when(constraint.getValue()).thenReturn(constraintValue); when(testedSelector.getNewOperatorDropdown(OperatorsOracle.STANDARD_OPERATORS, constraint)) .thenReturn(operatorsDropdown);
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(); } } });