Java Code Examples for org.neo4j.graphdb.GraphDatabaseService#findNodes()

The following examples show how to use org.neo4j.graphdb.GraphDatabaseService#findNodes() . 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: Neo4JApiQuerySwitchMonitoredInject.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Collection<Neo4jSwitchMonitoredInjectMatch> evaluate() {
	final Collection<Neo4jSwitchMonitoredInjectMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (final Transaction tx = graphDb.beginTx()) {
		// (sw:Switch)
		final Iterable<Node> sws = () -> graphDb.findNodes(Neo4jConstants.labelSwitch);
		for (final Node sw : sws) {
			final Map<String, Object> match = new HashMap<>();
			match.put(QueryConstants.VAR_SW, sw);
			matches.add(new Neo4jSwitchMonitoredInjectMatch(match));
		}
	}

	return matches;
}
 
Example 2
Source File: Neo4JApiQueryPosLength.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Collection<Neo4jPosLengthMatch> evaluate() {
	final Collection<Neo4jPosLengthMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		// (segment:Segment)
		final Iterable<Node> segments = () -> graphDb.findNodes(Neo4jConstants.labelSegment);
		for (final Node segment : segments) {
			final Number lengthNumber = (Number) segment.getProperty(LENGTH);
			final int length = Neo4jHelper.numberToInt(lengthNumber);

			// segment.length <= 0
			if (length <= 0) {
				final Map<String, Object> match = new HashMap<>();
				match.put(VAR_SEGMENT, segment);
				match.put(VAR_LENGTH, length);
				matches.add(new Neo4jPosLengthMatch(match));
			}
		}
	}

	return matches;
}
 
Example 3
Source File: Neo4JApiQuerySemaphoreNeighborInject.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Collection<Neo4jSemaphoreNeighborInjectMatch> evaluate() {
	final Collection<Neo4jSemaphoreNeighborInjectMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		// (route:Route)
		final Iterable<Node> routes = () -> graphDb.findNodes(Neo4jConstants.labelRoute);
		for (final Node route : routes) {
			Iterable<Relationship> entries = route.getRelationships(Direction.OUTGOING, Neo4jConstants.relationshipTypeEntry);

			for (Relationship entry : entries) {
				final Node semaphore = entry.getEndNode();

				final Map<String, Object> match = new HashMap<>();
				match.put(QueryConstants.VAR_ROUTE, route);
				match.put(QueryConstants.VAR_SEMAPHORE, semaphore);
				matches.add(new Neo4jSemaphoreNeighborInjectMatch(match));
			}
		}
	}

	return matches;
}
 
Example 4
Source File: Neo4JApiQueryPosLengthInject.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Collection<Neo4jPosLengthInjectMatch> evaluate() {
	final Collection<Neo4jPosLengthInjectMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		// (segment:Segment)
		final Iterable<Node> segments = () -> graphDb.findNodes(Neo4jConstants.labelSegment);
		for (final Node segment : segments) {
			final Map<String, Object> match = new HashMap<>();
			match.put(VAR_SEGMENT, segment);
			matches.add(new Neo4jPosLengthInjectMatch(match));
		}
	}

	return matches;
}
 
Example 5
Source File: Neo4JApiQuerySwitchSetInject.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Collection<Neo4jSwitchSetInjectMatch> evaluate() {
	final Collection<Neo4jSwitchSetInjectMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		// (sw:Switch)
		final Iterable<Node> sws = () -> graphDb.findNodes(Neo4jConstants.labelSwitch);
		for (final Node sw : sws) {
			final Map<String, Object> match = new HashMap<>();
			match.put(QueryConstants.VAR_SW, sw);
			matches.add(new Neo4jSwitchSetInjectMatch(match));
		}
	}

	return matches;
}
 
Example 6
Source File: Neo4JApiQueryRouteSensorInject.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Collection<Neo4jRouteSensorInjectMatch> evaluate() {
	final Collection<Neo4jRouteSensorInjectMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		// (route:Route)
		final Iterable<Node> routes = () -> graphDb.findNodes(Neo4jConstants.labelRoute);
		for (final Node route : routes) {

			final Iterable<Node> sensors = Neo4jUtil.getAdjacentNodes(route, Neo4jConstants.relationshipTypeRequires,
					Direction.OUTGOING, Neo4jConstants.labelSensor);

			for (final Node sensor : sensors) {
				final Map<String, Object> match = new HashMap<>();
				match.put(QueryConstants.VAR_ROUTE, route);
				match.put(QueryConstants.VAR_SENSOR, sensor);
				matches.add(new Neo4jRouteSensorInjectMatch(match));
			}
		}
	}

	return matches;
}
 
Example 7
Source File: GraphDbBootstrapTest.java    From extended-objects with Apache License 2.0 6 votes vote down vote up
@Test
public void bootstrap() throws URISyntaxException {
    GraphDatabaseService graphDatabaseService = new TestGraphDatabaseFactory().newImpermanentDatabase();
    Properties properties = new Properties();
    properties.put(GraphDatabaseService.class.getName(), graphDatabaseService);
    XOUnit xoUnit = XOUnit.builder().uri(new URI("graphDb:///")).provider(EmbeddedNeo4jXOProvider.class).types(singletonList(A.class))
            .properties(properties).build();
    XOManagerFactory xoManagerFactory = XO.createXOManagerFactory(xoUnit);
    XOManager xoManager = xoManagerFactory.createXOManager();
    xoManager.currentTransaction().begin();
    A a = xoManager.create(A.class);
    a.setName("Test");
    xoManager.currentTransaction().commit();
    xoManager.close();
    xoManagerFactory.close();
    try (Transaction transaction = graphDatabaseService.beginTx()) {
        ResourceIterator<Node> iterator = graphDatabaseService.findNodes(label("A"), "name", "Test");
        assertThat(iterator.hasNext(), equalTo(true));
        Node node = iterator.next();
        assertThat(node.hasLabel(label("A")), equalTo(true));
        assertThat(node.getProperty("name"), equalTo((Object) "Test"));
        transaction.success();
    }
}
 
Example 8
Source File: Neo4JApiQuerySwitchMonitored.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Collection<Neo4jSwitchMonitoredMatch> evaluate() {
	final Collection<Neo4jSwitchMonitoredMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		final Iterable<Node> sws = () -> graphDb.findNodes(Neo4jConstants.labelSwitch);
		// (sw:Switch)
		for (final Node sw : sws) {
			// (sw)-[:sensor]->(Sensor) NAC
			final Iterable<Relationship> relationshipSensors = sw.getRelationships(Direction.OUTGOING, Neo4jConstants.relationshipTypeMonitoredBy);

			boolean hasSensor = false;
			for (final Relationship relationshipSensor : relationshipSensors) {
				final Node sensor = relationshipSensor.getEndNode();
				if (sensor.hasLabel(Neo4jConstants.labelSensor)) {
					hasSensor = true;
					break;
				}
			}

			if (!hasSensor) {
				final Map<String, Object> match = new HashMap<>();
				match.put(VAR_SW, sw);
				matches.add(new Neo4jSwitchMonitoredMatch(match));
			}
		}
	}

	return matches;
}
 
Example 9
Source File: Neo4JApiQueryConnectedSegmentsInject.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Collection<Neo4jConnectedSegmentsInjectMatch> evaluate() throws IOException {
	final Collection<Neo4jConnectedSegmentsInjectMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (final Transaction tx = graphDb.beginTx()) {

		// (sensor:Sensor)
		final Iterable<Node> sensors = () -> graphDb.findNodes(Neo4jConstants.labelSensor);
		for (final Node sensor : sensors) {
			// (sensor:Sensor)<-[:sensor]-(segment1:Segment)
			final Iterable<Node> segment1s = Neo4jUtil.getAdjacentNodes(sensor, Neo4jConstants.relationshipTypeMonitoredBy, Direction.INCOMING,
					Neo4jConstants.labelSegment);

			for (final Node segment1 : segment1s) {
				// (segment1:Segment)-[:connectsTo]->(segment3:Segment)
				final Iterable<Node> segment3s = Neo4jUtil.getAdjacentNodes(segment1, Neo4jConstants.relationshipTypeConnectsTo, Direction.OUTGOING,
						Neo4jConstants.labelSegment);
				for (final Node segment3 : segment3s) {
					// (segment3:Segment)-[:sensor]->(sensor:Sensor)
					if (!Neo4jUtil.isConnected(segment3, sensor, Neo4jConstants.relationshipTypeMonitoredBy)) {
						continue;
					}

					final Map<String, Object> match = new HashMap<>();
					match.put(VAR_SENSOR, sensor);
					match.put(VAR_SEGMENT1, segment1);
					match.put(VAR_SEGMENT3, segment3);
					matches.add(new Neo4jConnectedSegmentsInjectMatch(match));
				}
			}
		}
	}
	return matches;
}
 
Example 10
Source File: Neo4JApiQueryRouteSensor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Collection<Neo4jRouteSensorMatch> evaluate() {
	final Collection<Neo4jRouteSensorMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (final Transaction tx = graphDb.beginTx()) {
		final Iterable<Node> routes = () -> graphDb.findNodes(Neo4jConstants.labelRoute);
		for (final Node route : routes) {
			// (route:Route)-[:follows]->(swp:switchPosition)
			final Iterable<Node> swPs = Neo4jUtil.getAdjacentNodes(route, Neo4jConstants.relationshipTypeFollows, Direction.OUTGOING, Neo4jConstants.labelSwitchPosition);
			for (final Node swP : swPs) {
				// (swP:switchPosition)-[:target]->(sw:Switch)
				final Iterable<Node> sws = Neo4jUtil.getAdjacentNodes(swP, Neo4jConstants.relationshipTypeTarget, Direction.OUTGOING, Neo4jConstants.labelSwitch);
				for (final Node sw : sws) {
					// (sw:Switch)-[:sensor]->(sensor:Sensor)
					final Iterable<Node> sensors = Neo4jUtil.getAdjacentNodes(sw, Neo4jConstants.relationshipTypeMonitoredBy, Direction.OUTGOING, Neo4jConstants.labelSensor);
					for (final Node sensor : sensors) {
						// (sensor:Sensor)<-[:requires]-(route:Route) NAC
						if (!Neo4jUtil.isConnected(route, sensor, Neo4jConstants.relationshipTypeRequires)) {
							final Map<String, Object> match = new HashMap<>();
							match.put(VAR_ROUTE, route);
							match.put(VAR_SENSOR, sensor);
							match.put(VAR_SWP, swP);
							match.put(VAR_SW, sw);
							matches.add(new Neo4jRouteSensorMatch(match));
						}
					}
				}
			}
		}
	}

	return matches;
}
 
Example 11
Source File: ReachabilityIndex.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
/***
 * Manage a reachability index object on a graph
 * 
 * @param graphDb
 *          The graph on which to build the reachability index
 */
@Inject
public ReachabilityIndex(GraphDatabaseService graphDb) {
  this.graphDb = graphDb;
  try (Transaction tx = graphDb.beginTx()) {
    ResourceIterator<Node> nodes = graphDb.findNodes(REACHABILITY_METADATA);
    metaDataNode = getOnlyElement(nodes, graphDb.createNode(REACHABILITY_METADATA));
    tx.success();
  }
}
 
Example 12
Source File: Neo4JApiQueryConnectedSegments.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Collection<Neo4jConnectedSegmentsMatch> evaluate() throws IOException {
	final Collection<Neo4jConnectedSegmentsMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (final Transaction tx = graphDb.beginTx()) {

		// (sensor:Sensor)
		final Iterable<Node> sensors = () -> graphDb.findNodes(Neo4jConstants.labelSensor);
		for (final Node sensor : sensors) {
			// (sensor:Sensor)<-[:sensor]-(segment1:Segment)
			final Iterable<Node> segment1s = Neo4jUtil.getAdjacentNodes(sensor, Neo4jConstants.relationshipTypeMonitoredBy,
					Direction.INCOMING, Neo4jConstants.labelSegment);

			for (final Node segment1 : segment1s) {
				// (segment1:Segment)-[:connectsTo]->(segment2:Segment)
				final Iterable<Node> segment2s = Neo4jUtil.getAdjacentNodes(segment1, Neo4jConstants.relationshipTypeConnectsTo,
						Direction.OUTGOING, Neo4jConstants.labelSegment);
				for (final Node segment2 : segment2s) {
					// (segment2:Segment)-[:sensor]->(sensor:Sensor)
					if (!Neo4jUtil.isConnected(segment2, sensor, Neo4jConstants.relationshipTypeMonitoredBy)) {
						continue;
					}
					// (segment2:Segment)-[:connectsTo]->(segment3:Segment)
					final Iterable<Node> segment3s = Neo4jUtil.getAdjacentNodes(segment2, Neo4jConstants.relationshipTypeConnectsTo,
							Direction.OUTGOING, Neo4jConstants.labelSegment);
					for (final Node segment3 : segment3s) {
						// (segment3:Segment)-[:sensor]->(sensor:Sensor)
						if (!Neo4jUtil.isConnected(segment3, sensor, Neo4jConstants.relationshipTypeMonitoredBy)) {
							continue;
						}
						// (segment3:Segment)-[:connectsTo]->(segment4:Segment)
						final Iterable<Node> segment4s = Neo4jUtil.getAdjacentNodes(segment3, Neo4jConstants.relationshipTypeConnectsTo,
								Direction.OUTGOING, Neo4jConstants.labelSegment);
						for (final Node segment4 : segment4s) {
							// (segment4:Segment)-[:sensor]->(sensor:Sensor)
							if (!Neo4jUtil.isConnected(segment4, sensor, Neo4jConstants.relationshipTypeMonitoredBy)) {
								continue;
							}

							// (segment4:Segment)-[:connectsTo]->(segment5:Segment)
							final Iterable<Node> segment5s = Neo4jUtil.getAdjacentNodes(segment4,
									Neo4jConstants.relationshipTypeConnectsTo, Direction.OUTGOING, Neo4jConstants.labelSegment);
							for (final Node segment5 : segment5s) {
								// (segment5:Segment)-[:sensor]->(sensor:Sensor)
								if (!Neo4jUtil.isConnected(segment5, sensor, Neo4jConstants.relationshipTypeMonitoredBy)) {
									continue;
								}

								// (segment5:Segment)-[:connectsTo]->(segment6:Segment)
								final Iterable<Node> segment6s = Neo4jUtil.getAdjacentNodes(segment5, Neo4jConstants.relationshipTypeConnectsTo, Direction.OUTGOING, Neo4jConstants.labelSegment);
								for (final Node segment6 : segment6s) {
									// (segment6:Segment)-[:sensor]->(sensor:Sensor)
									if (!Neo4jUtil.isConnected(segment6, sensor, Neo4jConstants.relationshipTypeMonitoredBy)) {
										continue;
									}

									final Map<String, Object> match = new HashMap<>();
									match.put(VAR_SENSOR, sensor);
									match.put(VAR_SEGMENT1, segment1);
									match.put(VAR_SEGMENT2, segment2);
									match.put(VAR_SEGMENT3, segment3);
									match.put(VAR_SEGMENT4, segment4);
									match.put(VAR_SEGMENT5, segment5);
									match.put(VAR_SEGMENT6, segment6);
									matches.add(new Neo4jConnectedSegmentsMatch(match));
								}
							}
						}
					}
				}
			}
		}
	}

	return matches;
}
 
Example 13
Source File: Neo4JApiQuerySwitchSet.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Collection<Neo4jSwitchSetMatch> evaluate() {
	final Collection<Neo4jSwitchSetMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (final Transaction tx = graphDb.beginTx()) {
		// (route:Route)
		final Iterable<Node> routes = () -> graphDb.findNodes(Neo4jConstants.labelRoute);
		for (final Node route : routes) {
			final boolean active = (boolean) route.getProperty(ModelConstants.ACTIVE);
			if (!active) {
				continue;
			}

			// (route:Route)-[:entry]->(semaphore:Semaphore)
			final Iterable<Node> semaphores = Neo4jUtil.getAdjacentNodes(route, Neo4jConstants.relationshipTypeEntry, Direction.OUTGOING,
					Neo4jConstants.labelSemaphore);
			for (final Node semaphore : semaphores) {

				// semaphore.signal = "GO"
				final String signal = (String) semaphore.getProperty(SIGNAL);
				if (!Signal.GO.toString().equals(signal)) {
					continue;
				}

				// (route:Route)-[:follows]->(swP:SwitchPosition)
				final Iterable<Node> swPs = Neo4jUtil.getAdjacentNodes(route, Neo4jConstants.relationshipTypeFollows, Direction.OUTGOING,
						Neo4jConstants.labelSwitchPosition);
				for (final Node swP : swPs) {
					// (swP:SwitchPosition)-[:target]->(sw:Switch)
					final Iterable<Node> sws = Neo4jUtil.getAdjacentNodes(swP, Neo4jConstants.relationshipTypeTarget,
							Direction.OUTGOING, Neo4jConstants.labelSwitch);

					for (final Node sw : sws) {
						final Object currentPosition = sw.getProperty(ModelConstants.CURRENTPOSITION);
						final Object position = swP.getProperty(ModelConstants.POSITION);

						if (!currentPosition.equals(position)) {
							final Map<String, Object> match = new HashMap<>();
							match.put(QueryConstants.VAR_SEMAPHORE, semaphore);
							match.put(QueryConstants.VAR_ROUTE, route);
							match.put(QueryConstants.VAR_SWP, swP);
							match.put(QueryConstants.VAR_SW, sw);
							match.put(QueryConstants.VAR_CURRENTPOSITION, currentPosition);
							match.put(QueryConstants.VAR_POSITION, position);
							matches.add(new Neo4jSwitchSetMatch(match));
						}
					}
				}
			}
		}
	}

	return matches;
}
 
Example 14
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;
}