-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson 1 The subgraph operator
Christopher Rost edited this page Sep 4, 2020
·
13 revisions
All labels start with a lowercase letter in our dataset, so please use 'person' instead of 'Person' in your tasks.
- Create a subgraph that contains ‘person’ vertices and ‘knows’ edges only.
Solution
Use the ByLabel
class to filter for vertices/edges of a specific label.
graph = graph.subgraph(new ByLabel<>("person"), new ByLabel<>("knows"));
- Get universities and persons that used the browser Firefox.
- Include all relationships between these vertices.
Some help
- Use the function
.vertexInducedSubgraph()
to only filter for vertices and get all edges between them. - Use the
ByLabel
orLabelIsIn
classes to filter for vertices/edges of a specific label. - Use the
ByProperty
class to filter for elements with a specific property and optional a value. - Use the
And
andOr
classes to build boolean relations.
Solution
graph = graph.vertexInducedSubgraph(
new Or<>(
new ByLabel<>("university"),
new And<>(new ByLabel<>("person"), new ByProperty<>("browserUsed", PropertyValue.create("Firefox")))));
- Some edge types have a
creationDate
property. Get all edges that contain this property and are crated in the period[2012-01-01 00:00 , 2012-01-05 00:00)
by using a user-defined edge filter function. - Only the source and target vertices of these edges should be included.
- Optional: Use parameters (by adding a constructor) to define the lower and upper bound for the condition.
Some help
- Use the function
.edgeInducedSubgraph()
to only filter for edges and get all connected vertices. - You can check the availability of a property by
edge.hasProperty("myProperty")
. - You get the value of a propety by calling
edge.getPropertyValue("myProperty").get{dataType}()
Solution
This is the solution of the task with the optional subtask included.
graph = graph.edgeInducedSubgraph(
new CreatedInPeriod<>(LocalDateTime.of(2012, 1, 1, 0, 0), LocalDateTime.of(2012, 1, 5, 0, 0)));
private static class CreatedInPeriod<E extends Edge> implements FilterFunction<E> {
private LocalDateTime lowerBound, upperBound;
public CreatedInPeriod(LocalDateTime start, LocalDateTime end) {
this.lowerBound = start;
this.upperBound = end;
}
@Override
public boolean filter(E edge) throws Exception {
if (edge.hasProperty("creationDate")) {
LocalDateTime creationDate = edge.getPropertyValue("creationDate").getDateTime();
return (creationDate.isEqual(lowerBound) || creationDate.isAfter(lowerBound)) &&
creationDate.isBefore(upperBound);
}
return false;
}
}