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