/** Draws the edge structure of the tree */ public void drawEdges(Graphics2D gg) { gg.setPaint(Color.black); Enumeration nodeList = argument.getBreadthFirstTraversal().elements(); // For each vertex... while (nodeList.hasMoreElements()) { // Get its edge list... TreeVertex vertex = (TreeVertex) nodeList.nextElement(); Enumeration edges = vertex.getEdgeList().elements(); // For each edge in the list... while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); // If we have several vertices on layer 0, only draw // edges for layers below that if (!(argument.isMultiRoots() && vertex.getLayer() == 0)) { // If the edge has been selected with the mouse, // use a thick line if (edge.isSelected()) { gg.setStroke(selectStroke); } gg.draw(edge.getShape(this)); // If we used a thick line, reset the stroke to normal // line for next edge. if (edge.isSelected()) { gg.setStroke(solidStroke); } TreeVertex edgeSource = edge.getDestVertex(); } } } }
// Walks the tree, checking to make sure that we haven't violated any // logical constraints. This is a method for debugging. public boolean check() { String path = pathLabel(); if (suffixLink != null) { String suffixPath = suffixLink.pathLabel(); if (!path.substring(1, path.length()).equals(suffixPath)) { logger.log( Level.SEVERE, String.format("Suffix Link for node (%s) didn't match: %s", path, suffixPath)); return false; } } for (char c : childEdges.keySet()) { TreeEdge e = childEdges.get(c); if (!e.check()) { return false; } } for (int k : terminalEdges.keySet()) { TreeEdge e = terminalEdges.get(k); if (!e.check()) { return false; } } return true; }
public void finishFinalEdges() { for (TreeEdge edge : edgesWithE) { if (edge.isEndSymbolic()) { edge.setEnd(lastE); } } edgesWithE.clear(); }
public String getInfo() { String info = "Edges: "; Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); info += edge.getSourceVertex().getLabel() + " => " + edge.getDestVertex().getLabel() + "; "; } return info; }
private void recurseSelectEdges(TreeVertex root) { Vector edges = root.getEdgeList(); for (int i = 0; i < edges.size(); i++) { TreeEdge edge = (TreeEdge) edges.elementAt(i); TreeVertex dest = edge.getDestVertex(); if (m_vertexList.contains(dest)) { edge.setSelected(true); recurseSelectEdges(dest); } } }
public String pathLabel() { if (parentEdge != null) { StringBuilder sb = new StringBuilder(parentEdge.headNode.pathLabel()); for (int i = 0; i < parentEdge.length(); i++) { sb.append(parentEdge.getChar(i)); } return sb.toString(); } else { return ""; } }
/** Determines if this subtree contains the given vertex */ public boolean containsTreeVertex(TreeVertex vertex) { if (vertex == null) return false; for (int i = 0; i < m_vertexList.size(); i++) { if ((TreeVertex) m_vertexList.elementAt(i) == vertex) return true; } for (int i = 0; i < m_edgeList.size(); i++) { TreeEdge edge = (TreeEdge) m_edgeList.elementAt(i); if (edge.getSourceVertex() == vertex || edge.getDestVertex() == vertex) { return true; } } return false; }
/** * Fills a SubtreeFrame with info about the subtree. Used to display info when user right clicks * on an existing subtree. */ public void buildSubtreeFrame(SubtreeFrame sFrame) { // Clear visited flag on all vertices in the tree // and determine the layer number of the root node int highestLayer = 100; Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); edge.getDestVertex().setVisited(false); edge.getSourceVertex().setVisited(false); if (edge.getSourceVertex().getLayer() < highestLayer) { highestLayer = edge.getSourceVertex().getLayer(); } } // Create list of premises and conclusion and assign // to text areas in the dialog Vector premiseList = new Vector(); String conclusion = ""; String criticalQuestions = ""; edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); TreeVertex source = edge.getSourceVertex(); TreeVertex dest = edge.getDestVertex(); if (!source.getVisited()) { if (source.getLayer() == highestLayer) { conclusion += (String) source.getLabel(); } else { premiseList.add((String) source.getLabel()); } source.setVisited(true); } if (!dest.getVisited()) { premiseList.add((String) dest.getLabel()); dest.setVisited(true); } } sFrame.setPremisesText(premiseList); sFrame.setConclusionText(conclusion); sFrame.loadArgTypeCombo(); // Assign the correct argument type to the combo box for (int index = 0; index < sFrame.selectArgumentCombo.getItemCount(); index++) { if (m_argumentType.getName().equals(sFrame.selectArgumentCombo.getItemAt(index))) { sFrame.selectArgumentCombo.setSelectedIndex(index); sFrame.selectArgumentCombo.setEditable(false); break; } } // Construct list of critical questions // Enumeration quesList = m_argumentType.getCriticalQuestions().elements(); // while (quesList.hasMoreElements()) { // criticalQuestions += (String)quesList.nextElement() + // "\r\n _____________________________ \r\n"; // } sFrame.updateTextBoxes(); }
/** Build a shape for the entire subtree by joining together the shapes for each of its edges. */ public Shape constructShape(DiagramBase diagram) { GeneralPath shape = new GeneralPath(); Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); if (!edge.visible) { continue; } Shape edgeShape = edge.getSchemeShape(diagram); PathIterator path = edgeShape.getPathIterator(null); shape.append(path, false); } BasicStroke stroke = new BasicStroke( diagram.getSubtreeLineWidth(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); shapeTable.put(diagram, stroke.createStrokedShape(shape)); return (Shape) shapeTable.get(diagram); }
public void addEdge(TreeEdge e) { if (e.start() == e.string.length() - 1) { int key = e.string.getIndex(); if (terminalEdges.containsKey(key)) { throw new IllegalArgumentException(); } e.headNode = this; terminalEdges.put(key, e); } else { char key = e.getChar(0); if (childEdges.containsKey(key)) { throw new IllegalArgumentException(); } e.headNode = this; childEdges.put(key, e); } }
public void print(int indent, PrintStream ps) { if (isLeaf()) { printSuffixes(ps); ps.println(); } else { int i = 0; for (char c : childEdges.keySet()) { TreeEdge edge = childEdges.get(c); edge.print(indent, i != 0, ps); i++; } for (int k : terminalEdges.keySet()) { TreeEdge edge = terminalEdges.get(k); edge.print(indent, i != 0, ps); i++; } } }
/** * Build a shape for the entire subtree by joining together the shapes for each of its edges. * Vertices included since needed for FullTextPanel. */ public Shape constructInternalShape(DiagramBase diagram, boolean includeVertices) { GeneralPath shape = new GeneralPath(); Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); Shape edgeShape = edge.getSchemeShape(diagram); PathIterator path = edgeShape.getPathIterator(null); shape.append(path, false); if (includeVertices) { Shape vertexShape; if (!edge.getSourceVertex().isVirtual()) { vertexShape = edge.getSourceVertex().getShape(diagram); path = vertexShape.getPathIterator(null); shape.append(path, false); } if (!edge.getDestVertex().isVirtual()) { vertexShape = edge.getDestVertex().getShape(diagram); path = vertexShape.getPathIterator(null); shape.append(path, false); } } } BasicStroke stroke = new BasicStroke( diagram.getSubtreeLineWidth() - DiagramBase.EDGE_OUTLINE_WIDTH + 1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); internalShapeTable.put(diagram, stroke.createStrokedShape(shape)); return (Shape) internalShapeTable.get(diagram); }
/* * Creates a line of width EDGE_SELECT_WIDTH for each edge * and tests if mouse click was in that Shape's boundary. * Returns the edge if one was selected, null otherwise. */ public TreeEdge testEdgeShapes(MouseEvent event) { if (argument == null || argument.getTree() == null) return null; double x = event.getX(); double y = event.getY(); BasicStroke edgeWidth = new BasicStroke(EDGE_SELECT_WIDTH); if (argument.getBreadthFirstTraversal() == null) return null; Enumeration nodeList = argument.getBreadthFirstTraversal().elements(); while (nodeList.hasMoreElements()) { TreeVertex vertex = (TreeVertex) nodeList.nextElement(); if (argument.isMultiRoots() && vertex.getLayer() == 0) continue; Enumeration edges = vertex.getEdgeList().elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); Shape shape = edge.getShape(this); Shape wideEdge = edgeWidth.createStrokedShape(shape); TreeVertex child = edge.getDestVertex(); if (wideEdge.contains(x, y)) { edge.setSelected(!edge.isSelected()); return edge; } } } return null; }
/** Build a shape for the entire subtree by joining together the shapes for each of its edges. */ public Shape constructFullTextShape(DiagramBase diagram) { GeneralPath shape = new GeneralPath(); Enumeration edges = m_edgeList.elements(); while (edges.hasMoreElements()) { TreeEdge edge = (TreeEdge) edges.nextElement(); if (!edge.visible) { continue; } Shape edgeShape = edge.getSchemeShape(diagram); PathIterator path = edgeShape.getPathIterator(null); shape.append(path, false); TreeVertex vertex = edge.getSourceVertex(); if (!vertex.isVirtual()) { shape.append(vertex.getShape(diagram).getPathIterator(null), false); } vertex = edge.getDestVertex(); if (!vertex.isVirtual()) { shape.append(vertex.getShape(diagram).getPathIterator(null), false); } } BasicStroke stroke = new BasicStroke(20, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); shapeTable.put(diagram, stroke.createStrokedShape(shape)); return (Shape) shapeTable.get(diagram); }
public void matchFrom(TreeNode startNode, boolean skipcount) { assert string != null || array != null; assert lastEdge == null; matchingFrom = start; matchedTo = start; // a base case. if (end - start <= 0) { return; } char nextChar = getChar(matchingFrom); assert startNode != null; System.err.println("startNode is " + startNode + " and nextChar is " + nextChar); if (startNode.childEdges.containsKey(nextChar)) { lastEdge = startNode.childEdges.get(nextChar); } else if (skipcount) { System.err.println("Failure Node: "); startNode.print(0, System.err); System.err.println(); System.err.flush(); throw new IllegalArgumentException(); } boolean keepMatching = lastEdge != null; while (keepMatching) { int remaining = end - matchingFrom; int matching = skipcount ? Math.min(lastEdge.length(), remaining) : (string != null ? lastEdge.countMatches(string, matchingFrom, end) : lastEdge.countMatches(array, matchingFrom, end)); matchedTo = matchingFrom + matching; if (matching < lastEdge.length()) { // we either matched all the way into the middle of the // current edge, or we found a mismatch in the current edge. // either way, update the matchedTo variable and we're done. keepMatching = false; } else { if (matching < remaining) { nextChar = getChar(matchedTo); if (lastEdge.tailNode.childEdges.containsKey(nextChar)) { lastEdge = lastEdge.tailNode.childEdges.get(nextChar); matchingFrom += matching; } else if (skipcount) { System.err.println("ERROR TREE: "); print(System.err); System.err.println(); System.err.flush(); String err = String.format( "[%s] node:'%s' (next: %c)", toString(), startNode.pathLabel(), nextChar); throw new IllegalArgumentException(err); } else { keepMatching = false; } } else { keepMatching = false; } } } }
// to print genereated mst public void printMST() { System.out.println((int) TreeWeight); for (TreeEdge e : MSTree) { e.printTreeEdge(e); } }
public void printMSTwithWeight() { System.out.println(TreeWeight); for (TreeEdge e : MSTree) { e.printTreeEdgeWithWeight(e); } }
public boolean inEdgeMiddle() { return lastEdge != null && lastMatchLength() < lastEdge.length(); }