]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.db/scl/Simantics/DB.scl
(refs #7807) Fixed bad signature of virtualSyncWriteXXX
[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 importJava "org.simantics.db.layer0.util.ExtendedUris" where
156     "Converts an absolute URI to a resource"
157     @JavaName resolveAbsoluteUri    
158     resource :: String -> <ReadGraph> Resource
159     
160     "Converts a relative URI to a resource starting from the given resource"
161     @JavaName resolveRelativeUri
162     relativeResource :: Resource -> String -> <ReadGraph> Resource
163
164 "Reads the value of a literal that is an object with the given subject and predicate"
165 @inline
166 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
167 relatedValue s p = relatedValue_ s p binding
168
169 @inline
170 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
171 possibleRelatedValue s p = possibleRelatedValue_ s p binding
172
173 class Browsable a where
174     fromUri :: String -> <ReadGraph> a
175
176     "Returns the URI of the given value."
177     uriOf :: a -> <ReadGraph> String
178     
179     "Returns the URI of the given value if it exists or Nothing."
180     possibleUriOf :: a -> <ReadGraph> Maybe String
181     
182     "Reads the name of the value."
183     nameOf :: a -> <ReadGraph> String
184     possibleNameOf :: a -> <ReadGraph> Maybe String
185     
186     valueOf :: Serializable v => a -> <ReadGraph> v
187     
188     variantValueOf :: a -> <ReadGraph> Variant 
189     
190     children :: a -> <ReadGraph> [a]
191     parent :: a -> <ReadGraph> a
192     possibleParent :: a -> <ReadGraph> Maybe a
193     
194     child :: a -> String -> <ReadGraph> a
195     possibleChild :: a -> String -> <ReadGraph> Maybe a
196     
197 instance Browsable Resource where
198     fromUri = resource
199     uriOf = uriOfResource
200     possibleUriOf = possibleUriOfResource
201     nameOf r = relatedValue r L0.HasName
202     possibleNameOf r = possibleRelatedValue r L0.HasName
203     @inline
204     valueOf r = valueOf_ r binding
205     variantValueOf = variantValueOf_
206     
207     children r = r # L0.ConsistsOf
208     parent r = singleObject r L0.PartOf
209     possibleParent r = possibleObject r L0.PartOf
210     
211     possibleChild = possibleResourceChild
212     child r n = fromJust (possibleResourceChild r n)
213
214 importJava "org.simantics.db.WriteOnlyGraph" where
215     markUndoPoint :: () -> <WriteGraph> ()
216
217 importJava "org.simantics.db.WriteGraph" where
218     "Creates a new resource."
219     newResource :: () -> <WriteGraph> Resource
220     "Adds a statement to the semantic graph."
221     claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
222     @JavaName claim
223     claimWithInverse :: Resource -> Resource -> Maybe Resource -> Resource -> <WriteGraph> ()
224     @JavaName claimLiteral
225     claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
226     @JavaName claimLiteral
227     untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
228     @JavaName claimValue
229     untypedClaimValue :: Resource -> a -> <WriteGraph> ()
230     @JavaName claimLiteral
231     claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
232     "Removes a statement with the given subject, predicate and object"
233     deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
234     @JavaName deny
235     denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
236     @JavaName deny
237     denyAllStatements :: Resource -> <WriteGraph> ()
238     newClusterSet :: Resource -> <WriteGraph> ()
239     @JavaName denyValue
240     denyValue :: Resource -> <WriteGraph> ()
241
242 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
243 claimAssertion type_ predicate object = do
244     ass = newResource ()
245     claim ass L0.HasPredicate predicate
246     claim ass L0.HasObject object
247     claim type_ L0.Asserts ass
248
249 "Sets the value of the literal that is an object with the given subject and predicate."
250 @inline
251 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
252 claimRelatedValue s p v = claimRelatedValue_ s p v binding
253
254 @inline
255 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
256 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
257
258 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
259     @JavaName getPossibleChild
260     possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
261
262 @deprecated "Use function children instead."
263 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
264 resourceChildrenOf r = r # L0.ConsistsOf
265
266 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
267     @JavaName add
268     addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
269     
270     @JavaName set
271     setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
272     
273     @JavaName getSingleOwnerList
274     parentOrderedSet :: Resource -> <ReadGraph> Resource
275     
276     @JavaName toList
277     elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
278     
279 importJava "org.simantics.db.common.utils.ListUtils" where
280     @JavaName toList
281     elementsOfList :: Resource -> <ReadGraph> [Resource]
282     
283     @JavaName create
284     createList :: [Resource] -> <WriteGraph> Resource
285
286     @JavaName create
287     createListWithType :: Resource -> [Resource] -> <WriteGraph> Resource
288     
289     @javaName insertBack
290     insertBack :: Resource -> [Resource] -> <WriteGraph> ()
291     
292     @javaName removeElement
293     removeElement :: Resource -> Resource -> <WriteGraph> Boolean
294     
295     @javaName swapWithPrevious
296     swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
297
298     @javaName swapWithNext
299     swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
300
301
302 importJava "org.simantics.db.common.utils.CommonDBUtils" where
303     isParent :: Resource -> Resource -> <ReadGraph> Boolean
304     possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
305     possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
306     objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
307     possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
308
309 importJava "org.simantics.db.common.utils.NameUtils" where
310     findFreshName :: String -> Resource -> <ReadGraph> String
311     findFreshEscapedName :: String -> Resource -> <ReadGraph> String
312
313 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
314 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
315 subject # predicate = collectionToList $ objects_ subject predicate
316
317 "statements subject predicate` returns all statements with the given subject and predicate."
318 statements :: Resource -> Resource -> <ReadGraph> [Statement]
319 statements subject predicate = collectionToList $ statements_ subject predicate
320
321 importJava "org.simantics.scl.db.SCLFunctions" where
322     "Executes a read transaction and waits that it completes."
323     syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
324     "Executes a write transaction and waits that it completes."
325     syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
326     "Executes a delayed write transaction and waits that it completes."
327     delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
328     
329     "Begins a read transaction and immediately returns."
330     asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
331     "Begins a write transaction and immediately returns."
332     asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
333     
334     virtualSyncWriteMem :: String -> (() -> <e,WriteGraph> a) -> <e,WriteGraph> a
335     virtualSyncWriteWS :: String -> (() -> <e,WriteGraph> a) -> <e,WriteGraph> a
336     
337     safeExec :: (() -> <Proc> a) -> <Proc> a
338     activateOnce :: Resource -> <Proc> ()
339     syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
340     resourceFromId :: Long -> <ReadGraph> Resource
341     enableDependencies :: () -> <WriteGraph> ()
342     disableDependencies :: () -> <WriteGraph> ()
343     
344     unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
345     unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
346
347     "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
348     subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
349     "Makes a new read request with given procedure for calculating the result. The request is always cached."
350     subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
351     "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."
352     subqueryL :: (<ReadGraph,Proc> a) -> (a -> <ReadGraph, e> ()) -> (Throwable -> <ReadGraph, e> ()) -> (<e> Boolean) -> <ReadGraph, Proc, e> ()
353     "Tries to convert the given Dynamic value to a value with the inferred type"
354     possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
355
356 importJava "org.simantics.db.layer0.util.Layer0Utils" where
357     undo :: () -> <Proc> String
358     undoOperations :: Integer -> <Proc> String
359     redo :: () -> <Proc> String
360     queryDebugSupport :: String -> <Proc> String
361     queryListSupport :: String -> <Proc> String
362     addCommentMetadata :: String -> <WriteGraph> ()
363     sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
364     makeSynchronous :: Boolean -> <ReadGraph> ()
365     listOntologies :: () -> <ReadGraph> [Resource]
366     emptyTrashBin :: () -> <Proc> ()
367     purgeDatabase :: () -> <Proc> ()
368     prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
369
370     @private
371     @JavaName copyTo
372     copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
373     @JavaName getPossiblePredicateByName
374     possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
375     @JavaName getPossiblePredicateByNameFromType
376     possiblePredicateByNameFromType :: Resource -> String -> <ReadGraph> Maybe Resource
377     
378     addMetadataListener :: ChangeListener -> <Proc> ()
379     removeMetadataListener :: ChangeListener -> <Proc> ()
380
381 copyTo :: Resource -> Resource -> <WriteGraph> Resource
382 copyTo targetContainer source = do
383     (collectionToList $ copyTo_ targetContainer source)!0
384
385 importJava "org.simantics.db.common.utils.CommonDBUtils" where
386     selectClusterSet :: Resource -> <WriteGraph> () 
387
388 importJava "org.simantics.Simantics" where
389     @JavaName getProjectResource
390     currentProject :: () -> /*<Proc>*/ Resource
391
392 // Move to somewhere more generic module
393 importJava "java.util.Collection" where
394     data Collection a
395
396 importJava "java.util.ArrayList" where
397     @JavaName "<init>"
398     collectionToList :: Collection a -> [a]
399 /*@macro
400 collectionToList :: Collection a -> [a]
401 collectionToList x = Java.unsafeCoerce x
402 */ 
403
404 resourceToCollection :: a -> Collection a
405 resourceToCollection x = Java.unsafeCoerce x
406
407 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
408 newEntity entitySpecs = do
409     entity = newResource ()
410     for entitySpecs (\spec -> spec entity)
411     entity
412
413 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
414 updateEntity entity entitySpecs = do
415     for entitySpecs (\spec -> spec entity)
416     entity
417
418 hasStatement predicate object entity = 
419     claim entity predicate object
420 hasProperty relation value entity = 
421     claimRelatedValue entity relation value
422 hasPossibleProperty relation value entity =
423     match value with
424       Just v -> claimRelatedValue entity relation v
425       Nothing -> ()
426 hasTypedProperty relation value t entity = 
427     claimRelatedValueWithType entity relation t value        
428 hasName (n :: String) = hasProperty L0.HasName n
429 hasLabel (l :: String) = hasProperty L0.HasLabel l
430 hasType t = hasStatement L0.InstanceOf t
431 hasParent p = hasStatement L0.PartOf p
432
433 importJava "org.simantics.databoard.util.URIStringUtils" where
434     @JavaName splitURISCL
435     splitURI :: String -> [String]
436     @JavaName escape
437     escapeURI :: String -> String
438     @JavaName unescape
439     unescapeURI :: String -> String
440
441 @private
442 importJava "org.simantics.db.layer0.request.ActiveModels" where
443     @JavaName getPossibleActiveModel
444     activeModel :: Resource -> <ReadGraph> Maybe Resource
445
446 "Gives the current active model."
447 currentModel :: <ReadGraph> Resource
448 currentModel = match activeModel (currentProject ()) with
449     Just model -> model
450     Nothing -> fail "No active model."
451
452 "Gives the current active model if it exists or Nothing otherwise."
453 possibleCurrentModel :: <ReadGraph> Maybe Resource
454 possibleCurrentModel = activeModel (currentProject ())
455     
456 startUndoPoint :: String -> <WriteGraph> ()
457 startUndoPoint string = do
458     markUndoPoint ()
459     addCommentMetadata (string)
460     ()
461
462 @inline
463 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
464 lift1Read f x = syncRead (\_ -> f x)
465
466 @inline
467 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
468 lift2Read f x y = syncRead (\_ -> f x y)
469
470 @inline
471 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
472 lift3Read f x y z = syncRead (\_ -> f x y z)
473
474 @inline
475 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
476 lift1Write f x = syncWrite (\_ -> f x)
477
478 @inline
479 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
480 lift2Write f x y = syncWrite (\_ -> f x y)
481
482 @inline
483 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
484 lift3Write f x y z = syncWrite (\_ -> f x y z)
485
486 """
487 Returns a child Browsable of the specified parent that has that is the child
488 of the specified parent with the specified name path.
489 """
490 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
491 childWithPath parent path = 
492     foldl (\r name -> match possibleChild r name with
493               Just c -> c
494               Nothing -> fail ("Didn't find " + name + ".") 
495           ) 
496           parent path
497
498 """
499 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
500 was found.
501 """
502 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
503 possibleChildWithPath parent path = 
504     foldl (\r name -> match r with
505               Just c -> possibleChild c name
506               Nothing -> Nothing 
507           ) 
508           (Just parent) path
509
510 importJava "org.simantics.db.MetadataI" where
511     data MetadataI
512
513 importJava "org.simantics.db.event.ChangeListener" where
514     data ChangeListener
515
516 importJava "org.simantics.db.layer0.genericrelation.DependencyChanges" where
517     data DependencyChanges