Project

General

Profile

RLog » History » Version 5

Paula Gearon, 05/25/2012 06:51 PM
Clarified the use of the term predicate and fixed the father/man example

1 3 Paula Gearon
h1. RLog
2
3 1 Paula Gearon
RLog is a language based on logic programming, which is specifically aimed at describing RDF.
4
5 3 Paula Gearon
Statements in RDF are equivalent to unary and binary predicates in logic. Most relationships can be described with these simple predicate, although occasionally predicates with higher arities may be called for. These higher arities are simulated in RDF using a "number of mechanisms":http://www.w3.org/TR/swbp-n-aryRelations/, including blank nodes with named operators, and lists. So the restriction to unary and binary arities is appropriate when dealing with RDF.
6 1 Paula Gearon
7
RLog programs are built using statements that resemble Horn Clauses. Statements are built using a series of predicates, and predicates are built using the basic elements of atoms and variables. A complete program is simply a list of statements and directives.
8
9
10 3 Paula Gearon
h3. A Note on URIs
11 1 Paula Gearon
12 3 Paula Gearon
RDF uses URIs as the basic syntactic element, which can be unwieldy in examples. "QNames":http://en.wikipedia.org/wiki/QName are a much shorter form for a URI which abbreviates an entire domain down to a short string. For instance, the _type_ predicate in RDF has a full URI of:
13
14 1 Paula Gearon
<pre>
15 4 Alex Hall -
  http://www.w3.org/1999/02/22-rdf-syntax-ns#
16
</pre>
17 3 Paula Gearon
18
In QName form, this is abbreviated to:
19
20 1 Paula Gearon
<pre>
21 4 Alex Hall -
  rdf:type
22
</pre>
23 3 Paula Gearon
24
The following text will stick to QNames, and will often use _unprefixed_ QNames to illustrate examples (e.g. *foo*).
25
26 1 Paula Gearon
RLog uses QNames exclusively for URIs.
27
28
----
29
30
31 3 Paula Gearon
h1. Basic Elements
32 1 Paula Gearon
33 3 Paula Gearon
34
35
h3. Atoms
36
37 1 Paula Gearon
38
RLog uses QNames, numbers, and quoted strings for atoms. QNames can be used anywhere. Strings are surrounded by double quotes. Since RLog represents RDF, then numbers and strings can usually only appear in the second element of a binary predicate.
39
40
All URIs are represented as QNames. A number of prefixes are pre-defined:
41 4 Alex Hall -
| rdf     | @http://www.w3.org/1999/02/22-rdf-syntax-ns#@ |
42
| rdfs    | @http://www.w3.org/2000/01/rdf-schema#@       |
43
| owl     | @http://www.w3.org/2002/07/owl#@              |
44
| xsd     | @http://www.w3.org/2001/XMLSchema#@           |
45
| mulgara | @http://mulgara.org/mulgara#@                 |
46
| krule   | @http://mulgara.org/owl/krule/#@              |
47
| foaf    | @http://xmlns.com/foaf/0.1/@                  |
48
| skos    | @http://www.w3.org/2004/02/skos/core#@        |
49
| dc      | @http://purl.org/dc/elements/1.1/@            |
50 1 Paula Gearon
51 3 Paula Gearon
Prefixes can be added using the *@prefix* instruction.
52 1 Paula Gearon
53
54 3 Paula Gearon
h3. Variables
55 1 Paula Gearon
56 3 Paula Gearon
57
Variables are represented with a single upper-case letter. This restricts each statement to only allowing a maximum of 26 variables. _This was considered adequate at design time, but if there is a legitimate need for more, then please contact the developers._
58
59 1 Paula Gearon
----
60
61
62 3 Paula Gearon
h1. Predicates
63
64
65
66
h3. Unary Predicates
67
68
A unary predicate indicates the type of an object. So the following statement indicates that the object named _foo_ is an instance of the _Bar_ type:
69
<pre>
70 1 Paula Gearon
  Bar(foo)
71 3 Paula Gearon
</pre>
72 1 Paula Gearon
73 3 Paula Gearon
This is expressed in RDF using the special predicate _rdf:type_:
74
<pre>
75 1 Paula Gearon
  foo  rdf:type  Bar .
76 3 Paula Gearon
</pre>
77 1 Paula Gearon
78
RLog enforces the RDF convention that types begin with an upper case letter.
79
80 3 Paula Gearon
81
h3. Binary Predicates
82
83 1 Paula Gearon
Binary predicates indicate a relationship between two resources:
84 3 Paula Gearon
<pre>
85 1 Paula Gearon
  friend(fred,barney)
86 3 Paula Gearon
</pre>
87 1 Paula Gearon
88
This is expressed in RDF using an "infix" notation, meaning that the predicate is in the middle:
89 3 Paula Gearon
<pre>
90 1 Paula Gearon
  fred  friend  barney .
91 3 Paula Gearon
</pre>
92 1 Paula Gearon
93
RLog enforces the RDF convention that relationships begin with a lower case letter.
94
95
----
96
97
98 3 Paula Gearon
h1. Statements
99
100
101 1 Paula Gearon
Mulgara uses 3 types of statements: Axioms, Consistency-Checks (which are a kind of Axiom), and Rules.
102
103
104 3 Paula Gearon
h3. Axioms
105
106 5 Paula Gearon
Axioms are a statement of fact. They consist of a single predicate followed by a dot. Note the term "predicate" here is being used in its logic sense, and should not be confused with the middle element in an RDF triple (a related, but slightly different thing).
107 3 Paula Gearon
108 1 Paula Gearon
The general form of an axiom is:
109
110 3 Paula Gearon
<pre>
111 4 Alex Hall -
  predicate.
112
</pre>
113 3 Paula Gearon
114
_Examples_
115
The following states that _:fred_ is a _foaf:Person_:
116
<pre>
117 4 Alex Hall -
  foaf:Person(:fred).
118 3 Paula Gearon
</pre>
119 1 Paula Gearon
120 3 Paula Gearon
This statement says that _:fred_ has the friend _:barney_:
121
<pre>
122 1 Paula Gearon
  :friend(:fred, :barney).
123
</pre>
124
125 3 Paula Gearon
Axioms can be considered as rules with the body set to "nothing" or "bottom" (to use the term from logic), meaning that nothing else is needed in order to generate the predicate in the _head_ of the statement.
126 1 Paula Gearon
127 3 Paula Gearon
128 1 Paula Gearon
h3. Rules
129
130 3 Paula Gearon
Rules are Horn Clauses, consisting of a head followed by a body. The basic form is:
131 1 Paula Gearon
132
<pre>
133 4 Alex Hall -
  head :- body.
134
</pre>
135 1 Paula Gearon
136 3 Paula Gearon
The _head_ is a single predicate (this may change in a later revision).
137 1 Paula Gearon
138 3 Paula Gearon
The _body_ is a list of comma-separated predicates.
139 1 Paula Gearon
140 3 Paula Gearon
So the general form is:
141
142
<pre>
143 4 Alex Hall -
  predicate :- predicate, predicate, ... .
144
</pre>
145 3 Paula Gearon
146
If the body of the rule is matched, then the head will be asserted. _Matching_ is the process of finding existing assertions which share the atoms of all the predicates. Once a match is found, then the variables in the predicate are bound to the corresponding values in the matched assertion. These binding are then used in the assertion described in the _head' of the statement.
147
148
_Examples_
149
The following statement declares that all instances of the class *transport:Car* are therefore instances of the class *transport:Vehicle*:
150
<pre>
151 1 Paula Gearon
  transport:Vehicle(X) :- transport:Car(X).
152 3 Paula Gearon
</pre>
153 1 Paula Gearon
154 3 Paula Gearon
This next statement declares that if any two resources are related by a predicate, and that predicate is an _rdfs:subPropertyOf_ another predicate, then those resources are also related by the second predicate.
155
<pre>
156 1 Paula Gearon
  B(X,Y) :- A(X,Y), rdfs:subPropertyOf(A,B).
157 3 Paula Gearon
</pre>
158 1 Paula Gearon
159 3 Paula Gearon
*Note:* all the variables appearing in the _head_ of a statement MUST appear in the body. This prevents the creation of blank nodes. This feature may be allowed in future, but it has the side-effect of allowing infinite generation of new node. To see this, consider the following program:
160
<pre>
161 1 Paula Gearon
  :father(X,Y) :- :Man(X).
162 5 Paula Gearon
  :Man(Y) :- :father(X,Y).
163 1 Paula Gearon
  :Man(:fred).
164
</pre>
165 5 Paula Gearon
This states that all men have fathers, and that anyone who is a father is a man. Finally, the entity *:fred* is declared to be a man.
166
167 1 Paula Gearon
This will continue looping until system resources are exhausted.
168
169
170
h3. Checks
171 3 Paula Gearon
172
Checks perform tests for consistency on the data. If a check fails, then an error is reported and the current operation is discarded. If a transaction is in progress, then it will be rolled back.
173
174 1 Paula Gearon
Checks are like rules without a _head_, meaning that the _body_ should imply "nothing" or "bottom" (to use the logic term). Anything matching the body of a check is considered an error, so the number of matches is reported in the error.
175
176 3 Paula Gearon
The general form of a check is:
177 1 Paula Gearon
178 3 Paula Gearon
<pre>
179 4 Alex Hall -
  :- predicate, predicate, ... .
180
</pre>
181 3 Paula Gearon
182
_Examples:_
183 1 Paula Gearon
This checks that if two things are declared to be the same, they cannot also be different:
184 3 Paula Gearon
<pre>
185 1 Paula Gearon
  :- owl:sameAs(X,Y), owl:differentFrom(X,Y).
186 3 Paula Gearon
</pre>
187
Note that this check is likely to work in conjunction with other rules, such as the ones declaring the symmetry of the _owl:sameAs_ and _owl:differentFrom_ predicates. Otherwise, a second check would be needed which reversed the order of X and Y in one of the predicates above.
188 1 Paula Gearon
189
This check tests that nothing can be declared to be "Nothing":
190 3 Paula Gearon
<pre>
191 1 Paula Gearon
  :- owl:Nothing(X).
192 3 Paula Gearon
</pre>
193 1 Paula Gearon
This test may seem redundant at first glance, but it is one of the fundamental tests for ontological consistency.
194
195
----
196
197
198
h1. Comments
199
200 3 Paula Gearon
201 1 Paula Gearon
Comments in Mulgara are created using a pair of dashes and extend to the end of that line. The general form is:
202 3 Paula Gearon
203
<pre>
204 4 Alex Hall -
  -- comment
205
</pre>
206 3 Paula Gearon
207
_Examples:_
208
<pre>
209
  -- This is full line comment
210 1 Paula Gearon
</pre>
211
212 3 Paula Gearon
The following adds a comment to a rule:
213
<pre>
214 1 Paula Gearon
  rdf:Property(A) :- A(X,Y).   -- RDFS rule #1
215
</pre>
216
217
----
218
219
220 3 Paula Gearon
h1. Directives
221 1 Paula Gearon
222
223 3 Paula Gearon
224
h3. @prefix
225
226
The *@prefix* directive is used to create a new prefix for use in QNames. It is only applied to QNames appearing after the *@prefix* statement. The syntax is the same as in N3:
227
228
<pre>
229 4 Alex Hall -
  @prefix ns: <IRI> .
230
</pre>
231 3 Paula Gearon
232 1 Paula Gearon
233 3 Paula Gearon
_Examples:_
234 1 Paula Gearon
<pre>
235 3 Paula Gearon
  @prefix owl11: <http://www.w3.org/2006/12/owl11#> .
236
</pre>
237 1 Paula Gearon
238
<pre>
239
  @prefix : <http://www.w3.org/2006/12/owl11#> .
240 3 Paula Gearon
</pre>
241 1 Paula Gearon
242
243 3 Paula Gearon
h3. @import
244 2 Paula Gearon
245
246 3 Paula Gearon
The *@import* directive is used to import the contents of another RLog file. A URI is provided to locate the file. If it is an absolute URL, then this is used to locate and read the file. If the URI is relative, then it is used to identify a path relative to the URL of the original file being loaded. Imports may be nested, but relative URIs are always relative to the original file, and not the most recent importing file.
247
248
The syntax is:
249
250
<pre>
251 4 Alex Hall -
  @import <IRI> .
252
</pre>
253 3 Paula Gearon
254
Examples:
255 2 Paula Gearon
<pre>
256 3 Paula Gearon
  @import <file:///tmp/rules.rl> .
257 2 Paula Gearon
</pre>
258 3 Paula Gearon
259
<pre>
260 2 Paula Gearon
  @import <second.dl> .
261 3 Paula Gearon
</pre>
262 2 Paula Gearon
263
----
264
265 3 Paula Gearon
266
h1. Example Programs
267
268 2 Paula Gearon
The following simple program describes 3 people and their relationships. It also describes a rule to deduce a new relationship:
269 3 Paula Gearon
<pre>
270 2 Paula Gearon
@prefix : <http://xmlns.com/foaf/0.1/> .
271
@prefix family: <http://family.com/data/> .
272
273
:Person(family:fred).
274
:Person(family:peter).
275
:Person(family:tom).
276
family:hasBrother(family:peter,family:fred).
277
family:hasFather(family:tom,family:peter).
278
279
family:hasUncle(A,C) :- family:hasFather(A,B), family:hasBrother(B,C).
280 3 Paula Gearon
</pre>
281 2 Paula Gearon
282
This next program describes an inconsistent system. It will therefore fail:
283 3 Paula Gearon
<pre>
284 2 Paula Gearon
@prefix : <http://xmlns.com/foaf/0.1/> .
285
@prefix family: <http://family.com/data/> .
286
287
:Person(family:peter).
288
:Person(family:tom).
289
family:hasParent(family:tom,family:peter).
290
family:hasChild(family:tom,family:peter).
291 1 Paula Gearon
292
family:hasParent(B,A) :- family:hasChild(A,B).
293
294
:- family:hasParent(A,B), family:hasParent(B,A).
295 3 Paula Gearon
</pre>
296 1 Paula Gearon
297 3 Paula Gearon
Of course, real programs will by much more complete, describing the inverse relationship between _hasParent_ and _hasChild_, and gender sets to describe the relationships between classes like _hasParent_ and _hasFather_.
298 1 Paula Gearon
299
The above programs are designed to be applied to an empty graph, though they will also work on pre-existing data.
300
301 3 Paula Gearon
The following commands will apply a program called */tmp/program.rlog* to an empty graph with the name *test:data*, and then display the results:
302
<pre>
303 1 Paula Gearon
  create <test:data>;
304
  apply <file:///tmp/program.rlog> to <test:data>;
305
  select $s $p $o from <test:data> where $s $p $o;
306 3 Paula Gearon
</pre>
307 1 Paula Gearon
All 3 of these commands are in TQL, though the final command can be replaced with the equivalent SPARQL:
308 3 Paula Gearon
<pre>
309 1 Paula Gearon
  select $s $p $o from <test:data> where { $s $p $o }
310 3 Paula Gearon
</pre>