예제 #1
0
  @Override
  public Container create(Integer graphId, Map<String, Object> extraParam) {
    LOGGER.debug("Gephi: Setup container, nodes and edges.");

    Container container = CONTAINER_FACTORY.newContainer();
    container.setReport(new Report());

    try (Connection con = graphDataSource.getConnection()) {
      con.setAutoCommit(false);

      Map<String, Object> graphParam = graphDataSource.getGraphByID(con, graphId);
      graphParam.putAll(extraParam);

      ContainerLoader cl = container.getLoader();

      setupDirectedMode(cl, graphParam);

      cl.addNodeColumn(KEY_URL, String.class);
      cl.addNodeColumn(KEY_TAG, String[].class);

      addNodes(graphId, con, cl, graphParam);
      addEdges(graphId, con, cl, graphParam);
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }

    return container;
  }
  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"));
    }
  }
예제 #3
0
  public void script() {

    NetworkLayer nl = MultiLayerNetwork.getSelected();

    if (pc == null) pc = Lookup.getDefault().lookup(ProjectController.class);

    // Init a project - and therefore a workspace
    Project pro = pc.getCurrentProject();

    if (pro == null) {
      pc.newProject();
      pro = pc.getCurrentProject();
    }

    Workspace workspace = pc.newWorkspace(pro);

    // Get controllers and models
    ImportController importController = Lookup.getDefault().lookup(ImportController.class);
    GraphModel graphModel =
        Lookup.getDefault().lookup(GraphController.class).getGraphModel(workspace);
    //        AttributeModel attributeModel =
    // Lookup.getDefault().lookup(AttributeController.class).getModel();

    // Import database
    EdgeListHiveDatabaseImpl db = new EdgeListHiveDatabaseImpl();

    db.setDBName("default"); // !!!
    db.setHost("127.0.0.1");
    //       db.setHost( HadoopClusterDefaults.HIVE_SERVER_IP );
    db.setPort(10000); // !!!

    db.setUsername("kamir");
    db.setPasswd("8cwrr");

    db.setSQLDriver(new HiveJDBCDriver());

    String tab = "edgelist_es_de_erfurt2";

    db.setNodeQuery(
        "SELECT UNIQUE( " + tab + ".source ) AS id, " + tab + ".source AS label FROM " + tab);
    db.setEdgeQuery(
        "SELECT "
            + tab
            + ".source AS source, "
            + tab
            + ".target AS target "
            + tab
            + ".q0 AS weight FROM "
            + tab);

    ImporterEdgeList edgeListImporter = new ImporterEdgeList();

    Container container = null;

    String url = SQLUtils.getUrl(db.getSQLDriver(), db.getHost(), db.getPort(), db.getDBName());

    System.out.println(url);

    javax.swing.JOptionPane.showMessageDialog(null, "Go ... Hive! \n> " + url);

    try {
      container = importController.importDatabase(db, edgeListImporter);

      //            container.setAllowAutoNode(false);      //Don't create missing nodes
      container.getLoader().setEdgeDefault(EdgeDirectionDefault.UNDIRECTED); // Force UNDIRECTED

      // Append imported data to GraphAPI
      importController.process(container, new DefaultProcessor(), workspace);
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      if (container != null) System.out.println(container.getReport().getText());
    }

    // See if graph is well imported
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    System.out.println("Nodes: " + graph.getNodeCount());
    System.out.println("Edges: " + graph.getEdgeCount());

    // Layout - 100 Yifan Hu passes
    //        YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(1f));
    //        layout.setGraphModel(graphModel);
    //        layout.resetPropertiesValues();
    //        layout.initAlgo();
    //        for (int i = 0; i < 100 && layout.canAlgo(); i++) {
    //            layout.goAlgo();
    //        }
    //        layout.endAlgo();

  }
  private void finishImport(Container[] containers) {
    Report finalReport = new Report();
    for (Container container : containers) {
      if (container.verify()) {
        Report report = container.getReport();
        report.close();
        finalReport.append(report);
      } else {
        // TODO
      }
    }
    finalReport.close();

    // Report panel
    ReportPanel reportPanel = new ReportPanel();
    reportPanel.setData(finalReport, containers);
    DialogDescriptor dd =
        new DialogDescriptor(
            reportPanel, NbBundle.getMessage(DesktopImportControllerUI.class, "ReportPanel.title"));
    Object response = DialogDisplayer.getDefault().notify(dd);
    reportPanel.destroy();
    finalReport.clean();
    for (Container c : containers) {
      c.getReport().clean();
    }
    if (!response.equals(NotifyDescriptor.OK_OPTION)) {
      return;
    }
    final Processor processor = reportPanel.getProcessor();

    // Project
    Workspace workspace = null;
    ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
    ProjectControllerUI pcui = Lookup.getDefault().lookup(ProjectControllerUI.class);
    if (pc.getCurrentProject() == null) {
      pcui.newProject();
      workspace = pc.getCurrentWorkspace();
    }

    // Process
    final ProcessorUI pui = getProcessorUI(processor);
    final ValidResult validResult = new ValidResult();
    if (pui != null) {
      try {
        final JPanel panel = pui.getPanel();
        if (panel != null) {
          SwingUtilities.invokeAndWait(
              new Runnable() {
                @Override
                public void run() {
                  String title =
                      NbBundle.getMessage(
                          DesktopImportControllerUI.class,
                          "DesktopImportControllerUI.processor.ui.dialog.title");

                  pui.setup(processor);
                  final DialogDescriptor dd2 = new DialogDescriptor(panel, title);
                  if (panel instanceof ValidationPanel) {
                    ValidationPanel vp = (ValidationPanel) panel;
                    vp.addChangeListener(
                        new ChangeListener() {
                          @Override
                          public void stateChanged(ChangeEvent e) {
                            dd2.setValid(!((ValidationPanel) e.getSource()).isProblem());
                          }
                        });
                    dd2.setValid(!vp.isProblem());
                  }
                  Object result = DialogDisplayer.getDefault().notify(dd2);
                  if (result.equals(NotifyDescriptor.CANCEL_OPTION)
                      || result.equals(NotifyDescriptor.CLOSED_OPTION)) {
                    validResult.setResult(false);
                  } else {
                    pui.unsetup(); // true
                    validResult.setResult(true);
                  }
                }
              });
        }
      } catch (InterruptedException ex) {
        Exceptions.printStackTrace(ex);
      } catch (InvocationTargetException ex) {
        Exceptions.printStackTrace(ex);
      }
    }
    if (validResult.isResult()) {
      controller.process(containers, processor, workspace);

      // StatusLine notify
      StatusDisplayer.getDefault()
          .setStatusText(
              NbBundle.getMessage(
                  DesktopImportControllerUI.class,
                  "DesktopImportControllerUI.status.multiImportSuccess",
                  containers.length));
    }
  }