Skip to content
Permalink
Browse files
Adding graph component
  • Loading branch information
effrafax committed Nov 12, 2019
1 parent 91e0eea commit d9378f8a774e6b60257645968041d4fa4ddfc890
Showing 17 changed files with 1,789 additions and 0 deletions.
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>archiva-components</artifactId>
<groupId>org.apache.archiva.components</groupId>
<version>3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>archiva-components-graph</artifactId>
<name>Archiva Components :: Graph</name>
<description>Simple implementation of graph structure and utility class for traversal.</description>


<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>


</project>
@@ -0,0 +1,80 @@
package org.apache.archiva.components.graph.api;
/*
* 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.
*/

/**
* The edge links a source vertex to a destination vertex.
* A edge instance is always part of a certain graph instance.
*
* @param <V> The vertex implementation.
*/
public interface Edge<V extends Vertex<V>> {

/**
* Returns the identifier of this edge. The id must be unique for given graph.
*
* @return the identifier of this edge
*/
String getId();

/**
* Returns the label of this edge. The label must not be unique.
* If the label was not set, it should return an empty string.
* @return the label of this edge, or a empty string
*/
String getLabel();

/**
* Sets the label of this edge to the given string.
* @param label the label string, that must not be <code>null</code>
* @throws NullPointerException if the label parameter is <code>null</code>
*/
void setLabel(String label) throws NullPointerException;

/**
* Returns the graph where this edge was created.
*
* @return the graph instance
*/
Graph<V> getGraph();

/**
* Returns the vertex, that is on the source end of this edge.
* @return the source vertex
*/
V getSource();

/**
* Returns the vertex, that is on the destination end of this edge.
* @return the destination vertex
*/
V getDestination();

/**
* Returns the weight of this edge. For standard graph implementations the default should be 1.0, but
* graph implementations may decide to set another default.
*
* @return the weight of this edge
*/
double getWeight();




}
@@ -0,0 +1,50 @@
package org.apache.archiva.components.graph.api;
/*
* 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.
*/

import org.apache.archiva.components.graph.base.SimpleVertex;

import java.util.Set;

/**
*
* A graph is the container for vertices. The vertices are connected by edges. Each vertex and
* edge instance is coupled to the graph where it was created.
*
* @param <V>
*/
public interface Graph<V extends Vertex<V>> {

V addVertex(String label);

void removeVertex(V vertex);

Edge<V> addEdge(String label, V inVertex, V outVertex);

void removeEdge(Edge<SimpleVertex> edge);

V getVertex(String id);

Edge<V> getEdge(String id);

Set<V> getVertices();



}
@@ -0,0 +1,107 @@
package org.apache.archiva.components.graph.api;
/*
* 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.
*/

import java.util.ArrayList;
import java.util.List;

/**
* This class gives information about the status of the graph traversal.
* It holds the errors encountered and information about detected cycles.
*
* @param <V>
*/
public class TraversalStatus<V extends Vertex<V>> {

private int cycles = 0;
private List<VisitError<V>> errorList;
private List<Vertex<V>> cycleVertices;

public TraversalStatus() {

}

/**
* Returns the list of errors
* @return a list of errors encountered while executing the consumer function on vertices
*/
public List<VisitError<V>> getErrorList() {
return errorList;
}

/**
* Returns true, if there were errors while running the consumer function.
* @return true, if errors occured on the consumer function, otherwise false
*/
public boolean hasErrors() {
return errorList!=null && errorList.size()>0;
}

/**
* Add the given error to the list.
*
* @param vertex the vertex, where the error occured
* @param e the exception
*/
public void addError(V vertex, Throwable e) {
if (errorList==null) {
errorList = new ArrayList<>();
}
errorList.add(new VisitError(vertex, e));
}

/**
* Add another cycle to the counter
* @param vertex
*/
public void registerCycle(Vertex<V> vertex) {
cycles++;
if (cycleVertices==null) {
cycleVertices = new ArrayList<>();
}
cycleVertices.add(vertex);
}

/**
* Returns true, if the traversal encountered a cycle.
* @return true, if cycle was encountered, otherwise false
*/
public boolean hasCycles() {
return cycles>0;
}

/**
* Returns the number of detected cycles.
*
* @return
*/
public int getCycleCount() {
return cycles;
}

/**
* Returns the vertices, where cycles were detected. The list may contain
* duplicated entries, if cycles where detected on different paths.
*
* @return the list of vertices where cycles were detected
*/
public List<Vertex<V>> getCycleVertices() {
return cycleVertices;
}
}
@@ -0,0 +1,70 @@
package org.apache.archiva.components.graph.api;
/*
* 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.
*/

import java.util.List;

/**
* The vertex is a node in a graph structure. The vertex may have connections to and from
* other vertices (edges).
*
* @param <V> The vertex implementation
*/
public interface Vertex<V extends Vertex<V>> {

/**
* Returns the identifier of this vertex. The identifier is unique for a given graph.
* @return the identifier
*/
String getId();

/**
* Returns the label of this vertex. The label is a name and must not be unique.
* If the label was not set it should return a empty string.
* @return the label of the vertex or a empty string
*/
String getLabel();

/**
* Sets the label of the vertex
* @param label sets the label string, must not be <code>null</code>
* @throws NullPointerException if the given label is <code>null</code>
*/
void setLabel(String label) throws NullPointerException;

/**
* Returns the graph where this vertex was created.
* @return the graph instance
*/
Graph<V> getGraph();

/**
* Returns all edges that connect to other nodes
* @return the edges where this vertex is the source
*/
List<Edge<V>> getOutEdges();

/**
* Returns all edges that connect other nodes to this vertex
* @return the edges where this vertex is the destination
*/
List<Edge<V>> getInEdges();


}
@@ -0,0 +1,38 @@
package org.apache.archiva.components.graph.api;
/*
* 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.
*/

public class VisitError<V extends Vertex<V>> {

private final Throwable exception;
private final V vertex;

public VisitError(V vertex, Throwable exception) {
this.exception = exception;
this.vertex = vertex;
}

public Throwable getException() {
return exception;
}

public V getVertex() {
return vertex;
}
}

0 comments on commit d9378f8

Please sign in to comment.