Skip to content

Commit

Permalink
IRIRef is back?
Browse files Browse the repository at this point in the history
  • Loading branch information
cordawyn committed Dec 14, 2012
1 parent 61c810a commit 4dea1f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
40 changes: 26 additions & 14 deletions Database/HSparql/QueryGenerator.hs
Expand Up @@ -28,6 +28,7 @@ module Database.HSparql.QueryGenerator

-- ** Auxiliary
, (.:.)
, iriRef

-- * Term Manipulation

Expand Down Expand Up @@ -117,7 +118,7 @@ createDescribeQuery q = execQuery specifyType qshow
-- Manipulate data within monad

-- |Add a prefix to the query, given an IRI reference, and return it.
prefix :: T.Text -> Node -> Query Prefix
prefix :: T.Text -> IRIRef -> Query Prefix
prefix pre ref = do
let p = Prefix pre ref
modify $ \s -> s { prefixes = p : prefixes s }
Expand Down Expand Up @@ -150,7 +151,7 @@ askTriple a b c = do
modify $ \s -> s { askTriples = appendTriple t (askTriples s) }
return t

describeIRI :: Node -> Query Node
describeIRI :: IRIRef -> Query IRIRef
describeIRI newIri = do
modify $ \s -> s { describeURI = Just newIri }
return newIri
Expand Down Expand Up @@ -187,8 +188,8 @@ filterExpr e = do
-- Random auxiliary

-- |Form a 'Node', with the 'Prefix' and reference name.
(.:.) :: Prefix -> T.Text -> Node
(.:.) (Prefix p _) s = unode $ T.append p $ T.append ":" s
(.:.) :: Prefix -> T.Text -> IRIRef
(.:.) = PrefixedName

-- Duplicate handling

Expand Down Expand Up @@ -229,7 +230,7 @@ class TermLike a where
instance TermLike Variable where
varOrTerm = Var

instance TermLike Node where
instance TermLike IRIRef where
varOrTerm = Term . IRIRefTerm

instance TermLike Expr where
Expand All @@ -246,8 +247,8 @@ instance TermLike T.Text where
instance TermLike (T.Text, T.Text) where
varOrTerm (s, lang') = Term . RDFLiteralTerm $ plainLL s lang'

instance TermLike (T.Text, Node) where
varOrTerm (s, (UNode ref)) = Term . RDFLiteralTerm $ typedL s ref
instance TermLike (T.Text, IRIRef) where
varOrTerm (s, ref) = Term . RDFLiteralTerm $ typedL s (getFQN ref)

instance TermLike Bool where
varOrTerm = Term . BooleanLiteralTerm
Expand Down Expand Up @@ -379,12 +380,22 @@ class QueryShow a where

data Duplicates = NoLimits | Distinct | Reduced

data Prefix = Prefix T.Text Node
data Prefix = Prefix T.Text IRIRef

data Variable = Variable Int

data IRIRef = IRIRef Node
| PrefixedName Prefix T.Text

iriRef :: T.Text -> IRIRef
iriRef uri = IRIRef $ unode uri

getFQN :: IRIRef -> T.Text
getFQN (IRIRef (UNode n)) = n
getFQN (PrefixedName (Prefix _ n) s) = T.append (getFQN n) s

-- Should support numeric literals, too
data GraphTerm = IRIRefTerm Node
data GraphTerm = IRIRefTerm IRIRef
| RDFLiteralTerm LValue
| NumericLiteralTerm Integer
| BooleanLiteralTerm Bool
Expand Down Expand Up @@ -436,7 +447,7 @@ data QueryData = QueryData
, pattern :: GroupGraphPattern
, constructTriples :: [Pattern] -- QTriple
, askTriples :: [Pattern]
, describeURI :: Maybe Node
, describeURI :: Maybe IRIRef
, duplicates :: Duplicates
, ordering :: [OrderBy]
}
Expand All @@ -456,7 +467,7 @@ data SelectQuery = SelectQuery
{ queryVars :: [Variable] }

data DescribeQuery = DescribeQuery
{ queryDescribe :: Node }
{ queryDescribe :: IRIRef }


-- QueryShow instances
Expand All @@ -474,10 +485,11 @@ instance QueryShow Prefix where
instance QueryShow Variable where
qshow (Variable v) = "?x" ++ show v

instance QueryShow Node where
qshow (UNode r) = "<" ++ (T.unpack r) ++ ">"
instance QueryShow IRIRef where
qshow (IRIRef (UNode r)) = "<" ++ (T.unpack r) ++ ">"
qshow (PrefixedName (Prefix pre _) s) = (T.unpack pre) ++ ":" ++ (T.unpack s)

instance QueryShow (Maybe Node) where
instance QueryShow (Maybe IRIRef) where
qshow (Just r) = qshow r
qshow Nothing = ""

Expand Down
20 changes: 10 additions & 10 deletions tests/Database/HSparql/ConnectionTest.hs
Expand Up @@ -32,9 +32,9 @@ test_selectQuery =

where endPoint = "http://localhost:3000"
query = do
resource <- prefix "dbprop" (RDF.unode "http://dbpedia.org/resource/")
dbpprop <- prefix "dbpedia" (RDF.unode "http://dbpedia.org/property/")
foaf <- prefix "foaf" (RDF.unode "http://xmlns.com/foaf/0.1/")
resource <- prefix "dbprop" (iriRef "http://dbpedia.org/resource/")
dbpprop <- prefix "dbpedia" (iriRef "http://dbpedia.org/property/")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")

x <- var
name <- var
Expand All @@ -50,8 +50,8 @@ test_askQuery = do

where endPoint = "http://localhost:3000"
query = do
resource <- prefix "dbpedia" (RDF.unode "http://dbpedia.org/resource/")
dbprop <- prefix "dbprop" (RDF.unode "http://dbpedia.org/property/")
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")

x <- var
ask <- askTriple x (dbprop .:. "genre") (resource .:. "Web_browser")
Expand All @@ -70,10 +70,10 @@ test_constructQuery =

where endPoint = "http://localhost:3000"
query = do
resource <- prefix "dbpedia" (RDF.unode "http://dbpedia.org/resource/")
dbpprop <- prefix "dbprop" (RDF.unode "http://dbpedia.org/property/")
foaf <- prefix "foaf" (RDF.unode "http://xmlns.com/foaf/0.1/")
example <- prefix "example" (RDF.unode "http://www.example.com/")
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
dbpprop <- prefix "dbprop" (iriRef "http://dbpedia.org/property/")
foaf <- prefix "foaf" (iriRef "http://xmlns.com/foaf/0.1/")
example <- prefix "example" (iriRef "http://www.example.com/")

x <- var
name <- var
Expand All @@ -93,6 +93,6 @@ test_describeQuery =

where endPoint = "http://localhost:3000"
query = do
resource <- prefix "dbpedia" (RDF.unode "http://dbpedia.org/resource/")
resource <- prefix "dbpedia" (iriRef "http://dbpedia.org/resource/")
uri <- describeIRI (resource .:. "Edinburgh")
return DescribeQuery { queryDescribe = uri }

0 comments on commit 4dea1f1

Please sign in to comment.