Skip to content

Commit

Permalink
minor refactors and changes to javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaleotti committed Jan 29, 2025
1 parent 5894609 commit a5f8e4b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.evomaster.client.java.distance.heuristics;

import java.util.Arrays;
import java.util.Objects;

public class TruthnessUtils {

Expand Down Expand Up @@ -30,7 +31,7 @@ public static double normalizeValue(double v) {

/**
* Returns a Truthness instance for comparing two integer values for equality.
*
* <p>
* This method calculates the distance between the two integer values, and creates a Truthness
* instance where the `ofTrue` field is 1 minus the normalized distance, and the `ofFalse` field
* is 1 if the values are not equal, otherwise 0.
Expand All @@ -50,12 +51,12 @@ public static Truthness getEqualityTruthness(int a, int b) {

/**
* Returns a Truthness instance for comparing two long values for equality.
*
* <p>
* This method calculates the distance between the two long values, and creates a Truthness
* instance where the `ofTrue` field is 1 minus the normalized distance, and the `ofFalse` field
* is 1 if the values are not equal, otherwise 0.
*
* @param a an long value
* @param a a long value
* @param b another long value
* @return a Truthness instance representing the equality comparison of the input integer values
*/
Expand All @@ -71,7 +72,7 @@ public static Truthness getEqualityTruthness(long a, long b) {

/**
* Returns a Truthness for comparing if one double value is less than another.
*
* <p>
* This method calculates the branch distance, returning <code>ofTrue</code>
* of 1.0d if the first value is less than the second, and 1.0d / (1.1d + distance)
* otherwise.
Expand All @@ -91,7 +92,7 @@ public static Truthness getLessThanTruthness(double a, double b) {

/**
* Returns a Truthness for comparing if one long value is less than another.
*
* <p>
* This method calculates the branch distance, returning <code>ofTrue</code>
* of 1.0d if the first value is less than the second, and 1.0d / (1.1d + distance)
* otherwise.
Expand All @@ -111,7 +112,7 @@ public static Truthness getLessThanTruthness(long a, long b) {

/**
* Returns a Truthness instance for comparing two double values for equality.
*
* <p>
* This method normalizes the distance between the two double values,
* and creates a Truthness instance where the `ofTrue` field is 1 minus the normalized distance,
* and the `ofFalse` field is 1 if the values are not equal, otherwise 0.
Expand Down Expand Up @@ -149,7 +150,7 @@ public static Truthness getTruthnessToEmpty(int len) {

/**
* Aggregates multiple Truthness instances using an AND operation.
*
* <p>
* This method returns a Truthness instance where the <code>ofTrue</code> field is the average of the `ofTrue`
* values of the input truthnesses, and the <code>ofFalse</code> field is either 1.0d if any of the input Truthness
* instances is false, or average of the `ofFalse` values from the provided Truthness instances if none of the
Expand All @@ -167,7 +168,7 @@ public static Truthness buildAndAggregationTruthness(Truthness... truthnesses) {

/**
* Aggregates multiple Truthness instances using an OR operation.
*
* <p>
* This method returns a Truthness instance where the <code>ofTrue</code> field is either 1.0d if any of the input
* Truthness instances is true, or the average of the `ofTrue` values from the provided Truthness instances if none
* of the given truthnesses is true. The <code>ofFalse</code> field is the average of the `ofFalse` values of the
Expand All @@ -186,7 +187,7 @@ public static Truthness buildOrAggregationTruthness(Truthness... truthnesses) {

/**
* Aggregates two Truthness instances using an XOR operation.
*
* <p>
* This method returns XOR(a,b) as (a AND NOT b) OR (NOT a AND b).
*
* @param left the first Truthness instance
Expand Down Expand Up @@ -219,7 +220,7 @@ private static double averageOfTrue(Truthness... truthnesses) {
* @param truthnesses an array of Truthness instances
*/
private static void checkValidTruthnesses(Truthness[] truthnesses) {
if (truthnesses == null || truthnesses.length == 0 || Arrays.stream(truthnesses).anyMatch(e -> e == null)) {
if (truthnesses == null || truthnesses.length == 0 || Arrays.stream(truthnesses).anyMatch(Objects::isNull)) {
throw new IllegalArgumentException("null or empty Truthness instance");
}
}
Expand All @@ -228,7 +229,7 @@ private static void checkValidTruthnesses(Truthness[] truthnesses) {
* Computes an average of the given values.
* If no values are given, an <code>IllegalArgumentException</code> is thrown.
*
* @param values a non empty list of double values.
* @param values a non-empty list of double values.
* @return
*/
private static double average(double... values) {
Expand Down Expand Up @@ -264,7 +265,7 @@ private static double averageOfFalse(Truthness... truthnesses) {
*/
private static double falseOrAverageFalse(Truthness... truthnesses) {
checkValidTruthnesses(truthnesses);
if (Arrays.stream(truthnesses).anyMatch(t -> t.isFalse())) {
if (Arrays.stream(truthnesses).anyMatch(Truthness::isFalse)) {
return 1.0d;
} else {
return averageOfFalse(truthnesses);
Expand All @@ -280,7 +281,7 @@ private static double falseOrAverageFalse(Truthness... truthnesses) {
*/
private static double trueOrAverageTrue(Truthness... truthnesses) {
checkValidTruthnesses(truthnesses);
if (Arrays.stream(truthnesses).anyMatch(t -> t.isTrue())) {
if (Arrays.stream(truthnesses).anyMatch(Truthness::isTrue)) {
return 1.0d;
} else {
return averageOfTrue(truthnesses);
Expand All @@ -289,7 +290,6 @@ private static double trueOrAverageTrue(Truthness... truthnesses) {

/**
* Builds a scaled Truthness instance.
*
* This method scales the given `ofTrueToScale` value using the provided `base` value
* and creates a Truthness instance where the `ofTrue` field is the scaled value and
* the `ofFalse` field is set to 1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private Truthness evaluateTruthnessForComparisonOperator(Object concreteLeftValu
truthnessOfExpression = calculateTruthnessForInstantComparison(convertToInstant(concreteLeftValue), convertToInstant(concreteRightValue), comparisonOperatorType);
} else if (concreteLeftValue instanceof OffsetTime || concreteRightValue instanceof OffsetTime) {
truthnessOfExpression = calculateTruthnessForInstantComparison(convertToInstant(concreteLeftValue), convertToInstant(concreteLeftValue), comparisonOperatorType);
} else if (concreteLeftValue instanceof Object[] || concreteRightValue instanceof Object[]) {
} else if (concreteLeftValue instanceof Object[] && concreteRightValue instanceof Object[]) {
truthnessOfExpression = calculateTruthnessForArrayComparison((Object[]) concreteLeftValue, (Object[]) concreteRightValue, comparisonOperatorType);
} else {
throw new UnsupportedOperationException("types not supported " + concreteLeftValue.getClass().getName() + " and " + concreteRightValue.getClass().getName());
Expand Down Expand Up @@ -311,7 +311,6 @@ public void visit(SignedExpression signedExpression) {
Object expressionValue = concreteValues.pop();
if (expressionValue == null) {
concreteValues.push(null);
return;
} else {
final Number numberWithoutSign = (Number) expressionValue;
final Number result;
Expand Down Expand Up @@ -767,7 +766,7 @@ public void visit(MySQLGroupConcat mySQLGroupConcat) {
@Override
public void visit(ExpressionList<?> expressionList) {
super.visit(expressionList);
List<Object> list = Stream.generate(() -> concreteValues.pop())
List<Object> list = Stream.generate(concreteValues::pop)
.limit(expressionList.size())
.collect(Collectors.toList());
Collections.reverse(list);
Expand Down Expand Up @@ -886,7 +885,7 @@ private static int toJavaIndex(Number sqlStartIndex) {
@Override
public void visit(ArrayConstructor arrayConstructor) {
super.visit(arrayConstructor);
List<Object> list = Stream.generate(() -> concreteValues.pop())
List<Object> list = Stream.generate(concreteValues::pop)
.limit(arrayConstructor.getExpressions().size())
.collect(Collectors.toList());
Collections.reverse(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class SqlStringUtils {

/**
* Removes enclosing single or double quotes from the input string.
*
* If the input string starts and ends with either single or double quotes, it removes the first and last
* characters of the string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.sql.Connection;
import java.sql.Time;
Expand Down Expand Up @@ -156,7 +154,7 @@ public void testTimestampWithTimeZoneColumn() throws Exception {

@Test
public void testTimeColumn() throws Exception {
// setup the database
// set up the database
SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE example_table (\n" +
" time_column TIME NOT NULL\n" +
");");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ public void testBetweenStrings() {
@Test
public void testBetweenDates() {
String sqlCommand = "SELECT birth_day FROM Persons WHERE birth_day BETWEEN '1990-07-01' AND '1990-07-31'";
QueryResult queryResult = new QueryResult(Arrays.asList("birth_day"), "Persons");
QueryResult queryResult = new QueryResult(Collections.singletonList("birth_day"), "Persons");
java.sql.Date date = java.sql.Date.valueOf("1990-07-15");
queryResult.addRow(Arrays.asList("birth_day"),"Persons",Collections.singletonList(date));
queryResult.addRow(Collections.singletonList("birth_day"),"Persons",Collections.singletonList(date));
SqlDistanceWithMetrics distanceWithMetrics = SqlHeuristicsCalculator.computeDistance(sqlCommand, null, null, queryResult);
double expectedDistance = 0;
assertEquals(expectedDistance, distanceWithMetrics.sqlDistance);
Expand All @@ -824,9 +824,9 @@ public void testBetweenDates() {
@Test
public void testBetweenTimes() {
String sqlCommand = "SELECT start_time FROM Schedules WHERE start_time BETWEEN '09:00:00' AND '17:00:00'";
QueryResult queryResult = new QueryResult(Arrays.asList("start_time"), "Schedules");
QueryResult queryResult = new QueryResult(Collections.singletonList("start_time"), "Schedules");
java.sql.Time time = Time.valueOf("12:30:45");
queryResult.addRow(Arrays.asList("start_time"),"Schedules",Collections.singletonList(time));
queryResult.addRow(Collections.singletonList("start_time"),"Schedules",Collections.singletonList(time));
SqlDistanceWithMetrics distanceWithMetrics = SqlHeuristicsCalculator.computeDistance(sqlCommand, null, null, queryResult);
double expectedDistance = 0;
assertEquals(expectedDistance, distanceWithMetrics.sqlDistance);
Expand All @@ -835,7 +835,7 @@ public void testBetweenTimes() {
@Test
public void testBetweenTimestamps() {
String sqlCommand = "SELECT event_timestamp FROM Events WHERE event_timestamp BETWEEN '2023-01-01 00:00:00' AND '2023-12-31 23:59:59'";
QueryResult queryResult = new QueryResult(Arrays.asList("event_timestamp"), "Events");
QueryResult queryResult = new QueryResult(Collections.singletonList("event_timestamp"), "Events");

String timestampString = "2023-01-14 12:30:45";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Expand Down Expand Up @@ -1144,7 +1144,7 @@ public void testNullSignedExpression() {
@Test
public void testNullBetweenTimestamps() {
String sqlCommand = "SELECT event_timestamp FROM Events WHERE NOT (event_timestamp BETWEEN NULL AND '2023-12-31 23:59:59')";
QueryResult queryResult = new QueryResult(Arrays.asList("event_timestamp"), "Events");
QueryResult queryResult = new QueryResult(Collections.singletonList("event_timestamp"), "Events");

String timestampString = "2023-01-14 12:30:45";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Expand Down

0 comments on commit a5f8e4b

Please sign in to comment.