Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public List<CypherCondition> getConditions() {
return conditions;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitAnd(this);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public String getVariableName() {
return variableName;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitAnyLabel(this);
}

@Override
public String toString() {
return variableName + ":%";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public Operand getRight() {
return right;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitComparison(this);
}

@Override
public String toString() {
if (right == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
* - Logical operators: AND, OR, NOT
*/
public interface CypherCondition {

/**
* Dispatches to the {@code visit} method of {@code visitor} matching this condition's concrete
* type (double dispatch), returning whatever the visitor produces for it.
*/
<T> T accept(CypherConditionVisitor<T> visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.evomaster.client.java.controller.neo4j.conditions;

/**
* Visitor over the typed {@link CypherCondition} boolean tree the parser produces. Each concrete
* condition dispatches to its own {@code visit} method through {@link CypherCondition#accept}, so a
* consumer handles every case explicitly and the compiler flags any implementation that forgets one
* (no {@code instanceof} chain, no silent fall-through for an unhandled condition type).
*
* @param <T> the result type each visit produces (e.g. a {@code Truthness} for the heuristics).
*/
public interface CypherConditionVisitor<T> {

T visitLabel(LabelCondition condition);

T visitAnyLabel(AnyLabelCondition condition);

T visitType(TypeCondition condition);

T visitProperty(PropertyCondition condition);

T visitComparison(ComparisonCondition condition);

T visitAnd(AndCondition condition);

T visitOr(OrCondition condition);

T visitXor(XorCondition condition);

T visitNot(NotCondition condition);

T visitRaw(RawCondition condition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public String getLabel() {
return label;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitLabel(this);
}

@Override
public String toString() {
return variableName + ":" + label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public CypherCondition getCondition() {
return condition;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitNot(this);
}

@Override
public String toString() {
return "NOT " + condition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public List<CypherCondition> getConditions() {
return conditions;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitOr(this);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public Operand getValue() {
return value;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitProperty(this);
}

@Override
public String toString() {
return variableName + "." + propertyKey + " = " + value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public String getExpression() {
return expression;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitRaw(this);
}

@Override
public String toString() {
return expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public String getType() {
return type;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitType(this);
}

@Override
public String toString() {
return "type(" + variableName + ") = " + type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public List<CypherCondition> getConditions() {
return conditions;
}

@Override
public <T> T accept(CypherConditionVisitor<T> visitor) {
return visitor.visitXor(this);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("(");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.evomaster.client.java.controller.neo4j.data;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
* An edge (relationship) of a captured Neo4j graph: a stable id, a single type, the ids of its two
* endpoint nodes (source and target), and its property map. A relationship in Neo4j is always stored
* with a direction (source → target).
*/
public class Neo4jEdge {

/**
* Stable identifier of this relationship, as reported by the driver ({@code elementId}). Unique
* within the captured graph, and used to tell two relationships apart when enumerating mappings.
*/
private final String id;

/**
* The relationship type, e.g. {@code KNOWS}. Neo4j gives a relationship exactly one type (unlike a
* node, which can carry several labels), so this is a single value and never {@code null}.
*/
private final String type;

/** Id of the node this relationship starts from, matching some {@link Neo4jNode#getId()}. */
private final String sourceId;

/** Id of the node this relationship points to, matching some {@link Neo4jNode#getId()}. */
private final String targetId;

/**
* The relationship's properties. Keys are property names as stored in Neo4j (e.g. {@code since});
* values are the corresponding property values, already converted to plain Java types by the graph
* reader ({@code String}, {@code Long}, {@code Double}, {@code Boolean}, ...). A key that is absent
* means the property is not set, which is distinct from a key present with a {@code null} value.
*/
private final Map<String, Object> properties;
Comment thread
andyfelder16 marked this conversation as resolved.

public Neo4jEdge(String id, String type, String sourceId, String targetId,
Map<String, Object> properties) {
this.id = Objects.requireNonNull(id, "id must not be null");
this.type = Objects.requireNonNull(type, "type must not be null");
this.sourceId = Objects.requireNonNull(sourceId, "sourceId must not be null");
this.targetId = Objects.requireNonNull(targetId, "targetId must not be null");
this.properties = properties != null ? new LinkedHashMap<>(properties) : new LinkedHashMap<>();
}

public String getId() {
return id;
}

public String getType() {
return type;
}

public String getSourceId() {
return sourceId;
}

public String getTargetId() {
return targetId;
}

public Map<String, Object> getProperties() {
return Collections.unmodifiableMap(properties);
}

/**
* Returns true when the property is present on this relationship. Distinguishes an absent property
* (the operand cannot be valuated) from a present property whose value is {@code null}.
*/
public boolean hasProperty(String key) {
return properties.containsKey(key);
}

/**
* The value of the given property, or {@code null} if it is absent <em>or</em> present with a
* {@code null} value. Use {@link #hasProperty(String)} to tell those two apart.
*/
public Object getProperty(String key) {
return properties.get(key);
}

@Override
public String toString() {
return "Neo4jEdge{" + id + ", type=" + type + ", " + sourceId + "->" + targetId
+ ", props=" + properties + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.evomaster.client.java.controller.neo4j.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* An in-memory snapshot of a Neo4j graph ({@code G}): the set of nodes and relationships against
* which a parsed query is scored. Built either by hand in tests or by reading the live database
* through the driver. Nodes are indexed by id so a relationship's endpoints can be resolved quickly.
*/
public class Neo4jGraph {

/** All nodes of the snapshot, in the order they were read. */
private final List<Neo4jNode> nodes;

/** All relationships of the snapshot, in the order they were read. */
private final List<Neo4jEdge> edges;

/**
* Index over {@link #nodes} for endpoint resolution. Keys are node ids ({@link Neo4jNode#getId()});
* values are the node carrying that id. Lets a relationship's {@code sourceId}/{@code targetId} be
* resolved in constant time while enumerating mappings, instead of scanning the node list.
*/
private final Map<String, Neo4jNode> nodesById;

public Neo4jGraph(List<Neo4jNode> nodes, List<Neo4jEdge> edges) {
this.nodes = nodes != null ? new ArrayList<>(nodes) : new ArrayList<>();
this.edges = edges != null ? new ArrayList<>(edges) : new ArrayList<>();
this.nodesById = new LinkedHashMap<>();
for (Neo4jNode n : this.nodes) {
nodesById.put(n.getId(), n);
}
}

public List<Neo4jNode> getNodes() {
return Collections.unmodifiableList(nodes);
}

public List<Neo4jEdge> getEdges() {
return Collections.unmodifiableList(edges);
}

public int nodeCount() {
return nodes.size();
}

public Neo4jNode getNodeById(String id) {
return nodesById.get(id);
}

@Override
public String toString() {
return "Neo4jGraph{nodes=" + nodes.size() + ", edges=" + edges.size() + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.evomaster.client.java.controller.neo4j.data;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* A node of a captured Neo4j graph, used as the {@code G} side when computing the heuristic
* {@code H(Q, G)}. It is the in-memory counterpart of a stored node: a stable id (the driver's
* {@code elementId}), the set of labels, and the property map.
* <p>
* This is a plain data holder built either by hand (tests) or by reading the live database; it has
* no relation to {@link org.evomaster.client.java.controller.neo4j.operations.PatternNode}, which is
* the structural node of a parsed query pattern.
*/
public class Neo4jNode {

/**
* Stable identifier of this node, as reported by the driver ({@code elementId}). Unique within the
* captured graph, and used to tell two nodes apart when enumerating mappings.
*/
private final String id;

/**
* The labels attached to this node, e.g. {@code {Person, Employee}}. A node may carry zero, one or
* many labels, which is why this is a set rather than a single value. Iteration order follows the
* order the labels were read, so scoring is deterministic.
*/
private final Set<String> labels;

/**
* The node's properties. Keys are property names as stored in Neo4j (e.g. {@code age}); values are
* the corresponding property values, already converted to plain Java types by the graph reader
* ({@code String}, {@code Long}, {@code Double}, {@code Boolean}, ...). A key that is absent means
* the property is not set, which is distinct from a key present with a {@code null} value.
*/
private final Map<String, Object> properties;
Comment thread
andyfelder16 marked this conversation as resolved.

public Neo4jNode(String id, Set<String> labels, Map<String, Object> properties) {
this.id = Objects.requireNonNull(id, "id must not be null");
this.labels = labels != null ? new LinkedHashSet<>(labels) : new LinkedHashSet<>();
this.properties = properties != null ? new LinkedHashMap<>(properties) : new LinkedHashMap<>();
}

public String getId() {
return id;
}

public Set<String> getLabels() {
return Collections.unmodifiableSet(labels);
}

public Map<String, Object> getProperties() {
return Collections.unmodifiableMap(properties);
}

/**
* Returns true when the property is present on this node. Distinguishes an absent property
* (the operand cannot be valuated) from a present property whose value is {@code null}.
*/
public boolean hasProperty(String key) {
return properties.containsKey(key);
}

/**
* The value of the given property, or {@code null} if it is absent <em>or</em> present with a
* {@code null} value. Use {@link #hasProperty(String)} to tell those two apart.
*/
public Object getProperty(String key) {
return properties.get(key);
}

@Override
public String toString() {
return "Neo4jNode{" + id + ", labels=" + labels + ", props=" + properties + "}";
}
}
Loading
Loading