4.10.2 Compiling and Running a Custom PGX Algorithm

To be able to compile and run a custom PGX algorithm, you must perform the following actions:
  1. Set the following two configuration parameters in the conf/pgx.conf file:
    • Set the graph_algorithm_language option to JAVA.
    • Set the java_home_dir option to the path to your Java home (use <system-java-home-dir> to have PGX infer Java home from the system properties).
    {
      "graph_algorithm_language": "JAVA",
      "java_home_dir": "<system-java-home-dir>"
    }
  2. Create a session.
    cd /opt/oracle/graph
    ./bin/opg4j 
    import oracle.pgx.algorithm.*;
    PgxSession session = Pgx.createSession("my-session");
    session = instance.create_session("my-session")
  3. Compile a PGX Algorithm. For example:
    opg4j> var myAlgorithm = session.compileProgram("/path/to/MyAlgorithm.java")
    myAlgorithm ==> CompiledProgram[name=MyAlgorithm]
    import oracle.pgx.algorithm.CompiledProgram;
    CompiledProgram myAlgorithm = session.compileProgram("/path/to/MyAlgorithm.java");
    my_algorithm = session.compile_program("/path/to/MyAlgorithm.java")
  4. Run the algorithm. For example:
    opg4j> var graph =session.readGraphWithProperties("/path/to/bank_graph_analytics.json")
    graph ==> PgxGraph[name=bank_graph_analytics,N=1000,E=5001,created=1633504705054]
    opg4j> var property = graph.createVertexProperty(PropertyType.INTEGER)
    property ==> VertexProperty[name=vertex_prop_integer_9,type=integer,graph=bank_graph_analytics]
    opg4j> myAlgorithm.run(graph, property)
    $6 ==> {
      "success" : true,
      "canceled" : false,
      "exception" : null,
      "returnValue" : 42,
      "executionTimeMs" : 0
    }
    import oracle.pgx.algorithm.VertexProperty;
    PgxGraph graph = session.readGraphWithProperties("/path/to/bank_graph_analytics.json");
    VertexProperty property = graph.createVertexProperty(PropertyType.INTEGER);
    myAlgorithm.run(graph, property);
    graph = session.read_graph_with_properties("/path/to/bank_graph_analytics.json")
    property = graph.create_vertex_property("integer")
    my_algorithm.run(graph, property)
    {'success': True, 'canceled': False, 'exception': None, 'return_value': 42, 'execution_time(ms)': 1}