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
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);
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:
@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>.
@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:
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();
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.
The resource that also had other predicates still exists, with those predicates intact.
Once we have the graph to our liking, we can write it back to a file:
$filesystem->update('./output.ttl', $graph->serialise($format));