Skip to content

Commit

Permalink
Merge pull request #808 from fluree/feature/sparql-iri-objects
Browse files Browse the repository at this point in the history
Feature/sparql iri objects
  • Loading branch information
dpetran authored Jun 18, 2024
2 parents 1b3d4ba + 3411099 commit 8a1f678
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion resources/sparql.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ ECHAR ::= #"\\[tbnrf]"
NIL ::= '(' WS* ')'
<WS> ::= <#"[\x20\x09\x0D\x0A]*\#[^\n]*\n*[\x20\x09\x0D\x0A]*|[\x20\x09\x0D\x0A]*">
ANON ::= '[' WS* ']'
<PN_CHARS_BASE> ::= #"[a-zA-Z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[\u10000-\uEFFFF]"
<PN_CHARS_BASE> ::= #"[a-zA-Z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[\x{10000}-\x{EFFFF}]"
<PN_CHARS_U> ::= PN_CHARS_BASE | #"_"
<VARNAME> ::= ( PN_CHARS_U | #"[0-9]" ) ( PN_CHARS_U | #"[0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]" )*
<PN_CHARS> ::= PN_CHARS_U | #"-|[0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]|/"
Expand Down
18 changes: 13 additions & 5 deletions src/clj/fluree/db/query/sparql/translator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,12 @@
(apply str (mapv parse-term elements)))

(defmethod parse-term :Object
[[_ obj]]
(parse-term obj))
;; Object ::= GraphNode
;; <GraphNode> ::= VarOrTerm | TriplesNode
[[_ [tag :as obj]]]
(if (= tag :iri)
{const/iri-id (parse-term obj)}
(parse-term obj)))

(defmethod parse-term :ObjectList
;; ObjectList ::= Object ( <','> WS Object )*
Expand All @@ -560,8 +564,8 @@
;; TriplesNode ::= Collection | BlankNodePropertyList
;; Collection ::= '(' GraphNode+ ')'
;; BlankNodePropertyList ::= '[' PropertyListNotEmpty ']'
[[_ path :as obj-path]]
(parse-term path))
[[_ & path :as obj-path]]
(mapv parse-term path))

(defmethod parse-term :ObjectPath
;; ObjectPath ::= GraphNodePath
Expand All @@ -570,7 +574,11 @@
;; CollectionPath ::= '(' GraphNodePath+ ')'
;; BlankNodePropertyListPath ::= '[' PropertyListPathNotEmpty ']'
[[_ & objs]]
(mapv parse-term objs))
(mapv (fn [[tag :as obj]]
(if (= tag :iri)
{const/iri-id (parse-term obj)}
(parse-term obj)))
objs))

(defmethod parse-term :PropertyListPathNotEmpty
;; PropertyListPathNotEmpty ::= ( VerbPath | VerbSimple ) ObjectListPath ( <';'> WS ( ( VerbPath | VerbSimple ) ObjectList )? )* WS
Expand Down
26 changes: 20 additions & 6 deletions test/fluree/db/query/sparql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,29 @@
(is (= [{"@id" "?person"
"person:handle" "jdoe"}]
where)))
(let [query "PREFIX psm: <http://srv.ktbl.de/data/psm/>
PREFIX ex: <http://example.org/>
SELECT ?p ?o
FROM <cookbook/base>
WHERE { ex:andrew ?p ?o. }"
(let [query "PREFIX ex: <http://example.org/>
SELECT ?p ?o
WHERE { ex:andrew ?p ?o. }"
{:keys [where]} (sparql/->fql query)]
(is (= [{"@id" "ex:andrew", "?p" "?o"}]
where)
"iri in subject position"))
(let [query "PREFIX ex: <http://example.org/>
SELECT ?s
WHERE { ?s ex:friend <urn:12345>. }"
{:keys [where]} (sparql/->fql query)]
(is (= [{"@id" "?s", "ex:friend" {"@id" "urn:12345"}}]
where)
"iri in object position"))
(let [query "PREFIX ex: <http://example.org/>
SELECT ?s
WHERE { ?s a <urn:12345>;
ex:friend ex:brad. }"
{:keys [where]} (sparql/->fql query)]
(is (= [{"@id" "?s", "@type" {"@id" "urn:12345"}}
{"@id" "?s", "ex:friend" {"@id" "ex:brad"}}]
where)
"iri in object position in an object list"))
(let [query "SELECT ?person WHERE {?person person:handle \"Los Angeles\"@en .}"
{:keys [where]} (sparql/->fql query)]
(is (= [{"@id" "?person",
Expand All @@ -111,7 +125,7 @@
(let [query "SELECT ?person
WHERE { ?person a schema:Person . }"
{:keys [where]} (sparql/->fql query)]
(is (= [{"@id" "?person", "@type" "schema:Person"}]
(is (= [{"@id" "?person", "@type" {"@id" "schema:Person"}}]
where)
"a as an alias for @type")))
(testing "multi clause"
Expand Down

0 comments on commit 8a1f678

Please sign in to comment.