Example #1
0
 function testLoadFromStringN3()
 {
     $mem =& ModelFactory::getDefaultModel();
     $n3String = '<http://example.com/res1> <http://example.com/label> "ttt" .';
     $mem->loadFromString($n3String, 'n3');
     $this->assertTrue($mem->contains(new Statement(new Resource('http://example.com/res1'), new Resource('http://example.com/label'), new Literal('ttt'))));
 }
Example #2
0
 function testGetOntModelIsBasedOnThisModel()
 {
     $mem =& ModelFactory::getDefaultModel();
     $ont = $mem->getOntModel(RDFS_VOCABULARY);
     $this->assertIdentical(0, $ont->size());
     $mem->add(new Statement(new Resource('http://example.org/#Fred'), new Resource('http://example.org/#name'), new Literal('Fred')));
     $this->assertIdentical(1, $ont->size());
 }
Example #3
0
function rdf2php($doc)
{
    // Prepare RDF
    #$rdfInput = $data;
    // Show the submitted RDF
    // Create a new MemModel
    $model = ModelFactory::getDefaultModel();
    $model->load($doc);
    return $model;
}
Example #4
0
 public function accept($extractionResult)
 {
     $model1 = ModelFactory::getDefaultModel("http://dbpedia.org/");
     // RAP model
     $count = 0;
     foreach (new ArrayObject($extractionResult->getTriples()) as $triple) {
         $count++;
         $tripleString = explode(">", $triple->toString());
         $s = str_replace("<", "", $tripleString[0]);
         $p = str_replace("<", "", $tripleString[1]);
         // $s = preg_replace("/<|>/","",$triple->getSubject());
         // $p = preg_replace("/<|>/","",$triple->getPredicate());
         $o = $tripleString[2];
         $subject = new Resource($s);
         $predicate = new Resource($p);
         if (strpos($o, "<")) {
             // echo "<br>" . $s. $p . $p;
             if (!strpos($o, "^^")) {
                 // echo " a";
                 $o = str_replace("<", "", $o);
                 $object = new Resource($o);
             } else {
                 // echo " b";
                 $pos = strpos($o, "^^");
                 $literal = substr($o, 0, $pos);
                 $object = new Literal($literal);
                 $object->setDatatype(substr($o, $pos + 3, strlen($o) - $pos - 3));
             }
         } else {
             // $lang = "en";
             if (preg_match("/(.*)(@)([a-zA-Z]+) \\.\$/", $o, $match)) {
                 $o = $match[1];
                 $lang = $match[3];
                 $object = new Literal($o, $lang);
             } else {
                 $object = new Literal($o);
             }
         }
         $statement = new Statement($subject, $predicate, $object);
         $model1->add($statement);
     }
     if ($count > 0) {
         echo "<br><br><h3>" . $extractionResult->getExtractorID() . "</h3>";
         echo $model1->writeAsHtmlTable();
     }
 }
Example #5
0
function buildDirModel($dir, $model, $rootname)
{
    ##Remove from $dir to output the part until s3db root;
    $dirFiles = scandir($dir);
    foreach ($dirFiles as $ind) {
        if (is_file($dir . '/' . $ind) && !ereg('^(s3id|config.inc.php|treeitem.*.js)', $ind)) {
            $fstat = lstat($dir . '/' . $ind);
            $lastModified = date('Y-m-d H:i:s', $fstat['mtime']);
            $path = str_replace($rootname, '', $dir);
            $path = $path == '' ? $ind : substr($path, 1, strlen($path)) . '/' . $ind;
            $subjResources = new Resource('http://www.s3db.org/central/s3dbfiles.php?file=' . $path);
            $statement = new Statement($subjResources, new Resource('http://purl.org/dc/elements/1.1/date'), new Literal($lastModified));
            $path = new Statement($subjResources, new Resource('http://s3db.org/scripts'), new Literal($path));
            $model->add($statement);
            $model->add($path);
        } elseif (is_dir($dir . '/' . $ind) && !ereg('^(.|..|extras)$', $ind)) {
            $newDir = $dir . '/' . $ind;
            $submodel = ModelFactory::getDefaultModel();
            $submodel = buildDirModel($newDir, $submodel, $rootname);
            $model->addModel($submodel);
        }
    }
    return $model;
}
Example #6
0
 function parse($uri)
 {
     if ($this->caching) {
         $cache_id = urlencode($uri);
         $cache =& new Cache_Lite(array('cacheDir' => $this->cache_dir, 'lifeTime' => $this->cache_lifetime, 'automaticCleaningFactor' => 128, 'automaticSerialization' => true));
         if ($data = $cache->get($cache_id)) {
             $this->foaf = $data;
             return true;
         } else {
             $model = ModelFactory::getDefaultModel();
             if (!file($uri)) {
                 return false;
             }
             $res = $model->load($uri);
             if ($res === false) {
                 $cache->save($model, $cache_id);
                 return false;
             } else {
                 $this->foaf = $model;
                 $cache->save($model, $cache_id);
                 return true;
             }
         }
     }
     $model = ModelFactory::getDefaultModel();
     if (!file($uri)) {
         return false;
     }
     $res = $model->load($uri);
     if ($res === false) {
         return false;
     } else {
         $this->foaf = $model;
         return true;
     }
 }
 function testGetOntModelForBaseModel()
 {
     $baseModel = ModelFactory::getDefaultModel();
     $resModel = ModelFactory::getOntModelForBaseModel($baseModel, RDFS_VOCABULARY);
     $this->assertIsEmptyResModel($resModel, 'OntModel');
     $this->addAStatement($baseModel);
     $this->assertIdentical(1, $resModel->size());
 }
    return $stmts;
}
// $model is mutable.
function addStmtsToModel($model, $stmts)
{
    foreach ($stmts as $stmt) {
        $model->add($stmt);
    }
}
define("FOAF", "http://xmlns.com/foaf/0.1/");
define("CCO", "http://purl.org/ontology/cco/mappings#");
define("SERENA", "http://www.serena.ac.uk/property/");
define("RDFS", "http://www.w3.org/2000/01/rdf-schema#");
define("RDF", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
define("OWL", "http://www.w3.org/2002/07/owl#");
$model = ModelFactory::getDefaultModel();
$name = $_POST["name"];
$fileName = generateFileName($name);
$userURI = toSerenAURI($fileName);
$stmtsAll = array();
/* First specify foaf:Person type */
$subjRes = new resource($userURI);
$predicateRes = new resource(RDF . "type");
$objRes = new resource(FOAF . "Person");
$stmt = new Statement($subjRes, $predicateRes, $objRes);
$stmtsAll = array($stmt);
/* Now get the singleton fields from the form */
$stmts = mapSingleFormField($userURI, "name", FOAF . "name", "", false);
$stmtsAll = array_merge($stmtsAll, $stmts);
$stmts = mapSingleFormField($userURI, "name", RDFS . "label", "", false);
$stmtsAll = array_merge($stmtsAll, $stmts);
<?php

// Only show serious errors.
error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_time_limit(99999);
//ini_set('memory_limit','5000M');
echo "Test 1\n";
//error_reporting(E_ALL);
require_once 'rdfapi-php/test/config.php';
include RDFAPI_INCLUDE_DIR . "RDFAPI.php";
// Create a new MemModel and load the document
$friends = ModelFactory::getDefaultModel();
//$friends->load('C:/work/services/Teaching/xampp/htdocs/RAP/dbpedia_persondata_en.nt');
$friends->load('Y:/data/3rd/rdf-teaching/xaa.nt');
echo "Loaded.\n";
//$friends->load('C:/work/services/Teaching/xampp/htdocs/foaf/foaf.rdf');
$querystring = '
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?x ?name
WHERE { ?x foaf:name ?name }
';
$result = $friends->sparqlQuery($querystring);
echo "Executed Query.\n";
error_reporting(E_ALL);
//var_dump($result);
//$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
//$mysqli = new mysqli("localhost", "root", "", "foaf");
//$insert = $mysqli->prepare("INSERT INTO friends VALUES (?, ?)");
$count = 0;
// for each row in the results...
foreach ($result as $row) {
Example #10
0
function rdfRead($F)
{
    extract($F);
    $fileclean = $file . '_clean';
    #Remove the files
    #$root = ($_REQUEST['root']!='')?$_REQUEST['root']:$argv[4];
    $root = $inputs['root'];
    $inData = $inputs;
    #Before parsing the RDF, remove the files from the document
    #$data = removeFiles($file); ##COMPLETED IN A SEPARATE SCRIPT (momentarily because of segmantation fault)
    $data = fread(fopen($fileclean, 'r+'), filesize($fileclean));
    define("RDFAPI_INCLUDE_DIR", S3DB_SERVER_ROOT . "/pearlib/rdfapi-php/api/");
    include RDFAPI_INCLUDE_DIR . "RdfAPI.php";
    include RDFAPI_INCLUDE_DIR . "syntax/SyntaxN3.php";
    include RDFAPI_INCLUDE_DIR . "syntax/SyntaxRDF.php";
    include S3DB_SERVER_ROOT . '/s3dbcore/rdf.resources.inc.php';
    include S3DB_SERVER_ROOT . '/rdfheader.inc.php';
    extract($resources);
    #Create an RDF model from the raw data
    #echo "Parsing the RDF data".chr(10);
    if (!is_file($file . '_model') || $inputs['clean'] == 1) {
        #$model = ntriples2php($data); ##=> Crashing when the model is too big :-( :-(
        if (filesize($fileclean) < 20000) {
            $model = ntriples2php($data);
        } else {
            #For big files, use the big file library ( which actually is smaller..)
            $model = arc_ntriples2php($fileclean);
            $m = ModelFactory::getDefaultModel();
            if (empty($model)) {
                echo "Your file could not be parsed.";
                exit;
            }
            foreach ($model as $triple) {
                $mS = new Resource($triple['s']);
                $mP = new Resource($triple['p']);
                $mO = $triple['o_type'] == 'literal' ? new Literal($triple['o']) : new Resource($triple['o']);
                $s = new Statement($mS, $mP, $mO);
                $m->add($s);
            }
            $model = $m;
        }
        if (!empty($model)) {
            unlink($fileclean);
        } else {
            echo "Your file seems to be empty.";
            exit;
        }
        $tmp = serialize($model);
        file_put_contents($file . '_model', $tmp);
    } else {
        $model = unserialize(file_get_contents($file . '_model'));
    }
    #Create an S3DB model from the RDF model
    #echo "Building and S3DB structure from the RDF data".chr(10).chr(13);
    if (!is_file($file . '_s3db') || $inputs['clean'] == 1) {
        $s3db = model2deploy($model, $resources, $root, $inData);
        #$s3db = arc_model2deploy($model, $resources, $root, $inData);
        $tmp = serialize($s3db);
        file_put_contents($file . '_s3db', $tmp);
    } else {
        $s3db = unserialize(file_get_contents($file . '_s3db'));
    }
    #include('rdfWrite.php');
    return $s3db;
}
Example #11
0
 function MwRdfCategories($article)
 {
     global $DCMES;
     $model = ModelFactory::getDefaultModel();
     $nt = $article->mTitle;
     $ar = MwRdfTitleResource($nt);
     $categories = $nt->getParentCategories();
     if (is_array($categories)) {
         foreach (array_keys($categories) as $category) {
             $cattitle = Title::newFromText($category);
             $model->add(new Statement($ar, $DCMES['subject'], MwRdfTitleResource($cattitle)));
         }
     }
     return $model;
 }
Example #12
0
function buildDirModel($dir, $model, $rootname, $user_id, $db)
{
    ##Remove from $dir to output the part until s3db root;
    $dirFiles = scandir($dir);
    #echo '<pre>';print_r($dirFiles);exit;
    #foreach ($dirFiles as $ind)
    for ($i = 0; $i < count($dirFiles); $i++) {
        $ind = $dirFiles[$i];
        if (is_file($dir . '/' . $ind) && !ereg('^(s3id|config.inc.php|treeitem.*.js|.*.tmp|.*[0-9]{8}$)', $ind)) {
            $fstat = lstat($dir . '/' . $ind);
            $lastModified = date('Y-m-d H:i:s', $fstat['mtime']);
            $path = str_replace($rootname, '', $dir);
            $path = $path == '' ? $ind : substr($path, 1, strlen($path)) . '/' . $ind;
            $path = addslashes($path);
            ###
            #Is there an item with this path value on path rule?
            #$item_id = findFileItemId($path,$user_id,$db);
            ###
            #Find the statement_id of this file on the local s3db
            $allFileIds = @file_get_contents('fileIds.tmp');
            $allFileIds = @unserialize($allFileIds);
            $file_id = @array_search($path, $allFileIds);
            if ($file_id == '') {
                echo "Finding ID of file " . $path . chr(10);
                $sql = "select statement_id from s3db_statement where rule_id = '" . $GLOBALS['update_project']['file']['rule_id'] . "' and file_name = '" . $path . "' order by created_on desc limit 1";
                $db->query($sql, __LINE__, __FILE__);
                if ($db->next_record()) {
                    $file_id = $db->f('statement_id');
                    $allFileIds[$file_id] = $path;
                }
            }
            if ($file_id == '') {
                $updated = fileUpdate($path, $user_id, $db);
                $file_id = $updated;
            }
            if ($file_id != '') {
                file_put_contents('fileIds.tmp', serialize($allFileIds));
                echo "writting item " . $path . " " . $file_id . chr(10);
                $subjResources = new Resource($GLOBALS['s3db_info']['deployment']['URI'] . 's3dbfiles.php?file_id=' . $file_id);
                $statement = new Statement($subjResources, new Resource('http://purl.org/dc/elements/1.1/date'), new Literal($lastModified));
                $path = new Statement($subjResources, new Resource('http://s3db.org/scripts'), new Literal($path));
                $model->add($statement);
                $model->add($path);
            } else {
                @file_put_contents('update_error_log.txt', "Could not find a file_id for " . $path . chr(10));
            }
        } elseif (is_dir($dir . '/' . $ind) && !ereg('^(.|..|extras)$', $ind)) {
            $newDir = $dir . '/' . $ind;
            $submodel = ModelFactory::getDefaultModel();
            $submodel = buildDirModel($newDir, $submodel, $rootname, $user_id, $db);
            $model->addModel($submodel);
        }
    }
    return $model;
}
Example #13
0
 /**
  * 
  *
  * @param string	$uri	Fragment URI
  * @return array	[]['label']
  * 			  ['comment]
  */
 private function _getVocabLabelAndComment($id, $lang)
 {
     $a = array('label' => '', 'comment' => '');
     $m = ModelFactory::getDefaultModel();
     $m->load($this->annotationsDirPath . $id);
     $it = $m->findAsIterator(NULL, RDF::TYPE(), LOOMP::ANNOTATION_SET());
     if ($it->hasNext()) {
         $s = $it->next()->getSubject();
         $a['label'] = $this->_getLiteralInLang($m->find($s, RDFS::LABEL(), NULL), $lang);
         $a['comment'] = $this->_getLiteralInLang($m->find($s, RDFS::COMMENT(), NULL), $lang);
     }
     return $a;
 }
define("RDFAPI_INCLUDE_DIR", dirname(__FILE__) . "/../../../www/admin/include/rdfapi-php/api/");
include_once RDFAPI_INCLUDE_DIR . "RdfAPI.php";
//We need to load our context, config and then fetch our existing store of code-list values.
$context = load_context(!($argv[1] == "default") ? $argv[1] : null);
$ns = load_config(!($argv[2] == "default") ? $argv[2] : null);
//We set up our code-list model. In future we might want to be able to fetch from the server as well and merge into our model
$codelists = ModelFactory::getDefaultModel();
$codelistfile = !($argv[3] == "default") ? $argv[3] : null;
if (file_exists("../data/rdf/codelists.rdf")) {
    $codelists->load("../data/rdf/codelists.rdf");
    log_message("Found an existing code-list file to work from", 0);
} else {
    log_message("No code-list file found");
}
//Set set up our target model for questions
$questions = ModelFactory::getDefaultModel();
$questions->addWithoutDuplicates(new Statement(new Resource("http://www.w3.org/Home/Lassila"), new Resource("http://description.org/schema/Description"), new Literal("Lassila's personal Homepage", "en")));
//Add namespaces
add_namespaces(&$questions, $ns);
add_namespaces(&$codelists, $ns);
print_r($codelists);
//Get our list of files and now loop through them to process
$files = directory_list();
foreach ($files as $file => $name) {
    $n++;
    if ($n > 2) {
        break 1;
    }
    // Limit routine - make sure we only run through a few times
    if (!file_exists("../data/rdf/{$name}.rdf")) {
        log_message("Processing {$name}");
Example #15
0
<?php

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
define("RDFAPI_INCLUDE_DIR", "C:/work/services/Teaching/xampp/htdocs/RAP/rdfapi-php/api/");
include RDFAPI_INCLUDE_DIR . "RDFAPI.php";
$friends = ModelFactory::getDefaultModel();
$friends->load('C:/work/services/Teaching/xampp/htdocs/foaf/foaf.rdf');
$friends_query = '
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE { ?x foaf:name ?name }';
echo $friends->sparqlQuery($friends_query, 'HTML');
die("done");
// Create a new MemModel and load the document
$employees = ModelFactory::getDefaultModel();
$employees->load('C:/work/services/Teaching/xampp/htdocs/RAP/rdfapi-php/doc/tutorial/employees.rdf');
//continue below
$querystring = '
PREFIX vcard <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?fullName
WHERE { ?x vcard:FN ?fullName }';
$result = $employees->sparqlQuery($querystring);
foreach ($result as $line) {
    $value = $line['?fullName'];
    if ($value != "") {
        echo $value->toString() . "<br/>";
    } else {
        echo "undbound<br/>";
    }
}