示例#1
0
 @Override
 public Object getValueFor(Edge edge) {
   if (showEdgesNodesLabels) {
     return edge.getTarget().getId() + " - " + edge.getTarget().getLabel();
   } else {
     return edge.getTarget().getId();
   }
 }
 private void writeEdgeColor(XMLStreamWriter xmlWriter, Edge edge) throws Exception {
   if (edge.getEdgeData().r() != -1) { // Edge has custom color
     int r = Math.round(edge.getEdgeData().r() * 255f);
     int g = Math.round(edge.getEdgeData().g() * 255f);
     int b = Math.round(edge.getEdgeData().b() * 255f);
     if (r != 0 || g != 0 || b != 0) {
       xmlWriter.writeStartElement(VIZ, EDGE_COLOR, VIZ_NAMESPACE);
       xmlWriter.writeAttribute("r", "" + r);
       xmlWriter.writeAttribute("g", "" + g);
       xmlWriter.writeAttribute("b", "" + b);
       if (edge.getEdgeData().alpha() != 1f) {
         xmlWriter.writeAttribute("a", "" + b);
       }
       xmlWriter.writeEndElement();
     }
   }
 }
示例#3
0
 @Override
 public Object getValueFor(Edge edge) {
   if (edge.isDirected()) {
     return NbBundle.getMessage(EdgesDataTable.class, "EdgeDataTable.type.column.directed");
   } else {
     return NbBundle.getMessage(
         EdgesDataTable.class, "EdgeDataTable.type.column.undirected");
   }
 }
  public void coarsen(HierarchicalGraph g) {
    HierarchicalGraph graph = g;
    int retract = 0;
    int count = 0;
    for (Edge e : graph.getEdgesAndMetaEdges().toArray()) {
      Node a = e.getSource();
      Node b = e.getTarget();
      count++;
      if (graph.getParent(a) == graph.getParent(b) && graph.getLevel(a) == 0) {
        float x = (a.getNodeData().x() + b.getNodeData().x()) / 2;
        float y = (a.getNodeData().y() + b.getNodeData().y()) / 2;

        Node parent = graph.groupNodes(new Node[] {a, b});
        parent.getNodeData().setX(x);
        parent.getNodeData().setY(y);
        graph.retract(parent);
        retract++;
      }
    }
  }
  private void writeEdge(Edge edge, boolean writeSeparator) throws IOException {
    if (edge != null) {
      if (edgeWeight) {
        writer.append(Float.toString(edge.getWeight()));
      } else {
        writer.append(Float.toString(1f));
      }
      if (writeSeparator) {
        writer.append(SEPARATOR);
      }

    } else {
      if (writeZero) {
        writer.append("0");
      }
      if (writeSeparator) {
        writer.append(SEPARATOR);
      }
    }
  }
  @Override
  public boolean evaluate(Graph graph, Edge edge) {
    DirectedGraph directedGraph = (DirectedGraph) graph;

    // Make sure the edge is not  self loop
    if (!edge.isSelfLoop()) {
      // Get the potential mutual edge
      Edge mutualEdge = directedGraph.getEdge(edge.getTarget(), edge.getSource());

      // Remove the mutual edge if exists
      // Make sure only one of the two mutual edges will bre removed
      if (mutualEdge != null && mutualEdge.getId() < edge.getId()) {
        return false;
      }
    }
    return true;
  }
  public void updateMetaEdges(HierarchicalGraph graph) {
    Modeler edgeInit = engine.getModelClasses()[AbstractEngine.CLASS_EDGE].getCurrentModeler();
    Modeler arrowInit = engine.getModelClasses()[AbstractEngine.CLASS_ARROW].getCurrentModeler();

    float minWeight = Float.POSITIVE_INFINITY;
    float maxWeight = Float.NEGATIVE_INFINITY;

    TimeInterval timeInterval = DynamicUtilities.getVisibleInterval(dynamicModel);

    for (Edge edge : graph.getMetaEdges()) {
      if (edge.getSource().getNodeData().getModel() == null
          || edge.getTarget().getNodeData().getModel() == null) {
        continue;
      }
      float weight = 1f;
      if (timeInterval == null) {
        weight = edge.getWeight();
      } else {
        weight = edge.getWeight(timeInterval.getLow(), timeInterval.getHigh());
      }
      minWeight = Math.min(minWeight, weight);
      maxWeight = Math.max(maxWeight, weight);
      Edge2dModel obj = (Edge2dModel) edge.getEdgeData().getModel();
      if (obj == null) {
        // Model is null, ADD
        obj = (Edge2dModel) edgeInit.initModel(edge.getEdgeData());
        engine.addObject(AbstractEngine.CLASS_EDGE, obj);
        if (!undirected && vizConfig.isShowArrows() && !edge.isSelfLoop()) {
          Arrow2dModel arrowObj = (Arrow2dModel) arrowInit.initModel(edge.getEdgeData());
          engine.addObject(AbstractEngine.CLASS_ARROW, arrowObj);
          arrowObj.setCacheMarker(cacheMarker);
          arrowObj.setWeight(weight);
          obj.setArrow(arrowObj);
        }
      } else if (!obj.isValid()) {
        engine.addObject(AbstractEngine.CLASS_EDGE, obj);
        if (!undirected && vizConfig.isShowArrows() && !edge.isSelfLoop()) {
          Arrow2dModel arrowObj = obj.getArrow();
          engine.addObject(AbstractEngine.CLASS_ARROW, arrowObj);
          arrowObj.setCacheMarker(cacheMarker);
          arrowObj.setWeight(weight);
        }
      } else {
        if (!undirected && vizConfig.isShowArrows() && !edge.isSelfLoop() && edge.isDirected()) {
          Arrow2dModel arrowObj = obj.getArrow();
          arrowObj.setCacheMarker(cacheMarker);
          arrowObj.setWeight(weight);
        }
      }
      obj.setWeight(weight);
      obj.setCacheMarker(cacheMarker);
    }

    limits.setMinMetaWeight(minWeight);
    limits.setMaxMetaWeight(maxWeight);
  }
示例#8
0
 @Override
 public Object getValueFor(Edge edge) {
   return edge.getTypeLabel() != null ? edge.getTypeLabel().toString() : null;
 }
  private void writeEdges(XMLStreamWriter xmlWriter, HierarchicalGraph graph) throws Exception {
    if (cancel) {
      return;
    }
    xmlWriter.writeStartElement(EDGES);

    AttributeColumn dynamicCol =
        dynamicCol = attributeModel.getEdgeTable().getColumn(DynamicModel.TIMEINTERVAL_COLUMN);

    EdgeIterable edgeIterable =
        exportHierarchy ? graph.getEdgesTree() : graph.getEdgesAndMetaEdges();
    for (Edge edge : edgeIterable) {
      xmlWriter.writeStartElement(EDGE);

      if (edge.getEdgeData().getId() != null
          && !edge.getEdgeData().getId().equals(Integer.toString(edge.getId()))) {
        xmlWriter.writeAttribute(EDGE_ID, edge.getEdgeData().getId());
      }
      xmlWriter.writeAttribute(EDGE_SOURCE, edge.getSource().getNodeData().getId());
      xmlWriter.writeAttribute(EDGE_TARGET, edge.getTarget().getNodeData().getId());

      if (edge.isDirected() && graphModel.isMixed()) {
        xmlWriter.writeAttribute(EDGE_TYPE, "directed");
      } else if (!edge.isDirected() && graphModel.isMixed()) {
        xmlWriter.writeAttribute(EDGE_TYPE, "undirected");
      }

      String label = edge.getEdgeData().getLabel();
      if (label != null && !label.isEmpty()) {
        xmlWriter.writeAttribute(EDGE_LABEL, label);
      }

      float weight = edge.getWeight();
      if (weight != 1f) {
        xmlWriter.writeAttribute(EDGE_WEIGHT, "" + weight);
      }

      if (exportDynamic && dynamicCol != null && visibleInterval != null) {
        TimeInterval timeInterval =
            (TimeInterval) edge.getEdgeData().getAttributes().getValue(dynamicCol.getIndex());
        if (timeInterval != null) {
          writeTimeInterval(xmlWriter, timeInterval);
        }
      }

      if (exportColors) {
        writeEdgeColor(xmlWriter, edge);
      }

      if (exportAttributes && edge.getEdgeData().getAttributes() != null) {
        AttributeRow attributeRow = (AttributeRow) edge.getEdgeData().getAttributes();
        writeAttValues(xmlWriter, attributeRow, visibleInterval);
      }

      xmlWriter.writeEndElement();
      Progress.progress(progress);
      if (cancel) {
        break;
      }
    }

    xmlWriter.writeEndElement();
  }
  private void exportData(Graph graph) throws Exception {
    int max = graph.getNodeCount();

    Progress.start(progressTicket, max);

    if (!list) {
      if (header) {
        writer.append(SEPARATOR);
        Node[] nodes = graph.getNodes().toArray();
        for (int i = 0; i < nodes.length; i++) {
          writeMatrixNode(nodes[i], i < nodes.length - 1);
        }
        writer.append(EOL);
      }
    }

    if (list) {
      Node[] nodes = graph.getNodes().toArray();
      for (int i = 0; i < nodes.length; i++) {
        Node n = nodes[i];
        List<Node> neighbours = new ArrayList<Node>();
        for (Edge e : graph.getEdges(n)) {
          if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
            Node m = graph.getOpposite(n, e);
            neighbours.add(m);
          }
        }

        for (Edge e : ((HierarchicalGraph) graph).getMetaEdges(n)) {
          if (!e.isDirected() || (e.isDirected() && n == e.getSource())) {
            Node m = graph.getOpposite(n, e);
            neighbours.add(m);
          }
        }

        writeListNode(n, !neighbours.isEmpty());
        for (int j = 0; j < neighbours.size(); j++) {
          writeListNode(neighbours.get(j), j < neighbours.size() - 1);
        }
        writer.append(EOL);
      }
    } else {
      if (graph instanceof DirectedGraph) {
        DirectedGraph directedGraph = (DirectedGraph) graph;
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : nodes) {
          if (cancel) {
            break;
          }
          writeMatrixNode(n, true);
          for (int j = 0; j < nodes.length; j++) {
            Node m = nodes[j];
            Edge e = directedGraph.getEdge(n, m);
            e = e == null ? ((HierarchicalDirectedGraph) directedGraph).getMetaEdge(n, m) : e;
            writeEdge(e, j < nodes.length - 1);
          }
          Progress.progress(progressTicket);
          writer.append(EOL);
        }
      } else if (graph instanceof UndirectedGraph) {
        UndirectedGraph undirectedGraph = (UndirectedGraph) graph;
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : nodes) {
          if (cancel) {
            break;
          }
          writeMatrixNode(n, true);
          for (int j = 0; j < nodes.length; j++) {
            Node m = nodes[j];
            Edge e = undirectedGraph.getEdge(n, m);
            e = e == null ? ((HierarchicalUndirectedGraph) undirectedGraph).getMetaEdge(n, m) : e;
            writeEdge(e, j < nodes.length - 1);
          }
          Progress.progress(progressTicket);
          writer.append(EOL);
        }
      } else {
        MixedGraph mixedGraph = (MixedGraph) graph;
        Node[] nodes = graph.getNodes().toArray();
        for (Node n : graph.getNodes()) {
          if (cancel) {
            break;
          }
          writeMatrixNode(n, true);
          for (int j = 0; j < nodes.length; j++) {
            Node m = nodes[j];
            Edge e = mixedGraph.getEdge(n, m);
            e = e == null ? ((HierarchicalMixedGraph) mixedGraph).getMetaEdge(n, m) : e;
            writeEdge(e, j < nodes.length - 1);
          }
          Progress.progress(progressTicket);
          writer.append(EOL);
        }
      }
    }

    graph.readUnlockAll();

    Progress.finish(progressTicket);
  }
  private void executeDynamic(DynamicStatistics statistics, DynamicLongTask dynamicLongTask) {
    GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
    GraphModel graphModel = graphController.getGraphModel();

    double window = statistics.getWindow();
    double tick = statistics.getTick();

    GraphView currentView = graphModel.getVisibleView();
    Interval bounds = statistics.getBounds();
    if (bounds == null) {
      if (currentView.isMainView()) {
        bounds = graphModel.getTimeBounds();
      } else {
        bounds = currentView.getTimeInterval();
      }
      statistics.setBounds(bounds);
    }

    if (dynamicLongTask != null) {
      // Count
      int c = (int) ((bounds.getHigh() - window - bounds.getLow()) / tick);
      dynamicLongTask.start(c);
    }

    // Init
    statistics.execute(graphModel);

    // Loop
    for (double low = bounds.getLow(); low <= bounds.getHigh() - window; low += tick) {
      double high = low + window;

      Graph graph = graphModel.getGraphVisible();

      graph.writeLock();

      GraphView view = graphModel.createView();
      Subgraph g = graphModel.getGraph(view);

      TimeIndex<Node> nodeIndex = graphModel.getNodeTimeIndex(currentView);
      if (Double.isInfinite(nodeIndex.getMinTimestamp())
          && Double.isInfinite(nodeIndex.getMaxTimestamp())) {
        for (Node node : graph.getNodes()) {
          g.addNode(node);
        }
      } else {
        for (Node node : nodeIndex.get(new Interval(low, high))) {
          g.addNode(node);
        }
      }

      TimeIndex<Edge> edgeIndex = graphModel.getEdgeTimeIndex(currentView);
      if (Double.isInfinite(edgeIndex.getMinTimestamp())
          && Double.isInfinite(edgeIndex.getMaxTimestamp())) {
        for (Edge edge : graph.getEdges()) {
          if (g.contains(edge.getSource()) && g.contains(edge.getTarget())) {
            g.addEdge(edge);
          }
        }
      } else {
        for (Edge edge : edgeIndex.get(new Interval(low, high))) {
          if (g.contains(edge.getSource()) && g.contains(edge.getTarget())) {
            g.addEdge(edge);
          }
        }
      }

      graph.writeUnlock();

      statistics.loop(g.getView(), new Interval(low, high));

      // Cancelled?
      if (dynamicLongTask != null && dynamicLongTask.isCancelled()) {
        return;
      } else if (dynamicLongTask != null) {
        dynamicLongTask.progress();
      }
    }
    statistics.end();
    model.addReport(statistics);
  }
示例#12
0
  @Test
  public void testPNG() {
    try {

      // Init a project - and therefore a workspace
      ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
      pc.newProject();
      Workspace workspace = pc.getCurrentWorkspace();

      // Append container to graph structure
      String sample = "/org/gephi/desktop/welcome/samples/Les Miserables.gexf";
      final InputStream stream = WelcomeTopComponent.class.getResourceAsStream(sample);
      ImportController importController = Lookup.getDefault().lookup(ImportController.class);
      FileImporter fileImporter = importController.getFileImporter(".gexf");
      Container container = importController.importFile(stream, fileImporter);

      importController.process(container, new DefaultProcessor(), workspace);

      // Add self loop
      GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
      Graph graph = graphController.getModel().getGraph();
      Node node = graph.getNode(12);
      System.out.println("Self loop " + node.getNodeData().getLabel());
      graph.addEdge(graphController.getModel().factory().newEdge(node, node, 31, true));

      // Set label edges
      for (Edge edge : graphController.getModel().getGraph().getEdges()) {
        edge.getEdgeData().setLabel("Label test");
      }

      // svg document

      // attribute ???
      AttributeController attributeController =
          Lookup.getDefault().lookup(AttributeController.class);
      AttributeModel attributeModel = null;

      // preview
      PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);
      PreviewModel previewModel = previewController.getModel(workspace);
      PreviewProperties previewProperties = previewModel.getProperties();
      previewController.refreshPreview(workspace);

      previewModel.getProperties().putValue("width", 1000);
      previewModel.getProperties().putValue("height", 1000);
      ProcessingTarget target =
          (ProcessingTarget)
              previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET, workspace);

      previewProperties.putValue(LegendModel.LEGEND_PROPERTIES, new LegendModel(workspace));

      LegendModel legendManager = previewProperties.getValue(LegendModel.LEGEND_PROPERTIES);
      Integer itemIndex = legendManager.getCurrentIndex();

      // creating item
      Item item = addGroupsItem(itemIndex, graph, attributeModel);
      //            Item item = addTextItem(itemIndex, graph, attributeModel);

      // add item
      LegendController.getInstance().addItemToLegendModel(workspace, item);

      // render
      previewController.refreshPreview(workspace);
      previewController.render(target);

      File fileToSave = new File("/Volumes/edubecks/edubecks/Untitled.png");
      OutputStream fos = new FileOutputStream(fileToSave);
      Writer writer = new OutputStreamWriter(fos, "UTF-8");

      //            // saving
      //            int width = 1000;
      //            int height = 1000;
      //            PGraphicsJava2D pg2 = (PGraphicsJava2D) target.getGraphics();
      //            BufferedImage img = new BufferedImage(width, height,
      // BufferedImage.TYPE_INT_ARGB);
      //
      //            System.out.println("@Var: pg2.pixels: "+pg2.pixels);
      //            img.setRGB(0, 0, width, height, pg2.pixels, 0, width);
      //            ImageIO.write(img, "png", fileToSave);
      //            stream.close();

      PNGExporter pngExporter = new PNGExporter();
      //            pngExporter.setHeight(2000);
      //            pngExporter.setWidth(2000);
      pngExporter.setWorkspace(workspace);
      pngExporter.setOutputStream(fos);
      pngExporter.execute();

    } catch (Exception e) {
      Exceptions.printStackTrace(e);
    }
  }
  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 saveGraph(Graph graph, boolean isFilterGraph) {

    int Counter = 0;
    JsonObject attrJson;

    // clear existing graph
    if (!isFilterGraph) {
      nodesMap.clear();
      edgesMap.clear();
    } else {
      nodesMapFilter.clear();
      edgesMapFilter.clear();
    }

    /*
     * Nodes Iteration
     */
    for (Node node : graph.getNodes()) {
      attrJson = new JsonObject();
      for (String attrKey : node.getAttributeKeys()) {
        attrJson.put(attrKey, node.getAttribute(attrKey));
      }

      attrJson.put("x", node.x());
      attrJson.put("y", node.y());
      attrJson.put("cR", node.r());
      attrJson.put("cG", node.g());
      attrJson.put("cB", node.b());
      attrJson.put("size", node.size());

      if (!isFilterGraph) {
        nodesMap.put(Counter++, attrJson);
      } else {
        nodesMapFilter.put(Counter++, attrJson);
      }
    }

    Counter = 0;

    /*
     * Edges Iteration
     */
    for (Edge edge : graph.getEdges()) {
      attrJson = new JsonObject();
      for (String attrKey : edge.getAttributeKeys()) {
        attrJson.put(attrKey, edge.getAttribute(attrKey));
      }
      attrJson.put("source", edge.getSource().getId());
      attrJson.put("target", edge.getTarget().getId());

      attrJson.put("cR", edge.r());
      attrJson.put("cG", edge.g());
      attrJson.put("cB", edge.b());
      attrJson.put("size", edge.getWeight());
      if (!isFilterGraph) {
        edgesMap.put(Counter++, attrJson);
      } else {
        edgesMapFilter.put(Counter++, attrJson);
      }
    }
  }