Custom Algorithm (PGX) Interpreter
Using the custom algorithm (PGX) interpreter, you can write your own custom PGX graph algorithms in a notebook paragraph in Graph Studio.
A custom algorithm (PGX) paragraph starts with %custom-algorithm-pgx
and a
custom graph algorithm can be written using Java syntax. See the PGX Algorithm
APIs in the Javadoc for more
information.
On running the custom algorithm (PGX) paragraph, the algorithm gets compiled. You can then use the compiled algorithm in a Java (PGX) or Python (PGX) paragraph.
Tip:
You can hover over the bottom part of a notebook paragraph and click the
For example, consider the following graph algorithm:
%custom-algorithm-pgx
package oracle.pgx.algorithms;
import oracle.pgx.algorithm.annotations.GraphAlgorithm;
import oracle.pgx.algorithm.PgxGraph;
import oracle.pgx.algorithm.VertexProperty;
import oracle.pgx.algorithm.annotations.Out;
@GraphAlgorithm
public class IndegreeCentrality {
public void indegreeCentrality(PgxGraph g, @Out VertexProperty<Integer> indegreeCentrality) {
g.getVertices().forEach(n ->
indegreeCentrality.set(n, (int) n.getInDegree())
);
}
}
After running the preceding code, you can integrate the compiled
algorithm (indegreeCentrality
) in a Java (PGX) or Python (PGX)
paragraph as shown:
var graph = session.getGraph("HR_GRAPH")
var centrality = graph.createVertexProperty(PropertyType.INTEGER, "centrality")
var algorithm = session.getCompiledProgram("indegreeCentrality")
algorithm.run(graph, centrality)
graph.queryPgql("SELECT x.centrality, x.last_name FROM MATCH (x:employees) ORDER BY x.centrality DESC LIMIT 10").print(out,10,0)
graph = session.get_graph("HR_GRAPH")
centrality = graph.create_vertex_property("integer", "centrality")
algorithm = session.get_compiled_program("indegreeCentrality")
algorithm.run(graph, centrality)
graph.query_pgql("SELECT x.centrality, x.last_name FROM MATCH (x:employees) ORDER BY x.centrality DESC LIMIT 10").print()
The graph query produces the following output:
+------------------------+
| centrality | last_name |
+------------------------+
| 14 | King |
| 9 | Kaufling |
| 8 | Weiss |
| 8 | Vollman |
| 8 | Fripp |
| 8 | Mourgos |
| 7 | Kochhar |
| 6 | Zlotkey |
| 6 | Russell |
| 6 | Cambrault |
+------------------------+
See Using Custom PGX Graph Algorithms in Oracle Database Graph Developer's Guide for Property Graph for more information.
Also, see Built-In Algorithms on GitHub for detailed information about the supported graph built-in algorithms.