PGX 20.1.1
Documentation

Modifying Loaded Graphs

This guide shows how to add and remove vertices and edges from already loaded graphs. This can be useful for "what-if" analyses.

Recap: Loading a Graph

At first we need to load a graph. For detailed instructions, refer to the Graph Loading Guide.

var graph = session.readGraphWithProperties("examples/graphs/sample.csv.json")
import oracle.pgx.api.*;

PgxSession session = Pgx.createSession("my-session");
PgxGraph graph = session.readGraphWithProperties("examples/graphs/sample.csv.json");

This will load the following sample graph:

sample_graph

Adding, Removing and Modifying Vertices

Now that we have a graph loaded we can start modifying it. First we need a GraphChangeSet - an object similar to the GraphBuilder. It provides methods for modifying all aspects of a graph.

We are going to add a new vertex 42 with the property value 23 for prop1. Additionally we are going to set the value of prop1 for vertex 128 to 5. Finally we will remove the vertex with ID 1908.

var changeSet = graph.<Integer>createChangeSet()

changeSet.addVertex(42).setProperty("prop", 23)
changeSet.updateVertex(128).setProperty("prop", 5)
changeSet.removeVertex(1908)

var updatedGraph = changeSet.build()

updatedGraph.hasVertex(42) // Evaluates to: true
updatedGraph.hasVertex(1908) // Evaluates to: false
import oracle.pgx.api.*;

GraphChangeSet<Integer> changeSet = graph.createChangeSet();

changeSet.addVertex(42).setProperty("prop", 23);
changeSet.updateVertex(128).setProperty("prop", 5);
changeSet.removeVertex(1908);

PgxGraph updatedGraph = changeSet.build();

assert updatedGraph.hasVertex(42) == true;
assert updatedGraph.hasVertex(1908) == false;

Notice that vertex 1908 has one edge attached in the original graph. This edge will be deleted automatically with the deletion of the vertex.

The resulting graph will have the following form:

updated sample graph

Adding Edges

The same way edges can be added to the graph. For example we can add a new triangle to the graph:

var changeSet2 = updatedGraph.<Integer>createChangeSet()

changeSet2.addEdge(333, 42).setProperty("cost", 42.3)
changeSet2.addEdge(42, 99)

var updatedGraph2 = changeSet2.build()

analyst.countTriangles(updatedGraph, true) // Evaluates to: 1
analyst.countTriangles(updatedGraph2, true) // Evaluates to: 2
import oracle.pgx.api.*;

GraphChangeSet<Integer> changeSet2 = graph.createChangeSet()

changeSet2.addEdge(333, 42).setProperty("cost", 42.42)
changeSet2.addEdge(42, 99)

PgxGraph updatedGraph2 = changeSet2.build();

assert analyst.countTriangles(updatedGraph, true) == 1;
assert analyst.countTriangles(updatedGraph2, true) == 2;

Note that we do not provide a value for the property cost for the second edge we add. In this case the property will be assigned a default value of 0.0.

This is the resulting graph:

second updated sample graph

Note that by calling changeSet2.build() we created a brand new graph with a unique name assigned by PGX; if need be, we can specify a name argument to the build() method. Additionally, we can create a new snapshot on top of the current graph with the buildNewSnapshot() method; for more information about this capability, you can refer to Graph Versioning. Please see the Graph Change Set API reference for detailed information on the graph ChangeSet.