1 import "JavaBuiltin" as Java
2 include "http://www.simantics.org/Layer0-1.1" as L0
8 "org.simantics.db.ReadGraph"
12 "org.simantics.db.WriteGraph"
14 importJava "org.simantics.db.Resource" where
15 "A resource is a node in a semantic graph."
18 "Returns the 64-bit unique identifier of the resource."
19 @JavaName getResourceId
20 resourceId :: Resource -> Long
22 importJava "org.simantics.db.ReadGraph" where
25 instance Ord Resource where
26 compare a b = compare (resourceId a) (resourceId b)
28 instance Show Resource where
29 show r = "#" + show (resourceId r)
31 instance Show Statement where
32 show s = "#" + show (resourceId ( subjectOf s)) + "#" + show (resourceId ( predicateOf s)) + "#" + show (resourceId ( objectOf s))
34 importJava "org.simantics.db.Statement" where
35 "A statement is an edge in a semantic graph."
39 subjectOf :: Statement -> Resource
40 @JavaName getPredicate
41 predicateOf :: Statement -> Resource
43 objectOf :: Statement -> Resource
45 importJava "org.simantics.db.ReadGraph" where
46 "Converts an absolute URI to a resource or returns `Nothing` if there is no such resource."
47 @JavaName getPossibleResource
48 possibleResource :: String -> <ReadGraph> (Maybe Resource)
50 getDataType :: Resource -> <ReadGraph> Datatype
52 @JavaName getSupertypes
53 superTypesOf :: Resource -> <ReadGraph> Set.T Resource
57 uriOfResource :: Resource -> <ReadGraph> String
60 objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
62 @JavaName getStatements
63 statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
65 @JavaName hasStatement
66 existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
68 @JavaName hasStatement
69 existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
71 "Assumes that there is exactly one object with the given subject and predicate and returns it."
72 @JavaName getSingleObject
73 singleObject :: Resource -> Resource -> <ReadGraph> Resource
76 If there is exactly one object with the given `subject` and `predicate`,
77 `possibleObject subject predicate` returns it. Otherwise, it returns
80 @JavaName getPossibleObject
81 possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
83 "Assumes that there is exactly one statement with the given subject and predicate and returns it."
84 @JavaName getSingleStatement
85 singleStatement :: Resource -> Resource -> <ReadGraph> Statement
87 @JavaName getRelatedVariantValue
88 relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
90 @JavaName getRelatedValue
91 relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
93 @JavaName getRelatedValue
94 untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
96 @JavaName getRelatedValue2
97 relatedValue2 :: Resource -> Resource -> <ReadGraph> a
99 @JavaName getRelatedVariantValue2
100 relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
102 @JavaName getPossibleRelatedValue
103 possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
105 @JavaName getPossibleRelatedValue
106 untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
110 valueOf_ :: Resource -> Binding a -> <ReadGraph> a
113 @JavaName getVariantValue
114 variantValueOf_ :: Resource -> <ReadGraph> Variant
117 untypedValueOf :: Resource -> <ReadGraph> Dynamic
119 @JavaName getPossibleValue
120 untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
123 inverseOf :: Resource -> <ReadGraph> Resource
125 @JavaName getSingleType
126 singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
128 @JavaName getPossibleType
129 possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
131 "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
132 isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
133 isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
134 isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
136 getRootLibrary :: () -> <ReadGraph> Resource
138 importJava "org.simantics.db.layer0.util.ExtendedUris" where
139 "Converts an absolute URI to a resource"
140 @JavaName resolveAbsoluteUri
141 resource :: String -> <ReadGraph> Resource
143 "Converts a relative URI to a resource starting from the given resource"
144 @JavaName resolveRelativeUri
145 relativeResource :: Resource -> String -> <ReadGraph> Resource
147 "Reads the value of a literal that is an object with the given subject and predicate"
149 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
150 relatedValue s p = relatedValue_ s p binding
153 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
154 possibleRelatedValue s p = possibleRelatedValue_ s p binding
156 class Browsable a where
157 fromUri :: String -> <ReadGraph> a
159 "Returns the URI of the given value."
160 uriOf :: a -> <ReadGraph> String
162 "Reads the name of the value."
163 nameOf :: a -> <ReadGraph> String
164 possibleNameOf :: a -> <ReadGraph> Maybe String
166 valueOf :: Serializable v => a -> <ReadGraph> v
168 variantValueOf :: a -> <ReadGraph> Variant
170 children :: a -> <ReadGraph> [a]
171 parent :: a -> <ReadGraph> a
172 possibleParent :: a -> <ReadGraph> Maybe a
174 child :: a -> String -> <ReadGraph> a
175 possibleChild :: a -> String -> <ReadGraph> Maybe a
177 instance Browsable Resource where
179 uriOf = uriOfResource
180 nameOf r = relatedValue r L0.HasName
181 possibleNameOf r = possibleRelatedValue r L0.HasName
183 valueOf r = valueOf_ r binding
184 variantValueOf = variantValueOf_
186 children r = r # L0.ConsistsOf
187 parent r = singleObject r L0.PartOf
188 possibleParent r = possibleObject r L0.PartOf
190 possibleChild = possibleResourceChild
191 child r n = fromJust (possibleResourceChild r n)
193 importJava "org.simantics.db.WriteOnlyGraph" where
194 markUndoPoint :: () -> <WriteGraph> ()
196 importJava "org.simantics.db.WriteGraph" where
197 "Creates a new resource."
198 newResource :: () -> <WriteGraph> Resource
199 "Adds a statement to the semantic graph."
200 claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
201 @JavaName claimLiteral
202 claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
203 @JavaName claimLiteral
204 untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
206 untypedClaimValue :: Resource -> a -> <WriteGraph> ()
207 @JavaName claimLiteral
208 claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
209 "Removes a statement with the given subject, predicate and object"
210 deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
212 denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
214 denyAllStatements :: Resource -> <WriteGraph> ()
215 newClusterSet :: Resource -> <WriteGraph> ()
217 denyValue :: Resource -> <WriteGraph> ()
219 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
220 claimAssertion type_ predicate object = do
222 claim ass L0.HasPredicate predicate
223 claim ass L0.HasObject object
224 claim type_ L0.Asserts ass
226 "Sets the value of the literal that is an object with the given subject and predicate."
228 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
229 claimRelatedValue s p v = claimRelatedValue_ s p v binding
232 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
233 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
235 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
236 @JavaName getPossibleChild
237 possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
239 @deprecated "Use function children instead."
240 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
241 resourceChildrenOf r = r # L0.ConsistsOf
243 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
245 addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
248 setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
250 @JavaName getSingleOwnerList
251 parentOrderedSet :: Resource -> <ReadGraph> Resource
254 elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
256 importJava "org.simantics.db.common.utils.ListUtils" where
258 elementsOfList :: Resource -> <ReadGraph> [Resource]
261 createList :: [Resource] -> <WriteGraph> Resource
264 insertBack :: Resource -> [Resource] -> <WriteGraph> ()
266 @javaName removeElement
267 removeElement :: Resource -> Resource -> <WriteGraph> Boolean
269 @javaName swapWithPrevious
270 swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
272 @javaName swapWithNext
273 swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
276 importJava "org.simantics.db.common.utils.CommonDBUtils" where
277 isParent :: Resource -> Resource -> <ReadGraph> Boolean
278 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
279 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
280 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
281 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
283 importJava "org.simantics.db.common.utils.NameUtils" where
284 findFreshName :: String -> Resource -> <ReadGraph> String
285 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
287 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
288 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
289 subject # predicate = collectionToList $ objects_ subject predicate
291 "statements subject predicate` returns all statements with the given subject and predicate."
292 statements :: Resource -> Resource -> <ReadGraph> [Statement]
293 statements subject predicate = collectionToList $ statements_ subject predicate
295 importJava "org.simantics.scl.db.SCLFunctions" where
296 "Executes a read transaction and waits that it completes."
297 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
298 "Executes a write transaction and waits that it completes."
299 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
300 "Executes a delayed write transaction and waits that it completes."
301 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
303 "Begins a read transaction and immediately returns."
304 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
305 "Begins a write transaction and immediately returns."
306 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
308 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
309 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
311 safeExec :: (() -> <Proc> a) -> <Proc> a
312 activateOnce :: Resource -> <Proc> ()
313 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
314 resourceFromId :: Long -> <ReadGraph> Resource
315 enableDependencies :: () -> <WriteGraph> ()
316 disableDependencies :: () -> <WriteGraph> ()
318 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
319 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
321 "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
322 subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
323 "Makes a new read request with given procedure for calculating the result. The request is always cached."
324 subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
326 importJava "org.simantics.db.layer0.util.Layer0Utils" where
327 undo :: () -> <Proc> String
328 undoOperations :: Integer -> <Proc> String
329 redo :: () -> <Proc> String
330 queryDebugSupport :: String -> <Proc> String
331 queryListSupport :: String -> <Proc> String
332 addCommentMetadata :: String -> <WriteGraph> ()
333 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
334 makeSynchronous :: Boolean -> <ReadGraph> ()
335 listOntologies :: () -> <ReadGraph> [Resource]
336 emptyTrashBin :: () -> <Proc> ()
337 purgeDatabase :: () -> <Proc> ()
341 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
342 @JavaName getPossiblePredicateByName
343 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
345 copyTo :: Resource -> Resource -> <WriteGraph> Resource
346 copyTo targetContainer source = do
347 (collectionToList $ copyTo_ targetContainer source)!0
349 importJava "org.simantics.db.common.utils.CommonDBUtils" where
350 selectClusterSet :: Resource -> <WriteGraph> ()
352 importJava "org.simantics.Simantics" where
353 @JavaName getProjectResource
354 currentProject :: () -> /*<Proc>*/ Resource
356 // Move to somewhere more generic module
357 importJava "java.util.Collection" where
360 importJava "java.util.ArrayList" where
362 collectionToList :: Collection a -> [a]
364 collectionToList :: Collection a -> [a]
365 collectionToList x = Java.unsafeCoerce x
368 resourceToCollection :: a -> Collection a
369 resourceToCollection x = Java.unsafeCoerce x
371 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
372 newEntity entitySpecs = do
373 entity = newResource ()
374 for entitySpecs (\spec -> spec entity)
377 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
378 updateEntity entity entitySpecs = do
379 for entitySpecs (\spec -> spec entity)
382 hasStatement predicate object entity =
383 claim entity predicate object
384 hasProperty relation value entity =
385 claimRelatedValue entity relation value
386 hasPossibleProperty relation value entity =
388 Just v -> claimRelatedValue entity relation v
390 hasTypedProperty relation value t entity =
391 claimRelatedValueWithType entity relation t value
392 hasName (n :: String) = hasProperty L0.HasName n
393 hasLabel (l :: String) = hasProperty L0.HasLabel l
394 hasType t = hasStatement L0.InstanceOf t
395 hasParent p = hasStatement L0.PartOf p
397 importJava "org.simantics.databoard.util.URIStringUtils" where
398 @JavaName splitURISCL
399 splitURI :: String -> [String]
401 escapeURI :: String -> String
403 unescapeURI :: String -> String
406 importJava "org.simantics.db.layer0.request.ActiveModels" where
407 @JavaName getPossibleActiveModel
408 activeModel :: Resource -> <ReadGraph> Maybe Resource
410 "Gives the current active model."
411 currentModel :: <ReadGraph> Resource
412 currentModel = match activeModel (currentProject ()) with
414 Nothing -> fail "No active model."
416 startUndoPoint :: String -> <WriteGraph> ()
417 startUndoPoint string = do
419 addCommentMetadata (string)
423 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
424 lift1Read f x = syncRead (\_ -> f x)
427 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
428 lift2Read f x y = syncRead (\_ -> f x y)
431 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
432 lift3Read f x y z = syncRead (\_ -> f x y z)
435 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
436 lift1Write f x = syncWrite (\_ -> f x)
439 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
440 lift2Write f x y = syncWrite (\_ -> f x y)
443 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
444 lift3Write f x y z = syncWrite (\_ -> f x y z)
447 Returns a child Browsable of the specified parent that has that is the child
448 of the specified parent with the specified name path.
450 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
451 childWithPath parent path =
452 foldl (\r name -> match possibleChild r name with
454 Nothing -> fail ("Didn't find " + name + ".")
459 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
462 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
463 possibleChildWithPath parent path =
464 foldl (\r name -> match r with
465 Just c -> possibleChild c name