Exemple #1
0
 private void setupDirectedMode(ContainerLoader cl, Map<String, Object> graphParam) {
   Integer directed = (Integer) graphParam.get("directed");
   if (Integer.valueOf(2).equals(directed)) {
     cl.setEdgeDefault(EdgeDirectionDefault.MIXED);
   } else if (Integer.valueOf(1).equals(directed)) {
     cl.setEdgeDefault(EdgeDirectionDefault.DIRECTED);
   } else {
     cl.setEdgeDefault(EdgeDirectionDefault.UNDIRECTED);
   }
 }
  public void convertToNetwork() throws IOException, InvalidFormatException {

    container = MyFileImporter.container;
    container.setEdgeDefault(EdgeDefault.UNDIRECTED);

    String firstDelimiter;
    String secondDelimiter;
    firstDelimiter = Utils.getCharacter(MyFileImporter.firstConnectorDelimiter);
    secondDelimiter = Utils.getCharacter(MyFileImporter.secondConnectorDelimiter);
    boolean oneTypeOfAgent =
        MyFileImporter.getFirstConnectedAgent().equals(MyFileImporter.getSecondConnectedAgent());

    nbColumnFirstAgent = MyFileImporter.firstConnectedAgentIndex;
    nbColumnSecondAgent = MyFileImporter.secondConnectedAgentIndex;

    Integer lineCounter = 0;

    InputStream inp;
    inp = new FileInputStream(fileName);
    Workbook wb = WorkbookFactory.create(inp);

    Row row;
    Sheet sheet = wb.getSheet(sheetName);
    int startingRow;
    if (MyFileImporter.headersPresent) {
      startingRow = 1;
    } else {
      startingRow = 0;
    }
    Set<String> linesFirstAgent = new HashSet();
    Set<String> linesSecondAgent = new HashSet();
    for (int i = startingRow; i <= sheet.getLastRowNum(); i++) {

      row = sheet.getRow(i);
      if (row == null) {
        break;
      }

      Cell cell = row.getCell(nbColumnFirstAgent);
      if (cell == null) {
        Issue issue =
            new Issue(
                "problem with line "
                    + lineCounter
                    + " (empty column "
                    + MyFileImporter.getFirstConnectedAgent()
                    + "). It was skipped in the conversion",
                Issue.Level.WARNING);
        MyFileImporter.getStaticReport().logIssue(issue);
        continue;
      }

      String firstAgent = row.getCell(nbColumnFirstAgent).getStringCellValue();

      if (firstAgent == null || firstAgent.isEmpty()) {
        Issue issue =
            new Issue(
                "problem with line "
                    + lineCounter
                    + " (empty column "
                    + MyFileImporter.getFirstConnectedAgent()
                    + "). It was skipped in the conversion",
                Issue.Level.WARNING);
        MyFileImporter.getStaticReport().logIssue(issue);
        continue;
      }

      if (MyFileImporter.removeDuplicates) {
        boolean newLine = linesFirstAgent.add(firstAgent);
        if (!newLine) {
          continue;
        }
      }

      String secondAgent = null;

      if (!oneTypeOfAgent) {
        cell = row.getCell(nbColumnSecondAgent);
        if (cell == null) {
          Issue issue =
              new Issue(
                  "problem with line "
                      + lineCounter
                      + " (empty column "
                      + MyFileImporter.getFirstConnectedAgent()
                      + "). It was skipped in the conversion",
                  Issue.Level.WARNING);
          MyFileImporter.getStaticReport().logIssue(issue);
          continue;
        }
        secondAgent = row.getCell(nbColumnSecondAgent).getStringCellValue();
        if (secondAgent == null || secondAgent.isEmpty()) {
          Issue issue =
              new Issue(
                  "problem with line "
                      + lineCounter
                      + " (empty column "
                      + MyFileImporter.getSecondConnectedAgent()
                      + "). It was skipped in the conversion",
                  Issue.Level.WARNING);
          MyFileImporter.getStaticReport().logIssue(issue);
          continue;
        }
        if (MyFileImporter.removeDuplicates) {
          boolean newLine = linesFirstAgent.add(firstAgent);
          if (!newLine) {
            continue;
          }
        }
      }
      lineCounter++;

      String[] firstAgentSplit;
      String[] secondAgentSplit;

      if (firstDelimiter != null) {
        firstAgentSplit = firstAgent.trim().split(firstDelimiter);
      } else {
        firstAgentSplit = new String[1];
        firstAgentSplit[0] = firstAgent;
      }
      for (String node : firstAgentSplit) {
        nodesFirst.add(node.trim());
      }

      if (!oneTypeOfAgent) {

        if (secondDelimiter != null) {
          secondAgentSplit = secondAgent.trim().split(secondDelimiter);
        } else {
          secondAgentSplit = new String[1];
          secondAgentSplit[0] = secondAgent;
        }
        for (String node : secondAgentSplit) {
          nodesSecond.add(node.trim());
        }
      } else {
        secondAgentSplit = null;
      }

      String[] both = ArrayUtils.addAll(firstAgentSplit, secondAgentSplit);
      // let's find all connections between all the tags for this picture
      Utils usefulTools = new Utils();
      List<String> connections = usefulTools.getListOfLinks(both, MyFileImporter.removeSelfLoops);
      edges.addAll(connections);
    }

    NodeDraft node;
    AttributeTable atNodes = container.getAttributeModel().getNodeTable();
    AttributeColumn acFrequency = atNodes.addColumn("frequency", AttributeType.INT);
    AttributeColumn acType = atNodes.addColumn("type", AttributeType.STRING);

    for (String n : nodesFirst.elementSet()) {
      node = container.factory().newNodeDraft();
      node.setId(n);
      node.setLabel(n);
      node.addAttributeValue(acFrequency, nodesFirst.count(n));
      node.addAttributeValue(acType, MyFileImporter.getFirstConnectedAgent());
      container.addNode(node);
    }

    for (String n : nodesSecond.elementSet()) {
      node = container.factory().newNodeDraft();
      node.setId(n);
      node.setLabel(n);
      node.addAttributeValue(acFrequency, nodesSecond.count(n));
      node.addAttributeValue(acType, MyFileImporter.getSecondConnectedAgent());
      container.addNode(node);
    }

    // loop for edges
    Integer idEdge = 0;
    EdgeDraft edge;
    for (String e : edges.elementSet()) {
      System.out.println("edge: " + e);

      String sourceNode = e.split("\\|")[0];
      String targetNode = e.split("\\|")[1];
      if (!MyFileImporter.innerLinksIncluded) {
        if ((nodesFirst.contains(sourceNode) & nodesFirst.contains(targetNode))
            || (nodesSecond.contains(sourceNode) & nodesSecond.contains(targetNode))) {
          continue;
        }
      }
      edge = container.factory().newEdgeDraft();
      idEdge = idEdge + 1;
      edge.setSource(container.getNode(sourceNode));
      edge.setTarget(container.getNode(targetNode));
      edge.setWeight((float) edges.count(e));
      edge.setId(String.valueOf(idEdge));
      edge.setType(EdgeDraft.EdgeType.UNDIRECTED);
      container.addEdge(edge);
    }
  }