use of org.evosuite.symbolic.solver.smt.SmtExpr in project evosuite by EvoSuite.
the class Z3Str2QueryPrinter method print.
public String print(SmtCheckSatQuery smtQuery) {
StringBuffer buff = new StringBuffer();
buff.append("\n");
for (SmtConstantDeclaration constantDeclaration : smtQuery.getConstantDeclarations()) {
String str = String.format("(declare-const %s %s)", constantDeclaration.getConstantName(), constantDeclaration.getConstantSort());
buff.append(str);
buff.append("\n");
}
for (SmtFunctionDefinition smtFunctionDefinition : smtQuery.getFunctionDefinitions()) {
String str = String.format("(define-fun %s)", smtFunctionDefinition.getFunctionDefinition());
buff.append(str);
buff.append("\n");
}
SmtExprPrinter printer = new SmtExprPrinter();
for (SmtAssertion assertionDeclaration : smtQuery.getAssertions()) {
SmtExpr formula = assertionDeclaration.getFormula();
String formulaStr = formula.accept(printer, null);
String str = String.format("(assert %s)", formulaStr);
buff.append(str);
buff.append("\n");
}
buff.append("(check-sat)");
buff.append("\n");
return buff.toString();
}
use of org.evosuite.symbolic.solver.smt.SmtExpr in project evosuite by EvoSuite.
the class Z3Str2Solver method buildSmtQuerty.
private static SmtCheckSatQuery buildSmtQuerty(Collection<Constraint<?>> constraints) {
ConstraintToZ3Str2Visitor v = new ConstraintToZ3Str2Visitor();
List<SmtAssertion> assertions = new LinkedList<SmtAssertion>();
SmtVariableCollector varCollector = new SmtVariableCollector();
SmtOperatorCollector opCollector = new SmtOperatorCollector();
for (Constraint<?> c : constraints) {
SmtExpr smtExpr = c.accept(v, null);
if (smtExpr != null) {
SmtAssertion newAssertion = new SmtAssertion(smtExpr);
assertions.add(newAssertion);
smtExpr.accept(varCollector, null);
smtExpr.accept(opCollector, null);
}
}
Set<SmtVariable> smtVariables = varCollector.getSmtVariables();
Set<Operator> smtOperators = opCollector.getOperators();
boolean addCharToIntFunction;
if (smtOperators.contains(SmtOperation.Operator.CHAR_TO_INT)) {
addCharToIntFunction = true;
} else {
addCharToIntFunction = false;
}
Set<SmtVariable> smtVariablesToDeclare = new HashSet<SmtVariable>(smtVariables);
if (addCharToIntFunction) {
Set<SmtStringVariable> charVariables = buildCharVariables();
smtVariablesToDeclare.addAll(charVariables);
}
List<SmtConstantDeclaration> constantDeclarations = new LinkedList<SmtConstantDeclaration>();
for (SmtVariable v1 : smtVariablesToDeclare) {
String varName = v1.getName();
if (v1 instanceof SmtIntVariable) {
SmtConstantDeclaration constantDecl = SmtExprBuilder.mkIntConstantDeclaration(varName);
constantDeclarations.add(constantDecl);
} else if (v1 instanceof SmtRealVariable) {
SmtConstantDeclaration constantDecl = SmtExprBuilder.mkRealConstantDeclaration(varName);
constantDeclarations.add(constantDecl);
} else if (v1 instanceof SmtStringVariable) {
SmtConstantDeclaration constantDecl = SmtExprBuilder.mkStringConstantDeclaration(varName);
constantDeclarations.add(constantDecl);
} else {
throw new RuntimeException("Unknown variable type " + v1.getClass().getCanonicalName());
}
}
List<SmtFunctionDefinition> functionDefinitions = new LinkedList<SmtFunctionDefinition>();
if (addCharToIntFunction) {
String charToInt = buildCharToIntFunction();
SmtFunctionDefinition newFunctionDef = new SmtFunctionDefinition(charToInt);
functionDefinitions.add(newFunctionDef);
}
SmtCheckSatQuery smtCheckSatQuery = new SmtCheckSatQuery(constantDeclarations, functionDefinitions, assertions);
return smtCheckSatQuery;
}
use of org.evosuite.symbolic.solver.smt.SmtExpr in project evosuite by EvoSuite.
the class ExprToCVC4Visitor method visit.
@Override
public SmtExpr visit(StringMultipleComparison e, Void v) {
Expression<String> leftOperand = e.getLeftOperand();
Expression<?> rightOperand = e.getRightOperand();
Operator op = e.getOperator();
ArrayList<Expression<?>> othersOperands = e.getOther();
SmtExpr left = leftOperand.accept(this, null);
SmtExpr right = rightOperand.accept(this, null);
List<SmtExpr> others = new LinkedList<SmtExpr>();
for (Expression<?> otherOperand : othersOperands) {
SmtExpr other = otherOperand.accept(this, null);
others.add(other);
}
if (isNull(left, right, others)) {
return null;
}
if (!isSymbolic(left, right, others)) {
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
switch(op) {
case STARTSWITH:
{
SmtExpr indexExpr = others.get(0);
if (indexExpr.equals(SmtExprBuilder.ZERO_INT)) {
SmtIntConstant oneExpr = SmtExprBuilder.ONE_INT;
SmtIntConstant zeroExpr = SmtExprBuilder.ZERO_INT;
SmtExpr startsWithFormula = SmtExprBuilder.mkStrPrefixOf(right, left);
SmtExpr ifThenElseFormula = SmtExprBuilder.mkITE(startsWithFormula, oneExpr, zeroExpr);
return ifThenElseFormula;
} else {
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
}
case EQUALS:
case EQUALSIGNORECASE:
case ENDSWITH:
case CONTAINS:
{
throw new IllegalArgumentException("Illegal StringMultipleComparison operator " + op);
}
case REGIONMATCHES:
case PATTERNMATCHES:
case APACHE_ORO_PATTERN_MATCHES:
{
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
default:
throw new UnsupportedOperationException("Not implemented yet! " + op);
}
}
use of org.evosuite.symbolic.solver.smt.SmtExpr in project evosuite by EvoSuite.
the class ExprToCVC4Visitor method visit.
@Override
public SmtExpr visit(IntegerToStringCast e, Void v) {
SmtExpr argument = e.getArgument().accept(this, null);
if (argument == null) {
return null;
}
if (!argument.isSymbolic()) {
String stringValue = e.getConcreteValue();
return SmtExprBuilder.mkStringConstant(stringValue);
}
SmtExpr intToStr = SmtExprBuilder.mkIntToStr(argument);
return intToStr;
}
use of org.evosuite.symbolic.solver.smt.SmtExpr in project evosuite by EvoSuite.
the class ExprToCVC4Visitor method visit.
@Override
public SmtExpr visit(RealConstant e, Void v) {
double doubleVal = e.getConcreteValue();
SmtExpr realExpr = createRatNumber(doubleVal);
return realExpr;
}
Aggregations