Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Semafora ObjectLogic Tutorial 2020

This document provides an introduction and tutorial to the ObjectLogic language. It includes an example ObjectLogic program that defines schemas for Person, Vehicle, Car, Boat, and Bike classes and instances of Person objects named Peter, Paul, and Mary. The tutorial then explains ObjectLogic syntax elements and built-in features such as numbers, strings, aggregation, and handling duplicates before covering rules, queries, and additional features.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Semafora ObjectLogic Tutorial 2020

This document provides an introduction and tutorial to the ObjectLogic language. It includes an example ObjectLogic program that defines schemas for Person, Vehicle, Car, Boat, and Bike classes and instances of Person objects named Peter, Paul, and Mary. The tutorial then explains ObjectLogic syntax elements and built-in features such as numbers, strings, aggregation, and handling duplicates before covering rules, queries, and additional features.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

ObjectLogic Tutorial

semafora systems GmbH


Wilhelm-Leuschner-Str. 7
64625 Bensheim
Germany
www.semafora-systems.com
ObjectLogi c Tutori a l

Table of Content
Example
................................................................................................................................................5

Syntax
................................................................................................................................................6
Basic Statements
Schema
..........................................................................................................................................................................................6
Level Sta tements
Subcl a s s -of
.......................................................................................................................................................................6
Sta tements
Cl a s s es Wi
.......................................................................................................................................................................6
thout Any Methods
Si gna ture.......................................................................................................................................................................6
Sta tements
Ins..........................................................................................................................................................................................7
ta nce Level Sta tements
Ins ta nce-of
.......................................................................................................................................................................7
Sta tements
Si gna ture.......................................................................................................................................................................7
Sta tements on Ins ta nce Level
F-Mol
..........................................................................................................................................................................................8
ecul es
Predi
..........................................................................................................................................................................................9
ca tes

Basic
................................................................................................................................................10
Syntax Elements
Terms
..........................................................................................................................................................................................10
Li ..........................................................................................................................................................................................11
s ts
Exa
..........................................................................................................................................................................................11
mpl es
Na..........................................................................................................................................................................................12
mes pa ces i n ObjectLogi c
Decl a ri ng
.......................................................................................................................................................................12
Na mes pa ces
Defa ul t .......................................................................................................................................................................12
Na mes pa ce
Us i ng Na.......................................................................................................................................................................12
mes pa ces i n ObjectLogi c Expres s i ons
Queryi ng.......................................................................................................................................................................13
for Na mes pa ces

Built-in
................................................................................................................................................14
Features
Numbers
..........................................................................................................................................................................................14
, Compa ri s ons a nd Ari thmeti c
Stri
..........................................................................................................................................................................................14
ng Ha ndl i ng
Aggrega
..........................................................................................................................................................................................15
ti ons
Ca l cul a ti.......................................................................................................................................................................15
ng s a l a ri es
Si mpl e counti
.......................................................................................................................................................................16
ng exa mpl e
ObjectLogi
.......................................................................................................................................................................17
c Counti ng Exa mpl e
Ha..........................................................................................................................................................................................18
ndl i ng of Dupl i ca tes

Rules
................................................................................................................................................19
and Queries
Rul
..........................................................................................................................................................................................19
es
Queri
..........................................................................................................................................................................................20
es

Range
................................................................................................................................................21
Restriction

Quantifier
................................................................................................................................................22
Scoping

Modules
................................................................................................................................................23

2
ObjectLogi c Tutori a l

3
ObjectLogi c Tutori a l

4
ObjectLogi c Tutori a l Exa mpl e

1 Example
Before expl a i ni ng the s ynta x a nd s ema nti cs i n deta i l , we wi l l gi ve a fi rs t i mpres s i on of ObjectLogi c by
pres enti ng a n ObjectLogi c-progra m us i ng ObjectLogi c s ynta x. We wi l l refer to the contents of thi s model i n
l a ter s ecti ons of the documenta ti on.

/* schema facts */
Car::Vehicle.
Boat::Vehicle.
Bike::Vehicle.

Person[
name {1:*} *=> xsd#string,
age {1:1} *=> xsd#integer,
friend {0:*} *=> Person].
Vehicle[
owner {1:1} *=> Person,
admissibleDriver {1:*} *=> Person].
Car[
passenger {1:*} *=> Person,
seats {1:*} *=> xsd#integer].

/* facts */
peter:Person[
name -> "Peter",
age -> 17].
paul:Person[
name -> "Paul",
age -> 21,
friend->peter].
mary:Person[
name -> "Mary",
age -> 17].
bike26:Bike[
owner -> paul].
car74:Car[
owner -> paul].

/* rules consisting of a rule head and a rule body */


?X[friend->?Y] :- ?Y:Person[friend->?X].
?X[admissibleDriver->?Y] :- ?X:Vehicle[owner->?Y].
?X[admissibleDriver->?Z] :- ?X:Vehicle[owner->?Y] AND ?Y:Person[friend->?Z].

/* query */
?- ?X[admissibleDriver->?Y] AND ?X:Vehicle[owner->paul].

The fi rs t s ecti on of thi s exa mpl e cons i s ts of a s et of s chema fa cts . The s chema repres ents the cl a s s es a nd
thei r rel a ti ons hi ps i n a n object-ori ented wa y, for exa mpl e, to i ndi ca te tha t ca r a nd bi ke a re s ubcl a s s es of
vehi cl e. It a l s o des cri bes tha t every vehi cl e ha s a n owner a nd potenti a l l y mul ti pl e a dmi s s i bl e dri vers , whi ch
a re pers ons . The s chema a l s o defi nes tha t ea ch pers on ha s a na me a nd a n a ge of type, s tri ng a nd i nteger,
res pecti vel y.
The s econd s ecti on ti tl ed "fa cts ", des cri bes tha t s ome peopl e bel ong to the cl a s s pers on a nd gi ves
i nforma ti on a bout them, s uch a s thei r na me a nd a ge. Al s o i t defi nes a rel a ti ons hi p between the objects
na mel y tha t Peter i s the fri end of Pa ul . Accordi ng to the object-ori ented pa ra di gm, rel a ti ons hi ps between
objects a re repres ented a s methods , e.g. a ppl yi ng the method fri end to the object Pa ul yi el ds the res ul t
object Peter. Al l thes e fa cts ma y be cons i dered a s the extens i ona l da ta ba s e of the ObjectLogi c progra m.
Hence, they form the fra mework of a n object ba s e whi ch i s compl eted by s ome cl os ure properti es .
The rul es i n the thi rd s ecti on of the exa mpl e deri ve new i nforma ti on from the gi ven object ba s e. Eva l ua ti ng
thes e rul es i n a bottom-up wa y, new rel a ti ons hi ps between the objects , denoted by the methods fri end a nd
a dmi s s i bl eDri ver, a re a dded to the object ba s e a s i ntenti ona l i nforma ti on.
The fi na l s ecti on of the exa mpl e conta i ns a query to the object ba s e. It i s a s ki ng for a l l vehi cl es tha t a re
owned by Pa ul . For ea ch s uch vehi cl e i t a l s o retri eves the a dmi s s i bl e dri vers .

5
ObjectLogi c Tutori a l Synta x Ba s i c Sta tements

2 Syntax Basic Statements


Accordi ng to the l ogi c-progra mmi ng pa ra di gm, ObjectLogi c a l s o provi des the noti on of predi ca tes whi ch
repres ent the a tomi c pi eces of knowl edge (s ta tements ), whi ch ca n be true or fa l s e. Si nce ObjectLogi c i s a l s o
ba s ed on the object-ori ented pa ra di gm, i t not onl y provi des pl a i n predi ca tes (a s e.g. known from Prol og) but
a l s o offers epi s temol ogi ca l pri mi ti ves for model i ng i n a n object-ori ented wa y, i .e. s ubcl a s s es , a nd i ns ta nces -
of rel a ti ons but a l s o s peci fi ca ti ons of the s i gna tures for methods or the defi ni ti on of the va l ues for method
a ppl i ca ti ons .
In thi s s ecti on we pres ent the di fferent ki nds of s ta tements a va i l a bl e i n ObjectLogi c.
Schema l evel s ta tements
Ins ta nce l evel s ta tements
Pl a i n predi ca tes
La ter i n thi s documenta ti on we wi l l a l s o di s cus s rul es , whi ch from a l ogi ca l poi nt of vi ew a l s o repres ent
s ta tements . The object-ori ented s ta tements of ObjectLogi c compri s e F-Atoms a nd F-Mol ecul es a nd a re
s ynta cti ca l l y di s ti ngui s hed from pl a i n predi ca tes .

2.1 Schema Level Statements


The s chema defi nes the voca bul a ry of a n ontol ogy, defi ni ng a concept hi era rchy a s wel l a s a ppl i ca bl e
methods for the concepts . In the fol l owi ng s ecti on we wi l l i ntroduce the ObjectLogi c s ynta x for defi ni ng the
s chema .

2.1.1 Subclass-of Statements


In order to defi ne the cl a s s hi era rchy i n ObjectLogi c the l a ngua ge provi des the s o-ca l l ed s ubcl a s s -F-a toms .
The s ubcl a s s rel a ti ons hi p between two cl a s s es i s denoted by a doubl e col on. In the fol l owi ng exa mpl e we
pres ent two s ubcl a s s -F-a toms tha t s ta te tha t the cl a s s es ca r a nd bi ke a re s ubcl a s s es of the cl a s s vehi cl e:

Car::Vehicle.
Bike::Vehicle .

In the s ubcl a s s -F-a toms , the cl a s s es a re denoted by i d-terms . Hence, cl a s s es ma y ha ve methods defi ned on
them a nd ma y be i ns ta nces of other cl a s s es , whi ch s erve a s a ki nd of meta cl a s s . Furthermore, va ri a bl es a re
a l s o permi tted i n a l l pos i ti ons of s ubcl a s s -F-a toms . A cl a s s ma y ha ve s evera l i ncompa ra bl e di rect
s upercl a s s es . Thus , the s ubcl a s s rel a ti ons hi p s peci fi es a pa rti a l order on the s et of cl a s s es , s o tha t the cl a s s
hi era rchy ma y be cons i dered a s a di rected a cycl i c (but not refl exi ve) gra ph wi th the cl a s s es a s i ts nodes . Note
tha t i n a na l ogy to Hi Log [CKW93] a cl a s s na me does not denote the s et of objects tha t a re i ns ta nces of tha t
cl a s s .

2.1.2 Classes Without Any Methods


As a s peci a l ca s e, i f we wa nt to repres ent a n object wi thout gi vi ng a ny properti es , we ca n a tta ch a n empty
s peci fi ca ti on l i s t to the object na me, e.g.
thing[].
In thi s exa mpl e a cl a s s thi ng i s "crea ted" tha t does not ha ve a ny properti es (yet). If we us e a s i mi l a r
expres s i on tha t cons i s ts s ol el y of a n object na me (wi thout the empty pa i r of bra ckets , i .e. thi ng.), i t i s trea ted
a s a 0-a ry predi ca te s ymbol (s ee the s ecti on bel ow).

2.1.3 Signature Statements


In ObjectLogi c, s i gna ture-F-a toms defi ne whi ch methods a re a ppl i ca bl e for i ns ta nces of certa i n cl a s s es . In
pa rti cul a r, a s i gna ture-F-a tom decl a res a method on a cl a s s a nd gi ves type res tri cti ons for pa ra meters a nd
res ul ts . Ea ch method ma y a l s o ha ve a ca rdi na l i ty res tri cti on tha t provi des a mi ni mum va l ue a nd a ma xi mum
va l ue of a t l ea s t how ma ny entri es ha ve to be provi ded for ea ch method a nd how ma ny entri es ma y be
provi ded a t mos t. Thes e res tri cti ons ma y be vi ewed a s typi ng cons tra i nts . Together wi th the cl a s s hi era rchy,
s i gna ture-F-a toms form the s chema of a n ObjectLogi c da ta ba s e. In genera l the s ynta x ma y be des cri bed a s
fol l ows :
<domain>[<methodname> {<mincard>:<maxcard>} *=> <range>].

6
ObjectLogi c Tutori a l Synta x Ba s i c Sta tements

Here a re s ome exa mpl es for s i gna ture-F-a toms :


Person[name {1:*} *=> xsd#string].
Person[friend {0:*} *=> Person].
Vehicle[owner(xsd#integer) {1:1} *=> Person] .

The fi rs t one s ta tes tha t the method na me i s defi ned for members of the cl a s s pers on a nd the corres pondi ng
res ul t object ha s to bel ong to the da ta type s tri ng. The ca rdi na l i ty i s defi ned to be a mi ni mum of 1 a nd a
ma xi mum of unl i mi ted (*). The s econd one defi nes the method fri end for members of the cl a s s pers on
res tri cti ng the res ul t objects to the cl a s s pers on. The ca rdi na l i ty i s defi ned wi th a mi ni mum of 0 a nd a ga i n
the ma xi mum i s unl i mi ted. Fi na l l y, the thi rd s i gna ture-F-a tom a l l ows the a ppl i ca ti on of the method owner to
objects bel ongi ng to the cl a s s vehi cl e wi th pa ra meter objects tha t a re va l ues of the da ta type i nteger. The
res ul t objects of s uch method a ppl i ca ti ons wi l l be i ns ta nces of the cl a s s pers on. The ca rdi na l i ty i s mi ni mum
1 a nd ma xi mum 1 a s wel l whi ch mea ns i t ha s to be exa ctl y 1.
By us i ng a l i s t of res ul t cl a s s es encl os ed by pa renthes es , s evera l s i gna ture-F-a toms ma y be combi ned i n a n
F-mol ecul e. Thi s i s equi va l ent to the conjuncti on of the a toms , i .e. the res ul t of the method i s requi red to be
i n a l l of thos e cl a s s es :
Vehicle[owner {1:*} *=> {Person, Adult}].
i s equi va l ent to
Vehicle[owner {1:*} *=> Person].
Vehicle[owner {1:*} *=> Adult].

ObjectLogi c a l s o s upports method overl oa di ng. Thi s mea ns tha t methods denoted by the s a me object na me
ma y be a ppl i ed to i ns ta nces of di fferent cl a s s es . Methods ma y even be overl oa ded a ccordi ng to thei r a ri ty, i .
e. number of pa ra meters . For exa mpl e, the method owner a ppl i ca bl e to i ns ta nces of the cl a s s vehi cl e ca n be
us ed a s a method wi thout pa ra meter or a s a method wi th one pa ra meter. The corres pondi ng s i gna ture-F-
a toms l ook l i ke thi s :
Vehicle[owner {1:*} *=> Person].
Vehicle[owner(xsd#integer) {1:1} *=> Person].

2.2 Instance Level Statements


On the i ns ta nce l evel i t i s pos s i bl e to expres s i nforma ti on for i ndi vi dua l s a ccordi ng to the s chema defi ned
for the ontol ogy. There a re s ynta x el ements i n ObjectLogi c for defi ni ng i ns ta nces of cl a s s es a nd thei r concrete
method a ppl i ca ti ons . The fol l owi ng s ecti on i ntroduces the ObjectLogi c s ynta x for thes e s ta tements .

2.2.1 Instance-of Statements


ObjectLogi c provi des s o-ca l l ed i s a -F-a toms i n order to a s s ert tha t a n object i s a n i ns ta nce of a certa i n cl a s s .
The cl a s s members hi p i s denoted by a s i ngl e col on s epa ra ti ng two i d-terms , repres enti ng the i ns ta nce a nd
the cl a s s . The fol l owi ng exa mpl e l i s ts three i s a -F-a toms expres s i ng tha t Peter a nd Pa ul a re members of the
cl a s s pers on, wherea s ca r74 i s a member of the cl a s s ca r.

peter:Person.
paul:Person.
car74:Car.

In contra s t to other object-ori ented l a ngua ges , where every object i ns ta nti a tes exa ctl y one cl a s s , ObjectLogi c
permi ts tha t a n object i s a n i ns ta nce of s evera l cl a s s es tha t a re not neces s a ri l y l i nked vi a the s ubcl a s s
rel a ti ons hi p.

2.2.2 Signature Statements on Instance Level


In ObjectLogi c, the a ppl i ca ti on of a method on a n object i s expres s ed by da ta -F-a toms whi ch cons i s t of a hos t
object, a method a nd a res ul t object, denoted by i d-terms .
peter[friend -> mary].

If more va l ues a re gi ven for a ttri butes , the va l ues mus t be encl os ed i n curl y bra ckets :
peter[friend->{paul, mary}].

Someti mes the res ul t of the i nvoca ti on of a method on a hos t object depends on other objects , too, i .e.

7
ObjectLogi c Tutori a l Synta x Ba s i c Sta tements

methods ca n a l s o ha ve pa ra meters . For exa mpl e, Pa ul mi ght s el l the ca r74 to Peter, whi ch mea ns tha t for
di fferent da tes the ca r ha s di fferent owners .
car74[owner(2007)-> paul].
car74[owner(2008)-> peter].

The s ynta x extends s tra i ghtforwa rdl y to methods wi th more tha n one pa ra meter by s epa ra ti ng the s i ngl e
va l ues wi th a comma .
Va ri a bl es ma y a l s o be us ed a t a l l pos i ti ons of a da ta -F-a tom, whi ch a l l ows queri es on method na mes s uch
as
?- paul[?X->?Y].

2.3 F-Molecules
Ins tea d of gi vi ng s evera l i ndi vi dua l a toms , i nforma ti on a bout a n object ca n be col l ected i n F-mol ecul es ,
whi ch combi ne mul ti pl e F-a tom s ta tements i n a conci s e wa y. For exa mpl e, the fol l owi ng F-mol ecul e denotes
tha t ca r74 i s a ca r whos e owner i s Pa ul a nd whos e a dmi s s i bl e dri vers a re Peter a nd Ma ry.
car74:Car[owner->paul, admissibleDriver->{peter, mary}].

Thi s F-mol ecul e ma y be s pl i t i nto s evera l F-a toms :


car74:Car.
car74[owner->paul].
car74[admissibleDriver->peter].
car74[admissibleDriver->mary].

For F-mol ecul es conta i ni ng mul ti -va l ued methods (methods wi th a ca rdi na l i ty tha t a l l ows more tha n onl y one
va l ue), the s et of res ul t objects ca n be di vi ded i nto s i ngl eton s ets (reca l l tha t the ObjectLogi c s ema nti cs i s
mul ti -va l ued, not s et-va l ued). For s i ngl eton s ets , i t i s a l l owed to omi t the curl y bra cket encl os i ng the res ul t
s et, s o tha t the two va ri a nts a bove a re equi va l ent, whi ch mea ns tha t they yi el d the s a me object ba s e.
The s a me ca n be done for s chema -l evel s ta tements s uch a s s ubcl a s s -F-a toms or s i gna ture-F-a toms . For tha t
purpos e, a s ubcl a s s rel a ti ons hi p ma y fol l ow a fter the hos t object. Then, a s peci fi ca ti on l i s t of s i gna tures
s epa ra ted by comma s , ma y be gi ven. If a s i gna ture conta i ns more tha n one cl a s s , thos e ca n be col l ected i n
pa renthes es , s epa ra ted by comma s :
Car::Vehicle[
passenger {1:*} *=> Person,
seats {1:*} *=> xsd#integer].

The fol l owi ng s et of F-a toms i s equi va l ent to the a bove F-mol ecul e:
Car::Vehicle.
Car[passenger {1:*} *=> Person].
Car[seats {1:*} *=> xsd#integer].

More compl ex nes ti ng i s a l s o pos s i bl e i n ObjectLogi c f-mol ecul es . Bes i des col l ecti ng the properti es of the
hos t object, the properti es of other objects a ppea ri ng i n a n F-mol ecul e, e.g. method objects or res ul t objects
ma y a l s o be i ns erted. Hence, a mol ecul e ma y not onl y repres ent the properti es of one s i ngl e object but ca n
a l s o i ncl ude nes ted i nforma ti on on di fferent objects , even recurs i vel y:
car74:Car[owner->paul:Person[friend->peter:Person[age->17]].

Thi s compl ex f-mol ecul e i s equi va l ent to the fol l owi ng s et of f-a toms :
peter:Person.
peter[age -> 17].
paul:Person.
paul[friend->peter].
car74:Car.
car74[owner -> paul].

8
ObjectLogi c Tutori a l Synta x Ba s i c Sta tements

2.4 Predicates
In ObjectLogi c, predi ca tes a re us ed i n the s a me wa y a s i n predi ca te l ogi c, e.g. i n Da ta l og. Thus , pres ervi ng
upwa rd-compa ti bi l i ty from Da ta l og to ObjectLogi c. A predi ca te s ymbol fol l owed by one or more terms
s epa ra ted by comma s a nd i ncl uded i n pa renthes es i s ca l l ed a P-a tom to di s ti ngui s h i t from F-a toms . The
exa mpl e bel ow s hows s ome P-a toms . The l a s t P-a tom cons i s ts s ol el y of a 0-a ry predi ca te s ymbol . Thes e a re
a l wa ys us ed wi thout pa renthes es .
owner(car74, paul).
adult(paul).
true .

Informa ti on expres s ed by P-a toms ca n us ua l l y a l s o be repres ented by F-a toms , thus obta i ni ng a more na tura l
s tyl e of model i ng. For exa mpl e, the i nforma ti on gi ven i n the fi rs t two P-a toms coul d a l s o be expres s ed i n the
fol l owi ng wa y:
car74[owner->paul].
paul:adult.

Note tha t the expres s i ons i n the two exa mpl es a bove a re a l terna ti ve but di s joi nt repres enta ti ons . They
ca nnot be us ed i n a mi xed ma nner, i .e. a query for owner(X,Y) does not retri eve a ny res ul ts for fa cts
repres ented i n the object-ori ented wa y wi th F-Atoms .

9
ObjectLogi c Tutori a l Ba s i c Synta x El ements

3 Basic Syntax Elements


The ObjectLogi c l a ngua ge a l l ows the us er to formul a te l ogi c progra ms tha t repres ent knowl edge a bout
objects , thei r rel a ti ons hi ps a nd a l s o a bout the cl a s s es they bel ong to. In a ddi ti on to thi s fa ctua l knowl edge,
rul es a nd queri es ca n be model ed tha t repres ent i mpl i ci t, i ntenti ona l knowl edge. The knowl edge
repres enta ti on i s ba s ed on the noti on of terms a nd predi ca tes a s known from the l ogi c-progra mmi ng worl d.
Terms repres ent a l l of the di fferent enti ti es of a n ObjectLogi c progra m, i .e. objects , cl a s s es , methods a nd
method va l ues . Beca us e a l l of thes e "fi rs t-cl a s s ci ti zens " ha ve na mes , we ca n query for them, whi ch gi ves
ObjectLogi c the a ppea l a nd pa rti a l l y a l s o the power of a s econd-order l a ngua ge. Of cours e, a l ogi c progra m
mus t a l s o ma ke a s s erti ons a bout the objects . Thes e a s s erti ons a re ma de us i ng l ogi ca l predi ca tes . Refer to
the "Sta tements " s ecti on.

3.1 Terms
In ObjectLogi c a l l objects ha ve na mes . Thi s i ncl udes cl a s s es a nd i ns ta nces , va l ues but a l s o methods . The
na mes of objects a re formed by l ogi ca l terms , known from da ta l og or prol og. Es s enti a l l y, there a re three types
of terms :
1. Cons ta nts , l i ke Pers on, ca r74 or a dmi s s i bl eDri ver
Ea ch cons ta nt s ta rts wi th a l etter fol l owed by (upperca s e or l owerca s e) l etters , di gi ts or the unders core
s ymbol "_" of the ASCII cha ra cter s et.
2. Functi ons , l i ke f(X), ma xi mumSpeed(germa ny, a utoba hn)
Functi ons a re compl ex terms tha t cons i s t of functi on s ymbol s (whi ch fol l ows the s a me gra mma r a s the
cons ta nts a bove) a nd a l i s t of one or more terms (encl os ed i n pa renthes i s ) repres enti ng the a rguments .
3. Va ri a bl es , s uch a s ?X or ?Y
Va ri a bl es fol l ow the s a me gra mma r a s the cons ta nts a bove. To di s ti ngui s h cons ta nts from va ri a bl es , the
l a tter a re a l wa ys decl a red by a l ea di ng ques ti onma rk. Va ri a bl es a re onl y us ed i n the context of rul es
a nd queri es .

Data Types
Cons ta nts repres ent a va l ue of one the fol l owi ng da ta types : s tri ngs , va ri ous number types (i nteger, deci ma l ,
doubl e), s tri ngs , IRIs , da te, ti me, da teTi me, bool ea n a nd s ome more.
Stri ng cons ta nts a re encl os ed by "quota ti on ma rks " a nd ma y conta i n a ny l ega l pri nta bl e cha ra cter.
ObjectLogi c ha s fi ve number da ta types , but norma l l y you onl y need two of them: i ntegers a nd doubl e
(doubl e preci s i on fl oa ti ng numbers ). Integers a re e.g. 0,1,17,-17, doubl e numbers a re e.g 0.0, 1.0, -2.7,
3.12e-12
IRI cons ta nts a re s ymbol s wi th a na mes pa ce. IRIs a re a n i nterna ti ona l i zed va ri a nt of URIs , i .e. they
s upport i nterna ti ona l cha ra cters . IRIs a re typi ca l l y us ed a s i denti fi ers for concepts a nd i ns ta nces .
ObjectLogi c s upports mul ti pl e s ynta x va ri a nts for thi s i mport da ta type:
Pers on: Thi s i denti fi er i s a l rea dy a n IRI, a s ObjectLogi c us es the defa ul t na mes pa ce for i t.
a #Pers on: Here the na mes pa ce i s s peci fi ed by a n prefi x a l i a s .
<http://mycompa ny.com/hr#Pers on>: Thi s i s a ful l qua l i fi ed IRI.
Other da ta types fol l ow the XML s chema s peci fi ca ti on, e.g.
"2010-06-25"^^_da te: The da te June, 25th 2010
"16:37:45"^^_ti me: The ti me 16:37:45
"2010-06-25T16:37:45"^^_da te: A da teTi me term
true: Bool ea n true va l ue
In a ddi ti on to the ba s i c terms , ObjectLogi c a l s o s upports l i s ts , whi ch i s des cri bed i n more deta i l bel ow.
Fol l owi ng the object-ori ented pa ra di gm, objects ma y be orga ni zed i n cl a s s es . Furthermore, methods
repres ent rel a ti ons hi ps between objects . Thi s i nforma ti on on objects i s expres s ed by F-a toms (cf. the
Sta tements -s ecti on).

10
ObjectLogi c Tutori a l Ba s i c Synta x El ements

3.2 Lists
Li s ts a re a s peci a l ki nd of term. In ObjectLogi c, l i s ts of terms ca n be repres ented a s i n Prol og. A l i s t conta i ni ng
the cons ta nts a to e l ooks l i ke thi s :
[a, b, c, d, e]

Due to the ca noni ca l ma ppi ng, even open l i s ts wi th no fi xed l ength ca n be repres ented, for exa mpl e:
[a, b, c, d | Tail]

The va ri a bl e Ta i l repres ents the currentl y not bound l i s t, fol l owi ng the fourth el ement of thi s l i s t. Note the
"|"-s ymbol a fter d. Thi s s ymbol s epa ra tes the rema i nder of the l i s t of the l i s t's fi rs t el ement. When repl a ci ng
"|" wi th "," (yi el di ng ) repres ents a l i s t of exa ctl y fi ve el ements , whos e fi rs t el ements a re fi xed a nd whos e
fi fth el ement i s not yet bound.
In thi s ca s e Ta i l ma y even a l s o repres ent a l i s t, but then the two exa mpl e l i s ts woul d s ti l l be di fferent a s ,i n
thi s ca s e, the l i s t Ta i l i s the fi fth el ement not the cdr. As s ume Ta i l to be [X, Y]. Then the two l i s ts woul d be
[a, b, c, d| Tail] = [a, b, c, d, X, Y]
[a, b, c, d, Tail] = [a, b, c, d, [X, Y]]

3.3 Examples
For l i s t opera ti ons you ma y us e the bui l t-i n fea tures conca t a nd i nl i s t (s ee cha pter "Bui l t-i n Fea tures ").
Defi ne a fa ct wi th a new l i s t:
p([a,b,c]).
Sepa ra te a l i s t:
?- p([?Head | ?Tail]).
The res ul t wi l l be:
?Head=a, ?Tail=[b,c]
Al l el ements of the l i s t:
?- _member([a,b,c],?X).
The res ul t wi l l be:
?X=a, ?X=b, ?X=c
Merge l i s ts :
?- _concat([a,b],[c,d],?X).
The res ul t wi l l be:
?X=[a,b,c,d]

Thi s i s a n extended exa mpl e ca l cul a ti ng a gra ph us i ng l i s ts :


// the edges of a graph between two nodes
edge(a,b).
edge(b,c).
edge(a,d).
edge(d,e).
edge(e,f).

// add each edge to a path containing two nodes


path([?Y,?X]) :- edge(?X,?Y).

// add every new edge to the appropriate path


path([?H1|?L]) :- path(?L) and _unify(?L,[?H2,?T]) and edge(?H2,?H1).
Thi s query outputs a l l pa ths of the gra ph:
?- path(?L).

11
ObjectLogi c Tutori a l Ba s i c Synta x El ements

3.4 Namespaces in ObjectLogic


Na mes pa ces a re us ed to di s ti ngui s h s a me na mes for di fferent i ntenti ons i n di fferent ontol ogi es . For
i ns ta nce, a concept na med "pers on" i n ontol ogy "ca r" i s the s a me concept a s the concept "pers on" i n the
ontol ogy "fi na nce". Hence, ha ndl i ng more tha n one defi ni ti on of a concept “pers on” i n di fferent ontol ogi es
needs a mecha ni s m to di s ti ngui s h thes e concepts . semafora therefore us es the noti on of na mes pa ces i n
ObjectLogi c, whi ch ena bl es RDF-l i ke i denti fi ers for objects , cl a s s es or properti es .

3.4.1 Declaring Namespaces


An ObjectLogi c fi l e ca n conta i n na mes pa ce decl a ra ti ons tha t a s s oci a te na mes pa ce URIs wi th a l i a s es , tha t
ca n be us ed to formul a te na mes pa ce terms i n a more conci s e wa y.
:- prefix cars="http://www.cars-r-us.tv/".
:- prefix finance="http://www.financeWorld.tv/".
:- prefix xsd="http://www.w3.org/2001/XMLSchema#".
:- default prefix ="http://www.myDomain.tv/private#".

The code a bove decl a res four na mes pa ces . It a s s oci a tes three of them wi th s hortcuts (or a l i a s es ) a nd the
l a s t i s decl a red a s the defa ul t-na mes pa ce. Ea ch na mes pa ce mus t repres ent a va l i d URI a ccordi ng to RFC 2396
a nd mus t end wi th ei ther "#", "/" or ":". Thi s i s es s enti a l s i nce thes e cha ra cters ma rk the s epa ra tor between
the na mes pa ce a nd the l oca l pa rt of a n i denti fi er. Thi s conventi on i s pa rti cul a rl y i mporta nt when exporti ng to
RDF/OWL or rea di ng from thes e forma ts .

3.4.2 Default Namespace


Objects tha t do not us e a decl a red na mes pa ce a l i a s refer to objects i n the defa ul t na mes pa ce, i n the
exa mpl e bel ow the URI http://www.myDoma i n.tv/pri va te#.
:- prefix cars="http://www.cars-r-us.tv/".
:- prefix finance="http://www.financeWorld.tv/".
:- prefix xsd="http://www.w3.org/2001/XMLSchema#".
:- default prefix ="http://www.myDomain.tv/private#".

me:cars#Person[cars#age -> 28].

The defa ul t mecha ni s m i s us ed when a l a rge number of objects , concepts , or methods from the s a me
na mes pa ce a re us ed, for exa mpl e:
me s ta nds for <http://www.myDoma i n.tv/pri va te#me>

3.4.3 Using Namespaces in ObjectLogic Expressions


In ObjectLogi c expres s i ons every concept, method, object, predi ca te a nd functi on ma y be qua l i fi ed by a
na mes pa ce. To s epa ra te the na mes pa ce from the na me, the "#"-s i gn i s us ed (a s conventi ona l l y us ed i n the
RDF worl d a nd i n HTML to l oca te l oca l l i nks i ns i de a Web pa ge). The fol l owi ng exa mpl es us e the na mes pa ce
decl a ra ti on from a bove:

cars#Car[
cars#driver {1:*} *=> cars#Person,
cars#passenger {1:*} *=> cars#Person,
cars#seats {1:*} *=> xsd#integer].
cars#Person[
cars#name {1:*} *=> xsd#string,
cars#age {1:1} *=> xsd#integer,
cars#drivingLicenseId {1:1} *=> xsd#string].

finance#Bank[
finance#customer {1:*} *=> finance#Person,
finance#location {1:*} *=> finance#City].
finance#Person[
cars#name {1:*} *=> xsd#string,
finance#monthlyIncome {1:*} *=> xsd#integer].

12
ObjectLogi c Tutori a l Ba s i c Synta x El ements

?Y[finance#hasBank -> ?X] :-


?Y:finance#Person AND
?X:finance#Bank[finance#customer -> ?Y].

me:cars#Person[cars#age -> 28].


myBank:finance#Bank[finance#location -> karlsruhe].

The s ema nti cs of a na mes pa ce-qua l i fi ed object i s a l wa ys a pa i r of s tri ngs , i .e. ea ch object i s repres ented by a
URI (i ts na mes pa ce) a nd a l oca l na me. Thus fi na nce#Pers on a nd ca rs #Pers on become cl ea rl y di s ti ngui s ha bl e.
Duri ng pa rs i ng of the ObjectLogi c progra m the a l i a s es a re res ol ved, s o tha t the fol l owi ng pa i rs a re
cons tructed.
fi na nce#Pers on s ta nds for <http://www.fi na nceWorl d.tv/Pers on>
ca rs #Pers on s ta nds for <http://www.ca rs -r-us .tv/Pers on>
If no decl a red na mes pa ce URI i s found for a us ed a l i a s , the a l i a s i ts el f i s a s s umed to repres ent the
na mes pa ce of a n ObjectLogi c object. URIs ca n a l s o be us ed di rectl y i n na mes pa ce terms , i .e. the us e of
a l i a s es i s opti ona l .
As des cri bed a bove, the endi ng cha ra cter of na mes pa ces i s i mporta nt for compa ti bi l i ty wi th RDF a nd OWL. In
ca s es where the na mes pa ce does not end wi th one of the cha ra cters "/", "#" or ":", the ObjectLogi c pa rs er
a utoma ti ca l l y a dds a "#" to the end of the na mes pa ce. Thi s pa tch i s a ppl i ed to l i tera l na mes pa ces a nd a l s o
to the na mes pa ce decl a ra ti on.

3.4.4 Querying for Namespaces


Thi s mecha ni s m ena bl es us ers even to query for na mes pa ces (concrete URIs not a l i a s es ) a nd to provi de
va ri a bl es i n na mes pa ces . For i ns ta nce, the fol l owi ng query a s ks for a l l na mes pa ces X tha t conta i n a concept
pers on.
?- ?X [_localName->Person, _namespace->?N].

The fol l owi ng i nference rul e i ntegra tes knowl edge from di fferent ontol ogi es us i ng the na mes pa ce
mecha ni s m (a nd a s o ca l l ed Skol em-functi on).
person(?Name)[?Attr -> ?Value] :-
(EXIST ?X
?X:finance#Person[?Attr -> ?Value, finance#name -> ?Name] OR
?X:cars#Person[?Attr -> ?Value, cars#name -> ?Name]).

13
ObjectLogi c Tutori a l Bui l t-i n Fea tures

4 Built-in Features
The semafora i mpl ementa ti on of ObjectLogi c provi des s ome bui l t-i n fea tures whi ch grea tl y extend the
expres s i vi ty a nd vers a ti l i ty of the l a ngua ge. OntoBroker s upports procedura l a tta chments tha t ca n be us ed to do
opera ti ons tha t a re not rea l l y s ui ta bl e for a l ogi cs -ba s ed mecha ni s m, s uch a s a ri thmeti c or s tri ng-opera ti ons .
Addi ti ona l l y, thi s mecha ni s m a l l ows a cces s to externa l da ta s ources a t run ti me a nd the i ntegra ti on of da ta
externa l to the knowl edge ba s e i nto the rea s oni ng a nd query-a ns weri ng proces s .
The procedura l a tta chments a re i ntegra ted i nto the l ogi c fra mework i n the s ha pe of bui l t-i n predi ca tes .
Thes e predi ca tes ca nnot be s ynta cti ca l l y di s ti ngui s hed from ordi na ry predi ca tes . The i nference engi ne ca l l s
s ome externa l Ja va code to compute the extens i on of the predi ca tes i ns tea d of executi ng i ts norma l l ogi cs -
ba s ed rea s oni ng.
An overvi ew of a l l of the bui l t-i ns provi ded by OntoBroker i s gi ven i n the OntoBroker documenta ti on. Here, we
onl y bri efl y des cri be a few bui l t-i ns .

4.1 Numbers, Comparisons and Arithmetic


Objects denoti ng numbers or s tri ngs a re di fferent from other objects beca us e the us ua l compa ri s on opera tors
a re defi ned for them, a s wel l a s s evera l a ri thmeti c functi ons . Wi thi n a query or a rul e body, rel a ti ons
between numbers or s tri ngs ma y be tes ted wi th the compa ri s on expres s i ons . For exa mpl e, the fol l owi ng
query a s ks for a l l ca r owners younger tha n 22:
?- ?X:Car[owner->?P] AND ?P[age->?A] AND ?A < 22.

The a ri thmeti c opera ti ons a ddi ti on +, s ubtra cti on -, mul ti pl i ca ti on * a nd di vi s i on / a re a l s o i mpl emented.
Ari thmeti c expres s i ons ma y be cons tructed i n the us ua l wa y. Even compl ex expres s i ons , e.g. 3 + 5 + 2 or 3 + 2 *
3 a re s upported. By defa ul t, mul ti pl i ca ti on a nd di vi s i on ha ve a hi gher precedence tha n a ddi ti on a nd
s ubtra cti on. As us ua l , the eva l ua ti on order ma y be cha nged us i ng pa renthes es , e.g. (3 + 2) * 3. The fol l owi ng
exa mpl e conta i ns the query tha t computes the a vera ge a ge of Peter a nd Pa ul .
?-
peter[age->?P1] AND
paul[age->?P2] AND
(?A is (?P1+?P2)/2.0) .

Addi ti ona l l y the fol l owi ng ma thema ti ca l functi ons a re i mpl emented:
sin,cos,tan,asin,acos,ceil,floor,exp,rint,sqrt,round,max,min,pow

4.2 String Handling


Ana l ogous l y to numbers , there a re s evera l predefi ned opera ti ons for s tri ngs . Thes e bui l t-i n predi ca tes a l l
ha ve a fi xed a ri ty a nd (a s for a l l bui l t-i n predi ca tes ) mus t not be us ed i n the hea d of rul e.

_isTypeOf(_string, <arg>)
i s true, i f <a rg> i s a s tri ng.
_concat(<string 1> , <string 2> , <string 3>)
s ucceeds i f <s tri ng 3> i s the conca tena ti on of <s tri ng 1> a nd <s tri ng 2>, e.g.,
?- _concat("a","b",?X).
returns the bi ndi ng ?X = "a b" wherea s
?- _concat("a",?Y,"ab").
l ea ds to ?Y = "b"
_cut(<string>,<n>,<variable>)
cuts the l a s t n cha ra cters from <s tri ng>
_tokenize(<string>,<delimiters>,<variable>)
brea ks the s tri ng i nto tokens a t the del i mi ters
_tokenizen(<string>, <n>,<delimiters>,<variable>)
brea ks the s tri ng i nto ma xi ma l n tokens a t the del i mi ter
_tolower(<string>,<variable>)

14
ObjectLogi c Tutori a l Bui l t-i n Fea tures

tra ns forms a l l cha ra cters i nto l ower cha ra cters


_toupper(<string>,<variable>)
tra ns forms a l l cha ra cters i nto upper cha ra cters
_regexp("<regular expression>",<string1>,<string2>)

Regul a r expres s i ons ma y be us ed to s ea rch i n s tri ngs wi th thi s predi ca te. The fi rs t pa ra meter defi nes the
s ea rch s tri ng a s a regul a r expres s i on. Regul a r expres s i ons a re defi ned a s PERL regul a r expres s i ons . The
s econd pa ra meter defi nes the s tri ng to s ea rch i n, a nd the l a s t pa ra meter defi nes the res ul ti ng s tri ng, i .e. the
regi on tha t ma tched the pa ttern, e.g.
married("peter").
married("tom").
married("mary").

The query "s ea rch for a l l ma rri ed peopl e wi th a "p" or "t" i n thei r na me":
?- married(?X) and _regexp("[pt]",?X,?Y).
del i vers
?X = "peter", ?Y = "p"
?X = "peter", ?Y = "t"
?X = "tom", ?Y="t"

4.3 Aggregations
Aggrega ti ons a re bui l t-i ns whi ch ha ve a s et of va l ues a s a doma i n. Aggrega ti ons mus t not occur i n rul e cycl es
a nd the ta ckl ed va l ues mus t not occur i n the hea d of rul es .

The s ynta x genera l l y l ooks l i ke thi s :


?- ?RESULT = sum{<input value>[<groupingkey>] | <query for values>}.

<i nput va l ue>: Thes e a re the i nput va l ues for the a ggrega ti on.
<groupi ngkey>: The groupi ng key groups the res ul ts to a group. Gi ven the fol l owi ng exa mpl e:

Gi ven the va l ues : g1, k1, 5


g2, k2, 10
g1, k3, 2

res ul ts i n the fol l owi ng va l ues :


g1, 7
g2, 10

<query for va l ues >: Query defi ni ng on how to get the va l ues for the i nput for the a ggrega ti on.
?RESULT: Va ri a bl e conta i ni ng the res ul t of the a ggrega ti on .

4.3.1 Calculating salaries


Here we ha ve s ome da ta a bout the empl oyees of a compa ny. Bi l l a nd Ma rc work i n s a l es , Joe, Ja ck, Sus a n a nd
Va l eri e devel op s oftwa re a nd Ja ne a nd Steve work i n the res ea rch depa rtment:
Bill[hasSalary->60].
Bill[worksIn->Sales].
Marc[hasSalary->60].
Marc[worksIn->Sales].

Joe[hasSalary->60].
Joe[worksIn->Development].
Jack[hasSalary->40].
Jack[worksIn->Development].
Susan[hasSalary->100].

15
ObjectLogi c Tutori a l Bui l t-i n Fea tures

Susan[worksIn->Development].
Valerie[hasSalary->20].
Valerie[worksIn->Development].

Jane[hasSalary->70].
Jane[worksIn->Research].
Steve[hasSalary->30].
Steve[worksIn->Research].

The CEO wa nts to fi nd out the depa rtment wi th the hi ghes t pers onnel cos ts . So s he executes the fol l owi ng
query:
?- ?SUM = sum{?S [?D] | ?X[worksIn->?D] AND ?X[hasSalary->?S]}.

The [?C] i s the groupi ng va ri a bl e (we wa nt to group by the depa rtment). As we wa nt to s um up the s a l a ri es ,
we s peci fy the s um (?S) a s the a ggrega ti on i nput. The res ul t i s
?D ?SUM
------------------
Sales 120
Development 220
Research 100

So the s oftwa re depa rtment ha s the hi ghes t pers onnel cos ts . But a s the number of empl oyees di ffers we
wa nt to ca l cul a te the a vera ge s a l a ri es i n ea ch depa rtment:
?- ?AVG = avg{?S [?C] | ?X[worksIn->?C] AND ?X[hasSalary->?S]}.

The res ul t i s
?D ?AVG
------------------
Sales 60
Development 55
Research 50

So the empl oyees of the s a l es depa rtment ha ve the hi ghes t a vera ge s a l a ry.

4.3.2 Simple counting example


The ontol ogy cons i s ts of the fol l owi ng fa cts :
p(gid1,key1,1).
p(gid1,key2,1).
p(gid1,key3,1).
p(gid1,key4,1).
p(gid2,key1,1).
p(gid2,key2,1).
p(gid2,key3,1).

The fol l owi ng query counts a l l of the exi s ti ng va l ues for every gi d (gi d1 a nd gi d2).
?- ?C = count{?Z [?X] | p(?X,?Y,?Z)}.

Identi ca l keys a re el i mi na ted, s o the res ul t i s


gid1,4
gid2,3

16
ObjectLogi c Tutori a l Bui l t-i n Fea tures

4.3.3 ObjectLogic Counting Example


A[ref {0:*} *=> A].
A[ref1 {0:*} *=> A].
a:A.
b:A.
c:A.
d:A.
a[ref->{a,b}].
a[ref->d].
a[ref1->d].
c[ref1->d].

To s ee, whi ch i ns ta nce X ha s how ma ny va l ues ATTVAL, us e the fol l owi ng query:
?- ?C = count{?ATTVAL [?X] | ?X[?ATT->?ATTVAL]}.

The res ul t i s

To s ee whi ch i ns ta nce X ha s how ma ny va l ues ATTVAL for whi ch a ttri bute ATT, us e:
?- ?C = count{?ATTVAL [?X,?ATT] | ?X[?ATT->?ATTVAL]}.

Thi s query wi l l del i ver the res ul ts


X ATT C
---------------
a ref 3
a ref1 1
c ref1 1

17
ObjectLogi c Tutori a l Bui l t-i n Fea tures

4.4 Handling of Duplicates


One i mpl i ci t fea ture of a ggrega ti ons i s the ha ndl i ng of dupl i ca tes . By defa ul t, a l l of the va ri a bl es tha t occur
i n the query of the a ggrega ti on, except for the groupi ng va ri a bl es , a re ta ken i nto cons i dera ti on when
determi ni ng the tupl es to cons i der. Exa mpl e:
?- ?Sum = sum { ?S [?C,?D] | ?C:Companay[department->?D], ?D:Department[member->?
E], ?E:Employee[salary->?S]}.

Here, dupl i ca te el i mi na ti on occurs on the tupl es (?E,?S) for ea ch group defi ned by (?C,?D).
Someti mes you need a di fferent s peci fi ca ti on of dupl i ca tes i n your a ggrega ti on. As s ume tha t you ha ve the
fol l owi ng ontol ogy:
Family[member {0:*} *=> Person].
Person[likes {0:*} *=> Thing].
Music:Thing.
Shopping:Thing.
Biking:Thing.

TheWatsons:Family[member -> {HenryWatson,SallyWatson}].


HenryWatson:Person[likes->{Music,Biking}].
SallyWatson:Person[likes->{Music,Shopping}].

You wa nt to count the number of di fferent thi ngs whi ch a re l i ked by a ny member of the Wa ts on fa mi l y. You
ma y s ta rt wi th a query for the a ggrega ti on l i ke
?- TheWatsons[member->?M], ?M[likes->?X].

For the ontol ogy a bove, you get the res ul ts :


?M,?X
HenryWatson,Music
HenryWatson,Biking
SallyWatson,Music
SallyWatson,Shopping

In order to count the l i ked thi ngs you ma y wri te:


?- ?C = count {?X | TheWatsons[member->?M], ?M[likes->?X]}.

The dupl i ca te el i mi na ti on i s performed on the tupl es (?M,?X) a s des cri bed a bove a nd you get the res ul t ?C =
4. So the Mus i c i s counted twi ce. Thi s i s not the i ntended wa y of counti ng. To fi x thi s i s s ue, you ha ve to us e
na med a nonymous va ri a bl es i n your query. Anonymous va ri a bl es a re i gnored i n the query res ul ts . Thei r
s ynta x i s ei ther jus t a ques ti on ma rk. In thi s ca s e, every occura nce i s a n own a nonymous va ri a bl e.
If you need the s a me a nonymous va ri a bl e i n two pl a ces , you need to us e a na med a nonymous va ri a bl e. The
s ynta x i s ?#na me, i .e. there i s a ha s h ma rk between ques ti on ma rk a nd the va ri a bl e na me. The corrected
query i s then
?- TheWatsons[member->?#M], ?#M[likes->?X].
a nd the res ul ts a re
?X
Music
Biking
Shopping

The counti ng a ggrega ti on i s then


?- ?C = count {?X | TheWatsons[member->?#M], ?#M[likes->?X]}.
a nd you get the expected res ul t ?C = 3.
Al s o note tha t the l a s t query ca n a l s o be wri tten us i ng a pa th expres s i on. I.e. i t i s equi va l ent to
?- ?C = count {?X | ?X = TheWatsons.member.likes}.
Thi s a l s o returns ?C = 3.
Note: You need a n Ontobroker >= 6.0.2 for the correct ha ndl i ng of a nonymous va ri a bl es i n a ggrega ti ons .
Na med a nonymous va ri a bl es were a l s o i ntroduced wi th OB 6.0.2.

18
ObjectLogi c Tutori a l Rul es a nd Queri es

5 Rules and Queries


An ObjectLogi c knowl edge ba s e cons i s ts of a number of (extens i ona l ) ground fa cts . In order to formul a te more
compl ex knowl edge, ObjectLogi c provi des the noti on of rul es whi ch a l l ows the s peci fi ca ti on of dependenci es
between known fa cts a nd the crea ti on of new, a ddi ti ona l fa cts ba s ed on the exi s ti ng ones .
Queri es a re s i mi l a r to SQL-queri es a nd ca n be us ed to retri eve fa cts from the ObjectLogi c knowl edge ba s e.
Si nce we a re us ua l l y i nteres ted i n the enta i l ment of a ppl i ed rul es to the ba s i c fa cts , queri es a ctua l l y return
fa cts from the deri ved model , whi ch i s bui l t from the cl os ure of a l l fa cts a nd rul es .

5.1 Rules
Ba s ed on a gi ven object ba s e (whi ch ca n be cons i dered a s a s et of fa cts ), rul es offer the pos s i bi l i ty to deri ve
new i nforma ti on, i .e. to extend the object ba s e i ntenti ona l l y. Rul es encode generi c i nforma ti on of the form:
Whenever the precondi ti on of a rul e i s s a ti s fi ed, the concl us i on of the rul e i s a l s o true.
The precondi ti on i s ca l l ed rul e body a nd i s formed by a n a rbi tra ry l ogi ca l formul a cons i s ti ng of P-Atoms
(predi ca tes ) or F-mol ecul es , whi ch a re combi ned by OR, NOT, AND, <--, --> a nd <-->.
A --> B i n the body i s a n a bbrevi a ti on for NOT A OR B,
A <-- B i s a n a bbrevi a ti on for NOT B OR A a nd
A <--> B i s a n a bbrevi a ti on for (A-->B) AND (B<--A).
Va ri a bl es i n the rul e body ma y be qua nti fi ed ei ther exi s tenti a l l y or uni vers a l l y. The concl us i on, the rul e
hea d, i s a conjuncti on of P-Atoms a nd F-mol ecul es . Synta cti ca l l y, the rul e hea d i s s epa ra ted from the rul e
body by the s ymbol :- a nd every rul e ends wi th a dot. Non-ground rul es us e va ri a bl es for pa s s i ng i nforma ti on
between s ub-goa l s a nd to the hea d. Every va ri a bl e i n the hea d of the rul e mus t a l s o occur i n a pos i ti ve F- or
P-Atom i n the body of the rul e.
As s ume a n object ba s e defi ni ng the methods fri end a nd owner for s ome pers ons . The rul es bel ow compute
the refl exi ve cl os ure of fri end a nd defi ne a new method a dmi s s i bl eDri ver ba s ed on fri end- a nd owner-fa cts .
?X[friend->?Y] :- ?Y:Person[friend->?X].
?X[admissibleDriver->?Y] :- ?X:Vehicle[owner->?Y].
?X[admissibleDriver->?Z] :- ?X:Vehicle[owner->?Y] AND ?Y:Person[friend->?Z].

Pa rti a l l ogi ca l formul a e i n the rul e body ma y be nega ted. E.g. the fol l owi ng rul e computes for every ca r ?X a l l
pers ons ?Y tha t a re prohi bi ted a s dri vers for ?X:
?X[prohibitedDriver->?Y] :-
?X:Car AND
?Y:Person AND
NOT ?X[admissibleDriver -> ?Y].

The fol l owi ng rul e computes a l l pers ons ?X tha t do ha ve (a t l ea s t one) fri end:
personWithFriends(?X) :-
?X:Person AND
(EXIST ?Y ?X[friend -> ?Y]).

Rul es ca n a l s o be i denti fi ed by rul e na mes , e.g. Mutua l Fri ends hi p i n the fol l owi ng rul e:
@{MutualFriendship} ?X[friend -> ?Y] :- ?Y:Person[friend -> ?X].

The rul e na me ca n be a ny a rbi tra ry ground term.

19
ObjectLogi c Tutori a l Rul es a nd Queri es

5.2 Queries
A query ca n be cons i dered a s a s peci a l ki nd of rul e wi th a n empty hea d. The fol l owi ng query a s ks a bout a l l
a dmi s s i bl e dri vers of ca r74:
?- car74[admissibleDriver -> ?Y].

The a ns wer to a query cons i s ts of a l l of the va ri a bl e bi ndi ngs of the type tha t the corres pondi ng ground
i ns ta nce of the rul e body i s true i n the object ba s e. Cons i deri ng the object ba s e des cri bed by the fa cts a nd
rul es of the exa mpl e from the begi nni ng of thi s ma nua l , the a bove query yi el ds the fol l owi ng va ri a bl e
bi ndi ngs :
?Y = paul
?Y = peter

Note tha t va ri a bl es i n a query ma y onl y be bound to i ndi vi dua l objects , never to s ets of objects , i .e. the a bove
query does not return ?X = {pa ul , peter}.
However, i n the ca s e of a query wi th a s et of ground i d-terms a t the res ul t pos i ti on, i t i s onl y checked i f a l l of
thes e res ul ts a re true i n the corres pondi ng object ba s e. There ma y a l s o be a ddi ti ona l res ul t objects i n the
da ta ba s e. Wi th the gi ven object ba s e, a l l of the fol l owi ng queri es yi el d the a ns wer true.
?- car74[admissibleDriver -> {peter, paul}].
?- car74[admissibleDriver -> {paul, peter}].
?- car74[admissibleDriver -> peter].
?- car74[admissibleDriver -> paul].

If we wa nt to know i f a s et of objects i s the exa ct res ul t of a mul ti -va l ued method a ppl i ed to a certa i n object,
we woul d need to us e nega ti on.
More compl ex queri es ca n be formul a ted tha t a l s o conta i n a rbi tra ry fi rs t-order formul a s i n the (rul e) body:
The fol l owi ng query computes the ma xi mum va l ue ?X for whi ch p(?X) hol ds . The rul e body expres s es tha t a l l ?
Y for whi ch p(?Y) (a l s o) hol ds mus t be l es s or equa l to the s ea rched ?X.
p(1).
p(2).
p(3).
?- p(?X) AND (FORALL ?Y (p(?Y) --> ?Y <= ?X)).

The res ul t wi l l be:


?X = 3.0

20
ObjectLogi c Tutori a l Ra nge Res tri cti on

6 Range Restriction
Al l of the va ri a bl es i n a rul e or a query mus t be ra nge-res tri cted, i .e. for ea ch va ri a bl e one or more of the
fol l owi ng condi ti ons mus t hol d:
1. The va ri a bl e occurs i n a pos i ti ve (not nega ted) body l i tera l whi ch i s not a bui l t-i n-l i tera l (s i mpl e bui l t-i n,
connector bui l t-i n, or a ggrega te).
2. The va ri a bl e i s bound top-down by cons ta nts i n the query or i n connected rul es
3. A va ri a bl e i s bound by the output of a bui l t-i n-l i tera l a nd a l l i nput-a rguments of the bui l t-i n a re ra nge-
res tri cted or ground. Whi ch a rguments a re i nput a nd output of a bui l t-i n i s defi ned by the s i gna tures of
the bui l t-i n.
Let us i l l us tra te the a bove topi cs i n exa mpl es . For the fol l owi ng rul e a l l of the va ri a bl es a re bound a nd hence
the query i s ra nge-res tri cted. The va ri a bl es ?X a nd ?Y a re bound beca us e they occur i n the pos i ti ve l i tera l p(?
X,?Y) (condi ti on 1). Bui l t-i n _a dd/3 ha s the s i gna ture {number,number,va ri a bl e} whi ch mea ns tha t the fi rs t two
(i nput) a rguments mus t be bound to numbers a nd the thi rd ca n be a va ri a bl e a nd i s hence a n output
pa ra meter. Thus va ri a bl e ?Z i s bound beca us e i t i s the output va ri a bl e of bui l t-i n a dd a nd a l l of the i nput
va ri a bl es of a dd a re bound (condi ti on 3).
?- p(?X,?Y) AND _add(?X,?Y,?Z).

In the next query a nd rul e the va ri a bl e ?Y of the rul e i s bound top-down by the cons ta nt 5 i n the query
(condi ti on 2). Va ri a bl e ?X i s a ga i n bound by the pos i ti ve l i tera l q(?X) (condi ti on 1) a nd thus ?Z i s bound a s a n
output pa ra meter of the a dd bui l t-i n (condi ti on 3).
p(?X,?Y) :- q(?X) AND _add(?X,?Y,?Z).
?- p(?X,5).

In the next rul e there i s a tra ns i ti ve dependency of va ri a bl e bi ndi ngs through the bui l t-i ns gi ven. Thus a l s o ?U
i s ra nge-res tri cted (condi ti on 1 a nd condi ti on 3).
p(?X,?Y) :- q(?X,?Y) AND _add(?X,?Y,?Z) and _add(?Z,?Y,?U).

The a bove menti oned condi ti ons ha ve the cons equence tha t a rul e or query i s not ra nge-res tri cted i f a
va ri a bl e onl y occurs i n a nega ted l i tera l . Rul es whi ch ha ve va ri a bl es occurri ng i n the hea d onl y a re obs cure
beca us e thes e va ri a bl es mus t be bound top-down i n every ca s e for the rul e to be ra nge-res tri cted.

21
ObjectLogi c Tutori a l Qua nti fi er Scopi ng

7 Quantifier Scoping
The qua nti fi ers FORALL a nd EXISTS i ntroduce va ri a bl es i n rul es a nd queri es . Synta cti ca l l y, va ri a bl es l i ke ?X
or ?Y do not di ffer from cons ta nt s ymbol s i n ObjectLogi c, hence the requi rement for expl i ci t decl a ra ti on wi th
qua nti fi ers . In the unus ua l s i tua ti on where there i s a confl i ct between a us ed va ri a bl e a nd a n exi s ti ng
cons ta nt, i t i s i mporta nt to know the s cope, i .e. the l i feti me of va ri a bl es . To i l l us tra te the noti on of va ri a bl e
s copes we pres ent a n exa mpl e formul a where a l l va ri a bl es a re underl i ned a nd a l l cons ta nts a re not.
p(?X,?Y) :- r(?X,?Y) AND (EXIST ?U q(?U,?Y)).
p(?X,?Y) :- (EXIST ?U q(?U,?Y) AND r(?U,?Y)).
p(?X,?Y) :- (EXIST ?U q(?U,?Y)) AND r(?U,?Y).

The rul e-of-thumb i s tha t ea ch qua nti fi ers bi nds va ri a bl es ti l l the end of the compl ete formul a . You ca n
overwri te thi s pa ttern onl y by i ntroduci ng pa renthes i s a nd, thus , expl i ci tl y i ntroduci ng a new s cope for the
qua nti fi er. Note: the s ema nti cs of the fi rs t a nd thi rd formul a a bove i s equi va l ent (the ?U i n the r predi ca te i s
a cons ta nt), wherea s formul a two i s di fferent (here, the ?U i n the r predi ca te i s bound by the EXIST
qua nti fi er).

22
ObjectLogi c Tutori a l Modul es

8 Modules
In s oftwa re engi neeri ng, modul es ha ve been i nvented to reduce compl exi ty. Cl os el y rel a ted a nd i nterwoven
thi ngs a re pa cka ged i n a common modul e, whi l s t l oos el y coupl ed thi ngs res i de i n di fferent modul es . The
communi ca ti on between modul es s houl d be mi ni ma l . Thes e pri nci pl es ha ve been tra ns ferred to knowl edge
ba s es . Rul es a nd fa cts des cri bi ng a cl os el y rel a ted pa rt of the doma i n res i de i n one modul e. Thus , a n enti re
knowl edge ba s e ca n be s pl i t up i nto di fferent modul es ea ch conta i ni ng cl os el y rel a ted s ta tements a bout the
doma i n. In s ome s ens e thi s concept i s orthogona l to the concept of na mes pa ces . Identi fi ers wi th di fferent
na mes pa ces ma y be a ddres s ed i n one a nd the s a me modul e. On the other ha nd, i denti fi ers a re gl oba l a cros s
a l l modul es whi ch mea ns tha t a n object wi th i denti fi er x i s the s a me object i n a l l modul es . Thus , modul es do
not s epa ra te objects , but s ta tements a bout objects . Both, ground s ta tements (s ta tements wi thout va ri a bl es )
a nd rul es a nd queri es a re a s s i gned to modul es .
Ea ch ObjectLogi c fi l e mus t conta i n ground s ta tements from exa ctl y one modul e. The (defa ul t) modul e ca n be
defi ned a t the begi nni ng of the fi l e:
:- module = module1.

The na me of a modul e ca n be a n a rbi tra ry ground term, i .e. a cons ta nt, a functi ona l term or a na mes pa ce
term. In the exa mpl e a bove we chos e a cons ta nt. When us i ng a na mes pa ce term a nd a n a ppropri a te a l i a s
exi s ts , i t ca n a l s o be us ed for the decl a ra ti on of the modul e, e.g.
:- prefix a="http://www.exmple.org/sample#".
:- module = a#sampleModule.

The modul e i s a s s umed to a ppl y for a l l s ubs equent ground fa cts , es p. i f they do not decl a re the modul e
expl i ci tl y. The nota ti on for expl i ci tl y a s s i gni ng a modul e to a ground fa ct l ooks l i ke thi s .
peter:Person@module1.
paul:Person@module1.
bike26:Bike[owner -> paul]@module1.

Important Note:
Si nce ea ch fi l e ca n conta i n onl y s ta tements from one modul e, the modul e references ca n be omi tted
wi thout cha ngi ng the s ema nti cs .

Modul e references a re more i mporta nt wi thi n rul es a nd queri es . As ground s ta tements , rul es a re a l wa ys
s tored i n the defi ni ng modul e, but they ca n i nfer s ta tements i n a ny modul e:
@{ancestorHasFather}
?X[hasAncestor ->?Y] :- ?X:Person, ?X[hasFather->?Y]@module2.
expres s es tha t the rul e na med a nces torHa s Fa ther res i des i n s a mpl eModul e. Al l hea d a nd body formul a s a re
a s s umi ng the s a me modul e i f the modul e i s not s peci fi ed expl i ci tl y.
The a bove rul e, thus , i s equi va l ent to:
@{ancestorHasFather}
?X[hasAncestor ->?Y]@a#sampleModule :- ?X:Person@a#sampleModule, ?X[hasFather-
>?Y]@a#sampleModule.

Ea ch l i tera l i n a rul e body a nd rul e hea d ca n us e i ts own modul e. For body l i tera l s thi s mea ns tha t the
rea s oner tri es to s ea rch for the fa ct i n the menti oned modul e. For hea d l i tera l s thi s mea ns , tha t the new fa ct
i s a s s erted to hol d true i n i ts modul e. A compl ex exa mpl e l ooks l i ke thi s :
friend(?X,?Y)@module2 :-
?X:Person[friend -> ?Y:Person]@module1.

Thi s rul e expres s es tha t modul e2 hol ds the (deri ved) fa ct fri end(?X,?Y) i f i t i s true i n modul e1 tha t the ?X
a nd ?Y a re pers ons a nd rel a ted vi a the fri end method. Si nce modul e na mes a re terms , i t i s even pos s i bl e to
us e va ri a bl es a s modul e na mes i n rul e bodi es .
friend(?X,?Y) :-
?X:Person[friend -> ?Y:Person]@?M.

Thi s rul e s ea rches for s ta tements a bout fri ends i n every modul e ?M a nd a s s erts a new fa ct i n the defa ul t
modul e.

23
ObjectLogi c Tutori a l Modul es

In our previ ous exa mpl es we us ed onl y cons ta nts for modul e na mes . In a ddi ti on to tha t, compl ex modul e
na mes , i .e. modul e na mes cons i s ti ng of functi ons a re a l s o a l l owed, for exa mpl e:
module(Arg1,...,Argn)

If Arg1, ... Argn conta i n va ri a bl es ea ch bi ndi ng l ea ds to a s epa ra te modul e na me, e.g. modul e(a ,f(b)). It i s
good pra cti ce to us e IRI terms a s modul e na mes , a nd hence crea te a uni vers a l l y uni que i denti fi er for the
modul es , for exa mpl e, wi th a decl a ra ti on s uch a s thi s :
:- prefix a="http://www.exmple.org/sample#".
:- module =a#sample.

24
ObjectLogi c Tutori a l

Legal Notice

semafora systems GmbH


Wilhelm-Leuschner-Str. 7
64625 Bensheim
Germany

semafora@semafora-systems.com
www.semafora-systems.com

Disclaimer

All of the data and settings in this program have been checked and tested extensively. Despite taking great
care and making extensive technical checks, we cannot guarantee absolute accuracy, or completely correct
contents. No responsibility will be taken for technical errors and incorrect information or for their
consequences, e.g. effects on other programs. We are grateful to be informed of any errors at any time.
The information in this document reflects the level of information available at the time of going to press. Any
necessary corrections will be covered by subsequent versions.
semafora systems GmbH does not accept any responsibility or liability for changes caused by circumstances for
which they are not responsible.
We will accept no liability for problems with the Application Time Tracking caused by incorrect usage or for
any complications caused by third-party software.

Copyright

© Copyright 2020 SemEO Services GmbH


All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express
permission of semafora systems GmbH. The information contained herein may be changed without prior notice.
These materials are subject to change without notice. These materials are provided by semafora systems GmbH
for informational purposes only, without representation or warranty of any kind, and semafora systems GmbH
shall not be liable for errors or omissions with respect to the materials. The only warranties for semafora systems
GmbH products and services are those that are set forth in the express warranty statements accompanying such
products and services, if any. Nothing herein should be construed as constituting an additional warranty.

Parts of the technology and used names in the products OntoStudio and OntoBroker are patent or trademark
protected.

Bensheim, January 2020

25

You might also like