Questo contenuto non è disponibile nella lingua selezionata.
16.44. Working with Nodes
Now let's switch to working with nodes. This first example returns a map of properties (keyed by property name) for a node at a specific Path:
Path path = ...
Map<Name,Property> propertiesByName = graph.getPropertiesByName().on(path);
This next example shows how the graph can be used to obtain and loop over the properties of a node:
Path path = ...
for ( Property property : graph.getProperties().on(path) ) {
...
}
Likewise, the next example shows how the graph can be used to obtain and loop over the children of a node:
Path path = ...
for ( Location child : graph.getChildren().of(path) ) {
Path childPath = child.getPath();
...
}
Notice that the examples pass a Path instance to the
on(...) and of(...) methods. Many of the Graph API methods take a variety of parameter types, including String, Paths, Locations, UUID, or Property parameters. This should make it easy to use in many different situations.
Of course, changing content is more interesting and offers more interesting possibilities. Here are a few examples:
Path path = ...
Location location = ...
Property idProp1 = ...
Property idProp2 = ...
UUID uuid = ...
graph.move(path).into(idProp1, idProp2);
graph.copy(path).into(location);
graph.delete(uuid);
graph.delete(idProp1,idProp2);
The methods shown above work immediately, as soon as each request is built. However, there is another way to use the Graph object, and that is in a batch mode. Simply create a Graph.Batch object using the
batch() method, create the requests on that batch object, and then execute all of the commands on the batch by calling its execute() method. That execute() method returns a Results interface that can be used to read the node information retrieved by the batched requests.
Method chaining works really well with the batch mode, since multiple commands can be assembled together very easily:
Path path = ...
String path2 = ...
Location location = ...
Property idProp1 = ...
Property idProp2 = ...
UUID uuid = ...
graph.batch().move(path).into(idProp1, idProp2)
.and().copy(path2).into(location)
.and().delete(uuid)
.execute();
Results results = graph.batch().read(path2)
.and().readChildren().of(idProp1,idProp2)
.and().readSugraphOfDepth(3).at(uuid2)
.execute();
for ( Location child : results.getNode(path2) ) {
...
}
Of course, this section provided just a hint of the Graph API . The Graph interface is actually quite complete and offers a full-featured approach for reading and updating a graph. For more information, see the Graph JavaDocs.