-
Notifications
You must be signed in to change notification settings - Fork 115
CQL Heuristics Calculator #1620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gonzalotguerrero
wants to merge
14
commits into
master
Choose a base branch
from
feature/cql-heuristics-calculator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
76bb1fb
Merge branch 'master' into feature/cql-heuristics-calculator
gonzalotguerrero 92aa301
First calculator draft
gonzalotguerrero 56c806a
Merge branch 'master' into feature/cql-heuristics-calculator
gonzalotguerrero c80e69d
Merge branch 'feature/cql-query-parser' into feature/cql-heuristics-c…
gonzalotguerrero 6b6ad91
Fix comparison of time-related values
gonzalotguerrero 059e9a5
Improve test class coverage
gonzalotguerrero 8455eb6
Add support for integer constants in time-related types
gonzalotguerrero b6e870b
Merge branch 'master' into feature/cql-heuristics-calculator
gonzalotguerrero a796494
Created CassandraOperationEvaluator
gonzalotguerrero bcf43e2
Minor refactorings
gonzalotguerrero d28282c
Merge branch 'master' into feature/cql-heuristics-calculator
gonzalotguerrero 288be95
PR review comments
gonzalotguerrero 8a566ee
Improve CqlParserUtils
gonzalotguerrero c9028dd
Merge branch 'master' into feature/cql-heuristics-calculator
gonzalotguerrero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
.../evomaster/client/java/controller/cassandra/calculator/CassandraHeuristicsCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package org.evomaster.client.java.controller.cassandra.calculator; | ||
|
|
||
| import org.evomaster.client.java.controller.cassandra.model.CassandraRow; | ||
| import org.evomaster.client.java.controller.cassandra.operations.CqlQueryOperation; | ||
| import org.evomaster.client.java.controller.cassandra.parser.CqlParserUtils; | ||
| import org.evomaster.client.java.distance.heuristics.DistanceHelper; | ||
| import org.evomaster.client.java.distance.heuristics.Truthness; | ||
| import org.evomaster.client.java.distance.heuristics.TruthnessUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Calculator designed to compute a branch-distance-based heuristic for a CQL (Cassandra Query Language) query, | ||
| * given the rows returned by executing that query against the database. | ||
| * <p> | ||
| * The distance reflects how far the query's WHERE condition is from being satisfied by | ||
| * the available data and is used to guide the search towards inserting data in the database | ||
| * that makes the executed queries return non-empty results during the execution of generated tests. | ||
| */ | ||
| public class CassandraHeuristicsCalculator { | ||
|
|
||
| public static final double C = DistanceHelper.H_NOT_NULL; | ||
| public static final double C_BETTER = 0.15; | ||
|
|
||
| public static final Truthness TRUE_TRUTHNESS = new Truthness(1.0, C); | ||
| public static final Truthness FALSE_TRUTHNESS = new Truthness(C, 1.0); | ||
| public static final Truthness FALSE_TRUTHNESS_BETTER = new Truthness(C_BETTER, 1.0); | ||
|
|
||
| private final CassandraOperationEvaluator evaluator = new CassandraOperationEvaluator(); | ||
|
|
||
| /** | ||
| * Computes the heuristic distance of the given CQL query with respect to the provided rows. | ||
| * A distance of {@code 0.0} means the query is satisfied, while values closer | ||
| * to {@code 1.0} indicate the query is further from being satisfied. | ||
| * | ||
| * @param cqlQuery the CQL query to evaluate | ||
| * @param returnedRows all rows in the table the query targets, used to evaluate how close | ||
| * the query's condition is to matching at least one row | ||
| * @return a distance value in {@code [0.0, 1.0]}, where {@code 0.0} represents the best case | ||
| */ | ||
| public double computeDistance(String cqlQuery, Iterable<CassandraRow> returnedRows) { | ||
| return 1.0 - computeHQuery(cqlQuery, returnedRows).getOfTrue(); | ||
| } | ||
|
|
||
| private Truthness computeHQuery(String cqlQuery, Iterable<CassandraRow> returnedRows) { | ||
| if (!CqlParserUtils.canParseCqlCommand(cqlQuery)) { | ||
| return FALSE_TRUTHNESS; | ||
| } | ||
|
|
||
| List<CassandraRow> rows = toList(returnedRows); | ||
|
|
||
| org.evomaster.client.java.controller.cassandra.parser.CqlParser.RootContext root = | ||
| CqlParserUtils.parseCqlCommand(cqlQuery); | ||
| CqlQueryOperation whereOp = CqlParserUtils.getWhereOperation(root); | ||
|
|
||
| Truthness hRowSet = computeHRowSet(rows); | ||
|
|
||
| if (whereOp == null) { | ||
| return hRowSet; | ||
| } | ||
|
|
||
| Truthness hCondition = computeHCondition(whereOp, rows); | ||
| return TruthnessUtils.buildAndAggregationTruthness(hRowSet, hCondition); | ||
| } | ||
|
|
||
| private Truthness computeHRowSet(List<CassandraRow> rows) { | ||
| return TruthnessUtils.getTruthnessToEmpty(rows.size()).invert(); | ||
| } | ||
|
|
||
| private Truthness computeHCondition(CqlQueryOperation condition, | ||
| List<CassandraRow> rows) { | ||
| if (rows.isEmpty()) { | ||
| return FALSE_TRUTHNESS; | ||
| } | ||
|
|
||
| double maxOfTrue = 0.0; | ||
| for (CassandraRow row : rows) { | ||
| double ofTrue = evaluator.evaluate(condition, row).getOfTrue(); | ||
| if (ofTrue >= 1.0) { | ||
| return TRUE_TRUTHNESS; | ||
| } else if (ofTrue > maxOfTrue) { | ||
| maxOfTrue = ofTrue; | ||
| } | ||
| } | ||
|
|
||
| return TruthnessUtils.buildScaledTruthness(C, maxOfTrue); | ||
| } | ||
|
|
||
| private static List<CassandraRow> toList(Iterable<CassandraRow> iterable) { | ||
| if (iterable instanceof List) { | ||
| return (List<CassandraRow>) iterable; | ||
| } | ||
|
|
||
| List<CassandraRow> list = new ArrayList<>(); | ||
| for (CassandraRow row : iterable) { | ||
| list.add(row); | ||
| } | ||
| return list; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Javadoc