1 import "JavaBuiltin" as Java
\r
2 include "http://www.simantics.org/Layer0-1.1" as L0
\r
8 "org.simantics.db.ReadGraph"
\r
12 "org.simantics.db.WriteGraph"
\r
14 importJava "org.simantics.db.Resource" where
\r
15 "A resource is a node in a semantic graph."
\r
18 "Returns the 64-bit unique identifier of the resource."
\r
19 @JavaName getResourceId
\r
20 resourceId :: Resource -> Long
\r
22 importJava "org.simantics.db.ReadGraph" where
\r
25 importJava "java.lang.Object" where
\r
27 resourceEquals :: Resource -> Resource -> Boolean
\r
29 resourceHash :: Resource -> Integer
\r
31 instance Eq Resource where
\r
32 (==) = resourceEquals
\r
34 instance Ord Resource where
\r
35 compare a b = compare (resourceId a) (resourceId b)
\r
37 instance Hashable Resource where
\r
38 hashP = hashP . resourceHash
\r
40 instance Show Resource where
\r
41 show r = "#" + show (resourceId r)
\r
43 instance Show Statement where
\r
44 show s = "#" + show (resourceId ( subjectOf s)) + "#" + show (resourceId ( predicateOf s)) + "#" + show (resourceId ( objectOf s))
\r
46 importJava "org.simantics.db.Statement" where
\r
47 "A statement is an edge in a semantic graph."
\r
50 @JavaName getSubject
\r
51 subjectOf :: Statement -> Resource
\r
52 @JavaName getPredicate
\r
53 predicateOf :: Statement -> Resource
\r
55 objectOf :: Statement -> Resource
\r
57 importJava "org.simantics.db.ReadGraph" where
\r
58 "Converts an absolute URI to a resource or returns `Nothing` if there is no such resource."
\r
59 @JavaName getPossibleResource
\r
60 possibleResource :: String -> <ReadGraph> (Maybe Resource)
\r
62 getDataType :: Resource -> <ReadGraph> Datatype
\r
64 @JavaName getSupertypes
\r
65 superTypesOf :: Resource -> <ReadGraph> Set.T Resource
\r
69 uriOfResource :: Resource -> <ReadGraph> String
\r
71 @JavaName getObjects
\r
72 objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
\r
74 @JavaName getStatements
\r
75 statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
\r
77 @JavaName hasStatement
\r
78 existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
\r
80 @JavaName hasStatement
\r
81 existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
\r
83 "Assumes that there is exactly one object with the given subject and predicate and returns it."
\r
84 @JavaName getSingleObject
\r
85 singleObject :: Resource -> Resource -> <ReadGraph> Resource
\r
88 If there is exactly one object with the given `subject` and `predicate`,
\r
89 `possibleObject subject predicate` returns it. Otherwise, it returns
\r
92 @JavaName getPossibleObject
\r
93 possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
\r
95 "Assumes that there is exactly one statement with the given subject and predicate and returns it."
\r
96 @JavaName getSingleStatement
\r
97 singleStatement :: Resource -> Resource -> <ReadGraph> Statement
\r
99 @JavaName getRelatedVariantValue
\r
100 relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
\r
102 @JavaName getRelatedValue
\r
103 relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
\r
105 @JavaName getRelatedValue
\r
106 untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
\r
108 @JavaName getRelatedValue2
\r
109 relatedValue2 :: Resource -> Resource -> <ReadGraph> a
\r
111 @JavaName getRelatedVariantValue2
\r
112 relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
\r
114 @JavaName getPossibleRelatedValue
\r
115 possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
\r
117 @JavaName getPossibleRelatedValue
\r
118 untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
\r
122 valueOf_ :: Resource -> Binding a -> <ReadGraph> a
\r
125 @JavaName getVariantValue
\r
126 variantValueOf_ :: Resource -> <ReadGraph> Variant
\r
129 untypedValueOf :: Resource -> <ReadGraph> Dynamic
\r
131 @JavaName getPossibleValue
\r
132 untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
\r
134 @JavaName getInverse
\r
135 inverseOf :: Resource -> <ReadGraph> Resource
\r
137 @JavaName getSingleType
\r
138 singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
\r
140 @JavaName getPossibleType
\r
141 possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
\r
143 "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
\r
144 isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
\r
145 isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
\r
146 isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
\r
148 getRootLibrary :: () -> <ReadGraph> Resource
\r
150 importJava "org.simantics.db.layer0.util.ExtendedUris" where
\r
151 "Converts an absolute URI to a resource"
\r
152 @JavaName resolveAbsoluteUri
\r
153 resource :: String -> <ReadGraph> Resource
\r
155 "Converts a relative URI to a resource starting from the given resource"
\r
156 @JavaName resolveRelativeUri
\r
157 relativeResource :: Resource -> String -> <ReadGraph> Resource
\r
159 "Reads the value of a literal that is an object with the given subject and predicate"
\r
161 relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
\r
162 relatedValue s p = relatedValue_ s p binding
\r
165 possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
\r
166 possibleRelatedValue s p = possibleRelatedValue_ s p binding
\r
168 class Browsable a where
\r
169 fromUri :: String -> <ReadGraph> a
\r
171 "Returns the URI of the given value."
\r
172 uriOf :: a -> <ReadGraph> String
\r
174 "Reads the name of the value."
\r
175 nameOf :: a -> <ReadGraph> String
\r
176 possibleNameOf :: a -> <ReadGraph> Maybe String
\r
178 valueOf :: Serializable v => a -> <ReadGraph> v
\r
180 variantValueOf :: a -> <ReadGraph> Variant
\r
182 children :: a -> <ReadGraph> [a]
\r
183 parent :: a -> <ReadGraph> a
\r
184 possibleParent :: a -> <ReadGraph> Maybe a
\r
186 child :: a -> String -> <ReadGraph> a
\r
187 possibleChild :: a -> String -> <ReadGraph> Maybe a
\r
189 instance Browsable Resource where
\r
191 uriOf = uriOfResource
\r
192 nameOf r = relatedValue r L0.HasName
\r
193 possibleNameOf r = possibleRelatedValue r L0.HasName
\r
195 valueOf r = valueOf_ r binding
\r
196 variantValueOf = variantValueOf_
\r
198 children r = r # L0.ConsistsOf
\r
199 parent r = singleObject r L0.PartOf
\r
200 possibleParent r = possibleObject r L0.PartOf
\r
202 possibleChild = possibleResourceChild
\r
203 child r n = fromJust (possibleResourceChild r n)
\r
205 importJava "org.simantics.db.WriteOnlyGraph" where
\r
206 markUndoPoint :: () -> <WriteGraph> ()
\r
208 importJava "org.simantics.db.WriteGraph" where
\r
209 "Creates a new resource."
\r
210 newResource :: () -> <WriteGraph> Resource
\r
211 "Adds a statement to the semantic graph."
\r
212 claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
\r
213 @JavaName claimLiteral
\r
214 claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
\r
215 @JavaName claimLiteral
\r
216 untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
\r
217 @JavaName claimValue
\r
218 untypedClaimValue :: Resource -> a -> <WriteGraph> ()
\r
219 @JavaName claimLiteral
\r
220 claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
\r
221 "Removes a statement with the given subject, predicate and object"
\r
222 deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
\r
224 denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
\r
226 denyAllStatements :: Resource -> <WriteGraph> ()
\r
227 newClusterSet :: Resource -> <WriteGraph> ()
\r
228 @JavaName denyValue
\r
229 denyValue :: Resource -> <WriteGraph> ()
\r
231 claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
\r
232 claimAssertion type_ predicate object = do
\r
233 ass = newResource ()
\r
234 claim ass L0.HasPredicate predicate
\r
235 claim ass L0.HasObject object
\r
236 claim type_ L0.Asserts ass
\r
238 "Sets the value of the literal that is an object with the given subject and predicate."
\r
240 claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
\r
241 claimRelatedValue s p v = claimRelatedValue_ s p v binding
\r
244 claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
\r
245 claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
\r
247 importJava "org.simantics.layer0.utils.direct.GraphUtils" where
\r
248 @JavaName getPossibleChild
\r
249 possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
\r
251 @deprecated "Use function children instead."
\r
252 resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
\r
253 resourceChildrenOf r = r # L0.ConsistsOf
\r
255 importJava "org.simantics.db.common.utils.OrderedSetUtils" where
\r
257 addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
\r
260 setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
\r
262 @JavaName getSingleOwnerList
\r
263 parentOrderedSet :: Resource -> <ReadGraph> Resource
\r
266 elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
\r
268 importJava "org.simantics.db.common.utils.ListUtils" where
\r
270 elementsOfList :: Resource -> <ReadGraph> [Resource]
\r
272 importJava "org.simantics.db.common.utils.CommonDBUtils" where
\r
273 isParent :: Resource -> Resource -> <ReadGraph> Boolean
\r
274 possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
\r
275 possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
\r
276 objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
\r
277 possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
\r
279 importJava "org.simantics.db.common.utils.NameUtils" where
\r
280 findFreshName :: String -> Resource -> <ReadGraph> String
\r
281 findFreshEscapedName :: String -> Resource -> <ReadGraph> String
\r
283 "`subject # predicate` returns all objects with the given `subject` and `predicate`."
\r
284 (#) :: Resource -> Resource -> <ReadGraph> [Resource]
\r
285 subject # predicate = collectionToList $ objects_ subject predicate
\r
287 "statements subject predicate` returns all statements with the given subject and predicate."
\r
288 statements :: Resource -> Resource -> <ReadGraph> [Statement]
\r
289 statements subject predicate = collectionToList $ statements_ subject predicate
\r
291 importJava "org.simantics.scl.db.SCLFunctions" where
\r
292 "Executes a read transaction and waits that it completes."
\r
293 syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
\r
294 "Executes a write transaction and waits that it completes."
\r
295 syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
\r
296 "Executes a delayed write transaction and waits that it completes."
\r
297 delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
\r
299 "Begins a read transaction and immediately returns."
\r
300 asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
\r
301 "Begins a write transaction and immediately returns."
\r
302 asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
\r
304 virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
\r
305 virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
\r
307 safeExec :: (() -> <Proc> a) -> <Proc> a
\r
308 activateOnce :: Resource -> <Proc> ()
\r
309 syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
\r
310 resourceFromId :: Long -> <ReadGraph> Resource
\r
311 enableDependencies :: () -> <WriteGraph> ()
\r
312 disableDependencies :: () -> <WriteGraph> ()
\r
314 unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
\r
315 unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
\r
317 importJava "org.simantics.db.layer0.util.Layer0Utils" where
\r
318 undo :: () -> <Proc> String
\r
319 undoOperations :: Integer -> <Proc> String
\r
320 redo :: () -> <Proc> String
\r
321 queryDebugSupport :: String -> <Proc> String
\r
322 queryListSupport :: String -> <Proc> String
\r
323 addCommentMetadata :: String -> <WriteGraph> ()
\r
324 sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
\r
325 makeSynchronous :: Boolean -> <ReadGraph> ()
\r
326 listOntologies :: () -> <ReadGraph> [Resource]
\r
330 copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
\r
331 @JavaName getPossiblePredicateByName
\r
332 possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
\r
334 copyTo :: Resource -> Resource -> <WriteGraph> Resource
\r
335 copyTo targetContainer source = do
\r
336 (collectionToList $ copyTo_ targetContainer source)!0
\r
338 importJava "org.simantics.db.common.utils.CommonDBUtils" where
\r
339 selectClusterSet :: Resource -> <WriteGraph> ()
\r
341 importJava "org.simantics.Simantics" where
\r
342 @JavaName getProjectResource
\r
343 currentProject :: () -> /*<Proc>*/ Resource
\r
345 // Move to somewhere more generic module
\r
346 importJava "java.util.Collection" where
\r
349 importJava "java.util.ArrayList" where
\r
351 collectionToList :: Collection a -> [a]
\r
353 collectionToList :: Collection a -> [a]
\r
354 collectionToList x = Java.unsafeCoerce x
\r
357 resourceToCollection :: a -> Collection a
\r
358 resourceToCollection x = Java.unsafeCoerce x
\r
360 newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
\r
361 newEntity entitySpecs = do
\r
362 entity = newResource ()
\r
363 for entitySpecs (\spec -> spec entity)
\r
366 updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
\r
367 updateEntity entity entitySpecs = do
\r
368 for entitySpecs (\spec -> spec entity)
\r
371 hasStatement predicate object entity =
\r
372 claim entity predicate object
\r
373 hasProperty relation value entity =
\r
374 claimRelatedValue entity relation value
\r
375 hasPossibleProperty relation value entity =
\r
377 Just v -> claimRelatedValue entity relation v
\r
379 hasTypedProperty relation value t entity =
\r
380 claimRelatedValueWithType entity relation t value
\r
381 hasName (n :: String) = hasProperty L0.HasName n
\r
382 hasLabel (l :: String) = hasProperty L0.HasLabel l
\r
383 hasType t = hasStatement L0.InstanceOf t
\r
384 hasParent p = hasStatement L0.PartOf p
\r
386 importJava "org.simantics.databoard.util.URIStringUtils" where
\r
387 @JavaName splitURISCL
\r
388 splitURI :: String -> [String]
\r
390 escapeURI :: String -> String
\r
392 unescapeURI :: String -> String
\r
395 importJava "org.simantics.db.layer0.request.ActiveModels" where
\r
396 @JavaName getPossibleActiveModel
\r
397 activeModel :: Resource -> <ReadGraph> Maybe Resource
\r
399 "Gives the current active model."
\r
400 currentModel :: <ReadGraph> Resource
\r
401 currentModel = match activeModel (currentProject ()) with
\r
402 Just model -> model
\r
403 Nothing -> fail "No active model."
\r
405 startUndoPoint :: String -> <WriteGraph> ()
\r
406 startUndoPoint string = do
\r
408 addCommentMetadata (string)
\r
412 lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
\r
413 lift1Read f x = syncRead (\_ -> f x)
\r
416 lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
\r
417 lift2Read f x y = syncRead (\_ -> f x y)
\r
420 lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
\r
421 lift3Read f x y z = syncRead (\_ -> f x y z)
\r
424 lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
\r
425 lift1Write f x = syncWrite (\_ -> f x)
\r
428 lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
\r
429 lift2Write f x y = syncWrite (\_ -> f x y)
\r
432 lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
\r
433 lift3Write f x y z = syncWrite (\_ -> f x y z)
\r
436 Returns a child Browsable of the specified parent that has that is the child
\r
437 of the specified parent with the specified name path.
\r
439 childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
\r
440 childWithPath parent path =
\r
441 foldl (\r name -> match possibleChild r name with
\r
443 Nothing -> fail ("Didn't find " + name + ".")
\r
448 Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
\r
451 possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
\r
452 possibleChildWithPath parent path =
\r
453 foldl (\r name -> match r with
\r
454 Just c -> possibleChild c name
\r
455 Nothing -> Nothing
\r