RLog » History » Version 3
Paula Gearon, 03/24/2009 09:38 PM
Added example programs
1 | 3 | Paula Gearon | |
---|---|---|---|
2 | h1. RLog |
||
3 | |||
4 | 1 | Paula Gearon | RLog is a language based on logic programming, which is specifically aimed at describing RDF. |
5 | |||
6 | 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. |
7 | 1 | Paula Gearon | |
8 | 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. |
||
9 | |||
10 | |||
11 | 3 | Paula Gearon | h3. A Note on URIs |
12 | 1 | Paula Gearon | |
13 | 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: |
14 | |||
15 | <pre> |
||
16 | |||
17 | In QName form, this is abbreviated to: |
||
18 | |||
19 | <pre> |
||
20 | |||
21 | The following text will stick to QNames, and will often use _unprefixed_ QNames to illustrate examples (e.g. *foo*). |
||
22 | |||
23 | 1 | Paula Gearon | RLog uses QNames exclusively for URIs. |
24 | |||
25 | ---- |
||
26 | |||
27 | |||
28 | 3 | Paula Gearon | h1. Basic Elements |
29 | 1 | Paula Gearon | |
30 | 3 | Paula Gearon | |
31 | |||
32 | h3. Atoms |
||
33 | |||
34 | |||
35 | 1 | Paula Gearon | 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. |
36 | |||
37 | All URIs are represented as QNames. A number of prefixes are pre-defined: |
||
38 | || rdf || !http://www.w3.org/1999/02/22-rdf-syntax-ns# || |
||
39 | || rdfs || !http://www.w3.org/2000/01/rdf-schema# || |
||
40 | || owl || !http://www.w3.org/2002/07/owl# || |
||
41 | || xsd || !http://www.w3.org/2001/XMLSchema# || |
||
42 | || mulgara || !http://mulgara.org/mulgara# || |
||
43 | || krule || !http://mulgara.org/owl/krule/# || |
||
44 | || foaf || !http://xmlns.com/foaf/0.1/ || |
||
45 | || skos || !http://www.w3.org/2004/02/skos/core# || |
||
46 | || dc || !http://purl.org/dc/elements/1.1/ || |
||
47 | |||
48 | 3 | Paula Gearon | Prefixes can be added using the *@prefix* instruction. |
49 | 1 | Paula Gearon | |
50 | |||
51 | 3 | Paula Gearon | h3. Variables |
52 | 1 | Paula Gearon | |
53 | 3 | Paula Gearon | |
54 | 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._ |
||
55 | |||
56 | 1 | Paula Gearon | ---- |
57 | |||
58 | |||
59 | 3 | Paula Gearon | h1. Predicates |
60 | |||
61 | |||
62 | |||
63 | h3. Unary Predicates |
||
64 | |||
65 | 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: |
||
66 | <pre> |
||
67 | 1 | Paula Gearon | Bar(foo) |
68 | 3 | Paula Gearon | </pre> |
69 | 1 | Paula Gearon | |
70 | 3 | Paula Gearon | This is expressed in RDF using the special predicate _rdf:type_: |
71 | <pre> |
||
72 | 1 | Paula Gearon | foo rdf:type Bar . |
73 | 3 | Paula Gearon | </pre> |
74 | 1 | Paula Gearon | |
75 | RLog enforces the RDF convention that types begin with an upper case letter. |
||
76 | |||
77 | 3 | Paula Gearon | |
78 | h3. Binary Predicates |
||
79 | |||
80 | 1 | Paula Gearon | Binary predicates indicate a relationship between two resources: |
81 | 3 | Paula Gearon | <pre> |
82 | 1 | Paula Gearon | friend(fred,barney) |
83 | 3 | Paula Gearon | </pre> |
84 | 1 | Paula Gearon | |
85 | This is expressed in RDF using an "infix" notation, meaning that the predicate is in the middle: |
||
86 | 3 | Paula Gearon | <pre> |
87 | 1 | Paula Gearon | fred friend barney . |
88 | 3 | Paula Gearon | </pre> |
89 | 1 | Paula Gearon | |
90 | RLog enforces the RDF convention that relationships begin with a lower case letter. |
||
91 | |||
92 | ---- |
||
93 | |||
94 | |||
95 | 3 | Paula Gearon | h1. Statements |
96 | |||
97 | |||
98 | 1 | Paula Gearon | Mulgara uses 3 types of statements: Axioms, Consistency-Checks (which are a kind of Axiom), and Rules. |
99 | |||
100 | 3 | Paula Gearon | |
101 | h3. Axioms |
||
102 | |||
103 | 1 | Paula Gearon | Axioms are a statement of fact. They consist of a single predicate followed by a dot. |
104 | |||
105 | 3 | Paula Gearon | The general form of an axiom is: |
106 | 1 | Paula Gearon | |
107 | 3 | Paula Gearon | <pre> |
108 | |||
109 | _Examples_ |
||
110 | The following states that _:fred_ is a _foaf:Person_: |
||
111 | <pre> |
||
112 | 1 | Paula Gearon | foaf:Person(fred). |
113 | 3 | Paula Gearon | </pre> |
114 | 1 | Paula Gearon | |
115 | 3 | Paula Gearon | This statement says that _:fred_ has the friend _:barney_: |
116 | <pre> |
||
117 | 1 | Paula Gearon | :friend(:fred, :barney). |
118 | 3 | Paula Gearon | </pre> |
119 | 1 | Paula Gearon | |
120 | 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. |
121 | 1 | Paula Gearon | |
122 | |||
123 | 3 | Paula Gearon | h3. Rules |
124 | 1 | Paula Gearon | |
125 | 3 | Paula Gearon | Rules are Horn Clauses, consisting of a head followed by a body. The basic form is: |
126 | 1 | Paula Gearon | |
127 | 3 | Paula Gearon | <pre> |
128 | 1 | Paula Gearon | |
129 | 3 | Paula Gearon | The _head_ is a single predicate (this may change in a later revision). |
130 | 1 | Paula Gearon | |
131 | 3 | Paula Gearon | The _body_ is a list of comma-separated predicates. |
132 | |||
133 | So the general form is: |
||
134 | |||
135 | <pre> |
||
136 | |||
137 | 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. |
||
138 | |||
139 | _Examples_ |
||
140 | The following statement declares that all instances of the class *transport:Car* are therefore instances of the class *transport:Vehicle*: |
||
141 | <pre> |
||
142 | 1 | Paula Gearon | transport:Vehicle(X) :- transport:Car(X). |
143 | 3 | Paula Gearon | </pre> |
144 | 1 | Paula Gearon | |
145 | 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. |
146 | <pre> |
||
147 | 1 | Paula Gearon | B(X,Y) :- A(X,Y), rdfs:subPropertyOf(A,B). |
148 | 3 | Paula Gearon | </pre> |
149 | 1 | Paula Gearon | |
150 | 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: |
151 | <pre> |
||
152 | 1 | Paula Gearon | :father(X,Y) :- :Man(X). |
153 | :Man(:fred). |
||
154 | 3 | Paula Gearon | </pre> |
155 | 1 | Paula Gearon | This will continue looping until system resources are exhausted. |
156 | |||
157 | 3 | Paula Gearon | |
158 | h3. Checks |
||
159 | |||
160 | 1 | Paula Gearon | 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. |
161 | |||
162 | 3 | 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. |
163 | 1 | Paula Gearon | |
164 | 3 | Paula Gearon | The general form of a check is: |
165 | 1 | Paula Gearon | |
166 | 3 | Paula Gearon | <pre> |
167 | |||
168 | _Examples:_ |
||
169 | 1 | Paula Gearon | This checks that if two things are declared to be the same, they cannot also be different: |
170 | 3 | Paula Gearon | <pre> |
171 | 1 | Paula Gearon | :- owl:sameAs(X,Y), owl:differentFrom(X,Y). |
172 | 3 | Paula Gearon | </pre> |
173 | 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. |
||
174 | 1 | Paula Gearon | |
175 | This check tests that nothing can be declared to be "Nothing": |
||
176 | 3 | Paula Gearon | <pre> |
177 | 1 | Paula Gearon | :- owl:Nothing(X). |
178 | 3 | Paula Gearon | </pre> |
179 | 1 | Paula Gearon | This test may seem redundant at first glance, but it is one of the fundamental tests for ontological consistency. |
180 | |||
181 | ---- |
||
182 | |||
183 | |||
184 | 3 | Paula Gearon | h1. Comments |
185 | 1 | Paula Gearon | |
186 | 3 | Paula Gearon | |
187 | Comments in Mulgara are created using a pair of dashes and extend to the end of that line. The general form is: |
||
188 | |||
189 | <pre> |
||
190 | |||
191 | _Examples:_ |
||
192 | <pre> |
||
193 | 2 | Paula Gearon | -- This is full line comment |
194 | 3 | Paula Gearon | </pre> |
195 | 1 | Paula Gearon | |
196 | The following adds a comment to a rule: |
||
197 | 3 | Paula Gearon | <pre> |
198 | 1 | Paula Gearon | rdf:Property(A) :- A(X,Y). -- RDFS rule #1 |
199 | 3 | Paula Gearon | </pre> |
200 | 1 | Paula Gearon | |
201 | ---- |
||
202 | |||
203 | |||
204 | 3 | Paula Gearon | h1. Directives |
205 | 1 | Paula Gearon | |
206 | |||
207 | 3 | Paula Gearon | |
208 | h3. @prefix |
||
209 | |||
210 | 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: |
||
211 | |||
212 | <pre> |
||
213 | |||
214 | |||
215 | _Examples:_ |
||
216 | <pre> |
||
217 | <pre> |
||
218 | 1 | Paula Gearon | @prefix owl11: <http://www.w3.org/2006/12/owl11#> . |
219 | 3 | Paula Gearon | </pre> |
220 | 1 | Paula Gearon | |
221 | 3 | Paula Gearon | <pre> |
222 | <pre> |
||
223 | 1 | Paula Gearon | @prefix : <http://www.w3.org/2006/12/owl11#> . |
224 | 3 | Paula Gearon | </pre> |
225 | 1 | Paula Gearon | |
226 | |||
227 | 3 | Paula Gearon | h3. @import |
228 | 2 | Paula Gearon | |
229 | |||
230 | 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. |
231 | |||
232 | The syntax is: |
||
233 | |||
234 | <pre> |
||
235 | |||
236 | 2 | Paula Gearon | Examples: |
237 | 3 | Paula Gearon | <pre> |
238 | <pre> |
||
239 | 2 | Paula Gearon | @import <file:///tmp/rules.rl> . |
240 | 3 | Paula Gearon | </pre> |
241 | 2 | Paula Gearon | |
242 | 3 | Paula Gearon | <pre> |
243 | <pre> |
||
244 | 2 | Paula Gearon | @import <second.dl> . |
245 | 3 | Paula Gearon | </pre> |
246 | 2 | Paula Gearon | |
247 | ---- |
||
248 | |||
249 | 3 | Paula Gearon | |
250 | h1. Example Programs |
||
251 | |||
252 | 2 | Paula Gearon | The following simple program describes 3 people and their relationships. It also describes a rule to deduce a new relationship: |
253 | 3 | Paula Gearon | <pre> |
254 | 2 | Paula Gearon | @prefix : <http://xmlns.com/foaf/0.1/> . |
255 | @prefix family: <http://family.com/data/> . |
||
256 | |||
257 | :Person(family:fred). |
||
258 | :Person(family:peter). |
||
259 | :Person(family:tom). |
||
260 | family:hasBrother(family:peter,family:fred). |
||
261 | family:hasFather(family:tom,family:peter). |
||
262 | |||
263 | family:hasUncle(A,C) :- family:hasFather(A,B), family:hasBrother(B,C). |
||
264 | 3 | Paula Gearon | </pre> |
265 | 2 | Paula Gearon | |
266 | This next program describes an inconsistent system. It will therefore fail: |
||
267 | 3 | Paula Gearon | <pre> |
268 | 2 | Paula Gearon | @prefix : <http://xmlns.com/foaf/0.1/> . |
269 | @prefix family: <http://family.com/data/> . |
||
270 | |||
271 | :Person(family:peter). |
||
272 | :Person(family:tom). |
||
273 | family:hasParent(family:tom,family:peter). |
||
274 | family:hasChild(family:tom,family:peter). |
||
275 | 1 | Paula Gearon | |
276 | family:hasParent(B,A) :- family:hasChild(A,B). |
||
277 | |||
278 | :- family:hasParent(A,B), family:hasParent(B,A). |
||
279 | 3 | Paula Gearon | </pre> |
280 | 1 | Paula Gearon | |
281 | 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_. |
282 | 1 | Paula Gearon | |
283 | The above programs are designed to be applied to an empty graph, though they will also work on pre-existing data. |
||
284 | |||
285 | 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: |
286 | <pre> |
||
287 | 1 | Paula Gearon | create <test:data>; |
288 | apply <file:///tmp/program.rlog> to <test:data>; |
||
289 | select $s $p $o from <test:data> where $s $p $o; |
||
290 | 3 | Paula Gearon | </pre> |
291 | 1 | Paula Gearon | All 3 of these commands are in TQL, though the final command can be replaced with the equivalent SPARQL: |
292 | 3 | Paula Gearon | <pre> |
293 | 1 | Paula Gearon | select $s $p $o from <test:data> where { $s $p $o } |
294 | 3 | Paula Gearon | </pre> |