Skip to content

add Neo4j heuristics calculator#1647

Open
andyfelder16 wants to merge 2 commits into
masterfrom
neo4j-heuristics-core
Open

add Neo4j heuristics calculator#1647
andyfelder16 wants to merge 2 commits into
masterfrom
neo4j-heuristics-core

Conversation

@andyfelder16

@andyfelder16 andyfelder16 commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

First of a few PRs splitting #1641, per review feedback.

  • Computes H(Q,G): how close a graph is to satisfying a parsed Cypher MATCH query, as a Truthness <ofTrue, ofFalse>
  • Neo4jHeuristicsCalculator, Neo4jStructuralMatcher (matched_elements), Neo4jConditionEvaluator (condition + operand evaluation), Neo4jMapping
  • Neo4jNode/Neo4jEdge/Neo4jGraph: in-memory graph model
  • Works in Truthness end-to-end, mirroring the SQL heuristics calculator
  • 11 tests

@andyfelder16
andyfelder16 requested a review from jgaleotti July 21, 2026 14:16
Truthness inner = evaluateCondition(((NotCondition) condition).getCondition(), mapping);
return inner == null ? null : inner.invert();
}
return null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of null shouldn't this throw an exception signalling that a case was not supported?

* Returns {@code ρ(condition, mapping)}, or {@code null} when the condition cannot be evaluated
* and must be skipped by the aggregation.
*/
Truthness evaluateCondition(CypherCondition condition, Neo4jMapping mapping) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you considered applying a visitor pattern to CypherCondition AST ?

Double da = asDouble(a);
Double db = asDouble(b);
if (da == null || db == null) {
return null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is null a valid input for this method?


private Truthness equalityTruthness(Object a, Object b) {
if (a == null || b == null) {
return null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if two references are null? what is the Neo4J semantics for this?

*/
static Truthness stringEqualityTruthness(String a, String b) {
if (a == null || b == null) {
return null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is a and b nullable? what is the semantics?

}

static Truthness getStartsWith(String str, String prefix) {
if (str.startsWith(prefix)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Objects.requireNonNull(...) whenever the arguments are intended not to be null.

* {@code [0,1]}, where 0 means the query is satisfied.
*/
public double computeDistance(MatchOperation query, Neo4jGraph graph) {
return 1.0d - computeHeuristic(query, graph).getOfTrue();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a variable to store computeHeuristic(query, graph)

* Count-based node availability: enough graph nodes to bind the pattern's nodes. Pure cardinality,
* no label/property check (those are conditions evaluated by H_where).
*/
Truthness computeHeuristicMatchNodes(int required, int available) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can values be negative? Add checks with IllegalArgumentException to signal invalid inputs.

return new Neo4jMapping(nodeBindings, edgeBindings);
}

Neo4jNode getNode(String variable) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Objects.requireNonNull(...) if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants