English 中文(简体)
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 the 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.





相关问题
OWL reasoning in Prolog query from AllegroGraph Python API

I noticed that in the AllegroGraph Python API tutorial here, whenever they want to use OWL reasoning they use the conn.getStatements method instead of issuing a Prolog or SPARQL query. Is it possible ...

RDF reading/parsing errors

I have some RDF files which I want to import into a tripplestore(AllegroGraph), but at the first file I get a SAX parser error, stating there is an unrecognized character. After removing the line in ...

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:...

Managing transactions with AllegroGraph s Jena API

I m uncertain as to the behaviour of the AllegroGraph triple store in regard to transactions. The tutorial talks about using two connections, but does not mention Jena models. If I use Model s begin()...

Is it possible to query AllegroGraph using roqet?

I am trying to query AllegroGraph 4.0 using roqet to extract data in CSV. Is it possible? In roqet man page it is not clear if it can query a triple store besides rdf files. About AG HTTP protocol: ...

热门标签