Java Code Examples for org.neo4j.graphdb.Node#equals()

The following examples show how to use org.neo4j.graphdb.Node#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Neo4jUtil.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isConnected(final Node source, final Node target, final RelationshipType relationshipType) {
	final int sourceDegree = source.getDegree(relationshipType, Direction.OUTGOING);
	final int targetDegree = target.getDegree(relationshipType, Direction.INCOMING);
	
	final Direction searchDirection;
	final Node searchSource;
	final Node searchTarget;
	if (sourceDegree <= targetDegree) {
		searchDirection = Direction.OUTGOING;
		searchSource = source;
		searchTarget = target;
	} else {
		searchDirection = Direction.INCOMING;
		searchSource = target;
		searchTarget = source;
	}
	
	final Iterator<Relationship> edges = searchSource.getRelationships(searchDirection, relationshipType).iterator();
	while (edges.hasNext()) {
		final Relationship edge = edges.next();
		final Node otherNode = edge.getOtherNode(searchSource);
		if (searchTarget.equals(otherNode)) {
			return true;
		}
	}

	return false;
}
 
Example 2
Source File: WriteOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean deleteStatusUpdate(final Node user, final Node statusUpdate) {
	// get the status update owner
	final Node statusUpdateAuthor = NeoUtils.getPrevSingleNode(
			statusUpdate, SocialGraphRelationshipType.UPDATE);

	// the status update is not owned by the user passed
	if (!user.equals(statusUpdateAuthor)) {
		return false;
	}

	// remove reference from previous status update
	final Node previousUpdate = NeoUtils.getPrevSingleNode(statusUpdate,
			SocialGraphRelationshipType.UPDATE);
	previousUpdate.getSingleRelationship(
			SocialGraphRelationshipType.UPDATE, Direction.OUTGOING)
			.delete();

	// update references to the next status update (if existing)
	final Node nextUpdate = NeoUtils.getNextSingleNode(statusUpdate,
			SocialGraphRelationshipType.UPDATE);
	if (nextUpdate != null) {
		statusUpdate.getSingleRelationship(
				SocialGraphRelationshipType.UPDATE, Direction.OUTGOING)
				.delete();
		previousUpdate.createRelationshipTo(nextUpdate,
				SocialGraphRelationshipType.UPDATE);
	}

	// delete the status update node
	statusUpdate.delete();
	return true;
}
 
Example 3
Source File: ReadOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean deleteStatusUpdate(final Node user, final Node statusUpdate) {
	// get the status update owner
	final Node statusUpdateAuthor = NeoUtils.getPrevSingleNode(
			statusUpdate, SocialGraphRelationshipType.UPDATE);

	// the status update is not owned by the user passed
	if (!user.equals(statusUpdateAuthor)) {
		return false;
	}

	// update ego network
	this.updateReplicaLayerStatusUpdateDeletion(user, statusUpdate);

	// remove reference from previous status update
	final Node previousUpdate = NeoUtils.getPrevSingleNode(statusUpdate,
			SocialGraphRelationshipType.UPDATE);
	previousUpdate.getSingleRelationship(
			SocialGraphRelationshipType.UPDATE, Direction.OUTGOING)
			.delete();

	// update references to the next status update (if existing)
	final Node nextUpdate = NeoUtils.getNextSingleNode(statusUpdate,
			SocialGraphRelationshipType.UPDATE);
	if (nextUpdate != null) {
		statusUpdate.getSingleRelationship(
				SocialGraphRelationshipType.UPDATE, Direction.OUTGOING)
				.delete();
		previousUpdate.createRelationshipTo(nextUpdate,
				SocialGraphRelationshipType.UPDATE);
	}

	// delete the status update node
	statusUpdate.delete();
	return true;
}
 
Example 4
Source File: VirtualRelationship.java    From neo4j-ml-procedures with Apache License 2.0 4 votes vote down vote up
@Override
public Node getOtherNode(Node node) {
    return node.equals(startNode) ? endNode : node.equals(endNode) ? startNode : null;
}
 
Example 5
Source File: Neo4JApiQuerySemaphoreNeighbor.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Collection<Neo4jSemaphoreNeighborMatch> evaluate() {
	final Collection<Neo4jSemaphoreNeighborMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (final Transaction tx = graphDb.beginTx()) {
		final Iterable<Node> route1s = () -> graphDb.findNodes(Neo4jConstants.labelRoute);
		for (final Node route1 : route1s) {
			// (route1:Route)-[:exit]->(semaphore:Semaphore)
			final Iterable<Node> semaphores = Neo4jUtil.getAdjacentNodes(route1, Neo4jConstants.relationshipTypeExit,
					Direction.OUTGOING, Neo4jConstants.labelSemaphore);
			for (final Node semaphore : semaphores) {

				// (route1:Route)-[:requires]->(sensor1:Sensor)
				final Iterable<Node> sensor1s = Neo4jUtil.getAdjacentNodes(route1, Neo4jConstants.relationshipTypeRequires,
						Direction.OUTGOING, Neo4jConstants.labelSensor);
				for (final Node sensor1 : sensor1s) {
					// (sensor1:Sensor)<-[:sensor]-(te1:TrackElement)
					final Iterable<Node> te1s = Neo4jUtil.getAdjacentNodes(sensor1, Neo4jConstants.relationshipTypeMonitoredBy, Direction.INCOMING, Neo4jConstants.labelTrackElement);
					for (final Node te1 : te1s) {
						// (te1:TrackElement)-[:connectsTo]->(te2:TrackElement)
						final Iterable<Node> te2s = Neo4jUtil.getAdjacentNodes(te1, Neo4jConstants.relationshipTypeConnectsTo, Direction.OUTGOING, Neo4jConstants.labelTrackElement);
						for (final Node te2 : te2s) {
							// (te2:TrackElement)-[:sensor]->(sensor2:Sensor)
							final Iterable<Node> sensor2s = Neo4jUtil.getAdjacentNodes(te2, Neo4jConstants.relationshipTypeMonitoredBy, Direction.OUTGOING, Neo4jConstants.labelSensor);
							for (final Node sensor2 : sensor2s) {
								// (sensor2:Sensor)<-[:requires]-(route2:Route),
								final Iterable<Node> route2s = Neo4jUtil.getAdjacentNodes(sensor2, Neo4jConstants.relationshipTypeRequires, Direction.INCOMING, Neo4jConstants.labelRoute);
								for (final Node route2 : route2s) {
									// route1 != route2 --> if (route1 == route2), continue
									if (route1.equals(route2)) {
										continue;
									}

									// (route2)-[:entry]->(semaphore) NAC
									if (!Neo4jUtil.isConnected(route2, semaphore, Neo4jConstants.relationshipTypeEntry)) {
										final Map<String, Object> match = new HashMap<>();
										match.put(VAR_SEMAPHORE, semaphore);
										match.put(VAR_ROUTE1, route1);
										match.put(VAR_ROUTE2, route2);
										match.put(VAR_SENSOR1, sensor1);
										match.put(VAR_SENSOR2, sensor2);
										match.put(VAR_TE1, te1);
										match.put(VAR_TE2, te2);
										matches.add(new Neo4jSemaphoreNeighborMatch(match));
										break;
									}
								}
							}
						}
					}
				}
			}
		}
	}

	return matches;
}
 
Example 6
Source File: WriteOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<JSONObject> readStatusUpdates(final Node poster,
		final Node user, final int numItems, boolean ownUpdates) {
	if (!poster.equals(user)) {
		ownUpdates = true;
	}

	final List<JSONObject> statusUpdates = new LinkedList<JSONObject>();

	// check if ego network stream is being accessed
	if (!ownUpdates) {
		final TreeSet<StatusUpdateUser> users = new TreeSet<StatusUpdateUser>(
				new StatusUpdateUserComparator());

		// loop through users followed
		Node userNode;
		StatusUpdateUser crrUser;
		for (Relationship relationship : poster.getRelationships(
				SocialGraphRelationshipType.FOLLOW, Direction.OUTGOING)) {
			userNode = relationship.getEndNode();

			// add users having status updates
			crrUser = new StatusUpdateUser(userNode);
			if (crrUser.hasStatusUpdate()) {
				users.add(crrUser);
			}
		}

		// handle user queue
		while ((statusUpdates.size() < numItems) && !users.isEmpty()) {
			crrUser = users.pollLast();

			// add last recent status update of current user
			statusUpdates.add(crrUser.getStatusUpdate());

			// re-add current user if more status updates available
			if (crrUser.hasStatusUpdate()) {
				users.add(crrUser);
			}
		}
	} else {
		// access single stream only
		final StatusUpdateUser posterNode = new StatusUpdateUser(poster);
		while ((statusUpdates.size() < numItems)
				&& posterNode.hasStatusUpdate()) {
			statusUpdates.add(posterNode.getStatusUpdate());
		}
	}

	return statusUpdates;
}
 
Example 7
Source File: ReadOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<JSONObject> readStatusUpdates(final Node poster,
		final Node user, final int numItems, boolean ownUpdates) {
	if (!poster.equals(user)) {
		ownUpdates = true;
	}

	final List<JSONObject> statusUpdates = new LinkedList<JSONObject>();

	// check if ego network stream is being accessed
	if (!ownUpdates) {
		final TreeSet<GraphityUser> users = new TreeSet<GraphityUser>(
				new StatusUpdateUserComparator());

		// load first user by the replica
		Node replicaAdded = NeoUtils.getNextSingleNode(poster,
				SocialGraphRelationshipType.GRAPHITY);
		Node userAdded;
		GraphityUser crrUser, lastUser = null;
		if (replicaAdded != null) {
			userAdded = NeoUtils.getNextSingleNode(replicaAdded,
					SocialGraphRelationshipType.REPLICA);
			crrUser = new GraphityUser(userAdded, replicaAdded);
			if (crrUser.hasStatusUpdate()) {
				lastUser = crrUser;
				users.add(crrUser);
			}
		}

		// handle user queue
		while ((statusUpdates.size() < numItems) && !users.isEmpty()) {
			crrUser = users.pollLast();

			// add last recent status update of current user
			statusUpdates.add(crrUser.getStatusUpdate());

			// re-add current user if more status updates available
			if (crrUser.hasStatusUpdate()) {
				users.add(crrUser);
			}

			// load additional user if necessary
			if (crrUser == lastUser) {
				replicaAdded = NeoUtils.getNextSingleNode(
						lastUser.getUserReplica(),
						SocialGraphRelationshipType.GRAPHITY);
				if (replicaAdded != null) {
					userAdded = NeoUtils.getNextSingleNode(replicaAdded,
							SocialGraphRelationshipType.REPLICA);
					lastUser = new GraphityUser(userAdded, replicaAdded);

					// add new user if updates available only
					if (lastUser.hasStatusUpdate()) {
						users.add(lastUser);
						continue;
					}
				}

				// further users do not need to be loaded
				lastUser = null;
			}
		}

	} else {
		// access single stream only
		final GraphityUser posterUser = new GraphityUser(poster, null);
		while ((statusUpdates.size() < numItems)
				&& posterUser.hasStatusUpdate()) {
			statusUpdates.add(posterUser.getStatusUpdate());
		}
	}

	return statusUpdates;
}
 
Example 8
Source File: ReadOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 4 votes vote down vote up
/**
 * update the replica layer for status update deletion
 * 
 * @param user
 *            owner of the status update being deleted
 * @param statusUpdate
 *            status update being deleted
 */
private void updateReplicaLayerStatusUpdateDeletion(final Node user,
		final Node statusUpdate) {
	final Node lastUpdate = NeoUtils.getNextSingleNode(user,
			SocialGraphRelationshipType.UPDATE);

	// update the ego network if the removal targets the last recent status
	// update
	if (statusUpdate.equals(lastUpdate)) {
		// get timestamp of the last recent status update in future
		long newTimestamp = 0;
		final Node nextStatusUpdate = NeoUtils.getNextSingleNode(
				statusUpdate, SocialGraphRelationshipType.UPDATE);
		if (nextStatusUpdate != null) {
			newTimestamp = (long) nextStatusUpdate
					.getProperty(Properties.StatusUpdate.TIMESTAMP);
		}

		// loop through followers
		Node replicaNode, following;
		for (Relationship replicated : user.getRelationships(
				SocialGraphRelationshipType.REPLICA, Direction.INCOMING)) {
			replicaNode = replicated.getEndNode();
			following = NeoUtils.getPrevSingleNode(replicaNode,
					SocialGraphRelationshipType.FOLLOW);

			// search for insertion index within following replica layer
			long crrTimestamp;
			Node prevReplica = following;
			Node nextReplica = null;
			while (true) {
				// get next user
				nextReplica = NeoUtils.getNextSingleNode(prevReplica,
						SocialGraphRelationshipType.GRAPHITY);
				if (nextReplica != null) {
					// ignore replica of the status update owner
					if (nextReplica.equals(replicaNode)) {
						prevReplica = nextReplica;
						continue;
					}

					crrTimestamp = getLastUpdateByReplica(nextReplica);

					// step on if current user has newer status updates
					if (crrTimestamp > newTimestamp) {
						prevReplica = nextReplica;
						continue;
					}
				}

				// insertion position has been found
				break;
			}

			// insert the replica
			if (nextReplica != null) {
				// bride the replica node
				final Node oldPrevReplica = NeoUtils.getNextSingleNode(
						replicaNode, SocialGraphRelationshipType.GRAPHITY);
				final Node oldNextReplica = NeoUtils.getNextSingleNode(
						replicaNode, SocialGraphRelationshipType.GRAPHITY);
				replicaNode.getSingleRelationship(
						SocialGraphRelationshipType.GRAPHITY,
						Direction.INCOMING).delete();

				if (oldNextReplica != null) {
					oldNextReplica.getSingleRelationship(
							SocialGraphRelationshipType.GRAPHITY,
							Direction.INCOMING).delete();
					oldPrevReplica.createRelationshipTo(oldNextReplica,
							SocialGraphRelationshipType.GRAPHITY);
				}

				// link to new neighbored nodes
				if (nextReplica != null) {
					replicaNode.createRelationshipTo(nextReplica,
							SocialGraphRelationshipType.GRAPHITY);
					prevReplica.getSingleRelationship(
							SocialGraphRelationshipType.GRAPHITY,
							Direction.OUTGOING);
				}
				prevReplica.createRelationshipTo(replicaNode,
						SocialGraphRelationshipType.GRAPHITY);
			}
		}

	}
}