public Graph retrieveGraph(GraphModel graphModel, boolean isFilterGraph) {
    Graph graph = graphModel.getGraph();

    graph.clear();
    Map<Integer, JsonObject> nMap;
    Map<Integer, JsonObject> eMap;
    if (!isFilterGraph) {
      nMap = nodesMap;
      eMap = edgesMap;
    } else {
      nMap = nodesMapFilter;
      eMap = edgesMapFilter;
    }

    for (Entry<Integer, JsonObject> enSet : nMap.entrySet()) {
      Map<String, Object> map = enSet.getValue().getMap();
      Node node = graphModel.factory().newNode(enSet.getValue().getValue("id"));
      for (Entry<String, Object> attSet : map.entrySet()) {
        if (!attSet.getKey().equalsIgnoreCase("id")
            && !attSet.getKey().equalsIgnoreCase("x")
            && !attSet.getKey().equalsIgnoreCase("y")
            && !attSet.getKey().equalsIgnoreCase("cR")
            && !attSet.getKey().equalsIgnoreCase("size")
            && !attSet.getKey().equalsIgnoreCase("cG")
            && !attSet.getKey().equalsIgnoreCase("cB")) {
          node.setAttribute(attSet.getKey(), attSet.getValue());
        }
        if (attSet.getKey().equalsIgnoreCase("x")) {
          node.setX((float) attSet.getValue());
        }
        if (attSet.getKey().equalsIgnoreCase("y")) {
          node.setY((float) attSet.getValue());
        }
      }
      node.setSize((float) Double.parseDouble(map.get("size").toString()));
      node.setColor(
          new Color(
              (float) Double.parseDouble(map.get("cR").toString()),
              (float) Double.parseDouble(map.get("cG").toString()),
              (float) Double.parseDouble(map.get("cB").toString())));
      graph.addNode(node);
    }
    for (Entry<Integer, JsonObject> enSet : eMap.entrySet()) {
      Map<String, Object> map = enSet.getValue().getMap();
      Edge edge =
          graphModel
              .factory()
              .newEdge(
                  map.get("id"),
                  graph.getNode(map.get("source")),
                  graph.getNode(map.get("target")),
                  1,
                  Double.parseDouble(map.get("weight").toString()),
                  true);
      for (Entry<String, Object> attSet : map.entrySet()) {
        if (!attSet.getKey().equalsIgnoreCase("id")
            && !attSet.getKey().equalsIgnoreCase("source")
            && !attSet.getKey().equalsIgnoreCase("target")
            && !attSet.getKey().equalsIgnoreCase("size")
            && !attSet.getKey().equalsIgnoreCase("cR")
            && !attSet.getKey().equalsIgnoreCase("cG")
            && !attSet.getKey().equalsIgnoreCase("cB")) {
          edge.setAttribute(attSet.getKey(), attSet.getValue());
        }
      }
      edge.setWeight(Double.parseDouble(map.get("size").toString()));
      edge.setColor(
          new Color(
              (float) Double.parseDouble(map.get("cR").toString()),
              (float) Double.parseDouble(map.get("cG").toString()),
              (float) Double.parseDouble(map.get("cB").toString())));
      graph.addEdge(edge);
    }

    return graph;
  }
  public void process() {
    // Workspace
    ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
    if (workspace == null) {
      workspace = pc.newWorkspace(pc.getCurrentProject());
      pc.openWorkspace(workspace);
    }
    if (container.getSource() != null) {
      pc.setSource(workspace, container.getSource());
    }

    // Architecture
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(workspace);

    HierarchicalGraph graph = null;
    switch (container.getEdgeDefault()) {
      case DIRECTED:
        graph = graphModel.getHierarchicalDirectedGraph();
        break;
      case UNDIRECTED:
        graph = graphModel.getHierarchicalUndirectedGraph();
        break;
      case MIXED:
        graph = graphModel.getHierarchicalMixedGraph();
        break;
      default:
        graph = graphModel.getHierarchicalMixedGraph();
        break;
    }
    GraphFactory factory = graphModel.factory();

    // Attributes - Creates columns for properties
    attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel(workspace);
    attributeModel.mergeModel(container.getAttributeModel());

    // Dynamic
    if (container.getTimeFormat() != null) {
      DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
      if (dynamicController != null) {
        dynamicController.setTimeFormat(container.getTimeFormat());
      }
    }

    int nodeCount = 0;
    // Create all nodes
    for (NodeDraftGetter draftNode : container.getNodes()) {
      Node n = factory.newNode(draftNode.isAutoId() ? null : draftNode.getId());
      flushToNode(draftNode, n);
      draftNode.setNode(n);
      nodeCount++;
    }

    // Push nodes in data structure
    for (NodeDraftGetter draftNode : container.getNodes()) {
      Node n = draftNode.getNode();
      NodeDraftGetter[] parents = draftNode.getParents();
      if (parents != null) {
        for (int i = 0; i < parents.length; i++) {
          Node parent = parents[i].getNode();
          graph.addNode(n, parent);
        }
      } else {
        graph.addNode(n);
      }
    }

    // Create all edges and push to data structure
    int edgeCount = 0;
    for (EdgeDraftGetter edge : container.getEdges()) {
      Node source = edge.getSource().getNode();
      Node target = edge.getTarget().getNode();
      Edge e = null;
      switch (container.getEdgeDefault()) {
        case DIRECTED:
          e =
              factory.newEdge(
                  edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), true);
          break;
        case UNDIRECTED:
          e =
              factory.newEdge(
                  edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), false);
          break;
        case MIXED:
          e =
              factory.newEdge(
                  edge.isAutoId() ? null : edge.getId(),
                  source,
                  target,
                  edge.getWeight(),
                  edge.getType().equals(EdgeType.UNDIRECTED) ? false : true);
          break;
      }

      flushToEdge(edge, e);
      edgeCount++;
      graph.addEdge(e);
    }
    workspace = null;
  }
Exemple #3
0
  private void doPoetGraph(String path, String poet) {
    System.out.println(poet);
    ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
    pc.newProject();
    Workspace workspace = pc.getCurrentWorkspace();
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
    UndirectedGraph directedGraph = graphModel.getUndirectedGraph();

    ExportController ec = Lookup.getDefault().lookup(ExportController.class);

    ArrayList<String> queue = new ArrayList<String>();
    ArrayList<Integer> depthqueue = new ArrayList<Integer>();
    queue.add(poet);
    depthqueue.add(0);
    int depth = 0;
    int nodecount = 0;

    while (queue.size() != 0 && depth < 3 && nodecount < 20) {
      String name = queue.remove(0);
      depth = depthqueue.remove(0);

      Node n = directedGraph.getNode(name);
      if (n == null) {
        n = graphModel.factory().newNode(name);
        n.getNodeData().setLabel(name);
        directedGraph.addNode(n);
        nodecount++;
      }

      Iterator<String> it = edges.get(poet).keySet().iterator();
      while (it.hasNext()) {
        String to = it.next();
        Node slave = directedGraph.getNode(to);
        if (slave == null) {
          slave = graphModel.factory().newNode(to);
          slave.getNodeData().setLabel(to);
          directedGraph.addNode(slave);
        }
        Edge e = graphModel.factory().newEdge(n, slave, edges.get(poet).get(to) * 15, false);
        directedGraph.addEdge(e);
        queue.add(to);
        depthqueue.add(depth + 1);
      }
    }

    // Preview
    PreviewModel previewModel = Lookup.getDefault().lookup(PreviewController.class).getModel();
    previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
    previewModel.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.BLUE));
    previewModel.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.005f));
    previewModel
        .getProperties()
        .putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.RED));
    previewModel
        .getProperties()
        .putValue(
            PreviewProperty.NODE_LABEL_FONT,
            previewModel
                .getProperties()
                .getFontValue(PreviewProperty.NODE_LABEL_FONT)
                .deriveFont(12));

    YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(1f));
    layout.setGraphModel(graphModel);
    layout.resetPropertiesValues();
    layout.setOptimalDistance(120f);

    layout.initAlgo();
    for (int i = 0; i < 100 && layout.canAlgo(); i++) {
      layout.goAlgo();
    }

    try {
      ec.exportFile(new File(path + poet + ".svg"));
    } catch (IOException ex) {
      ex.printStackTrace();
      return;
    }
  }