Search in sources :

Example 6 with Variable

use of org.evosuite.symbolic.expr.Variable 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;
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Variable(org.evosuite.symbolic.expr.Variable) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) SolverResult(org.evosuite.symbolic.solver.SolverResult) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable)

Example 7 with Variable

use of org.evosuite.symbolic.expr.Variable in project evosuite by EvoSuite.

the class ConcolicMutation method reduce.

/**
 * Apply cone of influence reduction to constraints with respect to the last
 * constraint in the list
 *
 * @param constraints
 * @return
 */
private static List<Constraint<?>> reduce(List<Constraint<?>> constraints) {
    Constraint<?> target = constraints.get(constraints.size() - 1);
    Set<Variable<?>> dependencies = getVariables(target);
    LinkedList<Constraint<?>> coi = new LinkedList<Constraint<?>>();
    coi.add(target);
    for (int i = constraints.size() - 2; i >= 0; i--) {
        Constraint<?> constraint = constraints.get(i);
        Set<Variable<?>> variables = getVariables(constraint);
        for (Variable<?> var : dependencies) {
            if (variables.contains(var)) {
                dependencies.addAll(variables);
                coi.addFirst(constraint);
                break;
            }
        }
    }
    return coi;
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) Constraint(org.evosuite.symbolic.expr.Constraint) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint)

Example 8 with Variable

use of org.evosuite.symbolic.expr.Variable in project evosuite by EvoSuite.

the class DSETestGenerator method reduce.

/**
 * Apply cone of influence reduction to constraints with respect to the last
 * constraint in the list
 *
 * @param constraints
 * @return
 */
private List<Constraint<?>> reduce(List<Constraint<?>> constraints) {
    Constraint<?> target = constraints.get(constraints.size() - 1);
    Set<Variable<?>> dependencies = getVariables(target);
    LinkedList<Constraint<?>> coi = new LinkedList<Constraint<?>>();
    if (dependencies.size() <= 0)
        return coi;
    coi.add(target);
    for (int i = constraints.size() - 2; i >= 0; i--) {
        Constraint<?> constraint = constraints.get(i);
        Set<Variable<?>> variables = getVariables(constraint);
        for (Variable<?> var : dependencies) {
            if (variables.contains(var)) {
                dependencies.addAll(variables);
                coi.addFirst(constraint);
                break;
            }
        }
    }
    return coi;
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) Constraint(org.evosuite.symbolic.expr.Constraint) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint)

Example 9 with Variable

use of org.evosuite.symbolic.expr.Variable in project evosuite by EvoSuite.

the class DSETestGenerator method isRelevant.

/**
 * Returns true iff the constraint has at least one variable that is
 *
 * @param constraint
 * @param targets
 * @return
 */
private boolean isRelevant(Constraint<?> constraint, Set<VariableReference> targets) {
    Set<Variable<?>> variables = constraint.getVariables();
    Set<String> targetNames = new HashSet<String>();
    for (VariableReference v : targets) {
        targetNames.add(v.getName());
    }
    for (Variable<?> var : variables) {
        if (targetNames.contains(var.getName()))
            return true;
    }
    return false;
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) VariableReference(org.evosuite.testcase.variable.VariableReference) HashSet(java.util.HashSet)

Aggregations

Variable (org.evosuite.symbolic.expr.Variable)9 Constraint (org.evosuite.symbolic.expr.Constraint)5 HashSet (java.util.HashSet)4 SolverResult (org.evosuite.symbolic.solver.SolverResult)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SmtCheckSatQuery (org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)3 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)2 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)2 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)2 SmtIntVariable (org.evosuite.symbolic.solver.smt.SmtIntVariable)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 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SolverErrorException (org.evosuite.symbolic.solver.SolverErrorException)1