1 import "JavaBuiltin" as Java
2 include "http://www.simantics.org/Layer0-1.1" as L0
9 "org.simantics.db.ReadGraph"
13 "org.simantics.db.WriteGraph"
15 importJava "org.simantics.db.Resource" where
16 "A resource is a node in a semantic graph."
19 "Returns the 64-bit unique identifier of the resource."
20 @JavaName getResourceId
21 resourceId :: Resource -> Long
23 importJava "org.simantics.db.ReadGraph" where
26 instance Ord Resource where
27 compare a b = compare (resourceId a) (resourceId b)
29 instance Show Resource where
30 show r = "#" + show (resourceId r)
32 instance Show Statement where
33 show s = "#" + show (resourceId ( subjectOf s)) + "#" + show (resourceId ( predicateOf s)) + "#" + show (resourceId ( objectOf s))
35 importJava "org.simantics.db.Statement" where
36 "A statement is an edge in a semantic graph."
40 subjectOf :: Statement -> Resource
41 @JavaName getPredicate
42 predicateOf :: Statement -> Resource
44 objectOf :: Statement -> Resource
46 importJava "org.simantics.db.ReadGraph" where
47 "Converts an absolute URI to a resource or returns `Nothing` if there is no such resource."
48 @JavaName getPossibleResource
49 possibleResource :: String -> <ReadGraph> (Maybe Resource)
51 getDataType :: Resource -> <ReadGraph> Datatype
53 @JavaName getSupertypes
54 superTypesOf :: Resource -> <ReadGraph> Set.T Resource
58 uriOfResource :: Resource -> <ReadGraph> String
61 objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
63 @JavaName getStatements
64 statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
66 @JavaName hasStatement
67 existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
69 @JavaName hasStatement
70 existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
72 "Assumes that there is exactly one object with the given subject and predicate and returns it."
73 @JavaName getSingleObject
74 singleObject :: Resource -> Resource -> <ReadGraph> Resource
77 If there is exactly one object with the given `subject` and `predicate`,
78 `possibleObject subject predicate` returns it. Otherwise, it returns
81 @JavaName getPossibleObject
82 possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
84 "Assumes that there is exactly one statement with the given subject and predicate and returns it."
85 @JavaName getSingleStatement
86 singleStatement :: Resource -> Resource -> <ReadGraph> Statement
88 @JavaName getRelatedVariantValue
89 relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
91 @JavaName getRelatedValue
92 relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
94 @JavaName getRelatedValue
95 untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
97 @JavaName getRelatedValue2
98 relatedValue2 :: Resource -> Resource -> <ReadGraph> a
100 @JavaName getRelatedVariantValue2
101 relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
103 @JavaName getPossibleRelatedValue
104 possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
106 @JavaName getPossibleRelatedValue
107 untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
111 valueOf_ :: Resource -> Binding a -> <ReadGraph> a
114 @JavaName getVariantValue
115 variantValueOf_ :: Resource -> <ReadGraph> Variant
118 untypedValueOf :: Resource -> <ReadGraph> Dynamic
120 @JavaName getPossibleValue
121 untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
124 inverseOf :: Resource -> <ReadGraph> Resource
126 @JavaName getSingleType
127 singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
129 @JavaName getPossibleType
130 possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
132 "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
133 isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
134 isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
135 isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
137 getRootLibrary :: () -> <ReadGraph> Resource
139 importJava "org.simantics.db.layer0.util.ExtendedUris" where
140 "Converts an absolute URI to a resource"
141 @JavaName resolveAbsoluteUri
142 resource :: String -> <ReadGraph> Resource
144 "Converts a relative URI to a resource starting from the given resource"
145 @JavaName resolveRelativeUri
146 relativeResource :: Resource -> String -> <ReadGraph> Resource
148 "Reads the value of a literal that is an object with the given subject and predicate"
150 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
151 relatedValue s p = relatedValue_ s p binding
154 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
155 possibleRelatedValue s p = possibleRelatedValue_ s p binding
157 class Browsable a where
158 fromUri :: String -> <ReadGraph> a
160 "Returns the URI of the given value."
161 uriOf :: a -> <ReadGraph> String
163 "Reads the name of the value."
164 nameOf :: a -> <ReadGraph> String
165 possibleNameOf :: a -> <ReadGraph> Maybe String
167 valueOf :: Serializable v => a -> <ReadGraph> v
169 variantValueOf :: a -> <ReadGraph> Variant
171 children :: a -> <ReadGraph> [a]
172 parent :: a -> <ReadGraph> a
173 possibleParent :: a -> <ReadGraph> Maybe a
175 child :: a -> String -> <ReadGraph> a
176 possibleChild :: a -> String -> <ReadGraph> Maybe a
178 instance Browsable Resource where
180 uriOf = uriOfResource
181 nameOf r = relatedValue r L0.HasName
182 possibleNameOf r = possibleRelatedValue r L0.HasName
184 valueOf r = valueOf_ r binding
185 variantValueOf = variantValueOf_
187 children r = r # L0.ConsistsOf
188 parent r = singleObject r L0.PartOf
189 possibleParent r = possibleObject r L0.PartOf
191 possibleChild = possibleResourceChild
192 child r n = fromJust (possibleResourceChild r n)
194 importJava "org.simantics.db.WriteOnlyGraph" where
195 markUndoPoint :: () -> <WriteGraph> ()
197 importJava "org.simantics.db.WriteGraph" where
198 "Creates a new resource."
199 newResource :: () -> <WriteGraph> Resource
200 "Adds a statement to the semantic graph."
201 claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
202 @JavaName claimLiteral
203 claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
204 @JavaName claimLiteral
205 untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
207 untypedClaimValue :: Resource -> a -> <WriteGraph> ()
208 @JavaName claimLiteral
209 claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
210 "Removes a statement with the given subject, predicate and object"
211 deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
213 denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
215 denyAllStatements :: Resource -> <WriteGraph> ()
216 newClusterSet :: Resource -> <WriteGraph> ()
218 denyValue :: Resource -> <WriteGraph> ()
220 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
221 claimAssertion type_ predicate object = do
223 claim ass L0.HasPredicate predicate
224 claim ass L0.HasObject object
225 claim type_ L0.Asserts ass
227 "Sets the value of the literal that is an object with the given subject and predicate."
229 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
230 claimRelatedValue s p v = claimRelatedValue_ s p v binding
233 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
234 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
236 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
237 @JavaName getPossibleChild
238 possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
240 @deprecated "Use function children instead."
241 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
242 resourceChildrenOf r = r # L0.ConsistsOf
244 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
246 addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
249 setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
251 @JavaName getSingleOwnerList
252 parentOrderedSet :: Resource -> <ReadGraph> Resource
255 elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
257 importJava "org.simantics.db.common.utils.ListUtils" where
259 elementsOfList :: Resource -> <ReadGraph> [Resource]
262 createList :: [Resource] -> <WriteGraph> Resource
265 insertBack :: Resource -> [Resource] -> <WriteGraph> ()
267 @javaName removeElement
268 removeElement :: Resource -> Resource -> <WriteGraph> Boolean
270 @javaName swapWithPrevious
271 swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
273 @javaName swapWithNext
274 swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
277 importJava "org.simantics.db.common.utils.CommonDBUtils" where
278 isParent :: Resource -> Resource -> <ReadGraph> Boolean
279 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
280 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
281 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
282 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
284 importJava "org.simantics.db.common.utils.NameUtils" where
285 findFreshName :: String -> Resource -> <ReadGraph> String
286 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
288 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
289 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
290 subject # predicate = collectionToList $ objects_ subject predicate
292 "statements subject predicate` returns all statements with the given subject and predicate."
293 statements :: Resource -> Resource -> <ReadGraph> [Statement]
294 statements subject predicate = collectionToList $ statements_ subject predicate
296 importJava "org.simantics.scl.db.SCLFunctions" where
297 "Executes a read transaction and waits that it completes."
298 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
299 "Executes a write transaction and waits that it completes."
300 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
301 "Executes a delayed write transaction and waits that it completes."
302 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
304 "Begins a read transaction and immediately returns."
305 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
306 "Begins a write transaction and immediately returns."
307 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
309 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
310 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
312 safeExec :: (() -> <Proc> a) -> <Proc> a
313 activateOnce :: Resource -> <Proc> ()
314 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
315 resourceFromId :: Long -> <ReadGraph> Resource
316 enableDependencies :: () -> <WriteGraph> ()
317 disableDependencies :: () -> <WriteGraph> ()
319 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
320 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
322 "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
323 subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
324 "Makes a new read request with given procedure for calculating the result. The request is always cached."
325 subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
326 "Tries to convert the given Dynamic value to a value with the inferred type"
327 possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
329 importJava "org.simantics.db.layer0.util.Layer0Utils" where
330 undo :: () -> <Proc> String
331 undoOperations :: Integer -> <Proc> String
332 redo :: () -> <Proc> String
333 queryDebugSupport :: String -> <Proc> String
334 queryListSupport :: String -> <Proc> String
335 addCommentMetadata :: String -> <WriteGraph> ()
336 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
337 makeSynchronous :: Boolean -> <ReadGraph> ()
338 listOntologies :: () -> <ReadGraph> [Resource]
339 emptyTrashBin :: () -> <Proc> ()
340 purgeDatabase :: () -> <Proc> ()
344 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
345 @JavaName getPossiblePredicateByName
346 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
348 copyTo :: Resource -> Resource -> <WriteGraph> Resource
349 copyTo targetContainer source = do
350 (collectionToList $ copyTo_ targetContainer source)!0
352 importJava "org.simantics.db.common.utils.CommonDBUtils" where
353 selectClusterSet :: Resource -> <WriteGraph> ()
355 importJava "org.simantics.Simantics" where
356 @JavaName getProjectResource
357 currentProject :: () -> /*<Proc>*/ Resource
359 // Move to somewhere more generic module
360 importJava "java.util.Collection" where
363 importJava "java.util.ArrayList" where
365 collectionToList :: Collection a -> [a]
367 collectionToList :: Collection a -> [a]
368 collectionToList x = Java.unsafeCoerce x
371 resourceToCollection :: a -> Collection a
372 resourceToCollection x = Java.unsafeCoerce x
374 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
375 newEntity entitySpecs = do
376 entity = newResource ()
377 for entitySpecs (\spec -> spec entity)
380 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
381 updateEntity entity entitySpecs = do
382 for entitySpecs (\spec -> spec entity)
385 hasStatement predicate object entity =
386 claim entity predicate object
387 hasProperty relation value entity =
388 claimRelatedValue entity relation value
389 hasPossibleProperty relation value entity =
391 Just v -> claimRelatedValue entity relation v
393 hasTypedProperty relation value t entity =
394 claimRelatedValueWithType entity relation t value
395 hasName (n :: String) = hasProperty L0.HasName n
396 hasLabel (l :: String) = hasProperty L0.HasLabel l
397 hasType t = hasStatement L0.InstanceOf t
398 hasParent p = hasStatement L0.PartOf p
400 importJava "org.simantics.databoard.util.URIStringUtils" where
401 @JavaName splitURISCL
402 splitURI :: String -> [String]
404 escapeURI :: String -> String
406 unescapeURI :: String -> String
409 importJava "org.simantics.db.layer0.request.ActiveModels" where
410 @JavaName getPossibleActiveModel
411 activeModel :: Resource -> <ReadGraph> Maybe Resource
413 "Gives the current active model."
414 currentModel :: <ReadGraph> Resource
415 currentModel = match activeModel (currentProject ()) with
417 Nothing -> fail "No active model."
419 startUndoPoint :: String -> <WriteGraph> ()
420 startUndoPoint string = do
422 addCommentMetadata (string)
426 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
427 lift1Read f x = syncRead (\_ -> f x)
430 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
431 lift2Read f x y = syncRead (\_ -> f x y)
434 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
435 lift3Read f x y z = syncRead (\_ -> f x y z)
438 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
439 lift1Write f x = syncWrite (\_ -> f x)
442 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
443 lift2Write f x y = syncWrite (\_ -> f x y)
446 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
447 lift3Write f x y z = syncWrite (\_ -> f x y z)
450 Returns a child Browsable of the specified parent that has that is the child
451 of the specified parent with the specified name path.
453 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
454 childWithPath parent path =
455 foldl (\r name -> match possibleChild r name with
457 Nothing -> fail ("Didn't find " + name + ".")
462 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
465 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
466 possibleChildWithPath parent path =
467 foldl (\r name -> match r with
468 Just c -> possibleChild c name