Search in sources :

Example 1 with DistanceCalculator

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

the class DistanceEstimator method getDistance.

/**
 * <p>
 * getDistance
 * </p>
 *
 * @param constraints
 *            a {@link java.util.Collection} object.
 * @return normalized distance in [0,1]
 */
public static double getDistance(Collection<Constraint<?>> constraints) {
    double result = 0;
    DistanceCalculator distanceCalculator = new DistanceCalculator();
    try {
        for (Constraint<?> c : constraints) {
            if (c instanceof StringConstraint) {
                StringConstraint string_constraint = (StringConstraint) c;
                try {
                    double strD = (double) string_constraint.accept(distanceCalculator, null);
                    result += normalize(strD);
                    log.debug("S: " + string_constraint + " strDist " + strD);
                } catch (Throwable t) {
                    log.debug("S: " + string_constraint + " strDist " + t);
                    result += 1.0;
                }
            } else if (c instanceof IntegerConstraint) {
                IntegerConstraint integer_constraint = (IntegerConstraint) c;
                long intD = (long) integer_constraint.accept(distanceCalculator, null);
                result += normalize(intD);
                log.debug("C: " + integer_constraint + " intDist " + intD);
            } else if (c instanceof RealConstraint) {
                RealConstraint real_constraint = (RealConstraint) c;
                double realD = (double) real_constraint.accept(distanceCalculator, null);
                result += normalize(realD);
                log.debug("C: " + real_constraint + " realDist " + realD);
            } else {
                throw new IllegalArgumentException("DistanceCalculator: got an unknown constraint: " + c);
            }
        }
        log.debug("Resulting distance: " + result);
        return Math.abs(result);
    } catch (Exception e) {
        return Double.MAX_VALUE;
    }
}
Also used : DistanceCalculator(org.evosuite.symbolic.expr.DistanceCalculator) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) RealConstraint(org.evosuite.symbolic.expr.RealConstraint)

Aggregations

DistanceCalculator (org.evosuite.symbolic.expr.DistanceCalculator)1 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)1 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)1 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)1