/// <summary> /// Removes the given Vertex from the graph; also removes all edges which use this vertex. /// </summary> /// <param name="toRemove">The Vertex which will be completely removed from the graph. </param> public void RemoveVertex(BaseVertex toRemove) { vertices.Remove(toRemove); foreach (BaseVertex vtx in vertices) vtx.RemoveConnections(toRemove); }
/// <summary> /// Removes all connections from this Vertex to a given target Vertex. /// </summary> /// <param name="target">Any connections from the current Vertex to target will be removed.</param> public void RemoveConnections(BaseVertex target) { for (int i = 0; i < connectsTo.Count; i++) { if (connectsTo[i].Connection.Name == target.Name) { connectsTo.RemoveAt(i); i--; } } }
// checks if node is in visited by name private bool isInList(ArrayList visited, BaseVertex node) { foreach (BaseVertex i in visited) { if (i.Name == node.Name) { return true; } } return false; }
/// <summary> /// Checks all the edges to see if they are epsilon transitions /// </summary> /// <param name="node">node to check</param> /// <returns>true if there is an epsilon transition</returns> private bool hasEpsilonTransition(BaseVertex node) { foreach (Edge e in node.Connections) { if (e.Condition == this.epsilonTransition) return true; } return false; }
/// <summary> /// Get list of nodes that are connected to the input node by epsilon transitions /// </summary> /// <param name="node"></param> /// <returns></returns> private ArrayList getEpsilonTransition(BaseVertex node) { ArrayList eNodes = new ArrayList(); foreach (Edge e in node.Connections) { if (e.Condition == this.epsilonTransition) { if (!this.isInList(eNodes, e.Connection)) { eNodes.Add(e.Connection); } } } return eNodes; }
/// <summary> /// perform an epsilon closure on a vertex /// </summary> /// <param name="v">vertex to perform e closure on</param> /// <returns>list of vertices in the epsilon closure of the vertex passed into the method</returns> public VertexSet eClosure(BaseVertex v) { Stack<BaseVertex> nodes = new Stack<BaseVertex>(); BaseVertex current; VertexSet visited = new VertexSet(new ArrayList()); // if the node passed doesn't have e transitions, return list with just passed in vertex, otherwise push onto stack and start algorithm if (this.hasEpsilonTransition(v)) { nodes.Push(v); } while (nodes.Count > 0) { current = nodes.Pop(); visited.addToSet(current); if (this.hasEpsilonTransition(current)) { ArrayList eNodes = this.getEpsilonTransition(current); foreach (BaseVertex newNode in eNodes) { if (!visited.isInSet(newNode)) { nodes.Push(newNode); } } } } if (visited.size() == 0) { visited.addToSet(v); } return visited; }
public Graph createGraph() { normalizeNames(); Graph g = new Graph(); BaseVertex[] verts = new BaseVertex[rowNames.Count]; Dictionary<String, BaseVertex> vertexTable = new Dictionary<string,BaseVertex>(); for (int x = 0; x < rowNames.Count; x++) { bool accepting = false; foreach (BaseVertex v in ((VertexSet)rowNames[x]).vertices) { if (v.Accepting) accepting = true; } BaseVertex vertex = g.CreateNewVertex(((VertexSet)rowNames[x]).ToDOTString(), accepting); vertexTable.Add(vertex.Name, vertex); verts[x] = vertex; } //iterate over vertices for (int i = 0; i < rowNames.Count; i++) { //iterate over alphabet for (int x = 0; x < columnNames.Count; x++) { String toVertexName = ((VertexSet)table[x][i]).ToDOTString(); verts[i].AddConnection(vertexTable[toVertexName], columnNames[x].ToString()); } } g.StartVertex = g.Vertices[1]; return g; }
/// <summary> /// tests if a BaseVertex is in the VertexSet /// </summary> /// <param name="node">node to check to see if it is in the set</param> /// <returns>true if the vertex is in the set, false otherwise</returns> public bool isInSet(BaseVertex node) { foreach (BaseVertex i in this.vertices) { if (i.Name == node.Name) return true; } return false; }
/// <summary> /// Creates a new Edge with the given target Vertex and condition /// </summary> /// <param name="nextVertex">The vertex pointed to by this edge. </param> /// <param name="condition">The condition which must be true for this vertex to be valid.</param> public Edge(BaseVertex nextVertex, string condition) { connectsTo = nextVertex; validOnCondition = condition; edgeText = String.Empty; }
public void AddConnection(BaseVertex newNode, string transitionString) { Edge newEdge = new Edge(newNode, transitionString); connectsTo.Add(newEdge); }
public void AddConnection(BaseVertex newNode, string transitionString, bool accepting) { Edge newEdge = new Edge(newNode, transitionString); connectsTo.Add(newEdge); this.isAccepting = accepting; }
public void AddConnection(BaseVertex newNode, string transitionString, bool accepting, bool isCharClass) { Edge newEdge = new Edge(newNode, transitionString, isCharClass); connectsTo.Add(newEdge); }
public bool CheckNodeEquality(BaseVertex that) { if (this.visited) { return true; } this.visited = true; if (this.connectsTo.Count == 0 && that.connectsTo.Count == 0) { return true; } if (this.connectsTo.Count != that.connectsTo.Count) { return false; } if (this.Accepting != that.Accepting) return false; foreach (Edge left in this.connectsTo) { int countEqualEdges = 0; foreach (Edge right in that.connectsTo) { if(left.ShaneEquals(right))// && left.Connection.visited == false && right.Connection.visited == false) { if (left.Connection.CheckNodeEquality(right.Connection)) { countEqualEdges++; } } } //didn't find a matching edge for this one, so its not the same node //finding 2 equivalent edges doesn't imply the vertices are different if (countEqualEdges == 0) { return false; } } return true; }
/// <summary> /// Creates a new Edge with the given target Vertex and condition /// </summary> /// <param name="nextVertex">The vertex pointed to by this edge. </param> /// <param name="condition">The condition which must be true for this vertex to be valid.</param> public Edge(BaseVertex nextVertex, string condition, bool isCharClass) { connectsTo = nextVertex; validOnCondition = condition; this.isCharClass = isCharClass; }
/// <summary> /// Creates a new Edge with the given target Vertex and condition /// </summary> /// <param name="nextVertex">The vertex pointed to by this edge. </param> /// <param name="condition">The condition which must be true for this vertex to be valid.</param> public Edge(BaseVertex nextVertex, string condition) { connectsTo = nextVertex; validOnCondition = condition; isCharClass = false; }
/// <summary> /// Creates and returns a new BaseVertex with the given name and adds it to the graph /// </summary> /// <param name="name">The name of the vertex to be added. The name must be unique. </param> /// <returns>The new Vertex which was just created</returns> public BaseVertex CreateNewVertex(string name, bool accepting) { BaseVertex newV = new BaseVertex(name, accepting); vertices.Add(newV); return newV; }
/// <summary> /// Creates a new Graph with a default start vertex Vertices. /// </summary> public Graph() { vertices = new List<BaseVertex>(); start = this.CreateNewVertex("start", true); }
/// <summary> /// Creates and returns a new BaseVertex with the given name and adds it to the graph /// </summary> /// <param name="name">The name of the vertex to be added. The name must be unique. </param> /// <returns>The new Vertex which was just created</returns> public BaseVertex CreateNewVertex(string name) { BaseVertex newV = new BaseVertex(name); vertices.Add(newV); return newV; }
/// <summary> /// Creates a new Edge with the given target Vertex and condition /// </summary> /// <param name="nextVertex">The vertex pointed to by this edge. </param> /// <param name="condition">The condition which must be true for this vertex to be valid.</param> /// <param name="edgeText">The text to show on for this edge</param> public Edge(BaseVertex nextVertex, string condition, string edgeText) { connectsTo = nextVertex; validOnCondition = condition; this.edgeText = edgeText; }
/// <summary> /// Adds a vertex to the VertexSet and updates the ID /// </summary> /// <param name="v">BaseVertex to add to the set</param> public void addToSet(BaseVertex v) { Debug.Assert(!this.isInSet(v)); // sanity check to ensure a set will never have duplicates vertices.Add(v); this.computeID(); }