private Constraint createAttributeConstraint(AttributeConstraint attributeConstraint) { Constraint result = null; Constraint constraint = null; AttributeOperand leftOperand = attributeConstraint.getLeftOperand(); IntegerVariable leftOperandVariable = getVariableForAttributeOperand(leftOperand); AttributeOperand rightOperand = attributeConstraint.getRightOperand(); IntegerVariable rightOperandVariable = getVariableForAttributeOperand(rightOperand); Relop operator = attributeConstraint.getOperator(); switch (operator) { case EQUAL: constraint = Choco.eq(leftOperandVariable, rightOperandVariable); break; case GREATER_THAN: constraint = Choco.gt(leftOperandVariable, rightOperandVariable); break; case GREATER_THAN_OR_EQUAL: constraint = Choco.geq(leftOperandVariable, rightOperandVariable); break; case LESS_THAN: constraint = Choco.lt(leftOperandVariable, rightOperandVariable); break; case LESS_THAN_OR_EQUAL: constraint = Choco.leq(leftOperandVariable, rightOperandVariable); break; case UNEQUAL: constraint = Choco.neq(leftOperandVariable, rightOperandVariable); break; default: break; } IntegerVariable leftFeatureVariable = getFeatureVariableForOperand(leftOperand); IntegerVariable rightFeatureVariable = getFeatureVariableForOperand(rightOperand); // if both are features and both are selected, then the attribute constraint must be evaluated if (leftFeatureVariable != null && rightFeatureVariable != null) { Constraint leftFeatureSelected = Choco.eq(leftFeatureVariable, 1); Constraint rightFeatureSelected = Choco.eq(rightFeatureVariable, 1); Constraint and = Choco.and(leftFeatureSelected, rightFeatureSelected); result = Choco.implies(and, constraint); } else { // if one of the two operand is a constant,the constraint will be evaluated if the host // feature is selected if (leftFeatureVariable != null) { result = getFeatureAttributeValueConstraint(leftFeatureVariable, constraint); } else if (rightFeatureVariable != null) { result = getFeatureAttributeValueConstraint(rightFeatureVariable, constraint); } } return result; }
private void createGroupConstraint(Group group) { // group with cardinality {n,m} represented as // ifThen(ParentFeature>0;sum(ChildFeature A, ChildFeature // B) in {n,m};) // if group cardinality is n=m, then // ifThen(ParentFeature>0;sum(ChildFeature A, ChildFeature B) = n) log.debug("Create constraint for group " + group); IntegerVariable parentFeatureVariable = getOrCreateVariable((Feature) group.eContainer()); IntegerExpressionVariable childFeatureSum = createChildFeatureVariable(group); int minCardinality = getMinChocoCardinality(group); int maxCardinality = getMaxChocoCardinality(group); Constraint ifConstraint = Choco.gt(parentFeatureVariable, 0); Constraint greaterThan = Choco.geq(childFeatureSum, minCardinality); Constraint smallerThan = Choco.leq(childFeatureSum, maxCardinality); Constraint thenConstraint = Choco.and(greaterThan, smallerThan); Constraint groupCardinalityConstraint = Choco.implies(ifConstraint, thenConstraint); getModel().addConstraint(groupCardinalityConstraint); }
private void createFeatureConstraint(Feature feature) { IntegerVariable childVariable = getOrCreateVariable(feature); int minCardinality = 0; int maxCardinality = 1; Constraint greaterThan = Choco.geq(childVariable, minCardinality); Constraint smallerThan = Choco.leq(childVariable, maxCardinality); Constraint thenConstraint = Choco.and(greaterThan, smallerThan); EObject featureContainer = feature.eContainer(); if (featureContainer instanceof Group) { Group parentGroup = (Group) featureContainer; EObject groupContainer = parentGroup.eContainer(); if (groupContainer instanceof Feature) { Feature parentFeature = (Feature) groupContainer; IntegerVariable parentVariable = getOrCreateVariable(parentFeature); // feature value must be in feature cardinality boundaries Constraint parentSelected = Choco.gt(parentVariable, 0); Constraint parentSelectedAndChildCardinality = Choco.implies(parentSelected, thenConstraint); getModel().addConstraint(parentSelectedAndChildCardinality); Constraint childSelected = Choco.gt(childVariable, 0); Constraint impliesConstraint = Choco.implies(childSelected, parentSelected); getModel().addConstraint(impliesConstraint); } } else { // handle rootgroup Constraint greater = Choco.gt(childVariable, minCardinality); getModel().addConstraint(greater); } }
public Constraint generateConstraint( ExpressionNode leftNode, ExpressionNode rightNode, InfixExpression.Operator operator) { IntegerExpressionVariable leftVariable = null, rightVariable = null; if (leftNode.getType() != ExpressionType.single_int) { leftVariable = constraintVariableModeling(leftNode); } if (rightNode.getType() != ExpressionType.single_int) { rightVariable = constraintVariableModeling(rightNode); } Constraint constraint = null; if (leftVariable != null && rightVariable != null) { if (operator.equals(InfixExpression.Operator.LESS)) { constraint = Choco.lt(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { constraint = Choco.leq(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER)) { constraint = Choco.gt(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { constraint = Choco.geq(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.EQUALS)) { constraint = Choco.eq(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { constraint = Choco.neq(leftVariable, rightVariable); } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } else if (leftVariable != null && rightVariable == null) { int rightValue = Integer.parseInt(rightNode.getValue()); if (operator.equals(InfixExpression.Operator.LESS)) { constraint = Choco.lt(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { constraint = Choco.leq(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.GREATER)) { constraint = Choco.gt(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { constraint = Choco.geq(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.EQUALS)) { constraint = Choco.eq(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { constraint = Choco.neq(leftVariable, rightValue); } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } else if (leftVariable == null && rightVariable != null) { int leftValue = Integer.parseInt(leftNode.getValue()); if (operator.equals(InfixExpression.Operator.LESS)) { constraint = Choco.lt(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { constraint = Choco.leq(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER)) { constraint = Choco.gt(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { constraint = Choco.geq(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.EQUALS)) { constraint = Choco.eq(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { constraint = Choco.neq(leftValue, rightVariable); } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } else if (leftVariable == null && rightVariable == null) { int leftValue = Integer.parseInt(leftNode.getValue()), rightValue = Integer.parseInt(rightNode.getValue()); boolean logicResult; Constraint falseConstraint = Choco.lt(Choco.makeIntVar("bigger", 5, 6), Choco.makeIntVar("smaller", 1, 2)), trueConstraint = Choco.gt(Choco.makeIntVar("bigger", 5, 6), Choco.makeIntVar("smaller", 1, 2)); if (operator.equals(InfixExpression.Operator.LESS)) { if (leftValue < rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { if (leftValue <= rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.GREATER)) { if (leftValue > rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { if (leftValue >= rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.EQUALS)) { if (leftValue == rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { if (leftValue != rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } if (constraint == null) { System.out.println("can't solve this kind of constraint"); } return constraint; }
public Object leq(Object exp1, Object exp2) { return Choco.leq((IntegerExpressionVariable) exp1, (IntegerExpressionVariable) exp2); }
public Object leq(Object exp, int value) { return Choco.leq((IntegerExpressionVariable) exp, value); }
public Object leq(int value, Object exp) { return Choco.leq(value, (IntegerExpressionVariable) exp); }