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"));
    }
  }
  private void writeNodes(XMLStreamWriter xmlWriter, HierarchicalGraph graph) throws Exception {
    if (cancel) {
      return;
    }
    xmlWriter.writeStartElement(NODES);

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

    NodeIterable nodeIterable = exportHierarchy ? graph.getNodesTree() : graph.getNodes();
    for (Node node : nodeIterable) {
      xmlWriter.writeStartElement(NODE);

      String id = node.getNodeData().getId();
      xmlWriter.writeAttribute(NODE_ID, id);
      if (node.getNodeData().getLabel() != null && !node.getNodeData().getLabel().isEmpty()) {
        xmlWriter.writeAttribute(NODE_LABEL, node.getNodeData().getLabel());
      }

      if (exportHierarchy) {
        Node parent = graph.getParent(node);
        if (parent != null) {
          xmlWriter.writeAttribute(NODE_PID, parent.getNodeData().getId());
        }
      }

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

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

      if (exportSize) {
        writeNodeSize(xmlWriter, node);
      }

      if (exportPosition) {
        writeNodePosition(xmlWriter, node);
      }

      if (exportColors) {
        writeNodeColor(xmlWriter, node);
      }

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

    xmlWriter.writeEndElement();
  }
  private void writeAttributes(XMLStreamWriter xmlWriter, AttributeTable table) throws Exception {
    List<AttributeColumn> staticCols = new ArrayList<AttributeColumn>();
    List<AttributeColumn> dynamicCols = new ArrayList<AttributeColumn>();
    String attClass = table == attributeModel.getNodeTable() ? "node" : "edge";

    for (AttributeColumn col : table.getColumns()) {

      if (!col.getOrigin().equals(AttributeOrigin.PROPERTY)) {
        if (exportDynamic && col.getType().isDynamicType()) {
          dynamicCols.add(col);
        } else {
          staticCols.add(col);
        }
      } else if (exportDynamic
          && col.getType().isDynamicType()
          && col.getType() != AttributeType.TIME_INTERVAL
          && col.getOrigin().equals(AttributeOrigin.PROPERTY)
          && col.getIndex() == PropertiesColumn.EDGE_WEIGHT.getIndex()) {
        dynamicCols.add(col);
      }
    }

    if (!staticCols.isEmpty()) {
      writeAttributes(xmlWriter, staticCols.toArray(new AttributeColumn[0]), "static", attClass);
    }
    if (!dynamicCols.isEmpty()) {
      writeAttributes(xmlWriter, dynamicCols.toArray(new AttributeColumn[0]), "dynamic", attClass);
    }
  }
Exemple #4
0
  public void setValue(AttributeValue value) {
    AttributeColumn column = value.getColumn();
    if (attributeTable.getColumn(column.getIndex()) != column) {
      column = attributeTable.getColumn(column);
      if (column == null) {
        throw new IllegalArgumentException(
            "The "
                + attributeTable.getName()
                + " value column "
                + value.getColumn().getId()
                + " with index "
                + value.getColumn().getIndex()
                + " doesn't exist");
      }
      value = attributeTable.getFactory().newValue(column, value.getValue());
    }

    setValue(column.getIndex(), (AttributeValueImpl) value, true);
  }
Exemple #5
0
 public Object getValue(AttributeColumn column) {
   if (column == null) {
     throw new NullPointerException();
   }
   updateColumns();
   int index = column.getIndex();
   if (checkIndexRange(index)) {
     AttributeValue val = values[index];
     if (val.getColumn() == column) {
       return val.getValue();
     }
   }
   return null;
 }
  private void writeAttributes(
      XMLStreamWriter xmlWriter, AttributeColumn[] cols, String mode, String attClass)
      throws Exception {
    if (exportAttributes) {
      xmlWriter.writeStartElement(ATTRIBUTES);
      xmlWriter.writeAttribute(ATTRIBUTES_CLASS, attClass);
      xmlWriter.writeAttribute(ATTRIBUTES_MODE, mode);

      for (AttributeColumn col : cols) {
        if (!col.getOrigin().equals(AttributeOrigin.PROPERTY)
            || (exportDynamic
                && col.getOrigin().equals(AttributeOrigin.PROPERTY)
                && col.getIndex() == PropertiesColumn.EDGE_WEIGHT.getIndex())) {
          xmlWriter.writeStartElement(ATTRIBUTE);
          xmlWriter.writeAttribute(ATTRIBUTE_ID, col.getId());
          xmlWriter.writeAttribute(ATTRIBUTE_TITLE, col.getTitle());
          if (col.getType().equals(AttributeType.INT)) {
            xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "integer");
          } else if (col.getType().isListType()) {
            if (col.getType().equals(AttributeType.LIST_INTEGER)) {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "listint");
            } else if (col.getType().equals(AttributeType.LIST_CHARACTER)) {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "listchar");
            } else {
              xmlWriter.writeAttribute(
                  ATTRIBUTE_TYPE, col.getType().getTypeString().toLowerCase().replace("_", ""));
            }
          } else if (col.getType().isDynamicType()) {
            AttributeType staticType = TypeConvertor.getStaticType(col.getType());
            if (staticType.equals(AttributeType.INT)) {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, "integer");
            } else {
              xmlWriter.writeAttribute(ATTRIBUTE_TYPE, staticType.getTypeString().toLowerCase());
            }
          } else {
            xmlWriter.writeAttribute(ATTRIBUTE_TYPE, col.getType().getTypeString().toLowerCase());
          }
          if (col.getDefaultValue() != null) {
            xmlWriter.writeStartElement(ATTRIBUTE_DEFAULT);
            xmlWriter.writeCharacters(col.getDefaultValue().toString());
            xmlWriter.writeEndElement();
          }
          xmlWriter.writeEndElement();
        }
      }

      xmlWriter.writeEndElement();
    }
  }
 private void writeColumns(Clusterer clusterer) {
   Cluster[] clusters = clusterer.getClusters();
   if (clusters != null && clusters.length > 0) {
     ClustererBuilder builder = getBuilder(clusterer);
     AttributeModel am = Lookup.getDefault().lookup(AttributeController.class).getModel();
     String id = "clustering_" + builder.getName();
     String title = "Clustering (" + builder.getName() + ")";
     AttributeColumn col = am.getNodeTable().getColumn(id);
     if (col == null) {
       col =
           am.getNodeTable()
               .addColumn(id, title, AttributeType.INT, AttributeOrigin.COMPUTED, null);
       StatusDisplayer.getDefault()
           .setStatusText("A new column \"" + title + "\" has been created");
     }
     for (int i = 0; i < clusters.length; i++) {
       Integer clusterId = new Integer(i);
       for (Node n : clusters[i].getNodes()) {
         n.getNodeData().getAttributes().setValue(col.getIndex(), clusterId);
       }
     }
   }
 }
  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 writeAttValues(
     XMLStreamWriter xmlWriter, AttributeRow row, TimeInterval visibleInterval) throws Exception {
   xmlWriter.writeStartElement(ATTVALUES);
   for (AttributeValue val : row.getValues()) {
     AttributeColumn col = val.getColumn();
     if (!col.getOrigin().equals(AttributeOrigin.PROPERTY)
         || (exportDynamic
             && col.getOrigin().equals(AttributeOrigin.PROPERTY)
             && col.getIndex() == PropertiesColumn.EDGE_WEIGHT.getIndex())) {
       AttributeType type = col.getType();
       if (type.isDynamicType()) {
         DynamicType dynamicValue = (DynamicType) val.getValue();
         if (dynamicValue != null && visibleInterval != null && exportDynamic) {
           List<Interval<?>> intervals =
               dynamicValue.getIntervals(visibleInterval.getLow(), visibleInterval.getHigh());
           for (Interval<?> interval : intervals) {
             Object value = interval.getValue();
             if (value != null) {
               xmlWriter.writeStartElement(ATTVALUE);
               xmlWriter.writeAttribute(ATTVALUE_FOR, col.getId());
               xmlWriter.writeAttribute(ATTVALUE_VALUE, value.toString());
               if (!Double.isInfinite(interval.getLow())) {
                 String intervalLow = formatTime(interval.getLow());
                 xmlWriter.writeAttribute(
                     interval.isLowExcluded() ? START_OPEN : START, intervalLow);
               }
               if (!Double.isInfinite(interval.getHigh())) {
                 String intervalHigh = formatTime(interval.getHigh());
                 xmlWriter.writeAttribute(
                     interval.isHighExcluded() ? END_OPEN : END, intervalHigh);
               }
               xmlWriter.writeEndElement();
             }
           }
         } else if (dynamicValue != null) {
           TimeInterval interval = visibleInterval;
           if (interval == null) {
             interval = new TimeInterval();
           }
           Object value =
               DynamicUtilities.getDynamicValue(
                   dynamicValue, interval.getLow(), interval.getHigh());
           if (value != null) {
             xmlWriter.writeStartElement(ATTVALUE);
             xmlWriter.writeAttribute(ATTVALUE_FOR, val.getColumn().getId());
             xmlWriter.writeAttribute(ATTVALUE_VALUE, value.toString());
             xmlWriter.writeEndElement();
           }
         }
       } else {
         if (val.getValue() != null) {
           xmlWriter.writeStartElement(ATTVALUE);
           xmlWriter.writeAttribute(ATTVALUE_FOR, col.getId());
           xmlWriter.writeAttribute(ATTVALUE_VALUE, val.getValue().toString());
           xmlWriter.writeEndElement();
         }
       }
     }
   }
   xmlWriter.writeEndElement();
 }