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 Constraint createExcludesConstraint(Feature lf, Feature rf) {
    IntegerVariable leftFeature = getOrCreateVariable(lf);
    IntegerVariable rightFeature = getOrCreateVariable(rf);

    Constraint leftSelected = Choco.gt(leftFeature, 0);
    Constraint rightNotSelected = Choco.eq(rightFeature, 0);
    Constraint leftRight = Choco.implies(leftSelected, rightNotSelected);

    Constraint rightSelected = Choco.gt(rightFeature, 0);
    Constraint leftNotSelected = Choco.eq(leftFeature, 0);
    Constraint rightLeft = Choco.implies(rightSelected, leftNotSelected);

    Constraint excludeConstraint = Choco.or(leftRight, rightLeft);
    return excludeConstraint;
  }
  private Constraint getMemberConstraint(Attribute attribute) {
    Constraint memberConstraint = null;

    IntegerVariable attributeValueVariable = getOrCreateVariable(attribute);

    // if attribute value is set, then the according attribute value must be
    // set
    if (FeatureModelHelper.isAttributeValueSet(attribute)) {
      int value = FeatureModelHelper.getAttributeValue(attribute);
      memberConstraint = Choco.eq(value, attributeValueVariable);
    } else {
      // check domain values
      Domain domain = attribute.getDomain();

      if (domain instanceof NumericalDomain) {
        NumericalDomain numericalDomain = (NumericalDomain) domain;
        // value of attribute must be in one of the intervals.
        int[] values = getNumericalValues(numericalDomain, attribute);
        memberConstraint = Choco.member(attributeValueVariable, values);

      } else if (domain instanceof DiscreteDomain) {
        DiscreteDomain discreteDomain = (DiscreteDomain) domain;
        int[] domainValues = discreteDomainValues.get(discreteDomain.getId());
        memberConstraint = Choco.member(attributeValueVariable, domainValues);
      }
    }
    return memberConstraint;
  }
  private Constraint getFeatureAttributeValueConstraint(
      IntegerVariable featureVariable, Constraint valueConstraint) {
    Constraint result = null;
    Constraint featureSelected = Choco.eq(featureVariable, 1);
    Constraint implies = Choco.implies(featureSelected, valueConstraint);
    result = Choco.implies(featureSelected, implies);

    return result;
  }
  private void transformAttribute(Attribute attribute) {
    // 0: attribute is disabled if feature is disabled
    // 1: attribute is enabled if feature is selected
    IntegerVariable featureVariable = getOrCreateVariable(attribute.getFeature());

    Constraint memberConstraint = getMemberConstraint(attribute);

    // if feature is enabled, then attribute value must be in bounds
    Constraint featureEnabled = Choco.eq(featureVariable, 1);
    Constraint checkAttribute = Choco.implies(featureEnabled, memberConstraint);
    getModel().addConstraint(checkAttribute);

    IntegerVariable attributeVariable = getOrCreateVariable(attribute);

    Constraint featureDisabled = Choco.eq(featureVariable, 0);
    Constraint attrDisabled = Choco.eq(attributeVariable, attributeDisabled);

    Constraint disabledAttr = Choco.ifOnlyIf(featureDisabled, attrDisabled);
    getModel().addConstraint(disabledAttr);
  }
  /**
   * Detect different equalities cliques.
   *
   * @param matrix matrix of equalities
   * @param nbIntVars nb of IntegerVariable within the model
   * @param color array of colir, ie nb different variable
   * @param domainByColor list of domain by color.
   * @return nb of different color found
   */
  private int detect(
      final ISparseMatrix matrix,
      final int nbIntVars,
      final int[] color,
      final TIntObjectHashMap<IntegerVariableMerger> domainByColor) {
    int nb = -1;
    IntegerVariableMerger dtmp = new IntegerVariableMerger();
    final Iterator<Long> it = matrix.iterator();
    while (it.hasNext()) {
      final long v = it.next();
      final int i = (int) (v / nbIntVars);
      final int j = (int) (v % nbIntVars);

      if (color[i] == -1) {
        nb++;
        color[i] = nb;
        domainByColor.put(nb, new IntegerVariableMerger(model.getIntVar(i)));
      }
      final IntegerVariableMerger d = domainByColor.get(color[i]);
      // backup
      dtmp.copy(d);
      if (d.intersection(model.getIntVar(j))) {
        color[j] = color[i];
        domainByColor.put(color[i], d);
      } else {
        add(Choco.eq(model.getIntVar(i), model.getIntVar(j)));
        // rollback
        d.copy(dtmp);
        if (color[j] == -1) {
          nb++;
          color[j] = nb;
          domainByColor.put(nb, new IntegerVariableMerger(model.getIntVar(j)));
        }
      }
    }
    return nb;
  }
  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;
  }
Beispiel #8
0
 public Object eq(Object exp1, Object exp2) {
   return Choco.eq((IntegerExpressionVariable) exp1, (IntegerExpressionVariable) exp1);
 }
Beispiel #9
0
 public Object eq(Object exp, int value) {
   return Choco.eq((IntegerExpressionVariable) exp, value);
 }
Beispiel #10
0
 public Object eq(int value, Object exp) {
   return Choco.eq(value, (IntegerExpressionVariable) exp);
 }