8.9 SHACL制約によるRDFデータの検証

この項では、Oracle RDF Graph Adapter for Eclipse RDF4Jを使用してSHACL (Shapes Constraint Language)制約によりRDFグラフを検証する方法について説明します。

SHACLは、RDFグラフに対して制約を指定するためのW3C標準です。たとえば、SHACLにより、ex:Personクラスのすべてのインスタンスにex:nameプロパティの値が存在する必要があることを指定できます。SHACLにより、RDFボキャブラリを定義します。これによって、制約を指定できるようになります。SHACL制約を含むRDFグラフを形状グラフと呼び、検証されるRDFグラフをデータ・グラフと呼びます。

RDF4Jでは、そのShaclSailクラスを介してSHACLがサポートされます。RDF4JにおけるSHACLエンジンでは、RDF4Jのトランザクション・メカニズムを介してグラフに対する変更がコミットされるときに、グラフが検証されます。RDF4JのSHACLサポートの一般情報は、RDF4Jのドキュメントを参照してください。

OracleShaclSailクラスを介してOracle Adapter for Eclipse RDF4JでShaclSailを使用している場合は、トランザクションがコミットされるときに、完全なSHACL検証が実行されます。データの一部を対象とした増分検証はサポートされていません。そのため、OracleShaclSailオブジェクトを介して多数の小規模な変更をコミットしないようにし、大規模なバルク検証操作にOracleShaclSailを使用する必要があります。RDF4JのSHACLエンジンにより、制約違反をチェックするために一連のSPARQL問合せがOracle Databaseに送信されます。バルク検証の一般的なワークフローを次に示します:

  1. Oracle Databaseに格納されているRDFデータ・グラフ用にOracleSailStoreオブジェクトを作成します。
  2. OracleSailStoreをラップするOracleShaclSailオブジェクトを作成します。
  3. OracleShaclSailオブジェクトからSailRepositoryを作成します。
  4. SailRepositoryでトランザクションを開始します。
  5. 形状グラフを、トランザクションの一部としてRDF4J.SHACL_SHAPE_GRAPHコンテキストに追加します(この形状グラフは様々なソースからロードできます)。
  6. そのトランザクションをコミットします。
  7. 制約に違反すると、RepositoryExceptionが発生します。
  8. 例外が発生した場合は、検証レポートを確認します。

次のコード部分で、これらのステップを示します:

// Get an OracleSailStore instance for the stored data graph to validate
OraclePool op = new OraclePool(jdbcURL, user, password);
NotifyingSail store = new OracleSailStore(op, "DATA_GRAPH", "SCOTT", "NET1");

// Create an OracleShaclSail on top of the underlying OracleSailStore
ShaclSail shaclSail = new OracleShaclSail(store);
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();

// Get a connection from the repository, start a transaction,
// load a shapes graph and commit the transaction to validate the data graph
try (SailRepositoryConnection conn = sailRepository.getConnection()) {
  conn.begin();
  // clear any existing shapes graph
  conn.clear(RDF4J.SHACL_SHAPE_GRAPH);
  // Add current shapes graph
  // Every person must have a name property
  StringReader shaclRules = new StringReader(
    String.join(
      "\n", "",
      "@prefix ex: <http://oracle.example.com/ns#> .",
      "@prefix sh: <http://www.w3.org/ns/shacl#> .",
      "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .",
      "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .",
      "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .",

      "ex:MinCountShape",
      "  a sh:NodeShape ;",
      "  sh:targetClass ex:Person ;",
      "  sh:property [",
      "    sh:path ex:name ;",
      "    sh:minCount 1 ;",
      "  ] ."
    )
  );

  try {
    conn.add(shaclRules, null, RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
  }
  catch (IOException e) {
    e.printStackTrace();
  }

  // Commit transaction to validate the data graph with the current shapes graph
  try {
    conn.commit();
  }
  catch (RepositoryException e) {
    Throwable cause = e.getCause();
    if (cause instanceof ValidationException) {
      Model validationReportModel = ((ValidationException) cause).validationReportAsModel();

      WriterConfig writerConfig = new WriterConfig()
        .set(BasicWriterSettings.INLINE_BLANK_NODES, true)
        .set(BasicWriterSettings.XSD_STRING_TO_PLAIN_LITERAL, true)
        .set(BasicWriterSettings.PRETTY_PRINT, true);

      Rio.write(validationReportModel, System.out, RDFFormat.TURTLE, writerConfig);
    }
    else {
      e.printStackTrace();
    }
  }
}

8.9.1 Oracle Adapter for Eclipse RDF4JでサポートされているSHACL機能

この項では、Oracle Adapter for Eclipse RDF4JでサポートされているSHACLコア制約を示します。

  • http://www.w3.org/ns/shacl#alternativePath
  • http://www.w3.org/ns/shacl#class
  • http://www.w3.org/ns/shacl#datatype
  • http://www.w3.org/ns/shacl#deactivated
  • http://www.w3.org/ns/shacl#flags
  • http://www.w3.org/ns/shacl#hasValue
  • http://www.w3.org/ns/shacl#in
  • http://www.w3.org/ns/shacl#inversePath
  • http://www.w3.org/ns/shacl#languageIn
  • http://www.w3.org/ns/shacl#maxCount
  • http://www.w3.org/ns/shacl#maxExclusive
  • http://www.w3.org/ns/shacl#maxInclusive
  • http://www.w3.org/ns/shacl#maxLength
  • http://www.w3.org/ns/shacl#minCount
  • http://www.w3.org/ns/shacl#minExclusive
  • http://www.w3.org/ns/shacl#minInclusive
  • http://www.w3.org/ns/shacl#minLength
  • http://www.w3.org/ns/shacl#node
  • http://www.w3.org/ns/shacl#nodeKind
  • http://www.w3.org/ns/shacl#path
  • http://www.w3.org/ns/shacl#pattern
  • http://www.w3.org/ns/shacl#property
  • http://www.w3.org/ns/shacl#severity
  • http://www.w3.org/ns/shacl#target
  • http://www.w3.org/ns/shacl#targetClass
  • http://www.w3.org/ns/shacl#targetNode
  • http://www.w3.org/ns/shacl#targetObjectsOf
  • http://www.w3.org/ns/shacl#targetSubjectsOf
  • http://www.w3.org/ns/shacl#uniqueLang
次の機能はデフォルトでは有効になっていませんが、システム・プロパティoracle.rdf4j.adapter.restrictShaclFeaturesを値Fに設定することで有効にできます。これらの機能を使用して制約を評価するときにRDF4JのSHACLエンジンの実装によってターゲット・ノードのセットがクライアント・プログラムのメモリーに読み込まれるため、これらの機能は小さいRDFデータセットでのみ使用してください。
  • http://www.w3.org/ns/shacl#and
  • http://www.w3.org/ns/shacl#not
  • http://www.w3.org/ns/shacl#or
  • http://www.w3.org/ns/shacl#qualifiedMaxCount
  • http://www.w3.org/ns/shacl#qualifiedMinCount
  • http://www.w3.org/ns/shacl#qualifiedValueShape
  • http://www.w3.org/ns/shacl#sparql

8.9.2 RDF4JのSHACL機能の使用に関する制限事項

この項では、RDF4JのSHACL機能の使用に関する制限事項を説明します。

Oracle Adapter for Eclipse RDF4Jでは、予約済名前付きグラフ(コンテキスト) http://rdf4j.org/schema/rdf4j#SHACLShapeGraphに格納されているSHACL形状のみがサポートされています。一般に、ShaclSailでは、setShapesGraphメソッドで識別された任意の名前付きグラフから形状グラフをロードできます。しかしながら、setShapesGraphはOracle Adapter for Eclipse RDF4Jではサポートされていません。