Building a simple Triplestore in PHP

Note: this is an early version and still in development phase; it will be likely updated in the near future

Lately I have been reading the excellent book Programming the Semantic Web. Since all of the examples in the book are written in Python it was hard for me to understand most of the examples so I decided to translate the code into PHP. It is shown how you can build a simple triplestore using PHP.

The SimpleGraph class stores three indexes that each represent a different permutation. The name of the index indicates the ordering of the terms in the index (i.e., the pos index stores the predicate, the object, and then the subject, in that order).

Each triple is represented in each index using a different permutation, and this allows any query across the triples to be answered simply by iterating over a single index.

Attached you can find the source code. I included an example as well, which can be used for test purposes. The file named unis.csv represents a comma-separated list for data about the belgian universities. Note that this list is certainly not exhaustive. Beneath a graphical overview for the University of Antwerp.

The following code shows how to list all triples that contain data about Universiteit Antwerpen. All terms that are not present should be addressed using the NULL value. The triples() function will return an array containing the triples.


$graph = new SimpleGraph();
$graph->load('unis.csv');
$graph->triples('Universiteit Antwerpen', NULL, NULL);

That’s all you need for a basic in-memory triplestore.

Currently, the performance is rather slow. It is able to store about 5000 triples. A future update will address performance.”,”Note: this is an early version and still in development phase; it will be likely updated in the near future

Leave a Reply

Your email address will not be published. Required fields are marked *