Problem: the complexity of the practical development of technology semantic network beginners.
I propose by joint efforts to work out a specific solution that allows you to run a small storage and learn how to make requests.
Objective: understand the creation, storage and access to the dictionary, the use of the query language at the primary level in practice.
')
If you participate in the discussion and suggest the best solutions, I will update this post as new information is found together. I hope in the end you get a little lesson for beginners.
Initial conditions: common, accessible, understandable and documented bundle of PHP + MySQL. We do not offer another, although we know that MySQL is not the best choice for an RDF repository, since the ideology of any relational database itself is poorly suited for working with loosely structured data.
We will not try to write everything from scratch, we are beginners and want, having understood the general principles, to try to do something so quickly. Also, we are not trying to clarify here the basic terms that become clear after reading the first few articles that have appeared.
The only (?) Intelligible ready-made solution for PHP + MySQL is
ARC . The whole installation consists in copying the folder and connecting the script with the indication of the database access data:
include_once("semantic/arc/ARC2.php");
$config = array(
'db_name' => 'db',
'db_user' => 'user',
'db_pwd' => 'password',
'store_name' => 'arc',
);
$store = ARC2::getStore($config);
The following code will create the necessary tables for work, if there are none:
if (!$store->isSetUp()) {
$store->setUp();
}
Theoretically, the engine is. It supports the storage of triples, recognition of triples in the most common formats, queries for SPARQL and even makes an access point.
RDF is a generic name for the entire model, technology for presenting data in the form of triples, or facts, or more formally triplets. There are several kinds of direct recordings of triples: the original RDF / XML, various brief notations like N3 or just an image in the form of a graph. Since, in addition to the triples themselves, it is necessary to make agreements on the meaning and interrelationships of predicates (types of links or graph edges from object to subject in the troika), they are described using RDF Schema (RDFS).
A language or rather a set of OWL rules offers a record of the entire ontology using classes, their relations, and specific objects (individuals). Again, OWL can be expressed in different ways: the same RDF / XML, OWL / XML, functional or "Manchester" syntax, etc. It can be said that OWL has a record of RDF triples with their description of RDFS at a higher thoughtful level, but also more difficult to master and understand.
After reading a bunch of articles, we find out that the recommended language for the description of ontologies is OWL. So far, version 2 has not yet been adopted (?), Therefore we use 1.
Translation of the OWL 1 description ,
translation of the working version of OWL2 for general reference.
So, now it is necessary to create a dictionary, that is, a description of the ontology structure. We will store the triples themselves separately in the database, in the repository. Of course, when developing dictionaries you need to make the most of existing ones. But we want to try to do something uncomplicated.
Suppose we have a working ordinary database of some conditional terms with descriptions. Let users create terms, define “contains” relationships, and add descriptions to them. Accordingly, we need to record triples:
1) “Term” - “hasDescription” - Description text;
2) "Term" - "contains" - "Term".
That is, we try to keep both kinds of triples: attributive and links. If we want to additionally indicate the sources of recorded information, for example, the authors, since neither OWL nor ARC have (?) Direct support for quadruples (quad), then we will have to build something extra ourselves. The simplest solution is to add a field like “Author” to the “xxx_triple” table. In this table, ARC stores triples and add values ​​to it when creating a triple.
OWL is operated with objects and their properties, so we need to create a test vocabulary containing:
1) the class "Term";
2) the term “contains” property;
3) the term “hasDescription” property;
An ontology can be described in different notations. Original for OWL is OWL / XML, but it can also be translated into the “old”, but everywhere used RDF / XML format. Let's try, for testing using an ontology editing program, for example,
Protege , to create a dictionary in RDF / XML. What is nice, you can add the OwlViz plugin to Protege, which allows you to see the graph in the diagram.
<?xml version="1.0"?>
<rdf:RDF
// ()
xmlns:test ="http://www.yourdomen.ru/ontology#"
// , ( ?)
xmlns:owl ="http://www.w3.org/2002/07/owl#"
xmlns:rdf ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd ="http://www.w3.org/2001/XMLSchema#">
//
<owl:Ontology rdf:about="">
</owl:Ontology>
//
<owl:Class rdf:ID="#Termin"/>
//
<owl:ObjectProperty rdf:ID="includes">
//
<rdfs:domain rdf:resource="#Termin"/>
<rdfs:range rdf:resource="#Termin"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:ID="#hasDescription">
// : , :
<rdfs:domain rdf:resource="#Termin"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:ObjectProperty>
</rdf:RDF>
Links on SPARQL in ARC:
semanticweb.narod.ru/3.htmljena.hpl.hp.com/~afs/SPARQL-Update.htmlwww.w3.org/TR/rdf-sparql-queryarc.semsol.org/docs/v2/sparql+The name or address of the subject, object, and predicate is given by IRI (like the URL, but does not have an obligation to exist). IRI is recorded in two ways:
1) using prefixes. The prefix is ​​set at the beginning of the request and is indicated for the desired IRI. The name of the prefix itself does not matter - they can be different in different requests.
PREFIX vCard: <www.w3.org/2001/vcard-rdf/3.0#>
vCard:FN
2) relative addresses in angle brackets:
<somewhere/JohnSmith>
At the beginning of the request, you can specify the basic prefix, which will be added at the beginning of those IRIs that end with “/”.
BASE <somewhere>
<JohnSmith/>
A text predicate (literal) is simply given in quotation marks, single or double - it does not matter. You can also specify the language and data type for it. The default type is text.
'text'
Variables are specified using the $ or? It probably makes sense to use? So that $ aaa is not recognized by php as a variable inside a query in double quotes.
SELECT ?title WHERE { <A> <B> ?title .}
You can put the top three in the top three, using "intermediate nodes" using square brackets or prefixes "_:". Example: find out Misha's grandfather:
som: haveDad [som: haveDad $ x]
Comments to the code are given by the grid. Triplets in the query must end in dots and are grouped by curly braces. If you really want it, you can record the same subject only for the first three, and then separate the next subject with semicolons:
{ <example/book3> dc:title "A new book" ;
dc:creator "ANOther" .}
SPARQL itself contains only the SELECT command. ARC uses language additions that allow you to use the words AVG / COUNT / MAX / MIN / SUM (SPARQL +), INSERT, DELETE, etc. (SPARQL / Update) in the query.
INSERT command
Original format:
INSERT { template } [ WHERE { pattern } ]
For example:
PREFIX dc: <purl.org/dc/elements/1.1>
INSERT { <example/egbook3> dc:title "This is an example title" . }
Inside the repository there can be different storage areas for triples for different projects, called graphs. Therefore, if there are several, then in the request you can specify the desired graph using the INTO and graph URI:
INSERT INTO <example.com> { <#foo> "baz" . }
DATA, — , , . INSERT ?
, ARC :
$rs = $store->query("INSERT INTO
{ <testSubject> <testPredicate> <testObject>.}");
print_r($rs); :
Array ( [query_type] => insert [result] => Array ( [t_count] => 1 [load_time] => 0.158 ) [query_time] => 0.283192873001 )
, id , .
: () , , db/testSubject .
INSERT , CONSTRUCT:
INSERT INTO <example.com/inferred>
CONSTRUCT { ?s foaf:knows ?o .} WHERE { ?s xfn:contact ?o .}
SELECT
$query = "SELECT ?poo WHERE { ?poo <testPredicate> <testObject> . }";
if ($row = $store->query($query, 'row')) echo $row['poo'];
'row' — . arc.semsol.org/docs/v2/store . (poo). , SELECT, . , . .
------------------------------------------------------
, .
, . , - . .
PS , - : & gt ; , <code> - ...