Easy RDF Example

This example shows how to read a RDF file, find a node, remove that node or a property from that node and write the result back to the file, using Flysystem

Set things up

Creating a Flysystem and EasyRDF Graph is simplicity itself:

  $filesystem = new Filesystem(new Local(__DIR__));
  
  
$graph = new EasyRdf_Graph();  

To make your live easier and the Turtle pretttier, it is possible to specify specific namespaces to be used as a prefix:

  $namespace 'https://purl.org/pdsinterop/link-metadata#';
  
EasyRdf_Namespace::set('lm'$namespace);

Read a RDF file

The next thing we will want to do is read the contents from a file and feed it to the Graph to parse it.

Read file ./input.ttl as RDF resource:

  $filename './input.ttl';

  
$contents $filesystem->read($filename);

  
$graph->parse($contents$format$filename);

If we compare the original file contents with the parsed contents, we can see the graph is slightly different, according to Easy RDF:

Original

@prefix : <#>.
@prefix tes: <testDeleted/>.
@prefix lin: <https://purl.org/pdsinterop/link-metadata#>.
@prefix stor: <./>.
@prefix te: <testForget/>.

@prefix dc: <http://purl.org/dc/terms/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .


tes: lin:deleted "Because we say so".

stor:testExtraRedirect lin:redirectTemporary <https://localhost/redirect-extra.html>.

te: lin:forget "You have the right to be forgotten";
    dc:title "Nested Test document" ;
    rdfs:comment "Dummy file for testing metadata file in a parent directory" ;
    rdfs:comment "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'" .

stor:testPermanentRedirect lin:redirectPermanent <https://localhost/redirect-permanent.html>.

stor:testTempRedirect lin:redirectTemporary <https://localhost/redirect-temporary.html>.

Parsed

@prefix lm: <https://purl.org/pdsinterop/link-metadata#> .
@prefix dc: <http://purl.org/dc/terms/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<testDeleted/> lm:deleted "Because we say so" .
<testExtraRedirect> lm:redirectTemporary <https://localhost/redirect-extra.html> .
<testForget/>
  lm:forget "You have the right to be forgotten" ;
  dc:title "Nested Test document" ;
  rdfs:comment "Dummy file for testing metadata file in a parent directory", "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'" .

<testPermanentRedirect> lm:redirectPermanent <https://localhost/redirect-permanent.html> .
<testTempRedirect> lm:redirectTemporary <https://localhost/redirect-temporary.html> .

To see what the parsed graph looks like, we can use $graph->dump() to get nice output:

Graph:
testDeleted/ (EasyRdf_Resource)
lm:deleted "Because we say so"
testExtraRedirect (EasyRdf_Resource)
lm:redirectTemporary https://localhost/redirect-extra.html
testForget/ (EasyRdf_Resource)
lm:forget "You have the right to be forgotten"
dc:title "Nested Test document"
rdfs:comment "Dummy file for testing metadata file in a parent directory", "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"
testPermanentRedirect (EasyRdf_Resource)
lm:redirectPermanent https://localhost/redirect-permanent.html
testTempRedirect (EasyRdf_Resource)
lm:redirectTemporary https://localhost/redirect-temporary.html

Find a node

There are various ways to get a node from a graph. Which one is best for you depends on the usecase you find yourself in.

Given that an RDF statement is: (subject) (predicate) (object)

If we use the following for our example:

<testForget/> lm:forget "You have the right to be forgotten"

It is possible to get that resource based on its subject or predicate.

  /*/ Get Resource by subject /*/
  
$resource $graph->resource('<testForget/>');
  
  
/*/ Get Resources by predicate /*/
  
$resourceMatching $graph->resourcesMatching('lm:forget');
  
  
/*/ Check if a resource has a specific predicate /*/
  
$hasPredicate $resource->hasProperty('lm:forget');
  
  
/*/ Get predicates, with prefix /*/
  
$predicatesPrefixed $resource->properties();

  
/*/ Get predicates, as full URIs /*/
  
$predicatesUris $resource->propertyUris();
  
  
/*/ Get value (i.e. object) based on subject and predicate /*/
  
$object $graph->get('<testForget/>''lm:forget');
  

It is possible to get all resources, so they can be iterated

  $resources $graph->resources();
Read 8 resources
  1. testDeleted/
    testDeleted/ (EasyRdf_Resource)
    lm:deleted "Because we say so"
    Local Name
    Properties
    array ( 0 => 'lm:deleted', )
    PropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#deleted', )
    URI
    testDeleted/
  2. testExtraRedirect
    testExtraRedirect (EasyRdf_Resource)
    lm:redirectTemporary https://localhost/redirect-extra.html
    Local Name
    testExtraRedirect
    Properties
    array ( 0 => 'lm:redirectTemporary', )
    PropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', )
    URI
    testExtraRedirect
  3. testForget/
    testForget/ (EasyRdf_Resource)
    lm:forget "You have the right to be forgotten"
    dc:title "Nested Test document"
    rdfs:comment "Dummy file for testing metadata file in a parent directory", "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"
    Local Name
    Properties
    array ( 0 => 'lm:forget', 1 => 'dc:title', 2 => 'rdfs:comment', )
    PropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#forget', 1 => 'http://purl.org/dc/terms/title', 2 => 'http://www.w3.org/2000/01/rdf-schema#comment', )
    URI
    testForget/
  4. testPermanentRedirect
    testPermanentRedirect (EasyRdf_Resource)
    lm:redirectPermanent https://localhost/redirect-permanent.html
    Local Name
    testPermanentRedirect
    Properties
    array ( 0 => 'lm:redirectPermanent', )
    PropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#redirectPermanent', )
    URI
    testPermanentRedirect
  5. testTempRedirect
    testTempRedirect (EasyRdf_Resource)
    lm:redirectTemporary https://localhost/redirect-temporary.html
    Local Name
    testTempRedirect
    Properties
    array ( 0 => 'lm:redirectTemporary', )
    PropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', )
    URI
    testTempRedirect
  6. https://localhost/redirect-extra.html
    Local Name
    redirect-extra.html
    ReversePropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', )
    URI
    https://localhost/redirect-extra.html
  7. https://localhost/redirect-permanent.html
    Local Name
    redirect-permanent.html
    ReversePropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#redirectPermanent', )
    URI
    https://localhost/redirect-permanent.html
  8. https://localhost/redirect-temporary.html
    Local Name
    redirect-temporary.html
    ReversePropertyUris
    array ( 0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', )
    URI
    https://localhost/redirect-temporary.html

Remove predicate from a resource for a specific namespace (URI base)

Lets say we want to remove all predicates that have the namespace https://purl.org/pdsinterop/link-metadata from all the resource:


  $namespace 
'https://purl.org/pdsinterop/link-metadata#';

  foreach (
$graph->resources() as $resource) {
    
$predicates $resource->propertyUris();

    foreach (
$predicates as $predicate) {
      if (
strpos($predicate$namespace) === 0) {
        
$graph->deleteSingleProperty($resource$predicate);
      }
    }
  }

If we compare the original dump with that after the predicate was removed, we can see that all subjects that only had the predicate are removed.

Parsed before delete

Graph:
testDeleted/ (EasyRdf_Resource)
lm:deleted "Because we say so"
testExtraRedirect (EasyRdf_Resource)
lm:redirectTemporary https://localhost/redirect-extra.html
testForget/ (EasyRdf_Resource)
lm:forget "You have the right to be forgotten"
dc:title "Nested Test document"
rdfs:comment "Dummy file for testing metadata file in a parent directory", "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"
testPermanentRedirect (EasyRdf_Resource)
lm:redirectPermanent https://localhost/redirect-permanent.html
testTempRedirect (EasyRdf_Resource)
lm:redirectTemporary https://localhost/redirect-temporary.html

Parsed after delete

Graph:
testForget/ (EasyRdf_Resource)
dc:title "Nested Test document"
rdfs:comment "Dummy file for testing metadata file in a parent directory", "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"

The resource that also had other predicates still exists, with those predicates intact.

Write result back to file

Once we have the graph to our liking, we can write it back to a file:

$filesystem->update('./output.ttl'$graph->serialise($format));