Search in sources :

Example 1 with SmtFunctionDeclaration

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

the class CVC4Solver method buildSmtCheckSatQuery.

private static SmtCheckSatQuery buildSmtCheckSatQuery(Collection<Constraint<?>> constraints) {
    ConstraintToCVC4Visitor v = new ConstraintToCVC4Visitor(true);
    SmtVariableCollector varCollector = new SmtVariableCollector();
    SmtOperatorCollector funCollector = new SmtOperatorCollector();
    List<SmtAssertion> smtAssertions = new LinkedList<SmtAssertion>();
    for (Constraint<?> c : constraints) {
        SmtExpr smtExpr = c.accept(v, null);
        if (smtExpr != null) {
            SmtAssertion smtAssertion = new SmtAssertion(smtExpr);
            smtAssertions.add(smtAssertion);
            smtExpr.accept(varCollector, null);
            smtExpr.accept(funCollector, null);
        }
    }
    Set<SmtVariable> variables = varCollector.getSmtVariables();
    if (variables.isEmpty()) {
        // no variables, constraint system is trivial
        return null;
    }
    List<SmtFunctionDefinition> functionDefinitions = new LinkedList<SmtFunctionDefinition>();
    final boolean addCharToInt = funCollector.getOperators().contains(Operator.CHAR_TO_INT);
    if (addCharToInt) {
        String charToIntFunction = buildCharToIntFunction();
        SmtFunctionDefinition funcDefinition = new SmtFunctionDefinition(charToIntFunction);
        functionDefinitions.add(funcDefinition);
    }
    final boolean addIntToChar = funCollector.getOperators().contains(Operator.INT_TO_CHAR);
    if (addIntToChar) {
        String intToCharFunction = buildIntToCharFunction();
        SmtFunctionDefinition funcDefinition = new SmtFunctionDefinition(intToCharFunction);
        functionDefinitions.add(funcDefinition);
    }
    List<SmtFunctionDeclaration> functionDeclarations = new LinkedList<SmtFunctionDeclaration>();
    for (SmtVariable var : variables) {
        String varName = var.getName();
        if (var instanceof SmtIntVariable) {
            SmtFunctionDeclaration intVar = SmtExprBuilder.mkIntFunctionDeclaration(varName);
            functionDeclarations.add(intVar);
        } else if (var instanceof SmtRealVariable) {
            SmtFunctionDeclaration realVar = SmtExprBuilder.mkRealFunctionDeclaration(varName);
            functionDeclarations.add(realVar);
        } else if (var instanceof SmtStringVariable) {
            SmtFunctionDeclaration stringVar = SmtExprBuilder.mkStringFunctionDeclaration(varName);
            functionDeclarations.add(stringVar);
        } else {
            throw new RuntimeException("Unknown variable type " + var.getClass().getCanonicalName());
        }
    }
    SmtCheckSatQuery smtQuery = new SmtCheckSatQuery(new LinkedList<SmtConstantDeclaration>(), functionDeclarations, functionDefinitions, smtAssertions);
    return smtQuery;
}
Also used : SmtFunctionDefinition(org.evosuite.symbolic.solver.smt.SmtFunctionDefinition) SmtAssertion(org.evosuite.symbolic.solver.smt.SmtAssertion) SmtVariableCollector(org.evosuite.symbolic.solver.smt.SmtVariableCollector) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) SmtStringVariable(org.evosuite.symbolic.solver.smt.SmtStringVariable) SmtOperatorCollector(org.evosuite.symbolic.solver.smt.SmtOperatorCollector) SmtIntVariable(org.evosuite.symbolic.solver.smt.SmtIntVariable) SmtFunctionDeclaration(org.evosuite.symbolic.solver.smt.SmtFunctionDeclaration) SmtConstantDeclaration(org.evosuite.symbolic.solver.smt.SmtConstantDeclaration) LinkedList(java.util.LinkedList) SmtRealVariable(org.evosuite.symbolic.solver.smt.SmtRealVariable) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) SmtVariable(org.evosuite.symbolic.solver.smt.SmtVariable)

Example 2 with SmtFunctionDeclaration

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

the class CVC4QueryPrinter method print.

public String print(SmtCheckSatQuery smtQuery) {
    StringBuffer buff = new StringBuffer();
    buff.append("\n");
    buff.append("(set-logic " + CVC4_LOGIC + ")");
    buff.append("\n");
    buff.append("(set-option :produce-models true)");
    buff.append("\n");
    buff.append("(set-option :strings-exp true)");
    buff.append("\n");
    for (SmtFunctionDeclaration functionDeclaration : smtQuery.getFunctionDeclarations()) {
        String str = String.format("(declare-fun %s () %s)", functionDeclaration.getFunctionName(), functionDeclaration.getFunctionSort());
        buff.append(str);
        buff.append("\n");
    }
    for (SmtFunctionDefinition functionDeclaration : smtQuery.getFunctionDefinitions()) {
        String str = String.format("(define-fun %s)", functionDeclaration.getFunctionDefinition());
        buff.append(str);
        buff.append("\n");
    }
    SmtExprPrinter printer = new SmtExprPrinter();
    for (SmtAssertion smtAssertion : smtQuery.getAssertions()) {
        SmtExpr smtExpr = smtAssertion.getFormula();
        String smtExprStr = smtExpr.accept(printer, null);
        String str = String.format("(assert %s)", smtExprStr);
        buff.append(str);
        buff.append("\n");
    }
    buff.append("(check-sat)");
    buff.append("\n");
    buff.append("(get-model)");
    buff.append("\n");
    buff.append("(exit)");
    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) SmtFunctionDeclaration(org.evosuite.symbolic.solver.smt.SmtFunctionDeclaration)

Aggregations

SmtAssertion (org.evosuite.symbolic.solver.smt.SmtAssertion)2 SmtExpr (org.evosuite.symbolic.solver.smt.SmtExpr)2 SmtFunctionDeclaration (org.evosuite.symbolic.solver.smt.SmtFunctionDeclaration)2 SmtFunctionDefinition (org.evosuite.symbolic.solver.smt.SmtFunctionDefinition)2 LinkedList (java.util.LinkedList)1 SmtCheckSatQuery (org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)1 SmtConstantDeclaration (org.evosuite.symbolic.solver.smt.SmtConstantDeclaration)1 SmtExprPrinter (org.evosuite.symbolic.solver.smt.SmtExprPrinter)1 SmtIntVariable (org.evosuite.symbolic.solver.smt.SmtIntVariable)1 SmtOperatorCollector (org.evosuite.symbolic.solver.smt.SmtOperatorCollector)1 SmtRealVariable (org.evosuite.symbolic.solver.smt.SmtRealVariable)1 SmtStringVariable (org.evosuite.symbolic.solver.smt.SmtStringVariable)1 SmtVariable (org.evosuite.symbolic.solver.smt.SmtVariable)1 SmtVariableCollector (org.evosuite.symbolic.solver.smt.SmtVariableCollector)1