summaryrefslogtreecommitdiffstats
path: root/Redland/MidLevel.hs
blob: fb3367accc4f6cd607b512d14df7cf2156534afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
{- |
Module      :  Redland.MidLevel
Maintainer  :  defanor <defanor@uberspace.net>
Stability   :  unstable
Portability :  non-portable (GHC extensions are used)

Mid-level <http://librdf.org/docs/api/index.html Redland RDF library>
bindings: using Haskell naming conventions, common Haskell types,
'ForeignPtr' with finalizers, exceptions, copied structures (as
opposed to the shared ones, as nodes in statement-related functions in
the C API). Closely follows the original API otherwise.

-}

module Redland.MidLevel where

import Foreign
import Foreign.C
import Control.Exception
import Control.Monad

import Redland.LowLevel


-- * Exceptions

data RedlandException = InitializationException
                      -- ^ Happens when an initializer returns NULL.
                      -- Gets thrown by 'initialize'.
                      | OperationException Int
                      -- ^ Happens on non-zero return value where a
                      -- zero is expected. Gets thrown by 'perform'.
                      | StringOperationException
                      -- ^ Gets thrown by 'justCString' and
                      -- 'justSharedCString'.
                      | ParseFailure
    deriving Show

instance Exception RedlandException


-- * FFI utility functions

-- todo: move these into a separate module?

type Initializer a = IO (ForeignPtr a)

-- | Initializes a Redland object, throws 'InitializationException' on
-- failure (i.e., if NULL is returned).
initialize :: IO (Ptr a) -> FinalizerPtr a -> Initializer a
initialize i f = do
  p <- i
  if p == nullPtr
    then throw InitializationException
    else newForeignPtr f p

-- | Performs an operation, throws 'OperationException' on failure
-- (i.e., on non-zero return value).
perform :: IO CInt -> IO ()
perform a = do
  r <- fromIntegral <$> a
  if r == 0
    then pure ()
    else throw (OperationException r)

-- | Initializes a Redland object, performs an action over it, makes
-- sure to call 'finalizeForeignPtr' afterwards.
withNew :: Initializer a -> (ForeignPtr a -> IO b) -> IO b
withNew i = bracket i finalizeForeignPtr

-- | An abstraction to use with 'withCString' and 'withForeignPtr'.
withNullablePtr :: (a -> (Ptr b -> c) -> c) -> Maybe a -> (Ptr b -> c) -> c
withNullablePtr _ Nothing a = a nullPtr
withNullablePtr f (Just x) a = f x a

-- | Checks whether a 'CString' is NULL, frees if it not, returns a
-- Haskell 'String'.
maybeCString :: CString -> IO (Maybe String)
maybeCString cStr = do
  r <- maybeSharedCString cStr
  free cStr
  pure r

maybeSharedCString :: CString -> IO (Maybe String)
maybeSharedCString cStr
  | cStr == nullPtr = pure Nothing
  | otherwise = Just <$> peekCString cStr

-- | Like 'maybeCString', but requires a string to be there, and
-- throws 'StringOperationException' if it isn't.
justCString :: CString -> IO String
justCString cStr = do
  r <- justSharedCString cStr
  free cStr
  pure r

-- | Like 'justCString', but doesn't 'free' the C string.
justSharedCString :: CString -> IO String
justSharedCString cStr
  | cStr == nullPtr = throw StringOperationException
  | otherwise = peekCString cStr


-- * World

-- not calling librdf_world_open
redlandWorld :: Initializer RedlandWorld
redlandWorld = initialize librdf_new_world p_librdf_free_world


-- * Hashes

redlandHash :: ForeignPtr RedlandWorld
            -- ^ world
            -> String
            -- ^ hash factory name (e.g., "memory")
            -> Initializer RedlandHash
redlandHash world name =
  withForeignPtr world $ \world' ->
  withCString name $ \name' ->
  initialize (librdf_new_hash world' name') p_librdf_free_hash

hashPutStrings :: ForeignPtr RedlandHash
               -- ^ hash
               -> String
               -- ^ key
               -> String
               -- ^ value
               -> IO ()
hashPutStrings hash key value =
  withForeignPtr hash $ \hash' ->
  withCString key $ \key' ->
  withCString value $ \value' ->
  perform $ librdf_hash_put_strings hash' key' value'

hashGet :: ForeignPtr RedlandHash
        -- ^ hash
        -> String
        -- ^ key
        -> IO (Maybe String)
hashGet hash key =
  withForeignPtr hash $ \hash' ->
  withCString key $ librdf_hash_get hash' >=> maybeCString

hashGetDel :: ForeignPtr RedlandHash
           -- ^ hash
           -> String
           -- ^ key
           -> IO (Maybe String)
hashGetDel hash key =
  withForeignPtr hash $ \hash' ->
  withCString key $ librdf_hash_get_del hash' >=> maybeCString


-- * RDF Graph (librdf_model)

redlandModel :: ForeignPtr RedlandWorld
             -- ^ world
             -> ForeignPtr RedlandStorage
             -- ^ storage
             -> String
             -- ^ options
             -> Initializer RedlandModel
             -- ^ model
redlandModel world storage opt =
  withForeignPtr world $ \world' ->
  withForeignPtr storage $ \storage' ->
  withCString opt $ \opt' ->
  initialize (librdf_new_model world' storage' opt') p_librdf_free_model

-- | Acts as a 'RedlandQueryResults' initializer.
modelQueryExecute :: ForeignPtr RedlandModel
                  -> ForeignPtr RedlandQuery
                  -> Initializer RedlandQueryResults
modelQueryExecute model query =
  withForeignPtr model $ \model' ->
  withForeignPtr query $ \query' ->
  initialize (librdf_model_query_execute model' query')
  p_librdf_free_query_results

-- | Acts as a 'RedlandStream' initializer.
modelFindStatements :: ForeignPtr RedlandModel
                    -> ForeignPtr RedlandStatement
                    -> Initializer RedlandStream
modelFindStatements model statement =
  withForeignPtr model $ \model' ->
  withForeignPtr statement $ \statement' ->
  initialize (librdf_model_find_statements model' statement')
  p_librdf_free_stream

modelSync :: ForeignPtr RedlandModel -> IO ()
modelSync model =
  withForeignPtr model $ \model' ->
  perform $ librdf_model_sync model'

modelLoad :: ForeignPtr RedlandModel
          -> ForeignPtr RedlandURI
          -- ^ source URI
          -> Maybe String
          -- ^ parser name
          -> Maybe String
          -- ^ MIME type
          -> Maybe (ForeignPtr RedlandURI)
          -- ^ type URI
          -> IO ()
modelLoad model source parser mime tURI =
  withForeignPtr model $ \model' ->
  withForeignPtr source $ \source' ->
  withNullablePtr withCString parser $ \parser' ->
  withNullablePtr withCString mime $ \mime' ->
  withNullablePtr withForeignPtr tURI $ \tURI' ->
  perform $ librdf_model_load model' source' parser' mime' tURI'

-- * RDF term (librdf_node)

redlandNode :: ForeignPtr RedlandWorld -> Initializer RedlandNode
redlandNode world =
  withForeignPtr world $ \world' ->
  initialize (librdf_new_node world') p_librdf_free_node

nodeFromNode :: ForeignPtr RedlandNode -> Initializer RedlandNode
nodeFromNode node =
  withForeignPtr node $ \node' ->
  initialize (librdf_new_node_from_node node') p_librdf_free_node

nodeFromBlankIdentifier :: ForeignPtr RedlandWorld
                        -> Maybe String
                        -> Initializer RedlandNode
nodeFromBlankIdentifier world identifier =
  withForeignPtr world $ \world' ->
  withNullablePtr withCString identifier $ \identifier' ->
  initialize (librdf_new_node_from_blank_identifier world' identifier')
  p_librdf_free_node

nodeFromLiteral :: ForeignPtr RedlandWorld
                -> String
                -> Maybe String
                -> Bool
                -> Initializer RedlandNode
nodeFromLiteral world val xmlLang isXML =
  withForeignPtr world $ \world' ->
  withCString val $ \val' ->
  withNullablePtr withCString xmlLang $ \xmlLang' ->
  let isXML' = if isXML then 1 else 0 in
    initialize (librdf_new_node_from_literal world' val' xmlLang' isXML')
    p_librdf_free_node

nodeFromTypedLiteral :: ForeignPtr RedlandWorld
                     -> String
                     -> Maybe String
                     -> Maybe (ForeignPtr RedlandURI)
                     -> Initializer RedlandNode
nodeFromTypedLiteral world val xmlLang litType =
  withForeignPtr world $ \world' ->
  withCString val $ \val' ->
  withNullablePtr withCString xmlLang $ \xmlLang' ->
  withNullablePtr withForeignPtr litType $ \litType' ->
  initialize (librdf_new_node_from_typed_literal world' val' xmlLang' litType')
  p_librdf_free_node

nodeFromURI :: ForeignPtr RedlandWorld
            -> ForeignPtr RedlandURI
            -> Initializer RedlandNode
nodeFromURI world uri =
  withForeignPtr world $ \world' ->
  withForeignPtr uri $ \uri' ->
  initialize (librdf_new_node_from_uri world' uri')
  p_librdf_free_node

nodeFromURIString :: ForeignPtr RedlandWorld
                  -> String
                  -> Initializer RedlandNode
nodeFromURIString world uriStr =
  withForeignPtr world $ \world' ->
  withCString uriStr $ \uriStr' ->
  initialize (librdf_new_node_from_uri_string world' uriStr')
  p_librdf_free_node

nodeGetBlankIdentifier :: ForeignPtr RedlandNode -> IO String
nodeGetBlankIdentifier node =
  withForeignPtr node $
  librdf_node_get_blank_identifier >=> justSharedCString

nodeGetLiteralValue :: ForeignPtr RedlandNode -> IO String
nodeGetLiteralValue node =
  withForeignPtr node $
  librdf_node_get_literal_value >=> justSharedCString

nodeGetURI :: ForeignPtr RedlandNode -> Initializer RedlandURI
nodeGetURI node =
  withForeignPtr node $ \node' -> do
  oldURI <- librdf_node_get_uri node'
  initialize (librdf_new_uri_from_uri oldURI) p_librdf_free_uri

nodeIsBlank :: ForeignPtr RedlandNode -> IO Bool
nodeIsBlank node =
  withForeignPtr node $ fmap (/= 0) . librdf_node_is_blank

nodeIsLiteral :: ForeignPtr RedlandNode -> IO Bool
nodeIsLiteral node =
  withForeignPtr node $ fmap (/= 0) . librdf_node_is_literal

nodeIsResource :: ForeignPtr RedlandNode -> IO Bool
nodeIsResource node =
  withForeignPtr node $ fmap (/= 0) . librdf_node_is_resource

nodeToString :: ForeignPtr RedlandNode -> IO String
nodeToString node =
  withForeignPtr node $ librdf_node_to_string >=> justCString


-- * Parsers

redlandParser :: ForeignPtr RedlandWorld
              -- ^ world
              -> Maybe String
              -- ^ parser name (e.g., "rdfxml", "turtle")
              -> Maybe String
              -- ^ MIME type
              -> Maybe (ForeignPtr RedlandURI)
              -- ^ type URI
              -> Initializer RedlandParser
redlandParser world name mimeType uri =
  withForeignPtr world $ \world' ->
  withNullablePtr withCString name $ \name' ->
  withNullablePtr withCString mimeType $ \mimeType' ->
  withNullablePtr withForeignPtr uri $ \uri' ->
  initialize (librdf_new_parser world' name' mimeType' uri')
  p_librdf_free_parser

parseStringIntoModel :: ForeignPtr RedlandParser
                     -- ^ parser
                     -> String
                     -- ^ string to parse
                     -> ForeignPtr RedlandURI
                     -- ^ base URI
                     -> ForeignPtr RedlandModel
                     -- ^ model
                     -> IO ()
parseStringIntoModel parser str uri model =
  withForeignPtr parser $ \parser' ->
  withCString str $ \str' ->
  withForeignPtr uri $ \uri' ->
  withForeignPtr model $ \model' ->
  perform $ librdf_parser_parse_string_into_model parser' str' uri' model'

parserGuessName2 :: ForeignPtr RedlandWorld
                 -> Maybe String
                 -- ^ MIME type
                 -> Maybe String
                 -- ^ content
                 -> Maybe String
                 -- ^ content identifier
                 -> IO (Maybe String)
parserGuessName2 world mime content contentId =
  withForeignPtr world $ \world' ->
  withNullablePtr withCString mime $ \mime' ->
  withNullablePtr withCString content $ \content' ->
  withNullablePtr withCString contentId $
  librdf_parser_guess_name2 world' mime' content' >=> maybeSharedCString


-- * Querying

redlandQuery :: ForeignPtr RedlandWorld
             -> String
             -- ^ language name
             -> Maybe (ForeignPtr RedlandURI)
             -- ^ language URI
             -> String
             -- ^ query string
             -> Maybe (ForeignPtr RedlandURI)
             -- ^ base URI
             -> Initializer RedlandQuery
redlandQuery world lang langURI query baseURI =
  withForeignPtr world $ \world' ->
  withCString lang $ \lang' ->
  withNullablePtr withForeignPtr langURI $ \langURI' ->
  withCString query $ \query' ->
  withNullablePtr withForeignPtr baseURI $ \baseURI' ->
  initialize (librdf_new_query world' lang' langURI' query' baseURI')
  p_librdf_free_query


-- * Query results

queryResultsGetCount :: ForeignPtr RedlandQueryResults -> IO Int
queryResultsGetCount results =
  withForeignPtr results $ fmap fromIntegral . librdf_query_results_get_count

queryResultsNext :: ForeignPtr RedlandQueryResults
                 -> IO Bool
                 -- ^ 'True' if it's not finished.
queryResultsNext results =
  withForeignPtr results $ fmap (== 0) . librdf_query_results_next

queryResultsFinished :: ForeignPtr RedlandQueryResults -> IO Bool
queryResultsFinished results =
  withForeignPtr results $ fmap (/= 0) . librdf_query_results_finished

-- | Acts as a 'RedlandNode' initializer.
queryResultsGetBindingValue :: ForeignPtr RedlandQueryResults
                            -> Int
                            -> Initializer RedlandNode
queryResultsGetBindingValue results offset =
  withForeignPtr results $ \results' ->
  initialize
  (librdf_query_results_get_binding_value results' (fromIntegral offset))
  p_librdf_free_node

queryResultsGetBindingName :: ForeignPtr RedlandQueryResults
                           -> Int
                           -> IO String
queryResultsGetBindingName results offset =
  withForeignPtr results $ \results' ->
  librdf_query_results_get_binding_name results' (fromIntegral offset)
  >>= justSharedCString

-- | Acts as a 'RedlandNode' initializer.
queryResultsGetBindingValueByName :: ForeignPtr RedlandQueryResults
                                  -> String
                                  -> Initializer RedlandNode
queryResultsGetBindingValueByName results name =
  withForeignPtr results $ \results' ->
  withCString name $ \name' ->
  initialize
  (librdf_query_results_get_binding_value_by_name results' name')
  p_librdf_free_node

queryResultsGetBindingsCount :: ForeignPtr RedlandQueryResults
                             -> IO Int
queryResultsGetBindingsCount results =
  withForeignPtr results $
  fmap fromIntegral . librdf_query_results_get_bindings_count

-- | Acts as a 'RedlandQueryResults' initializer.
queryExecute :: ForeignPtr RedlandQuery
             -> ForeignPtr RedlandModel
             -> Initializer RedlandQueryResults
queryExecute query model =
  withForeignPtr query $ \query' ->
  withForeignPtr model $ \model' ->
  initialize (librdf_query_execute query' model') p_librdf_free_query_results


-- * RDF Triple (librdf_statement)

redlandStatement :: ForeignPtr RedlandWorld -> Initializer RedlandStatement
redlandStatement world =
  withForeignPtr world $ \world' ->
  initialize (librdf_new_statement world') p_librdf_free_statement

statementFromNodes :: ForeignPtr RedlandWorld
                   -> Maybe (ForeignPtr RedlandNode)
                   -- ^ subject
                   -> Maybe (ForeignPtr RedlandNode)
                   -- ^ predicate
                   -> Maybe (ForeignPtr RedlandNode)
                   -- ^ object
                   -> Initializer RedlandStatement
statementFromNodes world subject predicate object =
  withForeignPtr world $ \world' ->
  withNullablePtr withForeignPtr subject $ \subject' ->
  withNullablePtr withForeignPtr predicate $ \predicate' ->
  withNullablePtr withForeignPtr object $ \object' -> do
  sCopy <- librdf_new_node_from_node subject'
  pCopy <- librdf_new_node_from_node predicate'
  oCopy <- librdf_new_node_from_node object'
  initialize
    (librdf_new_statement_from_nodes world' sCopy pCopy oCopy)
    p_librdf_free_statement

-- | An abstraction used for getting statement components.
statementGet :: (Ptr RedlandStatement -> IO (Ptr RedlandNode))
             -> ForeignPtr RedlandStatement
             -> IO (Maybe (ForeignPtr RedlandNode))
statementGet f statement =
  withForeignPtr statement $ \statement' -> do
  oldNode <- f statement'
  if oldNode == nullPtr
    then pure Nothing
    else Just <$>
         initialize (librdf_new_node_from_node oldNode) p_librdf_free_node

-- | An abstraction used for setting statement components.
statementSet :: (Ptr RedlandStatement -> Ptr RedlandNode -> IO ())
             -> ForeignPtr RedlandStatement
             -> Maybe (ForeignPtr RedlandNode)
             -> IO ()
statementSet f statement node =
  withForeignPtr statement $ \statement' ->
  case node of
    Nothing -> f statement' nullPtr
    Just node' -> withForeignPtr node' $ \node'' -> do
      nodeCopy <- librdf_new_node_from_node node''
      f statement' nodeCopy

statementGetSubject :: ForeignPtr RedlandStatement
                    -> IO (Maybe (ForeignPtr RedlandNode))
statementGetSubject = statementGet librdf_statement_get_subject

statementSetSubject :: ForeignPtr RedlandStatement
                    -> Maybe (ForeignPtr RedlandNode)
                    -> IO ()
statementSetSubject = statementSet librdf_statement_set_subject

statementGetPredicate :: ForeignPtr RedlandStatement
                      -> IO (Maybe (ForeignPtr RedlandNode))
statementGetPredicate = statementGet librdf_statement_get_predicate

statementSetPredicate :: ForeignPtr RedlandStatement
                      -> Maybe (ForeignPtr RedlandNode)
                      -> IO ()
statementSetPredicate = statementSet librdf_statement_set_predicate

statementGetObject :: ForeignPtr RedlandStatement
                   -> IO (Maybe (ForeignPtr RedlandNode))
statementGetObject = statementGet librdf_statement_get_object

statementSetObject :: ForeignPtr RedlandStatement
                   -> Maybe (ForeignPtr RedlandNode)
                   -> IO ()
statementSetObject = statementSet librdf_statement_set_object


-- * Triple stores

redlandStorage :: ForeignPtr RedlandWorld
               -- ^ world
               -> String
               -- ^ storage type name ("hashes", "memory", "file",
               -- etc)
               -> String
               -- ^ storage identifier
               -> String
               -- ^ options
               -> Initializer RedlandStorage
redlandStorage world sname name opt =
  withForeignPtr world $ \world' ->
  withCString sname $ \sname' ->
  withCString name $ \name' ->
  withCString opt $ \opt' ->
  initialize (librdf_new_storage world' sname' name' opt')
  p_librdf_free_storage

redlandStorageWithOptions :: ForeignPtr RedlandWorld
                          -- ^ world
                          -> String
                          -- ^ storage type name ("hashes", "memory",
                          -- "file", etc)
                          -> String
                          -- ^ storage identifier
                          -> ForeignPtr RedlandHash
                          -- ^ options
                          -> Initializer RedlandStorage
redlandStorageWithOptions world sname name opt =
  withForeignPtr world $ \world' ->
  withCString sname $ \sname' ->
  withCString name $ \name' ->
  withForeignPtr opt $ \opt' ->
  initialize (librdf_new_storage_with_options world' sname' name' opt')
  p_librdf_free_storage


-- * Stream of triples (librdf_statement)

streamEnd :: ForeignPtr RedlandStream -> IO Bool
streamEnd stream =
  withForeignPtr stream $ fmap (/= 0) . librdf_stream_end

streamNext :: ForeignPtr RedlandStream
           -> IO Bool
           -- ^ 'True' if it's not finished.
streamNext stream =
  withForeignPtr stream $ fmap (== 0) . librdf_stream_next

streamGetObject :: ForeignPtr RedlandStream
                -> Initializer RedlandStatement
streamGetObject stream =
  withForeignPtr stream $ \stream' -> do
  statement <- librdf_stream_get_object stream'
  initialize (librdf_new_statement_from_statement statement)
    p_librdf_free_statement


-- * URI

redlandURI :: ForeignPtr RedlandWorld
           -- ^ world
           -> String
           -- ^ URI string
           -> Initializer RedlandURI
redlandURI world uriStr =
  withForeignPtr world $ \world' ->
  withCString uriStr $ \uriStr' ->
  initialize (librdf_new_uri world' uriStr') p_librdf_free_uri

uriFromURI :: ForeignPtr RedlandURI -> Initializer RedlandURI
uriFromURI uri =
  withForeignPtr uri $ \uri' ->
  initialize (librdf_new_uri_from_uri uri') p_librdf_free_uri

uriAsString :: ForeignPtr RedlandURI -> IO String
uriAsString uri =
  withForeignPtr uri $ librdf_uri_as_string >=> justSharedCString