]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/scl/Simantics/DB.scl
dca263460991714e2e038a57da9fc1ead8250b9b
[simantics/platform.git] / bundles / org.simantics.scl.db / scl / Simantics / DB.scl
1 import "JavaBuiltin" as Java
2 include "http://www.simantics.org/Layer0-1.1" as L0
3 import "Map" as Map
4
5 infixl 5 (#)
6  
7 effect ReadGraph
8     "graph"
9     "org.simantics.db.ReadGraph"
10
11 effect WriteGraph
12     "graph"
13     "org.simantics.db.WriteGraph"
14     
15 importJava "org.simantics.db.Resource" where
16     "A resource is a node in a semantic graph."
17     data Resource
18     
19     "Returns the 64-bit unique identifier of the resource."
20     @JavaName getResourceId
21     resourceId :: Resource -> Long
22     
23     isPersistent :: Resource -> Boolean
24     
25 importJava "org.simantics.db.ReadGraph" where
26     data ReadGraphX
27     
28 instance Ord Resource where
29     compare a b = compare (resourceId a) (resourceId b)
30   
31 instance Show Resource where
32     show r = "#" + show (resourceId r)
33
34 instance Show Statement where
35     show s = "#" + show (resourceId ( subjectOf s)) + "#" + show (resourceId ( predicateOf s)) + "#" + show (resourceId ( objectOf s))
36
37 importJava "org.simantics.db.Statement" where
38     "A statement is an edge in a semantic graph."
39     data Statement
40     
41     @JavaName getSubject
42     subjectOf :: Statement -> Resource
43     @JavaName getPredicate
44     predicateOf :: Statement -> Resource
45     @JavaName getObject
46     objectOf :: Statement -> Resource
47     
48     isAsserted :: Statement -> Resource -> <ReadGraph> Boolean
49     
50 importJava "org.simantics.db.ReadGraph" where
51     "Converts an absolute URI to a resource or returns `Nothing` if there is no such resource."
52     @JavaName getPossibleResource
53     possibleResource :: String -> <ReadGraph> (Maybe Resource)
54     
55     getDataType :: Resource -> <ReadGraph> Datatype
56
57     @JavaName getSupertypes
58     superTypesOf :: Resource -> <ReadGraph> Set.T Resource
59     
60     @private
61     @JavaName getURI
62     uriOfResource :: Resource -> <ReadGraph> String
63
64     @private
65     @JavaName getPossibleURI
66     possibleUriOfResource :: Resource -> <ReadGraph> Maybe String
67     
68     @JavaName getObjects
69     objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
70     
71     @JavaName getStatements
72     statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
73     
74     @JavaName hasStatement
75     existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
76     
77     @JavaName hasStatement
78     existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
79
80     "Assumes that there is exactly one object with the given subject and predicate and returns it."
81     @JavaName getSingleObject
82     singleObject :: Resource -> Resource -> <ReadGraph> Resource
83
84     """
85     If there is exactly one object with the given `subject` and `predicate`,
86     `possibleObject subject predicate` returns it. Otherwise, it returns
87     `Nothing`.
88     """
89     @JavaName getPossibleObject
90     possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
91
92     "Assumes that there is exactly one statement with the given subject and predicate and returns it."
93     @JavaName getSingleStatement
94     singleStatement :: Resource -> Resource -> <ReadGraph> Statement
95
96     @JavaName getPossibleStatement
97     possibleStatement :: Resource -> Resource -> <ReadGraph> Maybe Statement
98
99     @JavaName getRelatedVariantValue
100     relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
101
102     @JavaName getRelatedValue
103     relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
104     
105     @JavaName getRelatedValue
106     untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
107
108     @JavaName getRelatedValue2
109     relatedValue2 :: Resource -> Resource -> <ReadGraph> a
110
111     @JavaName getRelatedVariantValue2
112     relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
113     
114     @JavaName getPossibleRelatedValue
115     possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
116     
117     @JavaName getPossibleRelatedValue
118     untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
119     
120     @private
121     @JavaName getValue
122     valueOf_ :: Resource -> Binding a -> <ReadGraph> a
123     
124     @private
125     @JavaName getVariantValue
126     variantValueOf_ :: Resource -> <ReadGraph> Variant
127     
128     @JavaName getValue
129     untypedValueOf :: Resource -> <ReadGraph> Dynamic
130     
131     @JavaName getPossibleValue
132     untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
133     
134     @JavaName getInverse
135     inverseOf :: Resource -> <ReadGraph> Resource
136     @JavaName getPossibleInverse
137     possibleInverseOf :: Resource -> <ReadGraph> Maybe Resource
138     
139     @JavaName getSingleType
140     singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
141     
142     @JavaName getPossibleType
143     possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
144     
145     "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
146     isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
147     isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
148     isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
149     
150     getRootLibrary :: () -> <ReadGraph> Resource
151     
152     @JavaName getPredicates
153     predicatesOf :: Resource -> <ReadGraph> [Resource]
154     
155     isImmutable :: Resource -> <ReadGraph> Boolean
156
157 importJava "org.simantics.db.layer0.util.ExtendedUris" where
158     "Converts an absolute URI to a resource"
159     @JavaName resolveAbsoluteUri    
160     resource :: String -> <ReadGraph> Resource
161     
162     "Converts a relative URI to a resource starting from the given resource"
163     @JavaName resolveRelativeUri
164     relativeResource :: Resource -> String -> <ReadGraph> Resource
165
166 "Reads the value of a literal that is an object with the given subject and predicate"
167 @inline
168 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
169 relatedValue s p = relatedValue_ s p binding
170
171 @inline
172 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
173 possibleRelatedValue s p = possibleRelatedValue_ s p binding
174
175 class Browsable a where
176     fromUri :: String -> <ReadGraph> a
177
178     "Returns the URI of the given value."
179     uriOf :: a -> <ReadGraph> String
180     
181     "Returns the URI of the given value if it exists or Nothing."
182     possibleUriOf :: a -> <ReadGraph> Maybe String
183     
184     "Reads the name of the value."
185     nameOf :: a -> <ReadGraph> String
186     possibleNameOf :: a -> <ReadGraph> Maybe String
187     
188     valueOf :: Serializable v => a -> <ReadGraph> v
189     
190     variantValueOf :: a -> <ReadGraph> Variant 
191     
192     children :: a -> <ReadGraph> [a]
193     parent :: a -> <ReadGraph> a
194     possibleParent :: a -> <ReadGraph> Maybe a
195     
196     child :: a -> String -> <ReadGraph> a
197     possibleChild :: a -> String -> <ReadGraph> Maybe a
198     
199 instance Browsable Resource where
200     fromUri = resource
201     uriOf = uriOfResource
202     possibleUriOf = possibleUriOfResource
203     nameOf r = relatedValue r L0.HasName
204     possibleNameOf r = possibleRelatedValue r L0.HasName
205     @inline
206     valueOf r = valueOf_ r binding
207     variantValueOf = variantValueOf_
208     
209     children r = r # L0.ConsistsOf
210     parent r = singleObject r L0.PartOf
211     possibleParent r = possibleObject r L0.PartOf
212     
213     possibleChild = possibleResourceChild
214     child r n = fromJust (possibleResourceChild r n)
215
216 importJava "org.simantics.db.WriteOnlyGraph" where
217     markUndoPoint :: () -> <WriteGraph> ()
218
219 importJava "org.simantics.db.WriteGraph" where
220     "Creates a new resource."
221     newResource :: () -> <WriteGraph> Resource
222     "Adds a statement to the semantic graph."
223     claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
224     @JavaName claim
225     claimWithInverse :: Resource -> Resource -> Maybe Resource -> Resource -> <WriteGraph> ()
226     @JavaName claimLiteral
227     claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
228     @JavaName claimLiteral
229     untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
230     @JavaName claimValue
231     untypedClaimValue :: Resource -> a -> <WriteGraph> ()
232     @JavaName claimLiteral
233     claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
234     "Removes a statement with the given subject, predicate and object"
235     deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
236     @JavaName deny
237     denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
238     @JavaName deny
239     denyAllStatements :: Resource -> <WriteGraph> ()
240     newClusterSet :: Resource -> <WriteGraph> ()
241     @JavaName denyValue
242     denyValue :: Resource -> <WriteGraph> ()
243
244 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
245 claimAssertion type_ predicate object = do
246     ass = newResource ()
247     claim ass L0.HasPredicate predicate
248     claim ass L0.HasObject object
249     claim type_ L0.Asserts ass
250
251 "Sets the value of the literal that is an object with the given subject and predicate."
252 @inline
253 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
254 claimRelatedValue s p v = claimRelatedValue_ s p v binding
255
256 @inline
257 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
258 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
259
260 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
261     @JavaName getPossibleChild
262     possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
263
264 @deprecated "Use function children instead."
265 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
266 resourceChildrenOf r = r # L0.ConsistsOf
267
268 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
269     @JavaName add
270     addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
271     
272     @JavaName set
273     setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
274     
275     @JavaName getSingleOwnerList
276     parentOrderedSet :: Resource -> <ReadGraph> Resource
277     
278     @JavaName toList
279     elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
280     
281 importJava "org.simantics.db.common.utils.ListUtils" where
282     "`elementsOfList l` returns elements of list `l`"
283     @JavaName toList
284     elementsOfList :: Resource -> <ReadGraph> [Resource]
285     
286     "`createList [e]` creates a new list containing elements `[e]`"
287     @JavaName create
288     createList :: [Resource] -> <WriteGraph> Resource
289
290     "`createListWithType t [e]` creates a new list with type `t` containing elements `[e]`"
291     @JavaName create
292     createListWithType :: Resource -> [Resource] -> <WriteGraph> Resource
293     
294     "`insertListBack l [e]` inserts elements `[e]` back to the list `l`"
295     @JavaName insertBack
296     insertListBack :: Resource -> [Resource] -> <WriteGraph> ()
297     
298     "`insertListFront l [e]` inserts elements `[e]` front of the list `l`"
299     @JavaName insertFront
300     insertListFront :: Resource -> [Resource] -> <WriteGraph> ()
301     
302     "`removeListElement l e` removes a element `e` from the list `l`"
303     @JavaName removeElement
304     removeListElement :: Resource -> Resource -> <WriteGraph> Boolean
305     
306     @JavaName swapWithPrevious
307     swapListElementWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
308
309     @JavaName swapWithNext
310     swapListElementWithNext :: Resource -> Resource -> <WriteGraph> Boolean
311
312
313 importJava "org.simantics.db.common.utils.CommonDBUtils" where
314     isParent :: Resource -> Resource -> <ReadGraph> Boolean
315     possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
316     possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
317     objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
318     possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
319
320 importJava "org.simantics.db.common.utils.NameUtils" where
321     findFreshName :: String -> Resource -> <ReadGraph> String
322     findFreshEscapedName :: String -> Resource -> <ReadGraph> String
323
324 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
325 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
326 subject # predicate = collectionToList $ objects_ subject predicate
327
328 "statements subject predicate` returns all statements with the given subject and predicate."
329 statements :: Resource -> Resource -> <ReadGraph> [Statement]
330 statements subject predicate = collectionToList $ statements_ subject predicate
331
332 importJava "org.simantics.scl.db.SCLFunctions" where
333     "Executes a read transaction and waits that it completes."
334     syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
335     "Executes a write transaction and waits that it completes."
336     syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
337     "Executes a delayed write transaction and waits that it completes."
338     delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
339     
340     "Begins a read transaction and immediately returns."
341     asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
342     "Begins a write transaction and immediately returns."
343     asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
344     
345     virtualSyncWriteMem :: String -> (() -> <e,WriteGraph> a) -> <e,WriteGraph> a
346     virtualSyncWriteWS :: String -> (() -> <e,WriteGraph> a) -> <e,WriteGraph> a
347     
348     safeExec :: (() -> <Proc> a) -> <Proc> a
349     activateOnce :: Resource -> <Proc> ()
350     syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
351     resourceFromId :: Long -> <ReadGraph> Resource
352     enableDependencies :: () -> <WriteGraph> ()
353     disableDependencies :: () -> <WriteGraph> ()
354     
355     unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
356     unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
357
358     "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
359     subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
360     "Makes a new read request with given procedure for calculating the result. The request is always cached."
361     subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
362     "Makes a new read asynchronous request with function to handle the request result. The last `isDisposed` function parameter is used to determine if the listener is still alive or not."
363     subqueryL :: (<ReadGraph,Proc> a) -> (a -> <ReadGraph, e> ()) -> (Throwable -> <ReadGraph, e> ()) -> (<e> Boolean) -> <ReadGraph, Proc, e> ()
364     "Tries to convert the given Dynamic value to a value with the inferred type"
365     possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
366
367 importJava "org.simantics.db.layer0.util.Layer0Utils" where
368     undo :: () -> <Proc> String
369     undoOperations :: Integer -> <Proc> String
370     redo :: () -> <Proc> String
371     queryDebugSupport :: String -> <Proc> String
372     queryListSupport :: String -> <Proc> String
373     addCommentMetadata :: String -> <WriteGraph> ()
374     sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
375     makeSynchronous :: Boolean -> <ReadGraph> ()
376     listOntologies :: () -> <ReadGraph> [Resource]
377     emptyTrashBin :: () -> <Proc> ()
378     purgeDatabase :: () -> <Proc> ()
379     prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
380
381     @private
382     @JavaName copyTo
383     copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
384     @JavaName getPossiblePredicateByName
385     possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
386     @JavaName getPossiblePredicateByNameFromType
387     possiblePredicateByNameFromType :: Resource -> String -> <ReadGraph> Maybe Resource
388     
389     addMetadataListener :: ChangeListener -> <Proc> ()
390     removeMetadataListener :: ChangeListener -> <Proc> ()
391
392 copyTo :: Resource -> Resource -> <WriteGraph> Resource
393 copyTo targetContainer source = do
394     (collectionToList $ copyTo_ targetContainer source)!0
395
396 importJava "org.simantics.db.common.utils.CommonDBUtils" where
397     selectClusterSet :: Resource -> <WriteGraph> () 
398
399 importJava "org.simantics.Simantics" where
400     @JavaName getProjectResource
401     currentProject :: () -> /*<Proc>*/ Resource
402
403 // Move to somewhere more generic module
404 importJava "java.util.Collection" where
405     data Collection a
406
407 importJava "java.util.ArrayList" where
408     @JavaName "<init>"
409     collectionToList :: Collection a -> [a]
410 /*@macro
411 collectionToList :: Collection a -> [a]
412 collectionToList x = Java.unsafeCoerce x
413 */ 
414
415 resourceToCollection :: a -> Collection a
416 resourceToCollection x = Java.unsafeCoerce x
417
418 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
419 newEntity entitySpecs = do
420     entity = newResource ()
421     for entitySpecs (\spec -> spec entity)
422     entity
423
424 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
425 updateEntity entity entitySpecs = do
426     for entitySpecs (\spec -> spec entity)
427     entity
428
429 hasStatement predicate object entity = 
430     claim entity predicate object
431 hasProperty relation value entity = 
432     claimRelatedValue entity relation value
433 hasPossibleProperty relation value entity =
434     match value with
435       Just v -> claimRelatedValue entity relation v
436       Nothing -> ()
437 hasTypedProperty relation value t entity = 
438     claimRelatedValueWithType entity relation t value        
439 hasName (n :: String) = hasProperty L0.HasName n
440 hasLabel (l :: String) = hasProperty L0.HasLabel l
441 hasType t = hasStatement L0.InstanceOf t
442 hasParent p = hasStatement L0.PartOf p
443
444 importJava "org.simantics.databoard.util.URIStringUtils" where
445     @JavaName splitURISCL
446     splitURI :: String -> [String]
447     @JavaName escape
448     escapeURI :: String -> String
449     @JavaName unescape
450     unescapeURI :: String -> String
451
452 @private
453 importJava "org.simantics.db.layer0.request.ActiveModels" where
454     @JavaName getPossibleActiveModel
455     activeModel :: Resource -> <ReadGraph> Maybe Resource
456
457 "Gives the current active model."
458 currentModel :: <ReadGraph> Resource
459 currentModel = match activeModel (currentProject ()) with
460     Just model -> model
461     Nothing -> fail "No active model."
462
463 "Gives the current active model if it exists or Nothing otherwise."
464 possibleCurrentModel :: <ReadGraph> Maybe Resource
465 possibleCurrentModel = activeModel (currentProject ())
466     
467 startUndoPoint :: String -> <WriteGraph> ()
468 startUndoPoint string = do
469     markUndoPoint ()
470     addCommentMetadata (string)
471     ()
472
473 @inline
474 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
475 lift1Read f x = syncRead (\_ -> f x)
476
477 @inline
478 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
479 lift2Read f x y = syncRead (\_ -> f x y)
480
481 @inline
482 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
483 lift3Read f x y z = syncRead (\_ -> f x y z)
484
485 @inline
486 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
487 lift1Write f x = syncWrite (\_ -> f x)
488
489 @inline
490 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
491 lift2Write f x y = syncWrite (\_ -> f x y)
492
493 @inline
494 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
495 lift3Write f x y z = syncWrite (\_ -> f x y z)
496
497 """
498 Returns a child Browsable of the specified parent that has that is the child
499 of the specified parent with the specified name path.
500 """
501 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
502 childWithPath parent path = 
503     foldl (\r name -> match possibleChild r name with
504               Just c -> c
505               Nothing -> fail ("Didn't find " + name + ".") 
506           ) 
507           parent path
508
509 """
510 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
511 was found.
512 """
513 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
514 possibleChildWithPath parent path = 
515     foldl (\r name -> match r with
516               Just c -> possibleChild c name
517               Nothing -> Nothing 
518           ) 
519           (Just parent) path
520
521 importJava "org.simantics.db.MetadataI" where
522     data MetadataI
523
524 importJava "org.simantics.db.event.ChangeListener" where
525     data ChangeListener
526
527 importJava "org.simantics.db.layer0.genericrelation.DependencyChanges" where
528     data DependencyChanges