<?php
// ----------------------------------------------------------------------------------
// PHPScript: rap_tutorial.php
// ----------------------------------------------------------------------------------

/**
 * RAP tutorial.
 *
 * <BR><BR>History:<UL>
 * <LI>10-16-2004                : Updated to use the ModelFactory</LI>
 * <LI>08-22-2003                : First version</LI>
 *
 * @author   Radoslaw Oldakowski <radol@gmx.de>
 */

echo "<h1>RAP Tutorial</h1>";

// Include all RAP classes
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!! This should reflect your own installation !!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
define("RDFAPI_INCLUDE_DIR", "C:/Apache/htdocs/rdf_api/api/");
include(RDFAPI_INCLUDE_DIR . "RdfAPI.php");
include(RDFAPI_INCLUDE_DIR . PACKAGE_VOCABULARY);


// Create subject and predicate of $statement1
$someDoc = new Resource ("http://www.example.org/someDocument.html");
$creator = new Resource ("http://www.purl.org/dc/elements/1.1/creator");

// Create and instance of object Statement with subject, predicate and object
// passed to the creator function.
$statement1 = new Statement ($someDoc, $creator, new Literal ("Radoslaw Oldakowski"));

// Create a new MemModel
$model1 = ModelFactory::getDefaultModel();

// Add the statement $statement1 to the model $model1
$model1->add($statement1);

// Create second MemModel
$model2 = new MemModel();

// Add two statements to $model2
$model2->add(new Statement($someDoc,
                           new Resource("http://www.example.org/myVocabulary/title"),
                           new Literal("RAP tutorial")));
$model2->add(new Statement($someDoc,
                           new Resource("http://www.example.org/myVocabulary/language"),
                           new Literal("English")));

// Add the second model ($model2) to the first one ($model1)
$model1->addModel($model2);


// Check the size of $model1
echo "<h4>Section 3, output the size of the MemModel: </h4>";
echo "\$model1 contains " .$model1->size() ." statements<br>";
echo "<br>";


// Output $model1 as HTML table
echo "<h4><br>Section 4, output the MemModel as HTML table: </h4>";
$model1->writeAsHtmlTable();

// Output the string serialization of $model1
echo "<h4><br>Section 4, output the plain text serialization of the MemModel: </h4>";
echo $model1->toStringIncludingTriples();

// Output the RDF/XML serialization of $model1
echo "<h4><br>Section 4, output the RDF/XML serialization of the MemModel: </h4>";
echo $model1->writeAsHtml();
echo "<br><br>";

// Save the model to file in RDF/XML and N3
$model1->saveAs("model1.rdf", "rdf"); 
$model1->saveAs("model1.n3", "n3"); 

// Replace the Literal for a BlankNode
$bNode = new BlankNode($model1);
$model1->replace(NULL, NULL, new Literal("Radoslaw Oldakowski"), $bNode);

// Add statements describing the BlankNode using pre-defined vCard vocabulary
$model1->add(new Statement($bNode, $VCARD_FN, new Literal("Radoslaw Oldakowski")));
$model1->add(new Statement($bNode, $VCARD_EMAIL, new Literal("radol@gmx.de")));

// Create a new literal, its datatype and then add it to the MemModel $model1
$age = new Literal("26");
$age->setDatatype("http://www.w3.org/TR/xmlschema-2/integer");
$model1->add(new Statement ($bNode, new Resource("http://www.example.org/myVocabulary/age"), $age));

// Create a statement iterator for $model1
$it = $model2->getStatementIterator();

// Use the iterator to output the string serialization of all statements
// Traverse model and output statements
echo "<h4><br>Section 8, traverse the Model:</h4>";
while ($it->hasNext()) {
	$statement = $it->next();
	echo "Statement number: " . $it->getCurrentPosition() . "<BR>";
	echo "Subject: " . $statement->getLabelSubject() . "<BR>";
	echo "Predicate: " . $statement->getLabelPredicate() . "<BR>";
	echo "Object: " . $statement->getLabelObject() . "<P>";
}
echo "<br>";

// Find all statements having subject equal $someDoc and print the result as HTML table
echo "<h4><br>Section 10, find statements:</h4>";
$result = $model1->find($someDoc, NULL, NULL);
$result->writeAsHtmlTable();
echo "<br><br>";

// Reify all statements of $model2 and show the corresponding reified model as HTML table
echo "<h4><br>Section 11, output the reified Model: </h4>";
$reified = $model2->reify();
$reified->writeAsHtmlTable();


// Close all MemModels used in this tutorial
$model1->close();
$model2->close();
$result->close();
$reified->close();

?>
