/** * Save all nodes and edges in GraphColor format. * * @param gra * @param outFileName must end with ".col" */ public void saveAGG2ColorGraph(GraGra gra, String outFileName) { if (outputFileName.endsWith(".col")) { final List<Arc> edges = new Vector<Arc>(); edges.addAll(gragra.getGraph(this.indx).getArcsSet()); final List<Node> nodes = new Vector<Node>(); nodes.addAll(gragra.getGraph(this.indx).getNodesSet()); final File f = new File(outputFileName); ByteArrayOutputStream baOut = null; FileOutputStream fos = null; try { fos = new FileOutputStream(f); // write comment baOut = new ByteArrayOutputStream(); String comment = "c AGG (.ggx) to Color Graph (.col)\n"; // put in out stream and file baOut.write(comment.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); // write problem baOut = new ByteArrayOutputStream(); String problem = "p edge "; problem = problem.concat(String.valueOf(nodes.size())); problem = problem.concat(" "); problem = problem.concat(String.valueOf(edges.size())); problem = problem.concat("\n"); // put in out stream and file baOut.write(problem.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); for (int i = 0; i < edges.size(); i++) { final Arc a = edges.get(i); int src = nodes.indexOf(a.getSource()) + 1; int tar = nodes.indexOf(a.getTarget()) + 1; String str = "e "; str = str.concat(String.valueOf(src)).concat(" "); str = str.concat(String.valueOf(tar).concat("\n")); // write edge line baOut = new ByteArrayOutputStream(); baOut.write(str.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); } } catch (IOException e) { } } }
public static void transform(GraGra grammar, GraTraEventListener l) { if (grammar == null) return; System.out.println(grammar.getGraTraOptions().toString()); createGraTraInstance(grammar); gratra.addGraTraListener(l); gratra.setGraGra(grammar); gratra.setHostGraph(grammar.getGraph()); gratra.enableWriteLogFile(writeLogFile); MorphCompletionStrategy strategy = CompletionStrategySelector.getDefault(); // strategy = new Completion_NAC(new Completion_InjCSP()); if (grammar.getGraTraOptions().isEmpty()) { grammar.setGraTraOptions(strategy); gratra.setCompletionStrategy(strategy); } else { if (grammar.getGraTraOptions().contains("showGraphAfterStep")) grammar.getGraTraOptions().remove("showGraphAfterStep"); gratra.setGraTraOptions(grammar.getGraTraOptions()); System.out.println("Options: " + grammar.getGraTraOptions()); System.out.println(); } grammar.destroyAllMatches(); if (priority) ((PriorityGraTraImpl) gratra).transform(); else if (layered) ((LayeredGraTraImpl) gratra).transform(); else if (ruleSequence) ((RuleSequencesGraTraImpl) gratra).transform(); else ((DefaultGraTraImpl) gratra).transform(); }
public AGG2ColorGraph(final String arg, final String arg1, final String arg2) { if (arg != null && arg.endsWith(".ggx")) { fileName = arg; } if (arg1 != null) { this.indx = 0; try { Integer I = Integer.valueOf(arg1); this.indx = I.intValue(); } catch (NumberFormatException ex) { } } if (arg2 != null && arg2.endsWith(".res")) { this.colorFileName = arg2; } if (fileName != null) { System.out.println("File name: " + fileName); /* load gragra */ gragra = load(fileName); } if (gragra != null) { if (this.colorFileName == null) { outputFileName = fileName.concat("-NodeEdge.col"); saveAGGNodeEdge2ColorGraph(gragra, outputFileName); System.out.println("Output file: " + outputFileName); // outputFileName = filename.concat(".col"); // saveAGG2ColorGraph(gragra, outputFileName); // System.out.println("Output file: " + outputFileName); // outputFileName = filename.concat(".aggcol"); // saveAGGColor(gragra, outputFileName); // System.out.println("AGG color Output file: " + outputFileName); } else { System.out.println("Import result from: " + this.colorFileName); final Graph graph = gragra.getGraph(this.indx); importColorGraph2AGG(gragra, graph); gragra.save(fileName); } } else System.out.println("Grammar: " + fileName + " FAILED!"); }
/** * Save color result which is computed by AGG grammar. * * @param gra * @param outFileName must end with ".aggcol" */ public void saveAGGColor(GraGra gra, String outFileName) { if (outputFileName.endsWith(".aggcol")) { final List<Node> edges = new Vector<Node>(); final List<Node> nodes = new Vector<Node>(); nodes.addAll(gragra.getGraph(this.indx).getNodesSet()); for (int i = 0; i < nodes.size(); i++) { final Node n = nodes.get(i); if (n.getType().getName().equals("Node")) { } else if (n.getType().getName().equals("Edge")) { edges.add(n); nodes.remove(i); i--; } else { nodes.remove(i); i--; } } final File f = new File(outputFileName); ByteArrayOutputStream baOut = null; FileOutputStream fos = null; try { fos = new FileOutputStream(f); // write comment baOut = new ByteArrayOutputStream(); String comment = "c AGG Color\n"; // put in out stream and file baOut.write(comment.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); // write problem baOut = new ByteArrayOutputStream(); String problem = "p edge "; problem = problem.concat(String.valueOf(nodes.size())); problem = problem.concat(" "); problem = problem.concat(String.valueOf(edges.size())); problem = problem.concat("\n"); // put in out stream and file baOut.write(problem.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); for (int i = 0; i < nodes.size(); i++) { final Node n = nodes.get(i); ValueTuple val = (ValueTuple) n.getAttribute(); String color = ((ValueMember) val.getMemberAt("color")).getExprAsText(); color = color.concat(" "); // write color baOut = new ByteArrayOutputStream(); baOut.write(color.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); } } catch (IOException e) { } } }
/** * Search the host graph of the grammar for subgraph with Node<--Edge-->Node structure, where each * edge starts at node Edge and ends at node Node. Save this subgraph in GraphColor format. * * @param gra * @param outFileName must end with ".col" */ public void saveAGGNodeEdge2ColorGraph(GraGra gra, String outFileName) { if (outputFileName.endsWith(".col")) { final List<Node> edges = new Vector<Node>(); final List<Node> nodes = new Vector<Node>(); nodes.addAll(gragra.getGraph(this.indx).getNodesSet()); for (int i = 0; i < nodes.size(); i++) { final Node n = nodes.get(i); if (n.getType().getName().equals("Node")) { } else if (n.getType().getName().equals("Edge")) { edges.add(n); nodes.remove(i); i--; } else { nodes.remove(i); i--; } } final File f = new File(outputFileName); ByteArrayOutputStream baOut = null; FileOutputStream fos = null; try { fos = new FileOutputStream(f); // write comment baOut = new ByteArrayOutputStream(); String comment = "c AGG to Color Graph (.col)\n"; // put in out stream and file baOut.write(comment.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); // write problem baOut = new ByteArrayOutputStream(); String problem = "p edge "; problem = problem.concat(String.valueOf(nodes.size())); problem = problem.concat(" "); problem = problem.concat(String.valueOf(edges.size())); problem = problem.concat("\n"); // put in out stream and file baOut.write(problem.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); for (int i = 0; i < edges.size(); i++) { final Node en = edges.get(i); Iterator<Arc> outs = en.getOutgoingArcs(); if (outs.hasNext()) { final Arc a = outs.next(); int index = nodes.indexOf(a.getTarget()) + 1; while (outs.hasNext()) { final Arc aj = outs.next(); int indxj = nodes.indexOf(aj.getTarget()) + 1; String str = "e "; str = str.concat(String.valueOf(index)).concat(" "); str = str.concat(String.valueOf(indxj).concat("\n")); // write edge line baOut = new ByteArrayOutputStream(); baOut.write(str.getBytes()); fos.write(baOut.toByteArray()); baOut.flush(); } } } } catch (IOException e) { } } }