Source for file N3Serializer.php
Documentation is available at N3Serializer.php
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
* PHP Notation3 Serializer
* This class serialises models to N3 Syntax.
* <li>Using [ ] for blank nodes, or _: if nescessary</li>
* <li>Literal datytype- and xmlLanguageTag support</li>
* Un-supported N3 Features include:
* TODO: * added namespace prefixes are persisent...
* @author Gunnar AA. Grimnes <ggrimnes@csd.abdn.ac.uk>, Daniel Westphal <mail@d-westphal.de>
* @version $Id: fsource_syntax__syntaxN3Serializer.php.html 443 2007-06-01 16:25:38Z cax $
define('MAGIC_STRING', '~~~');
var $done; // keeps track of already serialized resources
* Adds a new namespace prefix to use.
* Unknown namespaces will become ns0, ns1 etc.
* Serializes a model to N3 syntax.
* @param object Model $model
// copy default namespaces
global $default_prefixes;
foreach($default_prefixes as $prefix =>
$namespace)
$nps=
$this->model->getParsedNamespaces();
foreach($nps as $uri =>
$prefix){
foreach ($this->model->triples as $t) {
if ( is_a($s, 'Resource'))
$namespaces[$s->getNamespace()]=
1;
if ( is_a($p, 'Resource'))
$namespaces[$p->getNamespace()]=
1;
if ( is_a($o, 'Resource'))
$namespaces[$o->getNamespace()]=
1;
if (isset
($count[$uri])) {
$this->res .=
'# Generated by N3Serializer.php from RDF RAP.'.
LINEFEED
.
'# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html'
$this->doNamespaces($namespaces);
foreach ( $count as $k=>
$v) {
$this->doResource($resources[$k]);
foreach($ms as $mseach) {
// foreach ($count as $k=>$c) {
// if ( $c>$max) { $maxkey=$k; $max=$c; }
// print "$maxkey is subject of most triples! ($max) \n";
* Serializes a model and saves it into a file.
* Returns FALSE if the model couldn't be saved to the file.
* @param object MemModel $model
* @param string $filename
function saveAs(&$model, $filename) {
// write serialized model to file
$file_handle =
@fopen($filename, 'w');
/* ==================== Private Methods from here ==================== */
* Readies this object for serializing another model
* Makes ns0, ns1 etc. prefixes for unknown prefixes.
function doNamespaces(&$n) {
foreach ($n as $ns =>
$nonsense) {
* Fill in $resourcetext for a single resource.
* Will recurse into Objects of triples, but should never look ? (really?)
* @param object Resource $r
function doResource(&$r) {
$ts=
$this->model->find($r, null, null);
if (count($ts->triples)==
0) return;
if ( isset
($this->done[$r->getURI()]) &&
$this->done[$r->getURI()] ) {
if ( is_a($r, 'BlankNode')) {
//Oh bother, we must use the _:blah construct.
$this->done[$r->getURI()]=
TRUE;
if ( is_a($r, 'Resource') ) {
if ( is_a($r, 'BlankNode') ) {
//test, if this blanknode is referenced somewhere
$rbn=
$this->model->find(null, null, $r);
$out.=
'_:'.
$r->getLabel();
usort($ts->triples, 'statementsorter');
foreach ($ts->triples as $t) {
if ($lastp!=
'') $out.=
' ; ';
if ( is_a($o, 'Literal')) {
if ( $o->getLanguage()!=
'' ) {
$out.=
'@'.
$o->getLanguage();
if ( $o->getDatatype()!=
'' ) {
$out.=
'^^<'.
$o->getDatatype().
'>';
if (is_a($o, 'Resource')) {
if ( is_a($o,'BlankNode')) {
// $this->doResource($o);
// $out.=MAGIC_STRING.$o->getURI().MAGIC_STRING; #$this->resourcetext[$o->getURI()];
// $this->resourcetext_taken[$o->getURI()]=1;
$out .=
'_:'.
$o->getLabel();
function doURI(&$r, &$out) {
if ( $r->getURI()==
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
if ($r->getNamespace()!=
'') {
$out.=
$this->prefixes[$r->getNamespace()].
':'.
$r->getLocalName();
* Fix the resourcetext for a blanknode where the _: construct was used
function fixAnon($t,$a) {
Documentation generated on Fri, 1 Jun 2007 16:50:01 +0200 by phpDocumentor 1.3.2