10.4.4 Dynamically Expanding a Subgraph
You can expand an in-memory subgraph by loading another subgraph into memory and merging it with the current in-memory subgraph.
The PgxGraph.expandGraph()
method can be used to
expand a subgraph. The following applies when merging two graphs:
- Both the graphs can have separate sets of providers.
- A graph can have some providers same as the other graph. In this
case:
- The providers with the same names must have the same labels.
- The graph being merged must have the same or a common subset of properties as the base graph. However, it is possible that either of the graphs may have more number of properties.
The following example shows the expansion of the subgraph created in PGQL Based Subgraph Loading:
opg4j> graph = graph.expandGraph().
...> withPgql().
...> fromPgView("FRIENDS").
...> queryPgql("MATCH (v1:PERSON) -[e:FRIENDOF]-> (v2:PERSON) WHERE id(v1) = 'PERSONS(2)'").
...> preparedPgqlQuery("MATCH (v:PERSON) WHERE id(v) in ?").withStringArg(1, "PERSONS(4)").
...> expand()
graph ==> PgxGraph[name=anonymous_graph_152,N=4,E=3,created=1647347092964]
graph = graph.expandGraph()
.withPgql()
.fromPgView("FRIENDS")
.queryPgql("MATCH (v1:PERSON) -[e:FRIENDOF]-> (v2:PERSON) WHERE id(v1) = 'PERSONS(2)'")
.preparedPgqlQuery("MATCH (v:PERSON) WHERE id(v) in ?").withStringArg(1, "PERSONS(4)")
.expand();
>>> from pypgx.api import PreparedPgqlQuery
>>> from pypgx.api import PreparedPgqlQueryStringArgument
>>> graph = graph.expand_with_pgql(["MATCH (v1:PERSON) -[e:FRIENDOF]-> (v2:PERSON) WHERE id(v1) = 'PERSONS(2)'",
... PreparedPgqlQuery("MATCH (v:Person) WHERE id(v)=?", [PreparedPgqlQueryStringArgument("PERSONS(4)")])],
... pg_view_name="FRIENDS")
>>> graph
PgxGraph(name: anonymous_graph_66, v: 4, e: 3, directed: True, memory(Mb): 0)
The following displays the output for the preceding PGQL
query using the graph visualization tool. The subgraph is now
expanded to include the friendOf
relationship for
PERSONS(2)
in addition to
PERSONS(1)
which was already existing in
the subgraph.
Expanding a Subgraph by Explicitly Specifying the Schema Name
When expanding a graph, you can load another subgraph by
reading a PGQL property graph
from a different schema. For this, you must provide the schema name
as an argument to the
PgqlViewGraphExpander#fromPgView
API. You
must also ensure that you have READ
permission on
all the underlying metadata and data tables for the PGQL property graph.
For example:
opg4j> graph = graph.expandGraph().
...> withPgql().
...> fromPgView("GRAPHUSER", "FRIENDS").
...> queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(1)'").
...> expand()
graph ==> PgxGraph[name=anonymous_graph_18,N=1,E=0,created=1672848726308]
graph = graph.expandGraph()
.withPgql()
.fromPgView("GRAPHUSER", "FRIENDS")
.queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(1)'")
.expand();
>>> graph = graph.expand_with_pgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(1)'",
... pg_view_name="FRIENDS", schema="GRAPHUSER")
>>> graph
PgxGraph(name: anonymous_graph_6, v: 2, e: 0, directed: True, memory(Mb): 0)
Using Merging Strategy
When expanding a graph, some vertices and edges that are in the new graph data may have already been loaded in the base graph. In such cases, if the vertex and edge property values differ for all vertices and edges that are both in the base graph and in the new graph to be merged, then the following applies:
- If the merging strategy is
KEEP_CURRENT_VALUES
, then the vertex and edge property values coming from the new graph are ignored. - If the merging strategy is
UPDATE_WITH_NEW_VALUES
, then the vertex and edge property values are updated with the ones found in the new graph.
For example:
opg4j> import oracle.pgx.api.expansion.PropertyMergeStrategy
opg4j> graph = graph.expandGraph().
...> withPgql().
...> fromPgView("FRIENDS").
...> queryPgql("MATCH (v1:PERSON) -[e:FRIENDOF]-> (v2:PERSON) WHERE id(v1) = 'PERSONS(2)'").
...> preparedPgqlQuery("MATCH (v:PERSON) WHERE id(v) in ?").withStringArg(1, "PERSONS(4)").
...> vertexPropertiesMergingStrategy(PropertyMergeStrategy.UPDATE_WITH_NEW_VALUES).
...> expand()
import oracle.pgx.api.expansion.PropertyMergeStrategy;
graph = graph.expandGraph()
.withPgql()
.fromPgView("FRIENDS")
.queryPgql("MATCH (v1:PERSON) -[e:FRIENDOF]-> (v2:PERSON) WHERE id(v1) = 'PERSONS(2)'")
.preparedPgqlQuery("MATCH (v:PERSON) WHERE id(v) in ?").withStringArg(1, "PERSONS(4)")
.vertexPropertiesMergingStrategy(PropertyMergeStrategy.UPDATE_WITH_NEW_VALUES)
.expand();
Parent topic: Loading a Subgraph from a PGQL Property Graph