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]
260 importJava "org.simantics.db.common.utils.CommonDBUtils" where
261 isParent :: Resource -> Resource -> <ReadGraph> Boolean
262 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
263 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
264 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
265 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
267 importJava "org.simantics.db.common.utils.NameUtils" where
268 findFreshName :: String -> Resource -> <ReadGraph> String
269 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
271 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
272 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
273 subject # predicate = collectionToList $ objects_ subject predicate
275 "statements subject predicate` returns all statements with the given subject and predicate."
276 statements :: Resource -> Resource -> <ReadGraph> [Statement]
277 statements subject predicate = collectionToList $ statements_ subject predicate
279 importJava "org.simantics.scl.db.SCLFunctions" where
280 "Executes a read transaction and waits that it completes."
281 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
282 "Executes a write transaction and waits that it completes."
283 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
284 "Executes a delayed write transaction and waits that it completes."
285 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
287 "Begins a read transaction and immediately returns."
288 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
289 "Begins a write transaction and immediately returns."
290 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
292 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
293 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
295 safeExec :: (() -> <Proc> a) -> <Proc> a
296 activateOnce :: Resource -> <Proc> ()
297 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
298 resourceFromId :: Long -> <ReadGraph> Resource
299 enableDependencies :: () -> <WriteGraph> ()
300 disableDependencies :: () -> <WriteGraph> ()
302 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
303 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
305 importJava "org.simantics.db.layer0.util.Layer0Utils" where
306 undo :: () -> <Proc> String
307 undoOperations :: Integer -> <Proc> String
308 redo :: () -> <Proc> String
309 queryDebugSupport :: String -> <Proc> String
310 queryListSupport :: String -> <Proc> String
311 addCommentMetadata :: String -> <WriteGraph> ()
312 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
313 makeSynchronous :: Boolean -> <ReadGraph> ()
314 listOntologies :: () -> <ReadGraph> [Resource]
318 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
319 @JavaName getPossiblePredicateByName
320 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
322 copyTo :: Resource -> Resource -> <WriteGraph> Resource
323 copyTo targetContainer source = do
324 (collectionToList $ copyTo_ targetContainer source)!0
326 importJava "org.simantics.db.common.utils.CommonDBUtils" where
327 selectClusterSet :: Resource -> <WriteGraph> ()
329 importJava "org.simantics.Simantics" where
330 @JavaName getProjectResource
331 currentProject :: () -> /*<Proc>*/ Resource
333 // Move to somewhere more generic module
334 importJava "java.util.Collection" where
337 importJava "java.util.ArrayList" where
339 collectionToList :: Collection a -> [a]
341 collectionToList :: Collection a -> [a]
342 collectionToList x = Java.unsafeCoerce x
345 resourceToCollection :: a -> Collection a
346 resourceToCollection x = Java.unsafeCoerce x
348 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
349 newEntity entitySpecs = do
350 entity = newResource ()
351 for entitySpecs (\spec -> spec entity)
354 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
355 updateEntity entity entitySpecs = do
356 for entitySpecs (\spec -> spec entity)
359 hasStatement predicate object entity =
360 claim entity predicate object
361 hasProperty relation value entity =
362 claimRelatedValue entity relation value
363 hasPossibleProperty relation value entity =
365 Just v -> claimRelatedValue entity relation v
367 hasTypedProperty relation value t entity =
368 claimRelatedValueWithType entity relation t value
369 hasName (n :: String) = hasProperty L0.HasName n
370 hasLabel (l :: String) = hasProperty L0.HasLabel l
371 hasType t = hasStatement L0.InstanceOf t
372 hasParent p = hasStatement L0.PartOf p
374 importJava "org.simantics.databoard.util.URIStringUtils" where
375 @JavaName splitURISCL
376 splitURI :: String -> [String]
378 escapeURI :: String -> String
380 unescapeURI :: String -> String
383 importJava "org.simantics.db.layer0.request.ActiveModels" where
384 @JavaName getPossibleActiveModel
385 activeModel :: Resource -> <ReadGraph> Maybe Resource
387 "Gives the current active model."
388 currentModel :: <ReadGraph> Resource
389 currentModel = match activeModel (currentProject ()) with
391 Nothing -> fail "No active model."
393 startUndoPoint :: String -> <WriteGraph> ()
394 startUndoPoint string = do
396 addCommentMetadata (string)
400 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
401 lift1Read f x = syncRead (\_ -> f x)
404 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
405 lift2Read f x y = syncRead (\_ -> f x y)
408 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
409 lift3Read f x y z = syncRead (\_ -> f x y z)
412 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
413 lift1Write f x = syncWrite (\_ -> f x)
416 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
417 lift2Write f x y = syncWrite (\_ -> f x y)
420 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
421 lift3Write f x y z = syncWrite (\_ -> f x y z)
424 Returns a child Browsable of the specified parent that has that is the child
425 of the specified parent with the specified name path.
427 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
428 childWithPath parent path =
429 foldl (\r name -> match possibleChild r name with
431 Nothing -> fail ("Didn't find " + name + ".")
436 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
439 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
440 possibleChildWithPath parent path =
441 foldl (\r name -> match r with
442 Just c -> possibleChild c name