Skip to content
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 @@ -12,6 +12,11 @@
*/
package org.coreasm.network.plugins.graph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

import org.jgrapht.Graph;

import org.coreasm.engine.absstorage.Element;
Expand Down Expand Up @@ -55,7 +60,53 @@ public String getBackground() {

@Override
public String toString() {
return getGraph().toString();
Graph<Element, Element> graph = getGraph();
return toStringFromSets(graph, graph.vertexSet(), graph.edgeSet(), isDirected());
}

/**
* Returns a string of the parenthesized pair (V, E) representing this G=(V,E) graph. 'V' is the
* string representation of the vertex set, and 'E' is the string representation of the edge
* set. The vertex and edge order is sorted by their string representations.
*
* @param vertexSet the vertex set V to be printed
* @param edgeSet the edge set E to be printed
* @param directed true to use parens for each edge (representing directed); false to use curly
* braces (representing undirected)
*
* @return a string representation of (V,E)
*/
protected String toStringFromSets(Graph<Element, Element> graph,
Collection<? extends Element> vertexSet, Collection<? extends Element> edgeSet, boolean directed) {
List<Element> sortedVertices = new ArrayList<>(vertexSet);
sortedVertices.sort(Comparator.comparing(String::valueOf));

List<Element> sortedEdges = new ArrayList<>(edgeSet);
sortedEdges.sort(Comparator.comparing(String::valueOf));

List<String> renderedEdges = new ArrayList<>();
for (Element e : sortedEdges) {
StringBuilder sb = new StringBuilder();
sb.append(e);
sb.append("=");
if (directed) {
sb.append("(");
} else {
sb.append("{");
}
sb.append(graph.getEdgeSource(e));
sb.append(",");
sb.append(graph.getEdgeTarget(e));
if (directed) {
sb.append(")");
} else {
sb.append("}");
}

renderedEdges.add(sb.toString());
}

return "(" + sortedVertices + ", " + renderedEdges + ")";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function g : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g: ([a, b, c, x], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:x)=(c,x)])"
* @require "g: ([a, b, c, x], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:x)=(c,x)])"
* @require "dijkstraShortestPath(g, a, c): [(a:c)]"
* @require "dijkstraShortestPath(g, a, x): [(a:c), (c:x)]"
* @require "dijkstraShortestPath(g, b, x): [(b:c), (c:x)]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function g : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g: ([a, b, c, x, y], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (x:y)=(x,y)])"
* @require "g: ([a, b, c, x, y], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (x:y)=(x,y)])"
* @require "connectedSet(g, a): {a, b, c}"
* @require "connectedSet(g, y): {x, y}"
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function g2 : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g1: ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c)])"
* @require "g2: ([a, b, c, x, y], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (x:y)=(x,y)])"
* @require "g1: ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c)])"
* @require "g2: ([a, b, c, x, y], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (x:y)=(x,y)])"
* @require "isConnected(g1): true"
* @require "isConnected(g2): false"
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function g2 : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g1: ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c)])"
* @require "g2: ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a)])"
* @require "g1: ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c)])"
* @require "g2: ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a)])"
* @require "hasCycle(g1): false"
* @require "hasCycle(g2): true"
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ function g2 : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g1: ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a)])"
* @require "g2: ([a, b, c, x, y], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a), (x:y)=(x,y), (y:x)=(y,x)])"
* @require "g1: ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a)])"
* @require "g2: ([a, b, c, x, y], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a), (x:y)=(x,y), (y:x)=(y,x)])"
* @require "findCyclesWithVertex(g1, a): {a, b, c}"
* @require "findCyclesWithVertex(g2, a): {a, b, c}"
* @require "findCyclesWithVertex(g2, x): {x, y}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function g2 : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g1: ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a)])"
* @require "g2: ([a, b, c, x, y], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a), (x:y)=(x,y), (y:x)=(y,x)])"
* @require "g1: ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a)])"
* @require "g2: ([a, b, c, x, y], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a), (x:y)=(x,y), (y:x)=(y,x)])"
* @require "subgraph(g1, {a}): ([a], [])"
* @require "subgraph(g1, {a, b}): ([a, b], [(a:b)=(a,b)])"
* @require "subgraph(g1, {a, b, c}): ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a)])"
* @require "subgraph(g2, {a, b, c}): ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a)])"
* @require "subgraph(g1, {a, b, c}): ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a)])"
* @require "subgraph(g2, {a, b, c}): ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a)])"
* @require "subgraph(g2, {x, y}): ([x, y], [(x:y)=(x,y), (y:x)=(y,x)])"
*/
rule Start =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function g : -> GRAPH
* @minsteps 1
* @maxsteps 1
*
* @require "g: ([a, b, c], [(a:b)=(a,b), (b:c)=(b,c), (a:c)=(a,c), (c:a)=(c,a)])"
* @require "asUndirectedGraph(g): ([a, b, c], [(a:b)={a,b}, (b:c)={b,c}, (a:c)={a,c}, (c:a)={c,a}])"
* @require "g: ([a, b, c], [(a:b)=(a,b), (a:c)=(a,c), (b:c)=(b,c), (c:a)=(c,a)])"
* @require "asUndirectedGraph(g): ([a, b, c], [(a:b)={a,b}, (a:c)={a,c}, (b:c)={b,c}, (c:a)={c,a}])"
* @require "dijkstraShortestPath(g, c, b): [(c:a), (a:b)]"
* @require "dijkstraShortestPath(asUndirectedGraph(g), c, b): [(b:c)]"
*/
Expand Down
Loading