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,63 @@
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 {

private final String id;
private final String type;
private final String sourceId;
private final String targetId;
private final Map<String, Object> properties;

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);
}

public boolean hasProperty(String key) {
return properties.containsKey(key);
}

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,49 @@
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 {

private final List<Neo4jNode> nodes;
private final List<Neo4jEdge> edges;
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,59 @@
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 {

private final String id;
private final Set<String> labels;
private final Map<String, Object> properties;

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);
}

public Object getProperty(String key) {
return properties.get(key);
}

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