5.8 RDFグラフでのプロパティ・グラフ・ビューの作成

Oracle Graphでは、プロパティ・グラフとしてRDFデータを表示し、Oracle Databaseに格納されたRDFグラフを介して、プロパティ・グラフ・ビューを作成することで、グラフの分析操作を実行できます。

RDFモデル(または仮想モデル)を指定して、プロパティ・グラフ機能では2つのビュー(頂点用の<graph_name>VT$ビューおよびエッジ用の<graph_name>GE$ビュー)が作成されます。

PGUtils.createPropertyGraphViewOnRDFメソッドにより、RDFデータに対するプロパティ・グラフ・ビューをカスタマイズできます。

public static void createPropertyGraphViewOnRDF( Connection conn /* a Connection instance to Oracle database */,
      String pgGraphName /* the name of the property graph to be created */,
      String rdfModelName /* the name of the RDF model */,
      boolean virtualModel /* a flag represents if the RDF model
                              is virtual model or not; 
                              true – virtual mode, false – normal model*/,
      RDFPredicate[] predListForVertexAttrs /* an array of RDFPredicate objects specifying how to create vertex view using these predicates; each RDFPredicate includes two fields: an URL of the RDF predicate, the corresponding name of vertex key in the Property Graph. The mapping from RDF predicates to vertex keys will be created based on this parameter.  */,
      RDFPredicate[] predListForEdges /* an array of RDFPredicate specifying how to create edge view using these predicates; each RDFPredicate includes two (or three) fields: an URL of the RDF predicate, the edge label in the Property Graph, the weight of the edge (optional). The mapping from RDF predicates to edges will be created based on this parameter. */)

この操作には、プロパティ・グラフの名前、プロパティ・グラフ・ビューを生成するのに使用するRDFモデルの名前、およびトリプルを頂点またはエッジに解析する方法を決定するマッピングのセットが必要です。createPropertyGraphViewOnRDFメソッドには、RDF述語の頂点用のキー/値プロパティへのマッピング方法を指定するキー/値マッピング配列と、RDF述語のエッジへのマップ方法を指定するエッジ・マッピング配列が必要です。PGUtils.RDFPredicate APIによりRDFアサーションから頂点/エッジへのマップを作成できます。

キー/値マッピングの少なくとも1つのRDF述語に一致するトリプルに基づき、頂点が作成されます。マッピング配列で定義されたRDF述語のいずれかを満たす各トリプルは、トリプルのサブジェクトの内部RDFリソースIDに基づくIDと、自身のマッピングにより定義されたキーおよびトリプルのオブジェクトから取得した値を持つキー/値ペアを持つ頂点へと解析されます。

次の例は、プロパティ名がtitleのキー/値プロパティへのRDF述語URI http://purl.org/dc/elements/1.1/titleのキー/値マッピングを定義します。

String titleURL = "http://purl.org/dc/elements/1.1/title";
// create an RDFPredicate to specify how to map the RDF predicate to vertex keys
RDFPredicate titleRDFPredicate 
              = RDFPredicate.getInstance(titleURL /* RDF Predicate URI */ , 
                                         "title" /* property name */);

エッジのマッピング配列の少なくとも1つのRDF述語に一致するトリプルに基づき、エッジが作成されます。マッピング配列で定義されたRDF述語を満たす各トリプルは、行番号に基づくID、自身のマッピングにより定義されたエッジ・ラベル、トリプルのサブジェクトのRDFリソースIDから取得した出力頂点、トリプルのオブジェクトのRDFリソースIDから取得した入力頂点を持つエッジに解析されます。ここで解析される各トリプルに対し、キー/値マッピングから生成されていない場合は、2つの頂点が作成されます。

次の例は、RDF述語URI http://purl.org/dc/elements/1.1/referenceのラベルreferencesで重みが0.5dのエッジへのエッジ・マッピングを定義します。

String referencesURL = "http://purl.org/dc/terms/references";
// create an RDFPredicate to specify how to map the RDF predicate to edges
RDFPredicate referencesRDFPredicate    
                      = RDFPredicate.getInstance(referencesURL, "references", 0.5d);

次の例は、様々なパブリケーション、作成者および参照を記述する、RDFモデルarticlesに対するプロパティ・グラフ・ビューを作成します。生成されたプロパティ・グラフには、titleおよびcreatorを含む、キー/値プロパティを持つ頂点が含まれます。このプロパティ・グラフのエッジは、パブリケーション間の参照により決定されます。

Oracle oracle = null;
Connection conn = null;
OraclePropertyGraph pggraph = null;
try {
  // create the connection instance to Oracle database
  OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource();
  ds.setURL(jdbcUrl);
  conn = (OracleConnection) ds.getConnection(user, password);
 	
  // define some string variables for RDF predicates
  String titleURL = "http://purl.org/dc/elements/1.1/title";
  String creatorURL = "http://purl.org/dc/elements/1.1/creator";
  String serialnumberURL = "http://purl.org/dc/elements/1.1/serialnumber";
  String widthURL = "http://purl.org/dc/elements/1.1/width";
  String weightURL = "http://purl.org/dc/elements/1.1/weight";
  String onsaleURL = "http://purl.org/dc/elements/1.1/onsale";
  String publicationDateURL = "http://purl.org/dc/elements/1.1/publicationDate";
  String publicationTimeURL = "http://purl.org/dc/elements/1.1/publicationTime";
  String referencesURL = "http://purl.org/dc/terms/references";
     
  // create RDFPredicate[] predsForVertexAttrs to specify how to map 
  // RDF predicate to vertex keys
  RDFPredicate[] predsForVertexAttrs = new RDFPredicate[8];
  predsForVertexAttrs[0] = RDFPredicate.getInstance(titleURL, "title");
  predsForVertexAttrs[1] = RDFPredicate.getInstance(creatorURL, "creator");
  predsForVertexAttrs[2] = RDFPredicate.getInstance(serialnumberURL, 
                                                    "serialnumber");
  predsForVertexAttrs[3] = RDFPredicate.getInstance(widthURL, "width");
  predsForVertexAttrs[4] = RDFPredicate.getInstance(weightURL, "weight");
  predsForVertexAttrs[5] = RDFPredicate.getInstance(onsaleURL, "onsale");
  predsForVertexAttrs[6] = RDFPredicate.getInstance(publicationDateURL, 
                                                    "publicationDate");
  predsForVertexAttrs[7] = RDFPredicate.getInstance(publicationTimeURL, 
                                                    "publicationTime");

  // create RDFPredicate[] predsForEdges to specify how to map RDF predicates to 
  // edges
  RDFPredicate[] predsForEdges = new RDFPredicate[1];
  predsForEdges[0] = RDFPredicate.getInstance(referencesURL, "references", 0.5d);
      
  // create PG view on RDF model
  PGUtils.createPropertyGraphViewOnRDF(conn, "articles", "articles", false, 
                                       predsForVertexAttrs, predsForEdges);

  // get the Property Graph instance
  oracle = new Oracle(jdbcUrl, user, password);
  pggraph = OraclePropertyGraph.getInstance(oracle, "articles", 24);

  System.err.println("------ Vertices from property graph view ------");
  pggraph.getVertices();
  System.err.println("------ Edges from property graph view ------");
  pggraph.getEdges();
}
finally {
  pggraph.shutdown();
  oracle.dispose();
  conn.close();
}

articles RDFモデル(11トリプル)の次のトリプルを指定すると、出力のプロパティ・グラフには2つの頂点、1つは<http://nature.example.com/Article1> (v1)用、およびもう1つは <http://nature.example.com/Article2> (v2)用が含まれます。頂点v1の場合、8つのプロパティを持ち、その値はRDF述語と同じです。たとえば、v1のタイトルは“All about XYZ”です。同様に、頂点v2には、2つのプロパティ、titleおよびcreatorがあります。出力プロパティ・グラフには、エッジ・ラベル“references”で重みが0.5dの、頂点v1から頂点v2への1つのエッジ(eid:1)が含まれます。

<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/title> “All about XYZ”^^xsd:string.
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/creator> “Jane Smith”^^xsd:string. 
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/serialnumber> “123456”^^xsd:integer.
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/width> “10.5”^^xsd:float.
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/weight> “1.08”^^xsd:double. 
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/onsale> “false”^^xsd:boolean. 
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/publicationDate> “2016-03-08”^^xsd:date) 
<http://nature.example.com/Article1> <http://purl.org/dc/elements/1.1/publicationTime> “2016-03-08T10:10:10”^^xsd:dateTime) 
<http://nature.example.com/Article2> <http://purl.org/dc/elements/1.1/title> “A review of ABC”^^xsd:string.
<http://nature.example.com/Article2> <http://purl.org/dc/elements/1.1/creator> “Joe Bloggs”^^xsd:string.
<http://nature.example.com/Article1> <http://purl.org/dc/terms/references> <http://nature.example.com/Article2>.

前述のコードは次のような出力を生成します。内部RDFリソースID値は、異なるOracleデータベースの間では変動する可能性があります。

------ Vertices from property graph view ------
Vertex ID 7299961478807817799 {creator:str:Jane Smith, onsale:bol:false, publicationDate:dat:Mon Mar 07 16:00:00 PST 2016, publicationTime:dat:Tue Mar 08 02:10:10 PST 2016, serialnumber:dbl:123456.0, title:str:All about XYZ, weight:dbl:1.08, width:flo:10.5}
Vertex ID 7074365724528867041 {creator:str:Joe Bloggs, title:str:A review of ABC}
------ Edges from property graph view ------
Edge ID 1 from Vertex ID 7299961478807817799 {creator:str:Jane Smith, onsale:bol:false, publicationDate:dat:Mon Mar 07 16:00:00 PST 2016, publicationTime:dat:Tue Mar 08 02:10:10 PST 2016, serialnumber:dbl:123456.0, title:str:All about XYZ, weight:dbl:1.08, width:flo:10.5} =[references]=> Vertex ID 7074365724528867041 {creator:str:Joe Bloggs, title:str:A review of ABC} edgeKV[{weight:dbl:0.5}]