开发者

Allegrograph - Functors like RDF objects properties?

Using Allegrograph, Prolog functors are pretty awesome, but there is one downside.

Let's say you define a functor that links two entities, for example parentOf which equals to "!n:motherOf OR !n:fatherOf" which are both rdf object properties defined in your ontology (not functors).

Let's define the triplet "A !n:fatherOf B". Since "parentOf" is a functor and not an rdf object property, if you request for all properties that link A et B, you will only obtain t开发者_JAVA百科he triplet "A !n:fatherOf B" (but not "A parent B").

The only way to know if A is a parent of B is to ask the Boolean question directly.

So my question is : What's the way to obtain easily the result of "get the RDF triplets composed of FACTS + INFERRED FACTS generated by functors ?"


Prolog functors are part of a Prolog program. When you write a query over an AllegroGraph store using Prolog, you're really writing a Prolog program that derives a set of answers to the constraints expressed in the program.

parentOf isn't part of the store, it's part of the program.

What you're trying to do is materialize the knowledge implied by a Prolog program, such that it's available in the same form as the ground triples.

To do that you need to write a… Prolog program that derives the inferred knowledge and adds it to the store.

Here's some code that should help. It's some setup and example in Lisp, but the Prolog functors should be obvious.

;; Assume the prefix is set up. These are for the Lisp environment.
(register-namespace "ex" "http://example.com/")
(enable-!-reader)

;; Define functors for basic relationships.
(<-- (parent ?x ?y)  
     (father ?x ?y))  

(<- (parent ?x ?y)  
    (mother ?x ?y))  

(<-- (male ?x)  
     (q- ?x !ex:sex !ex:male))  

(<-- (female ?x)  
     (q- ?x !ex:sex !ex:female))  

(<-- (father ?x ?y)  
     (male ?x)  
     (q- ?x !ex:has-child ?y))  

(<-- (mother ?x ?y)  
     (female ?x)  
     (q- ?x !ex:has-child ?y)) 

;; Functors for adding triples.
(<-- (a- ?s ?p ?o)
 ;; Fails unless all parts ground.
 (lisp (add-triple ?s ?p ?o)))

(<-- (a- ?s ?p ?o ?g)
 ;; Fails unless all parts ground.
 (lisp (add-triple ?s ?p ?o ?g)))

(<-- (a-- ?s ?p ?o)
 ;; Fails unless all parts ground.
 (lispp (not (get-triple :s ?s :p ?p :o ?o)))
 (lisp (add-triple ?s ?p ?o)))

(<-- (a-- ?s ?p ?o ?g)
 ;; Fails unless all parts ground.
 (lispp (not (get-triple :s ?s :p ?p :o ?o :g ?g)))
 (lisp (add-triple ?s ?p ?o ?g)))

;; Add some sample data.
(create-triple-store "/tmp/foo")
(add-triple !ex:john !ex:sex !ex:male)
(add-triple !ex:dave !ex:sex !ex:male)
(add-triple !ex:alice !ex:sex !ex:female)
(add-triple !ex:alice !ex:has-child !ex:dave)
(add-triple !ex:john !ex:has-child !ex:dave)

;; Now who is a parent of whom?
(select (?parent ?child)
  (parent ?parent ?child))

;; Returns:
;; (("http://example.com/john" "http://example.com/dave")
;;  ("http://example.com/alice" "http://example.com/dave"))

;; Add the triples.
(select (?parent ?child)    ; never succeeds
  (parent ?parent ?child)
  (a-- ?parent !ex:parentOf ?child)
  (fail))

;; Now see what's in the store using the materialized triples.
(select (?parent ?child)
  (q- ?parent !ex:parentOf ?child))

;; Returns:
;; (("http://example.com/john" "http://example.com/dave")
;;  ("http://example.com/alice" "http://example.com/dave"))


Do you want something like this?

(<-- (parentOf ?a ?b)
    (or
        (q ?a !n:fatherOf ?b)
        (q ?a !n:motherOf ?b)))

(select (?a ?b)
    (parentOf ?a ?b))

The select statement will return the triples that involve either n:fatherOf or n:motherOf.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜