-
Notifications
You must be signed in to change notification settings - Fork 27
Howto interact with datastore
This page should contain advanced Erfurt information.
Be aware that most of this information is based on a MySQL backend!
If you are going to change anything in the data store you need to authenticate against it.
A quick and dirty way to directly import Erfurt config.ini and use it:
$backend = Erfurt_App::getInstance ()->getConfig()->store->backend;
$username = Erfurt_App::getInstance ()->getConfig()->store->{$backend}->username;
$password = Erfurt_App::getInstance ()->getConfig()->store->{$backend}->password;
Erfurt_App::getInstance ()->authenticate($username, $password);
After that Erfurt knows you.
If you need to set up a backend based on Erfurt you need many tables. Here you get a short install script which creates all 14 tables for you.
<?php
if ( false == file_exists ( dirname ( __FILE__ ) . '/Erfurt/config.ini' ) ) {
echo 'Abort: config.ini in Erfurt is missing! Please rename of the .ini-dist files to config.ini.';
exit ();
}
// installation area ---------------------------------------------------
require 'Erfurt/App.php';
require 'Erfurt/Cache/Backend/QueryCache/Database.php';
require 'Erfurt/Sparql/SimpleQuery.php';
require 'Erfurt/Store/Exception.php';
require 'Erfurt/Syntax/RdfParser.php';
// Get an Erfurt instance
$app = Erfurt_App::getInstance();
$store = $app->getStore();
// Authenticate to database backend
$backend = $app->getConfig()->store->backend;
$username = $app->getConfig()->store->{$backend}->username;
$password = $app->getConfig()->store->{$backend}->password;
$app->authenticate($username, $password);
// Create standard tables (try an addStatement call to provoke Erfurt create the tables)
$t = time ();
if ( false == $store->isModelAvailable('http://ns.ontowiki.net/SysOnt/', true) ) {
$store->getNewModel ( 'http://ns.ontowiki.net/SysOnt/' );
}
$store->addStatement (
'http://ns.ontowiki.net/SysOnt/', $t, $t,
array('value' => $t, 'type' => 'literal')
);
$store->deleteMatchingStatements (
'http://ns.ontowiki.net/SysOnt/', $t, $t,
array('value' => $t, 'type' => 'literal')
);
// Creates cache tables
$c = new Erfurt_Cache_Backend_QueryCache_Database ();
$c->createCacheStructure ();
// Creates versioning tables
$v = new Erfurt_Versioning ();
$v->isVersioningEnabled ();
A knowledge base is a container which groups your triples. Creating a knowledge base is easy:
Erfurt_App::getInstance ()->getStore ()->getNewModel ( $graphUri );
graphUri contains the URI for your new knowledge base. After execute this command your tables should be changed.
Table ef_graph:
| id | uri | uri_r | base | base_r | | ... | ... | ... | ... | ... | | newGraphId | graphUri | NULL | graphUri | NULL |
Table ef_stmt:
| id | g | s | p | o | s_r | p_r | o_r | st | ot | ol | od | od_r | | | newGraphId | graphUri | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://www.w3.org/2002/07/owl#Ontology | NULL | NULL | NULL | 0 | 0 | | | NULL |
With addStatement you are able to create a new triple. First function code:
addStatement ($graphUri,
$subject,
$predicate,
$object);
This function has four parameters:
- GraphURI: Its a URI. Same as a knowledge base in OntoWiki. In other words it is a container which groups your triples.
- Subject: URI of your subject.
- Predicate: same as 2. for predicate.
- Object: If you would like to save an URI, you need to use an array > The value-field contains your URI string and the type-field contains 'uri'.
<?php
// ...
Erfurt_App::getInstance()
->getStore()
->addStatement ('http://your.knowledge.base/',
'http://www.foobar.de/subject/',
'http://www.foobar.de/predicate/',
array ( 'value' => 'http://www.foobar.de/object/', 'type' => 'uri' ));
// ...
?>
This short code snippet gets an instance of Erfurt, returns a store instance and executes addStatement function with four parameters.
After execute this code you should have (in MySQL) in your ef_stmt table a new row:
| id | g | s | p | o | s_r | p_r | o_r | st | ot | ol | od | od_r | | | G | http://www.foobar.de/subject/ | http://www.foobar.de/predicate | http://www.foobar.de/object| NULL | NULL | NULL | 0 | 0 | | | NULL |
Be aware that **http://your.knowledge.base/ ** has the same ID in ef_graph table as G! Column g is a foreign key that identifies which triple belongs to which model / knowledge base.