@Override
  public Result solve(Expression<Boolean> f, Valuation result) {
    CoralExpressionGenerator root = new CoralExpressionGenerator();
    final Env[] sol = new Env[1];
    try {
      final PC pc = root.generateAssertion(f);
      logger.fine("Coral solving...");
      sol[0] = coralSolver.getCallable(pc).call();
    } catch (Exception e) {
      logger.severe("Coral threw exception. Returning DONT_KNOW");
      return Result.DONT_KNOW;
    }
    // TODO: not sure why the solution is found as the first element in the Env[]...
    Env coralSol = sol[0];
    Result coralRes = convertCoralRes(coralSol.getResult());

    if (result != null
        && coralRes == Result.SAT) { // result is requested besides satisfiability check
      HashMap<Variable<?>, SymLiteral> varMap = root.getVariables();
      for (Variable<?> v : varMap.keySet()) {
        SymNumber value = coralSol.getValue(varMap.get(v));

        // These checks make my eyes bleed
        if ((v.getType() instanceof FloatType)
            && !(value instanceof SymFloat)) { // TODO: ensure this is sound. It seems like a patch
          Float floatVal = value.evalNumber().floatValue();
          result.setParsedValue(v, floatVal.toString());
        } else if ((v.getType() instanceof DoubleType) && !(value instanceof SymDouble)) {
          Double dVal = value.evalNumber().doubleValue();
          result.setParsedValue(v, dVal.toString());
        } else if ((v.getType() instanceof SInt32Type) && !(value instanceof SymInt)) {
          Integer iVal = value.evalNumber().intValue();
          result.setParsedValue(v, iVal.toString());
        } else if ((v.getType() instanceof SInt64Type) && !(value instanceof SymLong)) {
          Long lVal = value.evalNumber().longValue();
          result.setParsedValue(v, lVal.toString());
        } else // This is the default case which should be the most frequent one
        result.setParsedValue(v, value.toString());
      }
      logger.finer("Satisfiable, valuation " + result);
    }
    return coralRes;
  }
Exemplo n.º 2
0
  @Test
  public void testIfThenElse() {

    Variable x = new Variable(BuiltinTypes.BOOL, "x");
    Variable y = new Variable(BuiltinTypes.SINT32, "y");
    Constant c = new Constant(BuiltinTypes.SINT32, 3);

    IfThenElse ite =
        new IfThenElse(
            x,
            new NumericCompound<>(y, NumericOperator.PLUS, c),
            new NumericCompound<>(y, NumericOperator.MINUS, c));

    System.out.println(ite);

    Valuation val = new Valuation();
    val.setValue(y, 10);

    val.setValue(x, true);
    System.out.println(val + ": " + ite.evaluate(val));

    val.setValue(x, false);
    System.out.println(val + ": " + ite.evaluate(val));
  }