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 isAsserted :: Statement -> Resource -> <ReadGraph> Boolean
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)
53 getDataType :: Resource -> <ReadGraph> Datatype
55 @JavaName getSupertypes
56 superTypesOf :: Resource -> <ReadGraph> Set.T Resource
60 uriOfResource :: Resource -> <ReadGraph> String
63 @JavaName getPossibleURI
64 possibleUriOfResource :: Resource -> <ReadGraph> Maybe String
67 objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
69 @JavaName getStatements
70 statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
72 @JavaName hasStatement
73 existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
75 @JavaName hasStatement
76 existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
78 "Assumes that there is exactly one object with the given subject and predicate and returns it."
79 @JavaName getSingleObject
80 singleObject :: Resource -> Resource -> <ReadGraph> Resource
83 If there is exactly one object with the given `subject` and `predicate`,
84 `possibleObject subject predicate` returns it. Otherwise, it returns
87 @JavaName getPossibleObject
88 possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
90 "Assumes that there is exactly one statement with the given subject and predicate and returns it."
91 @JavaName getSingleStatement
92 singleStatement :: Resource -> Resource -> <ReadGraph> Statement
94 @JavaName getPossibleStatement
95 possibleStatement :: Resource -> Resource -> <ReadGraph> Maybe Statement
97 @JavaName getRelatedVariantValue
98 relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
100 @JavaName getRelatedValue
101 relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
103 @JavaName getRelatedValue
104 untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
106 @JavaName getRelatedValue2
107 relatedValue2 :: Resource -> Resource -> <ReadGraph> a
109 @JavaName getRelatedVariantValue2
110 relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
112 @JavaName getPossibleRelatedValue
113 possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
115 @JavaName getPossibleRelatedValue
116 untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
120 valueOf_ :: Resource -> Binding a -> <ReadGraph> a
123 @JavaName getVariantValue
124 variantValueOf_ :: Resource -> <ReadGraph> Variant
127 untypedValueOf :: Resource -> <ReadGraph> Dynamic
129 @JavaName getPossibleValue
130 untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
133 inverseOf :: Resource -> <ReadGraph> Resource
135 @JavaName getSingleType
136 singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
138 @JavaName getPossibleType
139 possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
141 "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
142 isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
143 isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
144 isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
146 getRootLibrary :: () -> <ReadGraph> Resource
148 @JavaName getPredicates
149 predicatesOf :: Resource -> <ReadGraph> [Resource]
151 importJava "org.simantics.db.layer0.util.ExtendedUris" where
152 "Converts an absolute URI to a resource"
153 @JavaName resolveAbsoluteUri
154 resource :: String -> <ReadGraph> Resource
156 "Converts a relative URI to a resource starting from the given resource"
157 @JavaName resolveRelativeUri
158 relativeResource :: Resource -> String -> <ReadGraph> Resource
160 "Reads the value of a literal that is an object with the given subject and predicate"
162 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
163 relatedValue s p = relatedValue_ s p binding
166 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
167 possibleRelatedValue s p = possibleRelatedValue_ s p binding
169 class Browsable a where
170 fromUri :: String -> <ReadGraph> a
172 "Returns the URI of the given value."
173 uriOf :: a -> <ReadGraph> String
175 "Returns the URI of the given value if it exists or Nothing."
176 possibleUriOf :: a -> <ReadGraph> Maybe String
178 "Reads the name of the value."
179 nameOf :: a -> <ReadGraph> String
180 possibleNameOf :: a -> <ReadGraph> Maybe String
182 valueOf :: Serializable v => a -> <ReadGraph> v
184 variantValueOf :: a -> <ReadGraph> Variant
186 children :: a -> <ReadGraph> [a]
187 parent :: a -> <ReadGraph> a
188 possibleParent :: a -> <ReadGraph> Maybe a
190 child :: a -> String -> <ReadGraph> a
191 possibleChild :: a -> String -> <ReadGraph> Maybe a
193 instance Browsable Resource where
195 uriOf = uriOfResource
196 possibleUriOf = possibleUriOfResource
197 nameOf r = relatedValue r L0.HasName
198 possibleNameOf r = possibleRelatedValue r L0.HasName
200 valueOf r = valueOf_ r binding
201 variantValueOf = variantValueOf_
203 children r = r # L0.ConsistsOf
204 parent r = singleObject r L0.PartOf
205 possibleParent r = possibleObject r L0.PartOf
207 possibleChild = possibleResourceChild
208 child r n = fromJust (possibleResourceChild r n)
210 importJava "org.simantics.db.WriteOnlyGraph" where
211 markUndoPoint :: () -> <WriteGraph> ()
213 importJava "org.simantics.db.WriteGraph" where
214 "Creates a new resource."
215 newResource :: () -> <WriteGraph> Resource
216 "Adds a statement to the semantic graph."
217 claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
218 @JavaName claimLiteral
219 claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
220 @JavaName claimLiteral
221 untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
223 untypedClaimValue :: Resource -> a -> <WriteGraph> ()
224 @JavaName claimLiteral
225 claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
226 "Removes a statement with the given subject, predicate and object"
227 deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
229 denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
231 denyAllStatements :: Resource -> <WriteGraph> ()
232 newClusterSet :: Resource -> <WriteGraph> ()
234 denyValue :: Resource -> <WriteGraph> ()
236 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
237 claimAssertion type_ predicate object = do
239 claim ass L0.HasPredicate predicate
240 claim ass L0.HasObject object
241 claim type_ L0.Asserts ass
243 "Sets the value of the literal that is an object with the given subject and predicate."
245 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
246 claimRelatedValue s p v = claimRelatedValue_ s p v binding
249 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
250 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
252 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
253 @JavaName getPossibleChild
254 possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
256 @deprecated "Use function children instead."
257 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
258 resourceChildrenOf r = r # L0.ConsistsOf
260 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
262 addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
265 setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
267 @JavaName getSingleOwnerList
268 parentOrderedSet :: Resource -> <ReadGraph> Resource
271 elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
273 importJava "org.simantics.db.common.utils.ListUtils" where
275 elementsOfList :: Resource -> <ReadGraph> [Resource]
278 createList :: [Resource] -> <WriteGraph> Resource
281 createListWithType :: Resource -> [Resource] -> <WriteGraph> Resource
284 insertBack :: Resource -> [Resource] -> <WriteGraph> ()
286 @javaName removeElement
287 removeElement :: Resource -> Resource -> <WriteGraph> Boolean
289 @javaName swapWithPrevious
290 swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
292 @javaName swapWithNext
293 swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
296 importJava "org.simantics.db.common.utils.CommonDBUtils" where
297 isParent :: Resource -> Resource -> <ReadGraph> Boolean
298 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
299 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
300 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
301 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
303 importJava "org.simantics.db.common.utils.NameUtils" where
304 findFreshName :: String -> Resource -> <ReadGraph> String
305 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
307 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
308 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
309 subject # predicate = collectionToList $ objects_ subject predicate
311 "statements subject predicate` returns all statements with the given subject and predicate."
312 statements :: Resource -> Resource -> <ReadGraph> [Statement]
313 statements subject predicate = collectionToList $ statements_ subject predicate
315 importJava "org.simantics.scl.db.SCLFunctions" where
316 "Executes a read transaction and waits that it completes."
317 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
318 "Executes a write transaction and waits that it completes."
319 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
320 "Executes a delayed write transaction and waits that it completes."
321 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
323 "Begins a read transaction and immediately returns."
324 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
325 "Begins a write transaction and immediately returns."
326 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
328 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
329 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
331 safeExec :: (() -> <Proc> a) -> <Proc> a
332 activateOnce :: Resource -> <Proc> ()
333 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
334 resourceFromId :: Long -> <ReadGraph> Resource
335 enableDependencies :: () -> <WriteGraph> ()
336 disableDependencies :: () -> <WriteGraph> ()
338 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
339 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
341 "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
342 subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
343 "Makes a new read request with given procedure for calculating the result. The request is always cached."
344 subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
345 "Tries to convert the given Dynamic value to a value with the inferred type"
346 possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
348 importJava "org.simantics.db.layer0.util.Layer0Utils" where
349 undo :: () -> <Proc> String
350 undoOperations :: Integer -> <Proc> String
351 redo :: () -> <Proc> String
352 queryDebugSupport :: String -> <Proc> String
353 queryListSupport :: String -> <Proc> String
354 addCommentMetadata :: String -> <WriteGraph> ()
355 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
356 makeSynchronous :: Boolean -> <ReadGraph> ()
357 listOntologies :: () -> <ReadGraph> [Resource]
358 emptyTrashBin :: () -> <Proc> ()
359 purgeDatabase :: () -> <Proc> ()
360 prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
364 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
365 @JavaName getPossiblePredicateByName
366 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
367 @JavaName getPossiblePredicateByNameFromType
368 possiblePredicateByNameFromType :: Resource -> String -> <ReadGraph> Maybe Resource
370 copyTo :: Resource -> Resource -> <WriteGraph> Resource
371 copyTo targetContainer source = do
372 (collectionToList $ copyTo_ targetContainer source)!0
374 importJava "org.simantics.db.common.utils.CommonDBUtils" where
375 selectClusterSet :: Resource -> <WriteGraph> ()
377 importJava "org.simantics.Simantics" where
378 @JavaName getProjectResource
379 currentProject :: () -> /*<Proc>*/ Resource
381 // Move to somewhere more generic module
382 importJava "java.util.Collection" where
385 importJava "java.util.ArrayList" where
387 collectionToList :: Collection a -> [a]
389 collectionToList :: Collection a -> [a]
390 collectionToList x = Java.unsafeCoerce x
393 resourceToCollection :: a -> Collection a
394 resourceToCollection x = Java.unsafeCoerce x
396 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
397 newEntity entitySpecs = do
398 entity = newResource ()
399 for entitySpecs (\spec -> spec entity)
402 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
403 updateEntity entity entitySpecs = do
404 for entitySpecs (\spec -> spec entity)
407 hasStatement predicate object entity =
408 claim entity predicate object
409 hasProperty relation value entity =
410 claimRelatedValue entity relation value
411 hasPossibleProperty relation value entity =
413 Just v -> claimRelatedValue entity relation v
415 hasTypedProperty relation value t entity =
416 claimRelatedValueWithType entity relation t value
417 hasName (n :: String) = hasProperty L0.HasName n
418 hasLabel (l :: String) = hasProperty L0.HasLabel l
419 hasType t = hasStatement L0.InstanceOf t
420 hasParent p = hasStatement L0.PartOf p
422 importJava "org.simantics.databoard.util.URIStringUtils" where
423 @JavaName splitURISCL
424 splitURI :: String -> [String]
426 escapeURI :: String -> String
428 unescapeURI :: String -> String
431 importJava "org.simantics.db.layer0.request.ActiveModels" where
432 @JavaName getPossibleActiveModel
433 activeModel :: Resource -> <ReadGraph> Maybe Resource
435 "Gives the current active model."
436 currentModel :: <ReadGraph> Resource
437 currentModel = match activeModel (currentProject ()) with
439 Nothing -> fail "No active model."
441 "Gives the current active model if it exists or Nothing otherwise."
442 possibleCurrentModel :: <ReadGraph> Maybe Resource
443 possibleCurrentModel = activeModel (currentProject ())
445 startUndoPoint :: String -> <WriteGraph> ()
446 startUndoPoint string = do
448 addCommentMetadata (string)
452 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
453 lift1Read f x = syncRead (\_ -> f x)
456 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
457 lift2Read f x y = syncRead (\_ -> f x y)
460 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
461 lift3Read f x y z = syncRead (\_ -> f x y z)
464 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
465 lift1Write f x = syncWrite (\_ -> f x)
468 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
469 lift2Write f x y = syncWrite (\_ -> f x y)
472 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
473 lift3Write f x y z = syncWrite (\_ -> f x y z)
476 Returns a child Browsable of the specified parent that has that is the child
477 of the specified parent with the specified name path.
479 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
480 childWithPath parent path =
481 foldl (\r name -> match possibleChild r name with
483 Nothing -> fail ("Didn't find " + name + ".")
488 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
491 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
492 possibleChildWithPath parent path =
493 foldl (\r name -> match r with
494 Just c -> possibleChild c name