use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestIsInteger method testIsInteger.
@Test
public void testIsInteger() throws SolverEmptyQueryException {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new StringUnaryToIntegerExpression(new StringVariable("var0", "hello"), Operator.IS_INTEGER, 0L), Comparator.NE, new IntegerConstant(0)));
EvoSuiteSolver solver = new EvoSuiteSolver();
try {
SolverResult result = solver.solve(constraints);
assertTrue(result.isSAT());
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class Z3Solver method solve.
@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, IOException, SolverParseException, SolverEmptyQueryException, SolverErrorException {
long hard_timeout = 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 smtCheckSatQuery = buildSmtQuery(constraints, variables);
if (smtCheckSatQuery.getConstantDeclarations().isEmpty()) {
logger.debug("Z3 SMT query has no variables");
throw new SolverEmptyQueryException("Z3 SMT query has no variables");
}
if (smtCheckSatQuery.getAssertions().isEmpty()) {
Map<String, Object> emptySolution = new HashMap<String, Object>();
SolverResult emptySAT = SolverResult.newSAT(emptySolution);
return emptySAT;
}
Z3QueryPrinter printer = new Z3QueryPrinter();
String smtQueryStr = printer.print(smtCheckSatQuery, hard_timeout);
logger.debug("Z3 Query:");
logger.debug(smtQueryStr);
if (Properties.Z3_PATH == null) {
String errMsg = "Property Z3_PATH should be setted in order to use the Z3 Solver!";
logger.error(errMsg);
throw new IllegalStateException(errMsg);
}
String z3Cmd = Properties.Z3_PATH + " -smt2 -in";
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
launchNewProcess(z3Cmd, smtQueryStr, (int) hard_timeout, stdout);
String z3ResultStr = stdout.toString("UTF-8");
Map<String, Object> initialValues = getConcreteValues(variables);
Z3ResultParser resultParser;
if (this.addMissingVariables()) {
resultParser = new Z3ResultParser(initialValues);
} else {
resultParser = new Z3ResultParser();
}
SolverResult result = resultParser.parseResult(z3ResultStr);
return result;
}
use of org.evosuite.symbolic.solver.SolverResult 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;
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class EvoSuiteSolver method solve.
@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, SolverEmptyQueryException {
long timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
long startTimeMillis = System.currentTimeMillis();
Set<Variable<?>> variables = getVariables(constraints);
Map<String, Object> initialValues = getConcreteValues(variables);
double distance = DistanceEstimator.getDistance(constraints);
if (distance == 0.0) {
log.info("Initial distance already is 0.0, skipping search");
SolverResult satResult = SolverResult.newSAT(initialValues);
return satResult;
}
for (int attempt = 0; attempt <= Properties.DSE_VARIABLE_RESETS; attempt++) {
for (Variable<?> v : variables) {
long currentTimeMillis = System.currentTimeMillis();
long elapsed_solving_time = currentTimeMillis - startTimeMillis;
if (elapsed_solving_time > timeout) {
throw new SolverTimeoutException();
}
log.debug("Variable: " + v + ", " + variables);
if (v instanceof IntegerVariable) {
IntegerVariable integerVariable = (IntegerVariable) v;
IntegerAVM avm = new IntegerAVM(integerVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else if (v instanceof RealVariable) {
RealVariable realVariable = (RealVariable) v;
RealAVM avm = new RealAVM(realVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else if (v instanceof StringVariable) {
StringVariable strVariable = (StringVariable) v;
StringAVM avm = new StringAVM(strVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else {
throw new RuntimeException("Unknown variable type " + v.getClass().getName());
}
distance = DistanceEstimator.getDistance(constraints);
if (distance <= 0.0) {
log.info("Distance is 0, ending search");
break;
}
}
if (distance <= 0.0) {
log.info("Distance is 0, ending search");
break;
} else {
log.info("Randomizing variables");
randomizeValues(variables, getConstants(constraints));
}
}
// distance = DistanceEstimator.getDistance(constraints);
if (distance <= 0) {
log.debug("Distance is " + distance + ", found solution");
Map<String, Object> new_model = getConcreteValues(variables);
setConcreteValues(variables, initialValues);
SolverResult satResult = SolverResult.newSAT(new_model);
return satResult;
} else {
setConcreteValues(variables, initialValues);
log.debug("Returning null, search was not successful");
SolverResult unsatResult = SolverResult.newUNSAT();
return unsatResult;
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class CVC4ResultParser method parseModel.
private SolverResult parseModel(String cvc4ResultStr) {
Map<String, Object> solution = new HashMap<String, Object>();
String token;
StringTokenizer tokenizer = new StringTokenizer(cvc4ResultStr, "() \n\t", true);
// sat
token = tokenizer.nextToken();
//
token = tokenizer.nextToken();
// (
token = tokenizer.nextToken();
// model
token = tokenizer.nextToken();
// \n
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens()) {
// (
token = tokenizer.nextToken();
if (token.equals(")")) {
break;
}
// define-fun ?
token = tokenizer.nextToken();
if (token.equals("define-fun")) {
//
token = tokenizer.nextToken();
String fun_name = tokenizer.nextToken();
//
token = tokenizer.nextToken();
// (
token = tokenizer.nextToken();
// )
token = tokenizer.nextToken();
//
token = tokenizer.nextToken();
String typeName = tokenizer.nextToken();
if (typeName.equals("Int")) {
// " "
token = tokenizer.nextToken();
//
token = tokenizer.nextToken();
boolean neg = false;
String integerValueStr;
if (token.equals("(")) {
neg = true;
// -
token = tokenizer.nextToken();
// " "
token = tokenizer.nextToken();
integerValueStr = tokenizer.nextToken();
} else {
integerValueStr = token;
}
Long value;
if (neg) {
String absoluteIntegerValue = integerValueStr;
value = Long.parseLong("-" + absoluteIntegerValue);
} else {
value = Long.parseLong(integerValueStr);
}
solution.put(fun_name, value);
if (neg) {
// )
token = tokenizer.nextToken();
}
// )
token = tokenizer.nextToken();
// \n
token = tokenizer.nextToken();
} else if (typeName.equals("Real")) {
// " "
token = tokenizer.nextToken();
token = tokenizer.nextToken();
Double value;
if (!token.equals("(")) {
value = Double.parseDouble(token);
} else {
token = tokenizer.nextToken();
if (token.equals("-")) {
// " "
token = tokenizer.nextToken();
// ?
token = tokenizer.nextToken();
if (token.equals("(")) {
// "/"
token = tokenizer.nextToken();
// " "
token = tokenizer.nextToken();
String numeratorStr = tokenizer.nextToken();
// " "
token = tokenizer.nextToken();
String denominatorStr = tokenizer.nextToken();
value = parseRational(true, numeratorStr, denominatorStr);
// ")"
token = tokenizer.nextToken();
// ")"
token = tokenizer.nextToken();
} else {
String absoluteValueStr = token;
value = Double.parseDouble("-" + absoluteValueStr);
// )
token = tokenizer.nextToken();
}
} else {
if (token.equals("/")) {
// " "
token = tokenizer.nextToken();
String numeratorStr = tokenizer.nextToken();
// " "
token = tokenizer.nextToken();
String denominatorStr = tokenizer.nextToken();
value = parseRational(false, numeratorStr, denominatorStr);
// )
token = tokenizer.nextToken();
} else {
value = Double.parseDouble(token);
}
}
}
solution.put(fun_name, value);
// )
token = tokenizer.nextToken();
// \n
token = tokenizer.nextToken();
} else if (typeName.equals("String")) {
token = tokenizer.nextToken();
StringBuffer value = new StringBuffer();
while (!token.startsWith("\"")) {
// move until \" is found
token = tokenizer.nextToken();
}
value.append(token);
if (!token.substring(1).endsWith("\"")) {
String stringToken;
do {
if (!tokenizer.hasMoreTokens()) {
System.out.println("Error!");
}
stringToken = tokenizer.nextToken();
value.append(stringToken);
} while (// append until
!stringToken.endsWith("\""));
// \" is found
}
String stringWithQuotes = value.toString();
String stringWithoutQuotes = stringWithQuotes.substring(1, stringWithQuotes.length() - 1);
solution.put(fun_name, stringWithoutQuotes);
// )
token = tokenizer.nextToken();
// \n
token = tokenizer.nextToken();
} else {
// throw new IllegalArgumentException(
// "Must implement this production");
}
}
}
if (solution.isEmpty()) {
logger.warn("The CVC4 model has no variables");
return null;
} else {
logger.debug("Parsed values from CVC4 output");
for (String varName : solution.keySet()) {
String valueOf = String.valueOf(solution.get(varName));
logger.debug(varName + ":" + valueOf);
}
}
if (initialValues != null) {
if (!solution.keySet().equals(initialValues.keySet())) {
logger.debug("Adding missing values to Solver solution");
addMissingValues(initialValues, solution);
}
}
SolverResult satResult = SolverResult.newSAT(solution);
return satResult;
}
Aggregations