Skip to content
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

Simplify graph #1380

Merged
merged 7 commits into from
Jan 15, 2024
Merged
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 @@ -181,9 +181,7 @@ private Model findRawModel(Path from, Path p) {

private boolean addEdge(Path from, Path p, DefaultModelProblemCollector problems) {
try {
synchronized (dag) {
dag.addEdge(dag.addVertex(from.toString()), dag.addVertex(p.toString()));
}
dag.addEdge(from.toString(), p.toString());
return true;
} catch (Graph.CycleDetectedException e) {
problems.add(new DefaultModelProblem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,108 +28,52 @@
import java.util.Map;

class Graph {
private enum DfsState {
VISITING,
VISITED
}

final Map<String, Vertex> vertices = new LinkedHashMap<>();

public Vertex getVertex(String id) {
return vertices.get(id);
}

public Collection<Vertex> getVertices() {
return vertices.values();
}

Vertex addVertex(String label) {
return vertices.computeIfAbsent(label, Vertex::new);
}
final Map<String, List<String>> graph = new LinkedHashMap<>();

void addEdge(Vertex from, Vertex to) throws CycleDetectedException {
from.children.add(to);
to.parents.add(from);
List<String> cycle = findCycle(to);
synchronized void addEdge(String from, String to) throws CycleDetectedException {
graph.computeIfAbsent(from, l -> new ArrayList<>()).add(to);
List<String> cycle = visitCycle(graph, Collections.singleton(to), new HashMap<>(), new LinkedList<>());
if (cycle != null) {
// remove edge which introduced cycle
removeEdge(from, to);
throw new CycleDetectedException(
"Edge between '" + from.label + "' and '" + to.label + "' introduces to cycle in the graph", cycle);
"Edge between '" + from + "' and '" + to + "' introduces to cycle in the graph", cycle);
}
}

void removeEdge(Vertex from, Vertex to) {
from.children.remove(to);
to.parents.remove(from);
}

List<String> visitAll() {
return visitAll(vertices.values(), new HashMap<>(), new ArrayList<>());
}

List<String> findCycle(Vertex vertex) {
return visitCycle(Collections.singleton(vertex), new HashMap<>(), new LinkedList<>());
}

private static List<String> visitAll(
Collection<Vertex> children, Map<Vertex, DfsState> stateMap, List<String> list) {
for (Vertex v : children) {
DfsState state = stateMap.putIfAbsent(v, DfsState.VISITING);
if (state == null) {
visitAll(v.children, stateMap, list);
stateMap.put(v, DfsState.VISITED);
list.add(v.label);
}
}
return list;
private enum DfsState {
VISITING,
VISITED
}

private static List<String> visitCycle(
Collection<Vertex> children, Map<Vertex, DfsState> stateMap, LinkedList<String> cycle) {
for (Vertex v : children) {
DfsState state = stateMap.putIfAbsent(v, DfsState.VISITING);
if (state == null) {
cycle.addLast(v.label);
List<String> ret = visitCycle(v.children, stateMap, cycle);
if (ret != null) {
Map<String, List<String>> graph,
Collection<String> children,
Map<String, DfsState> stateMap,
LinkedList<String> cycle) {
if (children != null) {
for (String v : children) {
DfsState state = stateMap.putIfAbsent(v, DfsState.VISITING);
if (state == null) {
cycle.addLast(v);
List<String> ret = visitCycle(graph, graph.get(v), stateMap, cycle);
if (ret != null) {
return ret;
}
cycle.removeLast();
stateMap.put(v, DfsState.VISITED);
} else if (state == DfsState.VISITING) {
// we are already visiting this vertex, this mean we have a cycle
int pos = cycle.lastIndexOf(v);
List<String> ret = cycle.subList(pos, cycle.size());
ret.add(v);
return ret;
}
cycle.removeLast();
stateMap.put(v, DfsState.VISITED);
} else if (state == DfsState.VISITING) {
// we are already visiting this vertex, this mean we have a cycle
int pos = cycle.lastIndexOf(v.label);
List<String> ret = cycle.subList(pos, cycle.size());
ret.add(v.label);
return ret;
}
}
return null;
}

static class Vertex {
final String label;
final List<Vertex> children = new ArrayList<>();
final List<Vertex> parents = new ArrayList<>();

Vertex(String label) {
this.label = label;
}

String getLabel() {
return label;
}

List<Vertex> getChildren() {
return children;
}

List<Vertex> getParents() {
return parents;
}
}

public static class CycleDetectedException extends Exception {
private final List<String> cycle;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.building;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class GraphTest {

@Test
void testCycle() throws Graph.CycleDetectedException {
Graph graph = new Graph();
graph.addEdge("a1", "a2");
assertThrows(Graph.CycleDetectedException.class, () -> graph.addEdge("a2", "a1"));
}

@Test
public void testPerf() throws IOException {
List<String[]> data = new ArrayList<>();
String k = null;
for (String line : Files.readAllLines(Paths.get("src/test/resources/dag.txt"))) {
if (line.startsWith("\t")) {
data.add(new String[] {k, line.trim()});
} else {
k = line;
}
}
Collections.shuffle(data);

long t0 = System.nanoTime();
Graph g = new Graph();
data.parallelStream().forEach(s -> {
try {
g.addEdge(s[0], s[1]);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
long t1 = System.nanoTime();
}
}
Loading