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 insertBack :: Resource -> [Resource] -> <WriteGraph> ()
272 @javaName removeElement
273 removeElement :: Resource -> Resource -> <WriteGraph> Boolean
275 @javaName swapWithPrevious
276 swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
278 @javaName swapWithNext
279 swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
282 importJava "org.simantics.db.common.utils.CommonDBUtils" where
283 isParent :: Resource -> Resource -> <ReadGraph> Boolean
284 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
285 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
286 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
287 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
289 importJava "org.simantics.db.common.utils.NameUtils" where
290 findFreshName :: String -> Resource -> <ReadGraph> String
291 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
293 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
294 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
295 subject # predicate = collectionToList $ objects_ subject predicate
297 "statements subject predicate` returns all statements with the given subject and predicate."
298 statements :: Resource -> Resource -> <ReadGraph> [Statement]
299 statements subject predicate = collectionToList $ statements_ subject predicate
301 importJava "org.simantics.scl.db.SCLFunctions" where
302 "Executes a read transaction and waits that it completes."
303 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
304 "Executes a write transaction and waits that it completes."
305 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
306 "Executes a delayed write transaction and waits that it completes."
307 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
309 "Begins a read transaction and immediately returns."
310 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
311 "Begins a write transaction and immediately returns."
312 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
314 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
315 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
317 safeExec :: (() -> <Proc> a) -> <Proc> a
318 activateOnce :: Resource -> <Proc> ()
319 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
320 resourceFromId :: Long -> <ReadGraph> Resource
321 enableDependencies :: () -> <WriteGraph> ()
322 disableDependencies :: () -> <WriteGraph> ()
324 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
325 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
327 "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
328 subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
329 "Makes a new read request with given procedure for calculating the result. The request is always cached."
330 subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
331 "Tries to convert the given Dynamic value to a value with the inferred type"
332 possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
334 importJava "org.simantics.db.layer0.util.Layer0Utils" where
335 undo :: () -> <Proc> String
336 undoOperations :: Integer -> <Proc> String
337 redo :: () -> <Proc> String
338 queryDebugSupport :: String -> <Proc> String
339 queryListSupport :: String -> <Proc> String
340 addCommentMetadata :: String -> <WriteGraph> ()
341 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
342 makeSynchronous :: Boolean -> <ReadGraph> ()
343 listOntologies :: () -> <ReadGraph> [Resource]
344 emptyTrashBin :: () -> <Proc> ()
345 purgeDatabase :: () -> <Proc> ()
346 prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
350 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
351 @JavaName getPossiblePredicateByName
352 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
354 copyTo :: Resource -> Resource -> <WriteGraph> Resource
355 copyTo targetContainer source = do
356 (collectionToList $ copyTo_ targetContainer source)!0
358 importJava "org.simantics.db.common.utils.CommonDBUtils" where
359 selectClusterSet :: Resource -> <WriteGraph> ()
361 importJava "org.simantics.Simantics" where
362 @JavaName getProjectResource
363 currentProject :: () -> /*<Proc>*/ Resource
365 // Move to somewhere more generic module
366 importJava "java.util.Collection" where
369 importJava "java.util.ArrayList" where
371 collectionToList :: Collection a -> [a]
373 collectionToList :: Collection a -> [a]
374 collectionToList x = Java.unsafeCoerce x
377 resourceToCollection :: a -> Collection a
378 resourceToCollection x = Java.unsafeCoerce x
380 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
381 newEntity entitySpecs = do
382 entity = newResource ()
383 for entitySpecs (\spec -> spec entity)
386 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
387 updateEntity entity entitySpecs = do
388 for entitySpecs (\spec -> spec entity)
391 hasStatement predicate object entity =
392 claim entity predicate object
393 hasProperty relation value entity =
394 claimRelatedValue entity relation value
395 hasPossibleProperty relation value entity =
397 Just v -> claimRelatedValue entity relation v
399 hasTypedProperty relation value t entity =
400 claimRelatedValueWithType entity relation t value
401 hasName (n :: String) = hasProperty L0.HasName n
402 hasLabel (l :: String) = hasProperty L0.HasLabel l
403 hasType t = hasStatement L0.InstanceOf t
404 hasParent p = hasStatement L0.PartOf p
406 importJava "org.simantics.databoard.util.URIStringUtils" where
407 @JavaName splitURISCL
408 splitURI :: String -> [String]
410 escapeURI :: String -> String
412 unescapeURI :: String -> String
415 importJava "org.simantics.db.layer0.request.ActiveModels" where
416 @JavaName getPossibleActiveModel
417 activeModel :: Resource -> <ReadGraph> Maybe Resource
419 "Gives the current active model."
420 currentModel :: <ReadGraph> Resource
421 currentModel = match activeModel (currentProject ()) with
423 Nothing -> fail "No active model."
425 startUndoPoint :: String -> <WriteGraph> ()
426 startUndoPoint string = do
428 addCommentMetadata (string)
432 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
433 lift1Read f x = syncRead (\_ -> f x)
436 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
437 lift2Read f x y = syncRead (\_ -> f x y)
440 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
441 lift3Read f x y z = syncRead (\_ -> f x y z)
444 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
445 lift1Write f x = syncWrite (\_ -> f x)
448 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
449 lift2Write f x y = syncWrite (\_ -> f x y)
452 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
453 lift3Write f x y z = syncWrite (\_ -> f x y z)
456 Returns a child Browsable of the specified parent that has that is the child
457 of the specified parent with the specified name path.
459 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
460 childWithPath parent path =
461 foldl (\r name -> match possibleChild r name with
463 Nothing -> fail ("Didn't find " + name + ".")
468 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
471 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
472 possibleChildWithPath parent path =
473 foldl (\r name -> match r with
474 Just c -> possibleChild c name