7.10 RDFデータの解析関数
oracle.spatial.rdf.client.jena
パッケージのSemNetworkAnalyst
クラスを使用して、RDFデータで解析関数を実行することができます。
このサポートは、ネットワークデータ・モデル・グラフのロジックを基礎となるRDFデータ構造体に統合します。そのため、RDFデータでの解析関数を使用するには、Oracle Spatialトポロジおよびネットワーク・データ・モデル開発者ガイドで説明されている、ネットワーク・データ・モデル・グラフ機能についても理解しておく必要があります。
必要なNDM Javaライブラリ(sdonm.jar
とsdoutl.jar
を含む)は、ディレクトリ$ORACLE_HOME/md/jlib
の下にあります。xmlparserv2.jar
($ORACLE_HOME/xdk/lib
の下)を、classpath
定義に含める必要があることに注意してください。
例7-6 RDFデータ上の解析ファンクションの実行
例7-6では、SemNetworkAnalyst
クラス(NDM NetworkAnalyst
APIを内部的に使用)を使用します。
Oracle oracle = new Oracle(jdbcUrl, user, password); GraphOracleSem graph = new GraphOracleSem(oracle, modelName); Node nodeA = Node.createURI("http://A"); Node nodeB = Node.createURI("http://B"); Node nodeC = Node.createURI("http://C"); Node nodeD = Node.createURI("http://D"); Node nodeE = Node.createURI("http://E"); Node nodeF = Node.createURI("http://F"); Node nodeG = Node.createURI("http://G"); Node nodeX = Node.createURI("http://X"); // An anonymous node Node ano = Node.createAnon(new AnonId("m1")); Node relL = Node.createURI("http://likes"); Node relD = Node.createURI("http://dislikes"); Node relK = Node.createURI("http://knows"); Node relC = Node.createURI("http://differs"); graph.add(new Triple(nodeA, relL, nodeB)); graph.add(new Triple(nodeA, relC, nodeD)); graph.add(new Triple(nodeB, relL, nodeC)); graph.add(new Triple(nodeA, relD, nodeC)); graph.add(new Triple(nodeB, relD, ano)); graph.add(new Triple(nodeC, relL, nodeD)); graph.add(new Triple(nodeC, relK, nodeE)); graph.add(new Triple(ano, relL, nodeD)); graph.add(new Triple(ano, relL, nodeF)); graph.add(new Triple(ano, relD, nodeB)); // X only likes itself graph.add(new Triple(nodeX, relL, nodeX)); graph.commitTransaction(); HashMap<Node, Double> costMap = new HashMap<Node, Double>(); costMap.put(relL, Double.valueOf((double)0.5)); costMap.put(relD, Double.valueOf((double)1.5)); costMap.put(relC, Double.valueOf((double)5.5)); graph.setDOP(4); // this allows the underlying LINK/NODE tables // and indexes to be created in parallel. SemNetworkAnalyst sna = SemNetworkAnalyst.getInstance( graph, // network data source true, // directed graph true, // cleanup existing NODE and LINK table costMap ); psOut.println("From nodeA to nodeC"); Node[] nodeArray = sna.shortestPathDijkstra(nodeA, nodeC); printNodeArray(nodeArray, psOut); psOut.println("From nodeA to nodeD"); nodeArray = sna.shortestPathDijkstra( nodeA, nodeD); printNodeArray(nodeArray, psOut); psOut.println("From nodeA to nodeF"); nodeArray = sna.shortestPathAStar(nodeA, nodeF); printNodeArray(nodeArray, psOut); psOut.println("From ano to nodeC"); nodeArray = sna.shortestPathAStar(ano, nodeC); printNodeArray(nodeArray, psOut); psOut.println("From ano to nodeX"); nodeArray = sna.shortestPathAStar(ano, nodeX); printNodeArray(nodeArray, psOut); graph.close(); oracle.dispose(); ... ... // A helper function to print out a path public static void printNodeArray(Node[] nodeArray, PrintStream psOut) { if (nodeArray == null) { psOut.println("Node Array is null"); return; } if (nodeArray.length == 0) {psOut.println("Node Array is empty"); } int iFlag = 0; psOut.println("printNodeArray: full path starts"); for (int iHops = 0; iHops < nodeArray.length; iHops++) { psOut.println("printNodeArray: full path item " + iHops + " = " + ((iFlag == 0) ? "[n] ":"[e] ") + nodeArray[iHops]); iFlag = 1 - iFlag; } }
次に、例7-6について説明します。
-
GraphOracleSem
オブジェクトが構築され、少数のトリプルがGraphOracleSem
オブジェクトに追加されます。これらのトリプルは、複数の個人と、好き、嫌い、知人および他人などの関係を記述します。 -
コスト・マッピングは、数値コストの値を異なるリンク/述語(RDFグラフの)に割り当てるために作成されます。この場合、0.5、1.5および5.5は、それぞれに述語likes、dislikesおよびdiffersに割り当てられます。このコスト・マッピングはオプションです。マッピングがないとき、すべての述語は同じコスト1に割り当てられることになります。コスト・マッピングが指定される際、このマッピングは完全である必要はなく、コスト・マッピングに含まれない述語には、デフォルト値の1が割り当てられます。
例7-6の出力は、次のとおりです。この出力には、指定された開始ノードと終了ノードの最短経路が示されます。sna.shortestPathAStar(ano, nodeX)
の戻り値は、これらの2つのノード間には経路がないため、NULLになります。
From nodeA to nodeC printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://A ## "n" denotes Node printNodeArray: full path item 1 = [e] http://likes ## "e" denotes Edge (Link) printNodeArray: full path item 2 = [n] http://B printNodeArray: full path item 3 = [e] http://likes printNodeArray: full path item 4 = [n] http://C From nodeA to nodeD printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://A printNodeArray: full path item 1 = [e] http://likes printNodeArray: full path item 2 = [n] http://B printNodeArray: full path item 3 = [e] http://likes printNodeArray: full path item 4 = [n] http://C printNodeArray: full path item 5 = [e] http://likes printNodeArray: full path item 6 = [n] http://D From nodeA to nodeF printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://A printNodeArray: full path item 1 = [e] http://likes printNodeArray: full path item 2 = [n] http://B printNodeArray: full path item 3 = [e] http://dislikes printNodeArray: full path item 4 = [n] m1 printNodeArray: full path item 5 = [e] http://likes printNodeArray: full path item 6 = [n] http://F From ano to nodeC printNodeArray: full path starts printNodeArray: full path item 0 = [n] m1 printNodeArray: full path item 1 = [e] http://dislikes printNodeArray: full path item 2 = [n] http://B printNodeArray: full path item 3 = [e] http://likes printNodeArray: full path item 4 = [n] http://C From ano to nodeX Node Array is null
基礎となるRDFグラフ・ビュー(SEMM_<rdf_graph_name>またはRDFM_<rdf_graph_name>)は、NDM関数で直接使用することができないため、必要な表(指定されたRDFグラフに導出されるノードとリンクを含む)は、SemNetworkAnalyst
で作成されます。これらの表は、RDFグラフが変更されても自動更新されないため、SemNetworkAnalyst.getInstance
にcleanup
パラメータにtrue
を設定し、古いノードを削除して表をリンクさせ、更新された表を再構築します。
例7-7 RDFデータ上のNDM nearestNeighbors関数の実装
例7-7では、RDFデータ上にNDM nearestNeighbors
関数を実装します。これによってSemNetworkAnalyst
インスタンスからNetworkAnalyst
オブジェクトを取得し、ノードIDを取得してPointOnNet
オブジェクトを作成し、LogicalSubPath
オブジェクトを処理します。
%cat TestNearestNeighbor.java import java.io.*; import java.util.*; import org.apache.jena.graph.*; import org.apache.jena.update.*; import oracle.spatial.rdf.client.jena.*; import oracle.spatial.rdf.client.jena.SemNetworkAnalyst; import oracle.spatial.network.lod.LODGoalNode; import oracle.spatial.network.lod.LODNetworkConstraint; import oracle.spatial.network.lod.NetworkAnalyst; import oracle.spatial.network.lod.PointOnNet; import oracle.spatial.network.lod.LogicalSubPath; /** * This class implements a nearestNeighbors function on top of RDF data * using public APIs provided in SemNetworkAnalyst and Oracle Spatial NDM */ public class TestNearestNeighbor { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; PrintStream psOut = System.out; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); String szModelName = "test_nn"; // First construct a TBox and load a few axioms ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); String insertString = " PREFIX my: <http://my.com/> " + " INSERT DATA " + " { my:A my:likes my:B . " + " my:A my:likes my:C . " + " my:A my:knows my:D . " + " my:A my:dislikes my:X . " + " my:A my:dislikes my:Y . " + " my:C my:likes my:E . " + " my:C my:likes my:F . " + " my:C my:dislikes my:M . " + " my:D my:likes my:G . " + " my:D my:likes my:H . " + " my:F my:likes my:M . " + " } "; UpdateAction.parseExecute(insertString, model); GraphOracleSem g = model.getGraph(); g.commitTransaction(); g.setDOP(4); HashMap<Node, Double> costMap = new HashMap<Node, Double>(); costMap.put(Node.createURI("http://my.com/likes"), Double.valueOf(1.0)); costMap.put(Node.createURI("http://my.com/dislikes"), Double.valueOf(4.0)); costMap.put(Node.createURI("http://my.com/knows"), Double.valueOf(2.0)); SemNetworkAnalyst sna = SemNetworkAnalyst.getInstance( g, // source RDF graph true, // directed graph true, // cleanup old Node/Link tables costMap ); Node nodeStart = Node.createURI("http://my.com/A"); long origNodeID = sna.getNodeID(nodeStart); long[] lIDs = {origNodeID}; // translate from the original ID long nodeID = (sna.mapNodeIDs(lIDs))[0]; NetworkAnalyst networkAnalyst = sna.getEmbeddedNetworkAnalyst(); LogicalSubPath[] lsps = networkAnalyst.nearestNeighbors( new PointOnNet(nodeID), // startPoint 6, // numberOfNeighbors 1, // searchLinkLevel 1, // targetLinkLevel (LODNetworkConstraint) null, // constraint (LODGoalNode) null // goalNodeFilter ); if (lsps != null) { for (int idx = 0; idx < lsps.length; idx++) { LogicalSubPath lsp = lsps[idx]; Node[] nodePath = sna.processLogicalSubPath(lsp, nodeStart); psOut.println("Path " + idx); printNodeArray(nodePath, psOut); } } g.close(); sna.close(); oracle.dispose(); } public static void printNodeArray(Node[] nodeArray, PrintStream psOut) { if (nodeArray == null) { psOut.println("Node Array is null"); return; } if (nodeArray.length == 0) { psOut.println("Node Array is empty"); } int iFlag = 0; psOut.println("printNodeArray: full path starts"); for (int iHops = 0; iHops < nodeArray.length; iHops++) { psOut.println("printNodeArray: full path item " + iHops + " = " + ((iFlag == 0) ? "[n] ":"[e] ") + nodeArray[iHops]); iFlag = 1 - iFlag; } } }
例7-7の出力は、次のとおりです。
Path 0 printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://my.com/A printNodeArray: full path item 1 = [e] http://my.com/likes printNodeArray: full path item 2 = [n] http://my.com/C Path 1 printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://my.com/A printNodeArray: full path item 1 = [e] http://my.com/likes printNodeArray: full path item 2 = [n] http://my.com/B Path 2 printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://my.com/A printNodeArray: full path item 1 = [e] http://my.com/knows printNodeArray: full path item 2 = [n] http://my.com/D Path 3 printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://my.com/A printNodeArray: full path item 1 = [e] http://my.com/likes printNodeArray: full path item 2 = [n] http://my.com/C printNodeArray: full path item 3 = [e] http://my.com/likes printNodeArray: full path item 4 = [n] http://my.com/E Path 4 printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://my.com/A printNodeArray: full path item 1 = [e] http://my.com/likes printNodeArray: full path item 2 = [n] http://my.com/C printNodeArray: full path item 3 = [e] http://my.com/likes printNodeArray: full path item 4 = [n] http://my.com/F Path 5 printNodeArray: full path starts printNodeArray: full path item 0 = [n] http://my.com/A printNodeArray: full path item 1 = [e] http://my.com/knows printNodeArray: full path item 2 = [n] http://my.com/D printNodeArray: full path item 3 = [e] http://my.com/likes printNodeArray: full path item 4 = [n] http://my.com/H
7.10.1 グラフにおけるパスの文脈情報の生成
パスそのものに加え、グラフ内のパスに関する文脈情報を参照することは、有用な場合があります。SemNetworkAnalyst
クラスのbuildSurroundingSubGraph
メソッドは、DOTファイル(グラフ記述言語ファイル、拡張子.gv
)を、指定されたWriter
オブジェクトに出力できます。パスのノードごとに、最大10の直接の隣接が、パス周辺のサブグラフを生成するために使用されます。次の例は、文脈情報(特に例7-6で使用する解析ファンクションからの出力)を持つDOTファイルの作成方法を示したものです。
nodeArray = sna.shortestPathDijkstra(nodeA, nodeD); printNodeArray(nodeArray, psOut); FileWriter dotWriter = new FileWriter("Shortest_Path_A_to_D.gv"); sna.buildSurroundingSubGraph(nodeArray, dotWriter);
先の例から生成される出力DOTファイルは、次の例に示すように単純な形をしています。
% cat Shortest_Path_A_to_D.gv digraph { rankdir = LR; charset="utf-8"; "Rhttp://A" [ label="http://A" shape=rectangle,color=red,style = filled, ]; "Rhttp://B" [ label="http://B" shape=rectangle,color=red,style = filled, ]; "Rhttp://A" -> "Rhttp://B" [ label="http://likes" color=red, style=bold, ]; "Rhttp://C" [ label="http://C" shape=rectangle,color=red,style = filled, ]; "Rhttp://A" -> "Rhttp://C" [ label="http://dislikes" ]; "Rhttp://D" [ label="http://D" shape=rectangle,color=red,style = filled, ]; "Rhttp://A" -> "Rhttp://D" [ label="http://differs" ]; "Rhttp://B" -> "Rhttp://C" [ label="http://likes" color=red, style=bold, ]; "Rm1" [ label="m1" shape=ellipse,color=blue, ]; "Rhttp://B" -> "Rm1" [ label="http://dislikes" ]; "Rm1" -> "Rhttp://B" [ label="http://dislikes" ]; "Rhttp://C" -> "Rhttp://D" [ label="http://likes" color=red, style=bold, ]; "Rhttp://E" [ label="http://E" shape=ellipse,color=blue, ]; "Rhttp://C" -> "Rhttp://E" [ label="http://knows" ]; "Rm1" -> "Rhttp://D" [ label="http://likes" ]; }
SemNetworkAnalyst
クラスとGraphOracleSem
クラスのメソッドを使用して、より洗練された視覚表現で解析関数出力を生成することもできます。
前述のDOTファイルを、様々なイメージ形式に変換できます。図7-1は、前述のDOTファイルの情報を表現するイメージです。
親トピック: RDFデータの解析関数