function testMemUnequal() { foreach ($_SESSION['NegativeModelEqualstests'] as $name) { $_SESSION['test'] = 'testfileNegative' . $name . ' test'; $model1 = ModelFactory::getMemModel(); $model2 = ModelFactory::getMemModel(); $model1->load(MODEL_TESTFILES . 'data/testfileNegative' . $name . '_1.nt'); $model2->load(MODEL_TESTFILES . 'data/testfileNegative' . $name . '_2.nt'); $this->assertFalse($model1->equals($model2)); } }
/** * Main function * * @param $url location of the document * @return parsed model */ function generateModel($url) { $rss = null; if (substr($url, 0, 7) == "http://") { $rss = fetch_rss($url); } else { $string = file_get_contents($url); $rss = new MagpieRSS($string); } $model = ModelFactory::getMemModel(); $bn = new Resource($url); $this->type = $rss->feed_type; $version = $rss->feed_version; $nmsp = null; if ($version == "1.0") { $nmsp = "http://purl.org/rss/1.0/"; } if ($version == "0.91") { $nmsp = "http://my.netscape.com/publish/formats/rss-spec-0.91.html#"; } if ($version == "2.0") { $nmsp = "http://purl.org/rss/1.0/"; } if ($this->type == "Atom") { $nmsp = "http://www.w3.org/2005/Atom"; } if ($this->type == "Atom") { $this->channel($model, $rss, $bn, $nmsp, "feed"); } else { $this->channel($model, $rss, $bn, $nmsp, "channel"); } $this->image($model, $rss, $bn, $nmsp); $this->items($model, $rss, $bn, $nmsp); $model->addNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); if ($nmsp != null) { $model->addNamespace($this->type, $nmsp); } return $model; }
/** * Returns an OntModel. * $modelType has to be one of the following constants: * MEMMODEL, DBMODEL, INFMODELF, INFMODELB to create a OntModel * with a new model from defined type. * $vocabulary defines the ontology language. Currently only * RDFS_VOCABULARY is supported. You can supply a model base URI. * * @param constant $modelType * @param constant $vocabulary * @param string $baseURI * @return object OntModel * @access public */ function &getOntModel($modelType, $vocabulary, $baseURI = null) { switch ($modelType) { case DBMODEL: $baseModel = ModelFactory::getDefaultDbModel($baseURI); break; case INFMODELF: $baseModel = ModelFactory::getInfModelF($baseURI); break; case INFMODELB: $baseModel = ModelFactory::getInfModelB($baseURI); break; default: $baseModel = ModelFactory::getMemModel($baseURI); } return ModelFactory::getOntModelForBaseModel($baseModel, $vocabulary); }
function DBpediaLookup($item, &$resultsLabel, &$resourceFound) { global $QUERY_LLL, $FLICKRSERVICE; $resURI = DBPEDIA_URI_ROOT . wikipediaEncode($item); $photosDocURI = FLICKRWRAPPR_PHOTOS_DOC_URI_ROOT . wikipediaEncode($item); /* Prepare SPARQL query for label, latitude, longitude */ $queryString = parameterize($QUERY_LLL, array("RESOURCE" => $resURI, "GRAPH_URI" => SPARQL_GRAPH_URI)); $query = new ClientQuery(); $query->query($queryString); /* Perform SPARQL query */ $sparqlClient = ModelFactory::getSparqlClient(SPARQL_ENDPOINT_URL); $queryResult = $sparqlClient->query($query); /* Initialize result model */ $resultModel = ModelFactory::getMemModel(); $resultModel->addNamespace('foaf', 'http://xmlns.com/foaf/0.1/'); $resultModel->addNamespace('dcterms', 'http://purl.org/dc/terms/'); $resultModel->addNamespace('rdfs', 'http://www.w3.org/2000/01/rdf-schema#'); /* Perform flickr search */ $processedLabels = array(); $resourceFound = !empty($queryResult); if (is_array($queryResult)) { foreach ($queryResult as $resultElement) { $label = $resultElement['?label']->getLabel(); /* Workaround #1: Virtuoso trailing quote bug */ if (substr($label, -1) == '"') { $label = substr($label, 0, -1); } /* Workaround #2: For some reason, the result is UTF8-encoded twice [seems to be fixed now!] */ //$label = utf8_decode($label); /* Multiple languages may have the same label */ if (in_array($label, $processedLabels)) { continue; } else { $processedLabels[] = $label; } if ($resultElement['?lat'] instanceof Literal && $resultElement['?long'] instanceof Literal) { $flickrPhotos = $FLICKRSERVICE->getFlickrPhotos($label, $resultElement['?lat']->getLabel(), $resultElement['?long']->getLabel(), SEARCH_RADIUS_KM); } else { $flickrPhotos = $FLICKRSERVICE->getFlickrPhotos($label); } /* Process found photos */ foreach ($flickrPhotos as $flickrPhoto) { /* Provide the picture itself (small version) */ $resultModel->add(new Statement(new Resource($resURI), new Resource("http://xmlns.com/foaf/0.1/depiction"), new Resource($flickrPhoto['imgsmall']))); /* Provide its page on flickr */ $resultModel->add(new Statement(new Resource($flickrPhoto['imgsmall']), new Resource("http://xmlns.com/foaf/0.1/page"), new Resource($flickrPhoto['flickrpage']))); } if ($resultModel->size() >= MINIMUM_RESULT_COUNT * 2) { break; } } } /* $queryResult */ if ($resultModel->size() > 0) { /* Add metadata */ $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), new Resource("http://xmlns.com/foaf/0.1/Document"))); $resultsLabel = "Photos for DBpedia.org resource " . $_REQUEST['item']; $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://www.w3.org/2000/01/rdf-schema#label"), new Literal($resultsLabel, "en"))); $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://xmlns.com/foaf/0.1/primaryTopic"), new Resource($resURI))); $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://purl.org/dc/terms/license"), new Resource(FLICKR_TOS_URL))); $resultModel->add(new Statement(new Resource($photosDocURI), new Resource("http://xmlns.com/foaf/0.1/maker"), new Resource(FLICKRWRAPPR_HOMEPAGE))); $resultModel->add(new Statement(new Resource(FLICKRWRAPPR_HOMEPAGE), new Resource("http://www.w3.org/2000/01/rdf-schema#label"), new Literal("flickr(tm) wrappr", "en"))); } return $resultModel; }
<?php /** * Example: Sparql query on memory model */ require_once dirname(__FILE__) . '/config.php'; require_once RDFAPI_INCLUDE_DIR . '/model/ModelFactory.php'; $model = ModelFactory::getMemModel(); $model->load(SPARQL_TESTFILES . 'data/model9.n3'); $qs = 'SELECT * WHERE { ?s ?p ?o}'; $result = $model->sparqlQuery($qs); //header('Content-Type: text/html'); //echo $result . "\n"; var_dump($result);
function rdf_encode_api($data, $letter, $format, $db) { $m = ModelFactory::getMemModel(); define('s3db', 'http://www.s3db.org/core#'); define('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); define('rdfs', 'http://www.w3.org/2000/01/rdf-schema#'); foreach ($data as $ind => $res_data) { $ID = $res_data[$GLOBALS['COREletterInv'][$letter]]; #echo $letter;exit; #echo $GLOBALS['COREletterInv'][$letter];exit; $D = S3DB_URI_BASE; $URI = $D . '/' . $letter . $ID; $mResource = new Resource($URI); foreach ($res_data as $property => $value) { if (!empty($value)) { if (@in_array($property, @array_keys($GLOBALS['propertyURI'][$letter]))) { $predicate = new Resource($GLOBALS['propertyURI'][$letter][$property]); #else #{ # $predicate = new Resource (s3db.$property); #} if (in_array($property, array_keys($GLOBALS['pointer']))) { $s = new Statement($mResource, $predicate, new Resource($D . '/' . letter($GLOBALS['pointer'][$property]) . $value)); } elseif (in_array($property, $GLOBALS['COREids'])) { $s = new Statement($mResource, $predicate, new Resource($D . '/' . letter($property) . $value)); } elseif ($property == 'value' && RuleHasObjectId($ID, $db)) { $s = new Statement($mResource, $predicate, new Resource($D . '/I' . $value)); } else { $s = new Statement($mResource, $predicate, new Literal($value)); } $m->add($s); } } } if ($letter == 'S') { ##Outputalso a statment for the unserialized statement where subject is item_id, pred is rule, object is value if (RuleHasObjectId($ID, $db)) { $obj = new Resource($D . '/I' . $data[$ind]['value']); } else { $obj = new Literal($data[$ind]['value']); } $newS = new Statement(new Resource($D . '/I' . $data[$ind]['item_id']), new Resource($D . '/R' . $data[$ind]['rule_id']), $obj); $m->add($newS); } if ($letter == 'R') { ##Outputalso a statment for the unserialized statement where subject is item_id, pred is rule, object is value if ($data[$ind]['object_id'] != '') { $obj = new Resource($D . '/C' . $data[$ind]['object_id']); } else { $obj = new Literal($data[$ind]['object']); } $newS = new Statement(new Resource($D . '/C' . $data[$ind]['subject_id']), new Resource($D . '/I' . $data[$ind]['verb_id']), $obj); $m->add($newS); } #And for every element that is part of the core, output a statement that mentions where in the ontology they belong $ontoS = new Statement($mResource, new Resource(rdf . 'type'), new Resource(s3db . $GLOBALS['N3Names'][$GLOBALS['s3codes'][$letter]])); $m->add($ontoS); } $m->parsedNamespaces = array(rdf => 'rdf', rdfs => 'rdfs', s3db => 's3db', 'http://purl.org/dc/terms/' => 'dc', 'http://xmlns.com/foaf/0.1/' => 'foaf', $D . '/' => 'D'); #echo '<pre>';print_r($m); $filename = $GLOBALS['uploads'] . 'tmps3db/' . random_string('10') . ".rdf"; $m->saveAs($filename, $format); return file_get_contents($filename); }