Search in sources :

Example 6 with SmtCheckSatQuery

use of org.evosuite.symbolic.solver.smt.SmtCheckSatQuery in project evosuite by EvoSuite.

the class CVC4Solver method solve.

@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, SolverEmptyQueryException, SolverErrorException, SolverParseException, IOException {
    if (Properties.CVC4_PATH == null) {
        String errMsg = "Property CVC4_PATH should be setted in order to use the CVC4 Solver!";
        logger.error(errMsg);
        throw new IllegalStateException(errMsg);
    }
    // In fact, it cannot even produce models for non-linear theories
    if (!reWriteNonLinearConstraints && hasNonLinearConstraints(constraints)) {
        logger.debug("Skipping query due to (unsupported) non-linear constraints");
        throw new SolverEmptyQueryException("Skipping query due to (unsupported) non-linear constraints");
    }
    long cvcTimeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
    Set<Variable<?>> variables = new HashSet<Variable<?>>();
    for (Constraint<?> c : constraints) {
        Set<Variable<?>> c_variables = c.getVariables();
        variables.addAll(c_variables);
    }
    SmtCheckSatQuery smtQuery = buildSmtCheckSatQuery(constraints);
    if (smtQuery == null) {
        logger.debug("No variables found during the creation of the SMT query.");
        throw new SolverEmptyQueryException("No variables found during the creation of the SMT query.");
    }
    if (smtQuery.getAssertions().isEmpty()) {
        Map<String, Object> emptySolution = new HashMap<String, Object>();
        SolverResult emptySAT = SolverResult.newSAT(emptySolution);
        return emptySAT;
    }
    CVC4QueryPrinter printer = new CVC4QueryPrinter();
    String smtQueryStr = printer.print(smtQuery);
    if (smtQueryStr == null) {
        logger.debug("No variables found during constraint solving.");
        throw new SolverEmptyQueryException("No variables found during constraint solving.");
    }
    logger.debug("CVC4 Query:");
    logger.debug(smtQueryStr);
    String cvc4Cmd = buildCVC4cmd(cvcTimeout);
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    try {
        launchNewProcess(cvc4Cmd, smtQueryStr, (int) cvcTimeout, stdout);
        String cvc4ResultStr = stdout.toString("UTF-8");
        if (cvc4ResultStr.startsWith("unsat") && cvc4ResultStr.contains("(error \"Cannot get the current model unless immediately preceded by SAT/INVALID or UNKNOWN response.\")")) {
            // UNSAT
            SolverResult unsatResult = SolverResult.newUNSAT();
            return unsatResult;
        }
        if (cvc4ResultStr.contains("error")) {
            String errMsg = "An error occurred while executing CVC4!";
            logger.error(errMsg);
            throw new SolverErrorException(errMsg);
        }
        // parse solution
        Map<String, Object> initialValues = getConcreteValues(variables);
        CVC4ResultParser resultParser;
        if (addMissingVariables()) {
            resultParser = new CVC4ResultParser(initialValues);
        } else {
            resultParser = new CVC4ResultParser();
        }
        SolverResult solverResult = resultParser.parse(cvc4ResultStr);
        if (solverResult.isSAT()) {
            // check if the found solution is useful
            boolean check = checkSAT(constraints, solverResult);
            if (!check) {
                logger.debug("CVC4 solution does not solve the original constraint system. ");
                SolverResult unsatResult = SolverResult.newUNSAT();
                return unsatResult;
            }
        }
        return solverResult;
    } catch (IOException e) {
        if (e.getMessage().contains("Permission denied")) {
            logger.error("No permissions for running CVC4 binary");
        } else {
            logger.error("IO Exception during launching of CVC4 command");
        }
        throw e;
    }
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) SmtIntVariable(org.evosuite.symbolic.solver.smt.SmtIntVariable) SmtRealVariable(org.evosuite.symbolic.solver.smt.SmtRealVariable) SmtVariable(org.evosuite.symbolic.solver.smt.SmtVariable) SmtStringVariable(org.evosuite.symbolic.solver.smt.SmtStringVariable) HashMap(java.util.HashMap) SolverResult(org.evosuite.symbolic.solver.SolverResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SolverEmptyQueryException(org.evosuite.symbolic.solver.SolverEmptyQueryException) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) SolverErrorException(org.evosuite.symbolic.solver.SolverErrorException) HashSet(java.util.HashSet)

Aggregations

SmtCheckSatQuery (org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)6 SmtIntVariable (org.evosuite.symbolic.solver.smt.SmtIntVariable)4 SmtRealVariable (org.evosuite.symbolic.solver.smt.SmtRealVariable)4 SmtStringVariable (org.evosuite.symbolic.solver.smt.SmtStringVariable)4 SmtVariable (org.evosuite.symbolic.solver.smt.SmtVariable)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Variable (org.evosuite.symbolic.expr.Variable)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SolverResult (org.evosuite.symbolic.solver.SolverResult)3 SmtAssertion (org.evosuite.symbolic.solver.smt.SmtAssertion)3 SmtConstantDeclaration (org.evosuite.symbolic.solver.smt.SmtConstantDeclaration)3 SmtExpr (org.evosuite.symbolic.solver.smt.SmtExpr)3 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)2 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)2 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)2 SmtFunctionDefinition (org.evosuite.symbolic.solver.smt.SmtFunctionDefinition)2 SmtOperatorCollector (org.evosuite.symbolic.solver.smt.SmtOperatorCollector)2