Exemple #1
0
  public void rn() throws IOException {
    List<RoadLink> links = loadToDatabase("/Users/duduba/gj.mif", "/Users/duduba/gj.mid", false);

    GraphDatabaseService graphDb = neo.getDb();
    Index<Node> nodeIndex = neo.getNodeIndex();

    for (RoadLink link : links) {
      Transaction tx = graphDb.beginTx();
      try {

        Node fromNode = nodeIndex.get("id", link.fromNode).getSingle();
        if (fromNode == null) {
          fromNode = graphDb.createNode();
          fromNode.setProperty("id", link.fromNode);
          nodeIndex.add(fromNode, "id", link.fromNode);
        }
        Node toNode = nodeIndex.get("id", link.toNode).getSingle();
        if (toNode == null) {
          toNode = graphDb.createNode();
          toNode.setProperty("id", link.toNode);
          nodeIndex.add(toNode, "id", link.toNode);
        }

        Relationship r = fromNode.createRelationshipTo(toNode, Neo.RelTypes.TO);
        r.setProperty("no", link.no);
        r.setProperty("name", link.name);

        tx.success();
      } finally {
        tx.finish();
      }
    }
    logger.debug("haha, it's ok!");
  }
  private static List<Double> getWeightVectorForClass(
      Map<String, List<LinkedHashMap<String, Object>>> documents,
      String key,
      List<Integer> featureIndexList,
      GraphDatabaseService db) {
    List<Double> weightVector;

    Transaction tx = db.beginTx();
    // Get class id
    Long classId =
        db.findNodesByLabelAndProperty(DynamicLabel.label("Class"), "name", key)
            .iterator()
            .next()
            .getId();

    // Get weight vector for class
    List<Long> longs =
        documents
            .get(key)
            .stream()
            .map(a -> ((Integer) a.get("feature")).longValue())
            .collect(Collectors.toList());

    weightVector =
        featureIndexList
            .stream()
            .map(i -> longs.contains(i.longValue()) ? tfidf(db, i.longValue(), classId) : 0.0)
            .collect(Collectors.toList());
    tx.success();
    tx.close();
    return weightVector;
  }
  private static List<Node> getAllClasses(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> finalClasses = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Class"))
        .forEach(a -> finalClasses.add(a));
    tx.success();
    tx.close();

    return finalClasses.stream().map(a -> a).collect(Collectors.toList());
  }
  private static Map<String, List<LinkedHashMap<String, Object>>> getFeaturesForAllClasses(
      GraphDatabaseService db) {
    List<Node> classes = getAllClasses(db);

    Map<String, List<LinkedHashMap<String, Object>>> featureMap = new HashMap<>();

    Transaction tx = db.beginTx();
    for (Node thisClass : classes) {
      featureMap.put((String) thisClass.getProperty("name"), getFeaturesForClass(db, thisClass));
    }
    tx.success();
    tx.close();

    return featureMap;
  }
 // TODO handle existing indexes
 @SuppressWarnings("unchecked")
 @Override
 public <T extends PropertyContainer> Index<T> createIndex(Class<T> type, String indexName, IndexType indexType) {
     Transaction tx = delegate.beginTx();
     try {
         IndexManager indexManager = delegate.index();
         if (isNode(type)) {
             if (indexManager.existsForNodes(indexName))
                 return (Index<T>) checkAndGetExistingIndex(indexName, indexType, indexManager.forNodes(indexName));
             Index<Node> index = indexManager.forNodes(indexName, indexConfigFor(indexType));
             return (Index<T>) index;
         } else {
             if (indexManager.existsForRelationships(indexName))
                 return (Index<T>) checkAndGetExistingIndex(indexName, indexType, indexManager.forRelationships(indexName));
             return (Index<T>) indexManager.forRelationships(indexName, indexConfigFor(indexType));
         }
     } finally {
         tx.success();tx.close();
     }
 }
  private static List<Integer> getFeatureIndexList(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> patterns = new ArrayList<>();
    GlobalGraphOperations.at(db)
        .getAllNodesWithLabel(DynamicLabel.label("Pattern"))
        .forEach(a -> patterns.add(a));

    Collections.sort(
        patterns,
        (a, b) ->
            ((Integer) b.getProperty("threshold"))
                .compareTo(((Integer) a.getProperty("threshold"))));

    List<Integer> patternIds =
        patterns.stream().map(a -> ((Long) a.getId()).intValue()).collect(Collectors.toList());

    tx.success();
    tx.close();

    return patternIds;
  }