Skip to content

Commit

Permalink
more complex test with SO dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Aug 2, 2024
1 parent aa582a9 commit efcd424
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ private Set<GPNode> run(Map<GPNode, Set<GPNode>> incomingConflicts) {// returns
Set<N> currentNodeCandidates = candidates.get(currentNode);
Set<GPNode> exclusionConstraints = mutualExclusionConstraints.get(currentNode);
if(exclusionConstraints != null){
filterMutualExclusionConstraints(currentNodeCandidates, exclusionConstraints, Objects.requireNonNullElse(incomingConflicts.get(currentNode), new HashSet<>()));
filterMutualExclusionConstraints(currentNode, currentNodeCandidates, exclusionConstraints, Objects.requireNonNullElse(incomingConflicts.get(currentNode), new HashSet<>()));
}
for(N candidateNode : currentNodeCandidates){
Map<GPNode, Set<N>> newCandidates = new HashMap<>(candidates);
newCandidates.remove(currentNode);
Map<GPNode, Set<N>> newCandidates = candidates
.entrySet()
.stream()
.filter(entry -> !entry.getKey().equals(currentNode))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> new HashSet<>(entry.getValue())));
// Map<GPNode, Set<N>> newCandidates = new HashMap<>(candidates);
// newCandidates.remove(currentNode);
Map<GPNode, N> newAssignments = new HashMap<>(assignments);
newAssignments.put(currentNode, candidateNode);
Map<GPNode, Set<GPNode>> newIncomingConflicts = incomingConflicts// deep copy
Expand All @@ -183,7 +188,7 @@ private Set<GPNode> run(Map<GPNode, Set<GPNode>> incomingConflicts) {// returns
boolean valid = child.forwardChecking(currentNode, newIncomingConflicts, outgoingConflicts);
if(valid){
deadEnd = false;
Set<GPNode> jump = child.run(incomingConflicts);
Set<GPNode> jump = child.run(newIncomingConflicts);// the paper uses incomingConflicts (confIn) here but I think that's just a missing single quote
if(!jump.isEmpty() && !jump.contains(currentNode)){
return jump;
}
Expand Down Expand Up @@ -236,14 +241,20 @@ private GPNode pickNextNode() {
return candidate;
}

private void filterMutualExclusionConstraints(Set<N> candidatesForNode, Set<GPNode> exclusionConstraints, Set<GPNode> incomingConflicts) {
private void filterMutualExclusionConstraints(GPNode currentNode, Set<N> candidatesForNode, Set<GPNode> exclusionConstraints, Set<GPNode> incomingConflicts) {
for(Iterator<N> it = candidatesForNode.iterator(); it.hasNext();){
N graphCandidate = it.next();
for(GPNode exclusionConstraint : exclusionConstraints){
if(graphCandidate.equals(assignments.get(exclusionConstraint))){
incomingConflicts.add(exclusionConstraint);
it.remove();
}
filterMutualExclusionConstraintWithSpecificCandidate(currentNode, exclusionConstraints, incomingConflicts, it);
}

}

private void filterMutualExclusionConstraintWithSpecificCandidate(GPNode currentNode, Set<GPNode> exclusionConstraints, Set<GPNode> incomingConflicts, Iterator<N> it) {
N graphCandidate = it.next();
for(GPNode exclusionConstraint : exclusionConstraints){
if(graphCandidate.equals(assignments.get(exclusionConstraint))){
incomingConflicts.add(exclusionConstraint);
it.remove();
return;
}
}
}
Expand Down Expand Up @@ -272,6 +283,19 @@ private boolean forwardChecking(GPNode currentNode, Map<GPNode, Set<GPNode>> inc
outgoingConflicts.add(otherNode);
return false;
}
}else{
// not in the paper
// It is possible that two candidates for different nodes in the graph pattern exclude each other via a necessary edge
// In that case, one can be assigned without the other being removed
// This also needs to be tested

N currentNodeInGraph = Objects.requireNonNull(assignments.get(currentNode));
List<GPNode> conflictingNodes = checkHasNecessaryEdgesFindConflict(currentNode, otherNode, currentNodeInGraph);
if(!conflictingNodes.isEmpty()){
incomingConflicts.computeIfAbsent(currentNode, n -> new HashSet<>()).addAll(conflictingNodes);
// outgoingConflicts.addAll(conflictingNodes);
return false;
}
}
}

Expand Down Expand Up @@ -304,40 +328,47 @@ private boolean satisfiesRequirements(RelevantEdge currentEdge, E graphEdge, N n
return currentEdge.edge.edgeType().equals(graphEdge.edgeType()) &&
currentEdge.otherNode.nodeType().equals(neighbor.nodeType()) &&
checkAttributeRequirements(pattern.edgeRequirements().get(currentEdge.edge), graphEdge) &&
checkAttributeRequirements(pattern.nodeRequirements().get(currentEdge.otherNode), neighbor) &&
checkHasNecessaryEdges(currentEdge.otherNode, neighbor);
checkAttributeRequirements(pattern.nodeRequirements().get(currentEdge.otherNode), neighbor)
// && checkHasNecessaryEdges(currentEdge.otherNode, null, neighbor)//the paper isn't exactly clear whether this is necessary
;
}

private boolean checkHasNecessaryEdges(GPNode node, N graphNode) {
boolean satisfied = checkNecessaryEdgesOneDirection(
// private boolean checkHasNecessaryEdges(GPNode node, GPNode otherNodeFilter, N graphNode) {
// return checkHasNecessaryEdgesFindConflict(node, otherNodeFilter, graphNode).isEmpty();
// }

private List<GPNode> checkHasNecessaryEdgesFindConflict(GPNode node, GPNode otherNodeFilter, N graphNode) {
List<GPNode> conflictAccumulator = new ArrayList<>();
checkNecessaryEdgesOneDirection(
graph.findOutgoingEdges(graphNode), pattern.graph().outgoingEdges().get(node),
AttributedGraphEdge::target, GPEdge::target
AttributedGraphEdge::target, GPEdge::target,
otherNodeFilter, conflictAccumulator
);
if(!satisfied){
return false;
}

return checkNecessaryEdgesOneDirection(
checkNecessaryEdgesOneDirection(
graph.findIncomingEdges(graphNode), pattern.graph().incomingEdges().get(node),
AttributedGraphEdge::source, GPEdge::source
AttributedGraphEdge::source, GPEdge::source,
otherNodeFilter, conflictAccumulator
);
return conflictAccumulator;
}

private boolean checkNecessaryEdgesOneDirection(Collection<E> neighboringEdges, Collection<GPEdge> patternEdges, Function<E, N> edgeOtherNodeFinder, Function<GPEdge, GPNode> gpOtherNodeFinder) {
private void checkNecessaryEdgesOneDirection(Collection<E> neighboringEdges, Collection<GPEdge> patternEdges, Function<E, N> edgeOtherNodeFinder, Function<GPEdge, GPNode> gpOtherNodeFinder, GPNode otherNodeFilter, List<GPNode> conflictAccumulator) {
for(GPEdge edge : Objects.requireNonNullElse(patternEdges, List.<GPEdge>of())){
boolean isSatisfied = false;
GPNode otherNode = gpOtherNodeFinder.apply(edge);
N otherGraphNode = assignments.get(otherNode);
for(E graphEdge : neighboringEdges){
if(edge.edgeType().equals(graphEdge.edgeType()) && (otherGraphNode == null || edgeOtherNodeFinder.apply(graphEdge).equals(otherGraphNode))){
isSatisfied = true;
if(otherNodeFilter == null || otherNode.equals(otherNodeFilter)){
N otherGraphNode = assignments.get(otherNode);
for(E graphEdge : neighboringEdges){
if(edge.edgeType().equals(graphEdge.edgeType()) && (otherGraphNode == null || edgeOtherNodeFinder.apply(graphEdge).equals(otherGraphNode))){
isSatisfied = true;
}
}
if(!isSatisfied){
conflictAccumulator.add(otherNode);
}
}
if(!isSatisfied){
return false;
}
}
return true;
}

private boolean checkAttributeRequirements(List<AttributeRequirement> requirements, AttributeAware graphElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ public Collection<Neo4jEdge> findIncomingEdges(Neo4jNode node) {

private Collection<Neo4jEdge> findEdges(Neo4jNode node, Direction direction) {
Node internalNode = node.getDBNode();
ResourceIterable<Relationship> relationships = internalNode.getRelationships(direction);
List<Neo4jEdge> edges = new ArrayList<>();
for(Relationship relationship : relationships){
edges.add(new Neo4jEdge(relationship));
try(ResourceIterable<Relationship> relationships = internalNode.getRelationships(direction)){
List<Neo4jEdge> edges = new ArrayList<>();
for(Relationship relationship : relationships){
edges.add(new Neo4jEdge(relationship));
}
return Collections.unmodifiableList(edges);
}
return Collections.unmodifiableList(edges);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public AttributeValue<?> getAttribute(String key) {
case String s -> AttributeValue.attribute(s);
case Boolean b -> AttributeValue.attribute(b);
case Integer l -> AttributeValue.attribute(l);
case Long l -> AttributeValue.attribute((int) (long) l);
default -> throw new UnsupportedOperationException("unknown property type");
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public static synchronized GraphDatabaseService getDatabase() throws IOException
}
boolean databaseExists = Files.exists(DB_DIRECTORY);
DatabaseManagementService databaseManagementService = createManagementService();
if(!databaseExists){
databaseManagementService.shutdown();
loadDB();
databaseManagementService = createManagementService();
}
return createDB(databaseManagementService);
if(!databaseExists){
databaseManagementService.shutdown();
loadDB();
databaseManagementService = createManagementService();
}
return createDB(databaseManagementService);
}

private static DatabaseManagementService createManagementService() {
Expand All @@ -73,6 +73,7 @@ private static GraphDatabaseService createDB(DatabaseManagementService databaseM
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
service.shutdown();
}));
Neo4JSetup.graphDb = graphDb;
return graphDb;
}

Expand All @@ -83,22 +84,6 @@ private static void doSomething(GraphDatabaseService graphDb) {
tx.getAllRelationshipTypes().forEach(System.out::println);
System.out.println(tx.findNodes(Label.label("User")).next().getAllProperties());
System.out.println(tx.findNode(Label.label("User"), "uuid", 6309).getElementId());
// Node firstNode = tx.createNode();



// System.out.println(firstNode.getElementId());
// firstNode.setProperty("message", "Hello, ");
// Node secondNode = tx.createNode();
// secondNode.setProperty("message", "World!");
//
// Relationship relationship = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS);
// relationship.setProperty("message", "brave Neo4j ");
//
// System.out.print(firstNode.getProperty("message"));
// System.out.print(relationship.getProperty("message"));
// System.out.print(secondNode.getProperty("message"));
// tx.commit();
}
}

Expand All @@ -108,9 +93,6 @@ private static void loadDB() throws IOException, IncorrectFormat, InterruptedExc

DatabaseLayout layout = DatabaseLayout.of(Neo4jLayout.of(DB_DIRECTORY), DB_NAME);
deleteRecursively(layout.databaseDirectory());
// Path databasesDirectory = Path.of("db/data/databases");
// Files.createDirectories(databasesDirectory);
// DatabaseLayout layout = DatabaseLayout.ofFlat(databasesDirectory.resolve("neo4j"));
HttpResponse<InputStream> res = HttpClient
.newBuilder()
.followRedirects(Redirect.ALWAYS)
Expand All @@ -130,7 +112,7 @@ private static void loadDB() throws IOException, IncorrectFormat, InterruptedExc
os.flush();
loader.load(download, layout, true, false, DumpFormatSelector::decompress);
}finally{
// Files.delete(download);
Files.delete(download);
}
}

Expand Down
Loading

0 comments on commit efcd424

Please sign in to comment.