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