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