/**
   * Método para hacer el código a partir del contenido de los vértices en el
   * grafo.
   */
  public boolean make() {

    boolean codeGenerated = false;

    for (int i = 0; i < graphs.size(); i++) {
      g = graphs.get(i);

      if (i == 0) {
        addGlobalVariables(g);
      }

      vertices = g.getVertices();
      edges = g.getEdges();
      head = g.getHead();

      if (head == null) {
        continue;
      }

      if (g.getNumVertices() > 0) {
        recurse(head);
        codeGenerated = true;
      } else {
        codeGenerated = false;
      }
    }

    return codeGenerated;
  }
Ejemplo n.º 2
0
 private void populateLists(List<Vertex> verticies, List<Edge> edges) {
   for (Vertex v : graph.getVertices()) {
     verticies.add(v);
   }
   for (Edge e : graph.getEdges()) {
     edges.add(e);
   }
 }
  /**
   * Helper method for parseQuery. Writes the results of the queries to the output file.
   *
   * @param output initialized BufferedWriter for the output file
   * @param g initialized graph of edges and vertices
   * @param u1 one vertex used in commands
   * @param u2 another vertex used in commands
   * @param command command to execute
   */
  private static void printResults(
      BufferedWriter output, Graph g, Vertex u1, Vertex u2, String command) {
    final String commonInfluencers = "commonInfluencers";
    final String numRetweets = "numRetweets";

    final String VERTEX_NOT_FOUND_ERROR =
        "ERROR: One or more vertices does not exist in the graph.";
    final String INVALID_COMMAND_ERROR = "\tError: invalid command ";
    final String PATH_NOT_FOUND_ERROR = "\tPath not found.";

    List<Vertex> allVertices = new ArrayList<Vertex>(g.getVertices());

    try {
      output.write("query: " + command + " " + u1.toString() + " " + u2.toString());
      output.newLine();
      output.write("<result>");
      output.newLine();

      // check if vertices exist in graph
      if (!allVertices.contains(u1) || !allVertices.contains(u2)) {
        output.write(VERTEX_NOT_FOUND_ERROR);
        output.newLine();
        output.write("</result>");
        output.newLine();
        output.newLine();
        return;
      }
      // if query is commonInfluencers
      if (command.equals(commonInfluencers)) {
        List<Vertex> commonFollowers =
            new LinkedList<Vertex>(Algorithms.commonDownstreamVertices(g, u1, u2));
        for (Vertex v : commonFollowers) {
          output.write("\t" + v.toString());
          output.newLine();
        }
      }

      // if query is numRetweets
      else if (command.equals(numRetweets)) {
        // note switch in u1 and u2; this is because tweets go upstream
        int distance = Algorithms.shortestDistance(g, u2, u1);

        if (distance == -1) {
          output.write(PATH_NOT_FOUND_ERROR);
        } else {
          // implicitly convert distance to string as printing out ints somehow didn't work
          output.write("" + distance);
          // System.out.println(distance);
        }

        output.newLine();
      } else {
        output.write(INVALID_COMMAND_ERROR + command);
        output.newLine();
      }
      output.write("</result>");
      output.newLine();
      output.newLine();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }