RDF API for PHP V0.9.3

Test: Manipulate DbModel


This example is part of the RAP - Rdf API for PHP documentation.


1. Connect to rdf database (e.g. Access via ODBC)

Set the system DSN for the particular database file (control pannel -> administrative tools -> data sources (odbc) -> system DSN).

// Include RAP
define("RDFAPI_INCLUDE_DIR", "./../api/");
include(RDFAPI_INCLUDE_DIR . "RDFAPI.php");
 
// Connect to MsAccess database (rdf_db DSN)
// using connection settings defined in constants.php :
$rdf_database = ModelFactory::getDbStore();

Read more about Setting Database Connection.


2. Load and show a DbModel

// Load the model with modelURI "example1.rdf"
$dbModel = $rdf_database->getModel("example1.rdf");

// Output the model as HTML table
$dbModel->writeAsHtmlTable();


Note:   getModel() loads into memory only the modelURI and a database internal modelID, it also sets a database connection for the DbModel. This saves main memory and allows to gain advantage of database's indexing capabilities.

Output:

Base URI: http://www.w3.org/Home/Lassila.rdf#

Size: 4

No.

Subject

Predicate

Object

1.

Resource: http://www.w3.org/Home/Lassila

Resource: http://description.org/schema/Creator

Blank Node: b85740

2.

Blank Node: b85740

RDF Node: http://www.w3.org/1999/02/22-rdf-syntax-ns#type

Resource: http://description.org/schema/Person

3.

Blank Node: b85740

Resource: http://example.org/stuff/1.0/Name

Literal: Ora Lassila (rdf:datatype="http://www.w3.org/TR/xmlschema-2#string")

4.

Blank Node: b85740

Resource: http://example.org/stuff/1.0/Email

Literal: lassila@w3.org



3. Add a statement to the DbModel

// Ceate a new statement
$statement = new Statement(new Resource("http://www.w3.org/Home/Lassila"),
             new Resource("http://description.org/schema/Description"),
             new Literal("Lassilas persönliche Homepage", "de"));

// Add the statement to the DbModel
$dbModel->add($statement);

// Output the string serialization of the DbModel
echo $dbModel->toStringIncludingTriples();

Output:

MemModel[baseURI=http://www.w3.org/Home/Lassila.rdf#; size=5] Triple(Resource("http://www.w3.org/Home/Lassila"), Resource("http://description.org/schema/Creator"), bNode("b85740")) Triple(bNode("b85740"), Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), Resource("http://description.org/schema/Person")) Triple(bNode("b85740"), Resource("http://example.org/stuff/1.0/Name"), Literal("Ora Lassila", datatype="http://www.w3.org/TR/xmlschema-2#string")) Triple(bNode("b85740"), Resource("http://example.org/stuff/1.0/Email"), Literal("lassila@w3.org")) Triple(Resource("http://www.w3.org/Home/Lassila"), Resource("http://description.org/schema/Description"), Literal("Lassilas persönliche Homepage", lang="de"))


4. Search statements

// Search for statements having object $literal
$literal = new Literal("Lassilas persönliche Homepage", "de");
$res = $dbModel->find(NULL, NULL, $literal);

// Output the result
$res->writeAsHtmlTable();

Output:

Base URI: http://www.w3.org/Home/Lassila.rdf#

Size: 1

No.

Subject

Predicate

Object

1.

Resource: http://www.w3.org/Home/Lassila

Resource: http://description.org/schema/Description

Literal: Lassilas persönliche Homepage (xml:lang="de")



5. Replace nodes and serialize the DbModel to XML/RDF

// replace a literal
$dbModel->replace(NULL, NULL, new Literal("Lassilas persönliche Homepage", "de"),
                              new Literal ("Lassila's personal Homepage", "en"));

// Serialize to RDF
$dbModel->writeAsHtml();

Output:

<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF
   xml:base="http://www.w3.org/Home/Lassila.rdf#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
   xmlns:ns1="http://description.org/schema/"
   xmlns:ns2="http://example.org/stuff/1.0/">

<ns1:Person rdf:nodeID="b85740">
   <ns2:Email>lassila@w3.org</ns2:Email>
   <ns2:Name rdf:datatype="http://www.w3.org/TR/xmlschema-2#string">Ora Lassila</ns2:Name>
</ns1:Person>

<rdf:Description rdf:about="http://www.w3.org/Home/Lassila">
   <ns1:Creator rdf:nodeID="b85740"/>
   <ns1:Description xml:lang="en">Lassila's personal Homepage</ns1:Description>
</rdf:Description>

</rdf:RDF>


6. Remove a statement

$dbModel->remove(new Statement (new Resource("http://www.w3.org/Home/Lassila"),
                                new Resource("http://description.org/schema/Description"),
                                new Literal("Lassila's personal Homepage", "en")));

// Output the DbModel
$dbModel->writeAsHtmlTable();

Output:

Base URI: http://www.w3.org/Home/Lassila.rdf#

Size: 4

No.

Subject

Predicate

Object

1.

Resource: http://www.w3.org/Home/Lassila

Resource: http://description.org/schema/Creator

Blank Node: b85740

2.

Blank Node: b85740

RDF Node: http://www.w3.org/1999/02/22-rdf-syntax-ns#type

Resource: http://description.org/schema/Person

3.

Blank Node: b85740

Resource: http://example.org/stuff/1.0/Name

Literal: Ora Lassila (rdf:datatype="http://www.w3.org/TR/xmlschema-2#string")

4.

Blank Node: b85740

Resource: http://example.org/stuff/1.0/Email

Literal: lassila@w3.org



7. Generate a MemModel and compare both models

// Generate a MemModel
$memModel = $dbModel->getMemModel();

// Compare this DbModel withe the generated MemModel
$res = $dbModel->equals($memModel);

// Output the result
if ($res)
   echo "models are equal";
else
   echo "models are different";

Output:

models are equal


8. Save DbModel to file

// Save model to file (XML/RDF)
$dbModel->saveAs("Output.rdf");

// Save model to file (N3)
$dbModel->saveAs("Output.n3");

// close the DbModel
$dbModel->close();


Output:

Files: Output.rdf
        Output.n3