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

    // Get controllers and models
    ImportController importController = Lookup.getDefault().lookup(ImportController.class);

    // Import file
    Container container;
    try {
      File file =
          new File(
              getClass().getResource("/org/gephi/toolkit/demos/resources/polblogs.gml").toURI());
      container = importController.importFile(file);
      container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED); // Force DIRECTED
      container.setAllowAutoNode(false); // Don't create missing nodes
    } catch (Exception ex) {
      ex.printStackTrace();
      return;
    }

    // Append imported data to GraphAPI
    importController.process(container, new DefaultProcessor(), workspace);

    // List node columns
    AttributeController ac = Lookup.getDefault().lookup(AttributeController.class);
    AttributeModel model = ac.getModel();
    for (AttributeColumn col : model.getNodeTable().getColumns()) {
      System.out.println(col);
    }

    // Add boolean column
    AttributeColumn testCol = model.getNodeTable().addColumn("test", AttributeType.BOOLEAN);

    // Write values to nodes
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
    for (Node n : graphModel.getGraph().getNodes()) {
      n.getNodeData().getAttributes().setValue(testCol.getIndex(), Boolean.TRUE);
    }

    // Iterate values - fastest
    AttributeColumn sourceCol = model.getNodeTable().getColumn("source");
    for (Node n : graphModel.getGraph().getNodes()) {
      System.out.println(n.getNodeData().getAttributes().getValue(sourceCol.getIndex()));
    }

    // Iterate values - normal
    for (Node n : graphModel.getGraph().getNodes()) {
      System.out.println(n.getNodeData().getAttributes().getValue("source"));
    }
  }
  public AppearanceModelImpl(Workspace workspace) {
    this.workspace = workspace;
    this.graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel(workspace);
    this.defaultInterpolator = Interpolator.LINEAR;
    this.functionLock = new Object();
    this.transformerUIs = initTransformerUIs();
    this.nodeTransformers = initNodeTransformers();
    this.edgeTransformers = initEdgeTransformers();
    this.forcedPartition = new HashSet<String>();
    this.forcedRanking = new HashSet<String>();
    this.forcedColumnsRefresh = new ArrayList<Column>();

    // Functions
    functionsMain = new FunctionsModel(graphModel.getGraph());
    refreshFunctions(graphModel.getGraph());
  }
Example #3
0
  @Override
  public void loop(GraphView window, Interval interval) {
    Graph graph = graphModel.getGraph(window);

    int count = graph.getEdgeCount();

    graph.setAttribute(NB_EDGES, count, interval.getLow());
    graph.setAttribute(NB_EDGES, count, interval.getHigh());

    counts.put(interval.getLow(), count);
    counts.put(interval.getHigh(), count);
  }
 public void setEnable(boolean enable) {
   if (enable) {
     if (observer == null) {
       observer = graphModel.createGraphObserver(graphModel.getGraph(), false);
     }
   } else if (observer != null && !observer.isDestroyed()) {
     observer.destroy();
     observer = null;
   }
   if (!isAlive()) {
     start();
   }
 }
  public boolean execute() {
    GraphModel graphModel = workspace.getLookup().lookup(GraphModel.class);
    Graph graph = null;
    if (exportVisible) {
      graph = graphModel.getGraphVisible();
    } else {
      graph = graphModel.getGraph();
    }
    try {
      exportData(graph);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return !cancel;
  }
  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 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;
  }