Esempio n. 1
0
 @Override
 public WeightedPath findSinglePath(Node start, Node end) {
   lastMetadata = new Metadata();
   AStarIterator iterator = new AStarIterator(start, end);
   while (iterator.hasNext()) {
     Node node = iterator.next();
     GraphDatabaseService graphDb = node.getGraphDatabase();
     if (node.equals(end)) {
       // Hit, return path
       double weight = iterator.visitData.get(node.getId()).wayLength;
       LinkedList<Relationship> rels = new LinkedList<Relationship>();
       Relationship rel =
           graphDb.getRelationshipById(iterator.visitData.get(node.getId()).cameFromRelationship);
       while (rel != null) {
         rels.addFirst(rel);
         node = rel.getOtherNode(node);
         long nextRelId = iterator.visitData.get(node.getId()).cameFromRelationship;
         rel = nextRelId == -1 ? null : graphDb.getRelationshipById(nextRelId);
       }
       Path path = toPath(start, rels);
       lastMetadata.paths++;
       return new WeightedPathImpl(weight, path);
     }
   }
   return null;
 }
Esempio n. 2
0
  private static Iterable<LinkedList<Relationship>> getPaths(Hit hit, DirectionData data) {
    LevelData levelData = data.visitedNodes.get(hit.connectingNode);
    if (levelData.depth == 0) {
      Collection<LinkedList<Relationship>> result = new ArrayList<LinkedList<Relationship>>();
      result.add(new LinkedList<Relationship>());
      return result;
    }

    Collection<PathData> set = new ArrayList<PathData>();
    GraphDatabaseService graphDb = data.startNode.getGraphDatabase();
    for (long rel : levelData.relsToHere) {
      set.add(
          new PathData(
              hit.connectingNode,
              new LinkedList<Relationship>(Arrays.asList(graphDb.getRelationshipById(rel)))));
    }
    for (int i = 0; i < levelData.depth - 1; i++) {
      // One level
      Collection<PathData> nextSet = new ArrayList<PathData>();
      for (PathData entry : set) {
        // One path...
        Node otherNode = entry.rels.getFirst().getOtherNode(entry.node);
        LevelData otherLevelData = data.visitedNodes.get(otherNode);
        int counter = 0;
        for (long rel : otherLevelData.relsToHere) {
          // ...may split into several paths
          LinkedList<Relationship> rels =
              ++counter == otherLevelData.relsToHere.length
                  ?
                  // This is a little optimization which reduces number of
                  // lists being copied
                  entry.rels
                  : new LinkedList<Relationship>(entry.rels);
          rels.addFirst(graphDb.getRelationshipById(rel));
          nextSet.add(new PathData(otherNode, rels));
        }
      }
      set = nextSet;
    }

    return new IterableWrapper<LinkedList<Relationship>, PathData>(set) {
      @Override
      protected LinkedList<Relationship> underlyingObjectToObject(PathData object) {
        return object.rels;
      }
    };
  }
  @Test
  public void shouldProvideTheCorrectRelationshipData() {
    GraphDatabaseService db = dbRule.getGraphDatabaseService();

    // create a rel type so the next type id is non zero
    try (Transaction tx = db.beginTx()) {
      db.createNode()
          .createRelationshipTo(db.createNode(), DynamicRelationshipType.withName("TYPE"));
    }

    RelationshipType livesIn = DynamicRelationshipType.withName("LIVES_IN");
    long relId;

    try (Transaction tx = db.beginTx()) {
      Node person = db.createNode(DynamicLabel.label("Person"));

      Node city = db.createNode(DynamicLabel.label("City"));

      Relationship rel = person.createRelationshipTo(city, livesIn);
      rel.setProperty("since", 2009);
      relId = rel.getId();
      tx.success();
    }

    final Set<String> changedRelationships = new HashSet<>();

    db.registerTransactionEventHandler(
        new TransactionEventHandler.Adapter<Void>() {
          @Override
          public Void beforeCommit(TransactionData data) throws Exception {
            for (PropertyEntry<Relationship> entry : data.assignedRelationshipProperties()) {
              changedRelationships.add(entry.entity().getType().name());
            }

            return null;
          }
        });

    try (Transaction tx = db.beginTx()) {
      Relationship rel = db.getRelationshipById(relId);
      rel.setProperty("since", 2010);
      tx.success();
    }

    assertEquals(1, changedRelationships.size());
    assertTrue(
        livesIn + " not in " + changedRelationships.toString(),
        changedRelationships.contains(livesIn.name()));
  }
Esempio n. 4
0
 protected Relationship findRelationshipByUuid(String moduleId, String uuid) {
   return database.getRelationshipById(reader(moduleId).getRelationshipIdByUuid(uuid));
 }
 /** {@inheritDoc} */
 @Override
 protected Relationship fetch(GraphDatabaseService database) {
   return database.getRelationshipById(getGraphId());
 }
 @Override
 public Relationship getRelationshipById(long id) {
     return delegate.getRelationshipById(id);
 }
Esempio n. 7
0
 public REST checkRelationship(long id, String prop, Object value) {
   checkProperty(gds.getRelationshipById(id), prop, value);
   return this;
 }
Esempio n. 8
0
 private Relationship relationship() {
   return gds.getRelationshipById(id());
 }