Search in sources :

Example 1 with SmtVariableCollector

use of org.evosuite.symbolic.solver.smt.SmtVariableCollector 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 SmtVariableCollector

use of org.evosuite.symbolic.solver.smt.SmtVariableCollector 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)

Aggregations

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