Search in sources :

Example 41 with SmtExpr

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();
}
Also used : SmtFunctionDefinition(org.evosuite.symbolic.solver.smt.SmtFunctionDefinition) SmtAssertion(org.evosuite.symbolic.solver.smt.SmtAssertion) SmtExprPrinter(org.evosuite.symbolic.solver.smt.SmtExprPrinter) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) SmtConstantDeclaration(org.evosuite.symbolic.solver.smt.SmtConstantDeclaration)

Example 42 with SmtExpr

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;
}
Also used : Operator(org.evosuite.symbolic.solver.smt.SmtOperation.Operator) SmtFunctionDefinition(org.evosuite.symbolic.solver.smt.SmtFunctionDefinition) SmtStringVariable(org.evosuite.symbolic.solver.smt.SmtStringVariable) SmtOperatorCollector(org.evosuite.symbolic.solver.smt.SmtOperatorCollector) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) SmtVariable(org.evosuite.symbolic.solver.smt.SmtVariable) HashSet(java.util.HashSet) SmtAssertion(org.evosuite.symbolic.solver.smt.SmtAssertion) SmtVariableCollector(org.evosuite.symbolic.solver.smt.SmtVariableCollector) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) SmtIntVariable(org.evosuite.symbolic.solver.smt.SmtIntVariable) SmtConstantDeclaration(org.evosuite.symbolic.solver.smt.SmtConstantDeclaration) LinkedList(java.util.LinkedList) SmtRealVariable(org.evosuite.symbolic.solver.smt.SmtRealVariable)

Example 43 with SmtExpr

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);
    }
}
Also used : Operator(org.evosuite.symbolic.expr.Operator) SmtIntConstant(org.evosuite.symbolic.solver.smt.SmtIntConstant) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) LinkedList(java.util.LinkedList) IntegerBinaryExpression(org.evosuite.symbolic.expr.bv.IntegerBinaryExpression) StringBinaryExpression(org.evosuite.symbolic.expr.str.StringBinaryExpression) GetFieldExpression(org.evosuite.symbolic.expr.ref.GetFieldExpression) StringMultipleToIntegerExpression(org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression) IntegerUnaryExpression(org.evosuite.symbolic.expr.bv.IntegerUnaryExpression) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) RealUnaryExpression(org.evosuite.symbolic.expr.fp.RealUnaryExpression) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) Expression(org.evosuite.symbolic.expr.Expression) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) RealBinaryExpression(org.evosuite.symbolic.expr.fp.RealBinaryExpression) RealUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.RealUnaryToIntegerExpression) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)

Example 44 with SmtExpr

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;
}
Also used : SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr)

Example 45 with SmtExpr

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;
}
Also used : SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr)

Aggregations

SmtExpr (org.evosuite.symbolic.solver.smt.SmtExpr)60 Operator (org.evosuite.symbolic.expr.Operator)14 LinkedList (java.util.LinkedList)8 Comparator (org.evosuite.symbolic.expr.Comparator)6 SmtAssertion (org.evosuite.symbolic.solver.smt.SmtAssertion)6 SmtIntConstant (org.evosuite.symbolic.solver.smt.SmtIntConstant)6 SmtConstantDeclaration (org.evosuite.symbolic.solver.smt.SmtConstantDeclaration)5 Expression (org.evosuite.symbolic.expr.Expression)4 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)4 IntegerUnaryExpression (org.evosuite.symbolic.expr.bv.IntegerUnaryExpression)4 RealUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.RealUnaryToIntegerExpression)4 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)4 StringMultipleToIntegerExpression (org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression)4 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)4 RealBinaryExpression (org.evosuite.symbolic.expr.fp.RealBinaryExpression)4 RealUnaryExpression (org.evosuite.symbolic.expr.fp.RealUnaryExpression)4 GetFieldExpression (org.evosuite.symbolic.expr.ref.GetFieldExpression)4 StringBinaryExpression (org.evosuite.symbolic.expr.str.StringBinaryExpression)4 StringMultipleExpression (org.evosuite.symbolic.expr.str.StringMultipleExpression)4 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)4