protected GraGra load() throws Exception {
   GraGra gra = null;
   if (this.dirName.equals("")) {
     this.chooser = new JFileChooser(System.getProperty("user.dir"));
   } else {
     this.chooser = new JFileChooser(this.dirName);
   }
   this.chooser.setFileFilter(new AGGFileFilter("rsx", "AGG Files (.rsx)"));
   int returnVal = this.chooser.showOpenDialog(this.arsGUI);
   this.dirName = this.chooser.getCurrentDirectory().toString();
   if (returnVal == JFileChooser.APPROVE_OPTION) {
     if (this.chooser.getSelectedFile() != null
         && !this.chooser.getSelectedFile().getName().equals("")) {
       this.fileName = this.chooser.getSelectedFile().getName();
       try {
         gra = this.ars.load(this.dirName + File.separator + this.fileName);
         if (gra != null) {
           gra.setDirName(this.dirName);
           gra.setFileName(this.fileName);
         }
       } catch (Exception ex) {
         throw ex;
       }
     }
   }
   return gra;
 }
Exemplo n.º 2
0
  /**
   * 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) {
      }
    }
  }
Exemplo n.º 3
0
  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();
  }
Exemplo n.º 4
0
  public GraGra load(String fName) {
    if (fName.endsWith(".ggx")) {
      h = new XMLHelper();
      if (h.read_from_xml(fName)) {

        // create a gragra
        GraGra gra = new GraGra(false);
        h.getTopObject(gra);
        gra.setFileName(fName);
        return gra;
      }
      return null;
    }
    return null;
  }
Exemplo n.º 5
0
 static void createGraTraInstance(GraGra grammar) {
   if (grammar.getGraTraOptions().contains("priority")) {
     gratra = new PriorityGraTraImpl();
     priority = true;
     System.out.println("Transformation by rule priority ...");
   } else if (grammar.getGraTraOptions().contains("layered")) {
     gratra = new LayeredGraTraImpl();
     layered = true;
     System.out.println("Layered transformation ...");
   } else if (grammar.getGraTraOptions().contains("ruleSequence")) {
     gratra = new RuleSequencesGraTraImpl();
     ruleSequence = true;
     System.out.println("Transformation by rule sequences ...");
   } else {
     gratra = new DefaultGraTraImpl();
     System.out.println("Transformation  non-deterministically ...");
   }
 }
Exemplo n.º 6
0
  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!");
  }
Exemplo n.º 7
0
  public aggEngineTest(String filename) throws IOException {
    fileName = filename;
    System.out.println("File name:  " + fileName);
    gragra = load(fileName);

    if (gragra != null) {
      //			int levelOfTGcheck = gragra.getLevelOfTypeGraphCheck();
      //			if (gragra.getTypeSet().hasInheritance()) {
      //				if (levelOfTGcheck != TypeSet.DISABLED)
      //					gragra.setLevelOfTypeGraphCheck(levelOfTGcheck);
      //				else
      //					gragra.setLevelOfTypeGraphCheck(TypeSet.ENABLED_MAX);
      //			} else {
      //				gragra.setLevelOfTypeGraphCheck(levelOfTGcheck);
      //			}

      // olga
      gragra.setLevelOfTypeGraphCheck(TypeSet.ENABLED_MAX);
      createGraTra();

      for (int i = 0; i < gragra.getListOfEnabledRules().size(); i++) {
        // System.out.println("Rule No. " + (i) + " Rule Name: " +
        // gragra.getListOfRules().get(i).getName());
        System.out.println(
            "Rule No. " + (i) + " Rule Name: " + gragra.getListOfEnabledRules().get(i).getName());
      }

      System.out.println("Total Number of rules are: " + gragra.getListOfEnabledRules().size());
      int[] sequence = readSeq();
      Rule[] rule;
      rule = new Rule[sequence.length];
      boolean done;
      for (int i = 0; i < sequence.length; i++) {
        rule[i] = gragra.getListOfEnabledRules().get(sequence[i]);
        setPars(rule[i]);
        done = doStep(rule[i]);
        if (done) {
          System.out.println("Match found, Step was performed ");

          // olga
          if (i < sequence.length - 1)
            propagateOutParamToInParamOfRule(
                outParVal, gragra.getListOfEnabledRules().get(sequence[i + 1]));
        } else {
          System.out.println("Match not found, Step was not performed");
          System.out.println("The transformation cannot continue...");
          break;
        }
        System.out.println(
            "The parameters values for "
                + rule[i].getName()
                + " are: "
                + rule[i].getInputParameters());
      }
      System.out.println("\n\n End of program");
    }
  }
Exemplo n.º 8
0
 public static void save(GraGra gra, String outFileName) {
   if (outFileName.equals("")) outputFileName = gra.getName() + "_out.ggx";
   else if (outFileName.equals("_out.ggx"))
     outputFileName = fileName.substring(0, fileName.length() - 4) + "_out.ggx";
   else if (outFileName.indexOf(".ggx") == -1) outputFileName = outFileName.concat(".ggx");
   else if (outFileName.equals(fileName))
     outputFileName = fileName.substring(0, fileName.length() - 4) + "_out.ggx";
   else {
     outputFileName = outFileName;
   }
   if (outputFileName.endsWith(".ggx")) {
     XMLHelper xmlh = new XMLHelper();
     xmlh.addTopObject(gra);
     xmlh.save_to_xml(outputFileName);
   }
 }
Exemplo n.º 9
0
  public boolean doStep(Rule r) {
    Match match = gratra.createMatch(r);
    System.out.println("The match was: " + match.getRule().getName().toString());

    // olga
    while (match.nextCompletion()) {
      if (match.isValid()) {
        try {
          Morphism co = StaticStep.execute(match);
          System.out.println("Rule  " + match.getRule().getName() + " : step is done");
          didTransformation = true;
          Graph graph = co.getOriginal();
          Graph graph2 = co.getImage();
          System.out.println();
          //					System.out.println("The image parameters are: " + graph.toString());
          //					System.out.println("The OUTPUT parameters are: " + graph2.toString());

          // olga
          outPar = getOutputParameters(r, outPar);
          for (int i = 0; i < outPar.size(); i++) {
            VarMember p = outPar.get(i);
            String parVal = getValueOfOutputParameter(p, r, (OrdinaryMorphism) co);
            this.outParVal.put(p.getName(), parVal);
          }

          ((OrdinaryMorphism) co).dispose();
          return true;
        } catch (TypeException ex) {
          ex.printStackTrace();
          gragra.destroyMatch(match);
          System.out.println(
              "Rule  "
                  + match.getRule().getName()
                  + " : step of match failed : "
                  + ex.getMessage());
        }
      } else
        System.out.println(
            "Rule  "
                + match.getRule().getName()
                + " : a match completion is not valid; try to find the next ones");
    }
    System.out.println("Rule  " + match.getRule().getName() + " : match could not be found");
    return false;
    // gragra.destroyMatch(match);
    // match.clear();
  }
Exemplo n.º 10
0
  /**
   * 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) {
      }
    }
  }
Exemplo n.º 11
0
  /**
   * 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) {
      }
    }
  }