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 objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
65 @JavaName getStatements
66 statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
68 @JavaName hasStatement
69 existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
71 @JavaName hasStatement
72 existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
74 "Assumes that there is exactly one object with the given subject and predicate and returns it."
75 @JavaName getSingleObject
76 singleObject :: Resource -> Resource -> <ReadGraph> Resource
79 If there is exactly one object with the given `subject` and `predicate`,
80 `possibleObject subject predicate` returns it. Otherwise, it returns
83 @JavaName getPossibleObject
84 possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
86 "Assumes that there is exactly one statement with the given subject and predicate and returns it."
87 @JavaName getSingleStatement
88 singleStatement :: Resource -> Resource -> <ReadGraph> Statement
90 @JavaName getPossibleStatement
91 possibleStatement :: Resource -> Resource -> <ReadGraph> Maybe Statement
93 @JavaName getRelatedVariantValue
94 relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
96 @JavaName getRelatedValue
97 relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
99 @JavaName getRelatedValue
100 untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
102 @JavaName getRelatedValue2
103 relatedValue2 :: Resource -> Resource -> <ReadGraph> a
105 @JavaName getRelatedVariantValue2
106 relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
108 @JavaName getPossibleRelatedValue
109 possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
111 @JavaName getPossibleRelatedValue
112 untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
116 valueOf_ :: Resource -> Binding a -> <ReadGraph> a
119 @JavaName getVariantValue
120 variantValueOf_ :: Resource -> <ReadGraph> Variant
123 untypedValueOf :: Resource -> <ReadGraph> Dynamic
125 @JavaName getPossibleValue
126 untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
129 inverseOf :: Resource -> <ReadGraph> Resource
131 @JavaName getSingleType
132 singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
134 @JavaName getPossibleType
135 possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
137 "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
138 isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
139 isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
140 isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
142 getRootLibrary :: () -> <ReadGraph> Resource
144 importJava "org.simantics.db.layer0.util.ExtendedUris" where
145 "Converts an absolute URI to a resource"
146 @JavaName resolveAbsoluteUri
147 resource :: String -> <ReadGraph> Resource
149 "Converts a relative URI to a resource starting from the given resource"
150 @JavaName resolveRelativeUri
151 relativeResource :: Resource -> String -> <ReadGraph> Resource
153 "Reads the value of a literal that is an object with the given subject and predicate"
155 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
156 relatedValue s p = relatedValue_ s p binding
159 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
160 possibleRelatedValue s p = possibleRelatedValue_ s p binding
162 class Browsable a where
163 fromUri :: String -> <ReadGraph> a
165 "Returns the URI of the given value."
166 uriOf :: a -> <ReadGraph> String
168 "Reads the name of the value."
169 nameOf :: a -> <ReadGraph> String
170 possibleNameOf :: a -> <ReadGraph> Maybe String
172 valueOf :: Serializable v => a -> <ReadGraph> v
174 variantValueOf :: a -> <ReadGraph> Variant
176 children :: a -> <ReadGraph> [a]
177 parent :: a -> <ReadGraph> a
178 possibleParent :: a -> <ReadGraph> Maybe a
180 child :: a -> String -> <ReadGraph> a
181 possibleChild :: a -> String -> <ReadGraph> Maybe a
183 instance Browsable Resource where
185 uriOf = uriOfResource
186 nameOf r = relatedValue r L0.HasName
187 possibleNameOf r = possibleRelatedValue r L0.HasName
189 valueOf r = valueOf_ r binding
190 variantValueOf = variantValueOf_
192 children r = r # L0.ConsistsOf
193 parent r = singleObject r L0.PartOf
194 possibleParent r = possibleObject r L0.PartOf
196 possibleChild = possibleResourceChild
197 child r n = fromJust (possibleResourceChild r n)
199 importJava "org.simantics.db.WriteOnlyGraph" where
200 markUndoPoint :: () -> <WriteGraph> ()
202 importJava "org.simantics.db.WriteGraph" where
203 "Creates a new resource."
204 newResource :: () -> <WriteGraph> Resource
205 "Adds a statement to the semantic graph."
206 claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
207 @JavaName claimLiteral
208 claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
209 @JavaName claimLiteral
210 untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
212 untypedClaimValue :: Resource -> a -> <WriteGraph> ()
213 @JavaName claimLiteral
214 claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
215 "Removes a statement with the given subject, predicate and object"
216 deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
218 denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
220 denyAllStatements :: Resource -> <WriteGraph> ()
221 newClusterSet :: Resource -> <WriteGraph> ()
223 denyValue :: Resource -> <WriteGraph> ()
225 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
226 claimAssertion type_ predicate object = do
228 claim ass L0.HasPredicate predicate
229 claim ass L0.HasObject object
230 claim type_ L0.Asserts ass
232 "Sets the value of the literal that is an object with the given subject and predicate."
234 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
235 claimRelatedValue s p v = claimRelatedValue_ s p v binding
238 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
239 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
241 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
242 @JavaName getPossibleChild
243 possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
245 @deprecated "Use function children instead."
246 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
247 resourceChildrenOf r = r # L0.ConsistsOf
249 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
251 addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
254 setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
256 @JavaName getSingleOwnerList
257 parentOrderedSet :: Resource -> <ReadGraph> Resource
260 elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
262 importJava "org.simantics.db.common.utils.ListUtils" where
264 elementsOfList :: Resource -> <ReadGraph> [Resource]
267 createList :: [Resource] -> <WriteGraph> Resource
270 createListWithType :: Resource -> [Resource] -> <WriteGraph> Resource
273 insertBack :: Resource -> [Resource] -> <WriteGraph> ()
275 @javaName removeElement
276 removeElement :: Resource -> Resource -> <WriteGraph> Boolean
278 @javaName swapWithPrevious
279 swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
281 @javaName swapWithNext
282 swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
285 importJava "org.simantics.db.common.utils.CommonDBUtils" where
286 isParent :: Resource -> Resource -> <ReadGraph> Boolean
287 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
288 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
289 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
290 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
292 importJava "org.simantics.db.common.utils.NameUtils" where
293 findFreshName :: String -> Resource -> <ReadGraph> String
294 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
296 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
297 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
298 subject # predicate = collectionToList $ objects_ subject predicate
300 "statements subject predicate` returns all statements with the given subject and predicate."
301 statements :: Resource -> Resource -> <ReadGraph> [Statement]
302 statements subject predicate = collectionToList $ statements_ subject predicate
304 importJava "org.simantics.scl.db.SCLFunctions" where
305 "Executes a read transaction and waits that it completes."
306 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
307 "Executes a write transaction and waits that it completes."
308 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
309 "Executes a delayed write transaction and waits that it completes."
310 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
312 "Begins a read transaction and immediately returns."
313 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
314 "Begins a write transaction and immediately returns."
315 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
317 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
318 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
320 safeExec :: (() -> <Proc> a) -> <Proc> a
321 activateOnce :: Resource -> <Proc> ()
322 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
323 resourceFromId :: Long -> <ReadGraph> Resource
324 enableDependencies :: () -> <WriteGraph> ()
325 disableDependencies :: () -> <WriteGraph> ()
327 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
328 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
330 "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
331 subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
332 "Makes a new read request with given procedure for calculating the result. The request is always cached."
333 subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
334 "Tries to convert the given Dynamic value to a value with the inferred type"
335 possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
337 importJava "org.simantics.db.layer0.util.Layer0Utils" where
338 undo :: () -> <Proc> String
339 undoOperations :: Integer -> <Proc> String
340 redo :: () -> <Proc> String
341 queryDebugSupport :: String -> <Proc> String
342 queryListSupport :: String -> <Proc> String
343 addCommentMetadata :: String -> <WriteGraph> ()
344 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
345 makeSynchronous :: Boolean -> <ReadGraph> ()
346 listOntologies :: () -> <ReadGraph> [Resource]
347 emptyTrashBin :: () -> <Proc> ()
348 purgeDatabase :: () -> <Proc> ()
349 prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
353 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
354 @JavaName getPossiblePredicateByName
355 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
357 copyTo :: Resource -> Resource -> <WriteGraph> Resource
358 copyTo targetContainer source = do
359 (collectionToList $ copyTo_ targetContainer source)!0
361 importJava "org.simantics.db.common.utils.CommonDBUtils" where
362 selectClusterSet :: Resource -> <WriteGraph> ()
364 importJava "org.simantics.Simantics" where
365 @JavaName getProjectResource
366 currentProject :: () -> /*<Proc>*/ Resource
368 // Move to somewhere more generic module
369 importJava "java.util.Collection" where
372 importJava "java.util.ArrayList" where
374 collectionToList :: Collection a -> [a]
376 collectionToList :: Collection a -> [a]
377 collectionToList x = Java.unsafeCoerce x
380 resourceToCollection :: a -> Collection a
381 resourceToCollection x = Java.unsafeCoerce x
383 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
384 newEntity entitySpecs = do
385 entity = newResource ()
386 for entitySpecs (\spec -> spec entity)
389 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
390 updateEntity entity entitySpecs = do
391 for entitySpecs (\spec -> spec entity)
394 hasStatement predicate object entity =
395 claim entity predicate object
396 hasProperty relation value entity =
397 claimRelatedValue entity relation value
398 hasPossibleProperty relation value entity =
400 Just v -> claimRelatedValue entity relation v
402 hasTypedProperty relation value t entity =
403 claimRelatedValueWithType entity relation t value
404 hasName (n :: String) = hasProperty L0.HasName n
405 hasLabel (l :: String) = hasProperty L0.HasLabel l
406 hasType t = hasStatement L0.InstanceOf t
407 hasParent p = hasStatement L0.PartOf p
409 importJava "org.simantics.databoard.util.URIStringUtils" where
410 @JavaName splitURISCL
411 splitURI :: String -> [String]
413 escapeURI :: String -> String
415 unescapeURI :: String -> String
418 importJava "org.simantics.db.layer0.request.ActiveModels" where
419 @JavaName getPossibleActiveModel
420 activeModel :: Resource -> <ReadGraph> Maybe Resource
422 "Gives the current active model."
423 currentModel :: <ReadGraph> Resource
424 currentModel = match activeModel (currentProject ()) with
426 Nothing -> fail "No active model."
428 startUndoPoint :: String -> <WriteGraph> ()
429 startUndoPoint string = do
431 addCommentMetadata (string)
435 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
436 lift1Read f x = syncRead (\_ -> f x)
439 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
440 lift2Read f x y = syncRead (\_ -> f x y)
443 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
444 lift3Read f x y z = syncRead (\_ -> f x y z)
447 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
448 lift1Write f x = syncWrite (\_ -> f x)
451 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
452 lift2Write f x y = syncWrite (\_ -> f x y)
455 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
456 lift3Write f x y z = syncWrite (\_ -> f x y z)
459 Returns a child Browsable of the specified parent that has that is the child
460 of the specified parent with the specified name path.
462 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
463 childWithPath parent path =
464 foldl (\r name -> match possibleChild r name with
466 Nothing -> fail ("Didn't find " + name + ".")
471 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
474 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
475 possibleChildWithPath parent path =
476 foldl (\r name -> match r with
477 Just c -> possibleChild c name