Пример #1
0
 @Override
 public Object getValueFor(Edge edge) {
   if (showEdgesNodesLabels) {
     return edge.getSource().getId() + " - " + edge.getSource().getLabel();
   } else {
     return edge.getSource().getId();
   }
 }
  private void updateEdges(HierarchicalGraph graph) {
    Modeler edgeInit = engine.getModelClasses()[AbstractEngine.CLASS_EDGE].getCurrentModeler();
    Modeler arrowInit = engine.getModelClasses()[AbstractEngine.CLASS_ARROW].getCurrentModeler();

    EdgeIterable edgeIterable;
    edgeIterable = graph.getEdges();

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

    TimeInterval timeInterval = DynamicUtilities.getVisibleInterval(dynamicModel);

    for (Edge edge : edgeIterable) {
      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.setMinWeight(minWeight);
    limits.setMaxWeight(maxWeight);
  }
  @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 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 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);
  }
  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);
      }
    }
  }