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;
}
}
Aggregations