@Override protected void generateConstraint(final FieldConstraint constr, final LHSGeneratorContext gctx) { boolean generateTemplateCheck = isTemplateKey(constr); if (generateTemplateCheck) { if (constr instanceof SingleFieldConstraint && ((SingleFieldConstraint) constr).getConnectives() != null) { // if there are connectives, and the first is a template key, then all templates keys must be checked up front // individual connectives, that have template keys, will still need to be checked too. SingleFieldConstraint sconstr = (SingleFieldConstraint) constr; buf.append("@if{" + ((SingleFieldConstraint) constr).getValue() + " != empty"); for (int j = 0; j < sconstr.getConnectives().length; j++) { final ConnectiveConstraint conn = sconstr.getConnectives()[j]; if (conn.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_TEMPLATE) { buf.append(" || " + conn.getValue() + " != empty"); } } buf.append("}"); } else { buf.append("@if{" + ((SingleFieldConstraint) constr).getValue() + " != empty}"); } } buf.append("@code{hasLHSOutput" + gctx.getDepth() + "_" + gctx.getOffset() + " = true}"); super.generateConstraint(constr, gctx); if (generateTemplateCheck) { buf.append("@end{}"); } }
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 void visitSingleFieldConstraint(SingleFieldConstraintEBLeftSide sfexp) { String genericType = sfexp.getExpressionLeftSide().getGenericType(); String factType = sfexp.getExpressionLeftSide().getPreviousClassType(); if (factType == null) { factType = sfexp.getExpressionLeftSide().getClassType(); } InterpolationVariable var = new InterpolationVariable(sfexp.getValue(), genericType, factType, sfexp.getFieldName()); if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfexp.getConstraintValueType() && !vars.containsKey(var)) { vars.put(var, vars.size()); } //Visit Connection constraints if (sfexp.getConnectives() != null) { for (int i = 0; i < sfexp.getConnectives().length; i++) { final ConnectiveConstraint cc = sfexp.getConnectives()[i]; InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), genericType, factType, cc.getFieldName()); if (BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.containsKey(ccVar)) { vars.put(ccVar, vars.size()); } } } }
@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 ConnectiveConstraint visitConnectiveConstraint(ConnectiveConstraint cc) { ConnectiveConstraint clone = new ConnectiveConstraint(); clone.setConstraintValueType(cc.getConstraintValueType()); clone.setExpressionValue((ExpressionFormLine) visit(cc.getExpressionValue())); clone.setFactType(cc.getFactType()); clone.setFieldName(cc.getFieldName()); clone.setFieldType(cc.getFieldType()); clone.setOperator(cc.getOperator()); for (Map.Entry<String, String> entry : cc.getParameters().entrySet()) { clone.setParameter(entry.getKey(), entry.getValue()); } clone.setValue(cc.getValue()); return clone; }
private void visitSingleFieldConstraint(final SingleFieldConstraint sfc) { final InterpolationVariable var = new InterpolationVariable(sfc.getValue(), sfc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), sfc.getFieldName()); if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfc.getConstraintValueType() && !vars.contains(var)) { vars.add(var); } else { hasNonTemplateOutput = true; } //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.contains(ccVar)) { vars.add(ccVar); } else { hasNonTemplateOutput = true; } } } }
@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 ConnectiveConstraint visitConnectiveConstraint(ConnectiveConstraint cc) { ConnectiveConstraint clone = new ConnectiveConstraint(); clone.setConstraintValueType(cc.getConstraintValueType()); clone.setExpressionValue((ExpressionFormLine) visit(cc.getExpressionValue())); clone.setFactType(cc.getFactType()); clone.setFieldName(cc.getFieldName()); clone.setFieldType(cc.getFieldType()); clone.setOperator(cc.getOperator()); for (Map.Entry<String, String> entry : cc.getParameters().entrySet()) { clone.setParameter(entry.getKey(), entry.getValue()); } clone.setValue(cc.getValue()); return clone; }
for (int i = 0; i < sfexp.getConnectives().length; i++) { final ConnectiveConstraint cc = sfexp.getConnectives()[i]; InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), genericType, factType,
conn.getOperator(), connectiveParameters, conn.getValue(), conn.getExpressionValue(), cctx,