]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/scl/Simantics/DB.scl
Lots of fixes for PrettyPrintTG
[simantics/platform.git] / bundles / org.simantics.scl.db / scl / Simantics / DB.scl
index ae092cfa8d62aafe6cd203d29e20e9a7f9885903..df556086ed6ab61d0aa1458f5d6da06e24955172 100644 (file)
-import "JavaBuiltin" as Java\r
-include "http://www.simantics.org/Layer0-1.1" as L0\r
-\r
-infixl 5 (#)\r
\r
-effect ReadGraph\r
-    "graph"\r
-    "org.simantics.db.ReadGraph"\r
-\r
-effect WriteGraph\r
-    "graph"\r
-    "org.simantics.db.WriteGraph"\r
-    \r
-importJava "org.simantics.db.Resource" where\r
-    "A resource is a node in a semantic graph."\r
-    data Resource\r
-    \r
-    "Returns the 64-bit unique identifier of the resource."\r
-    @JavaName getResourceId\r
-    resourceId :: Resource -> Long\r
-    \r
-importJava "org.simantics.db.ReadGraph" where\r
-    data ReadGraphX\r
-    \r
-instance Ord Resource where\r
-    compare a b = compare (resourceId a) (resourceId b)\r
-  \r
-instance Show Resource where\r
-    show r = "#" + show (resourceId r)\r
-\r
-instance Show Statement where\r
-    show s = "#" + show (resourceId ( subjectOf s)) + "#" + show (resourceId ( predicateOf s)) + "#" + show (resourceId ( objectOf s))\r
-\r
-importJava "org.simantics.db.Statement" where\r
-    "A statement is an edge in a semantic graph."\r
-    data Statement\r
-    \r
-    @JavaName getSubject\r
-    subjectOf :: Statement -> Resource\r
-    @JavaName getPredicate\r
-    predicateOf :: Statement -> Resource\r
-    @JavaName getObject\r
-    objectOf :: Statement -> Resource\r
-    \r
-importJava "org.simantics.db.ReadGraph" where\r
-    "Converts an absolute URI to a resource or returns `Nothing` if there is no such resource."\r
-    @JavaName getPossibleResource\r
-    possibleResource :: String -> <ReadGraph> (Maybe Resource)\r
-    \r
-    getDataType :: Resource -> <ReadGraph> Datatype\r
-\r
-    @JavaName getSupertypes\r
-    superTypesOf :: Resource -> <ReadGraph> Set.T Resource\r
-    \r
-    @private\r
-    @JavaName getURI\r
-    uriOfResource :: Resource -> <ReadGraph> String\r
-    \r
-    @JavaName getObjects\r
-    objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource\r
-    \r
-    @JavaName getStatements\r
-    statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement\r
-    \r
-    @JavaName hasStatement\r
-    existsStatement :: Resource -> Resource -> <ReadGraph> Boolean\r
-    \r
-    @JavaName hasStatement\r
-    existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean\r
-\r
-    "Assumes that there is exactly one object with the given subject and predicate and returns it."\r
-    @JavaName getSingleObject\r
-    singleObject :: Resource -> Resource -> <ReadGraph> Resource\r
-\r
-    """\r
-    If there is exactly one object with the given `subject` and `predicate`,\r
-    `possibleObject subject predicate` returns it. Otherwise, it returns\r
-    `Nothing`.\r
-    """\r
-    @JavaName getPossibleObject\r
-    possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource\r
-\r
-    "Assumes that there is exactly one statement with the given subject and predicate and returns it."\r
-    @JavaName getSingleStatement\r
-    singleStatement :: Resource -> Resource -> <ReadGraph> Statement\r
-\r
-    @JavaName getRelatedVariantValue\r
-    relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant\r
-\r
-    @JavaName getRelatedValue\r
-    relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a\r
-    \r
-    @JavaName getRelatedValue\r
-    untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a\r
-\r
-    @JavaName getRelatedValue2\r
-    relatedValue2 :: Resource -> Resource -> <ReadGraph> a\r
-\r
-    @JavaName getRelatedVariantValue2\r
-    relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant\r
-    \r
-    @JavaName getPossibleRelatedValue\r
-    possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a\r
-    \r
-    @JavaName getPossibleRelatedValue\r
-    untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a\r
-    \r
-    @private\r
-    @JavaName getValue\r
-    valueOf_ :: Resource -> Binding a -> <ReadGraph> a\r
-    \r
-    @private\r
-    @JavaName getVariantValue\r
-    variantValueOf_ :: Resource -> <ReadGraph> Variant\r
-    \r
-    @JavaName getValue\r
-    untypedValueOf :: Resource -> <ReadGraph> Dynamic\r
-    \r
-    @JavaName getPossibleValue\r
-    untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic\r
-    \r
-    @JavaName getInverse\r
-    inverseOf :: Resource -> <ReadGraph> Resource\r
-    \r
-    @JavaName getSingleType\r
-    singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource\r
-    \r
-    @JavaName getPossibleType\r
-    possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource\r
-    \r
-    "`isInstanceOf r t` returns true, if `r` is an instance of `t`"\r
-    isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean\r
-    isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean\r
-    isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean\r
-    \r
-    getRootLibrary :: () -> <ReadGraph> Resource\r
-\r
-importJava "org.simantics.db.layer0.util.ExtendedUris" where\r
-    "Converts an absolute URI to a resource"\r
-    @JavaName resolveAbsoluteUri    \r
-    resource :: String -> <ReadGraph> Resource\r
-    \r
-    "Converts a relative URI to a resource starting from the given resource"\r
-    @JavaName resolveRelativeUri\r
-    relativeResource :: Resource -> String -> <ReadGraph> Resource\r
-\r
-"Reads the value of a literal that is an object with the given subject and predicate"\r
-@inline\r
-relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a\r
-relatedValue s p = relatedValue_ s p binding\r
-\r
-@inline\r
-possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a\r
-possibleRelatedValue s p = possibleRelatedValue_ s p binding\r
-\r
-class Browsable a where\r
-    fromUri :: String -> <ReadGraph> a\r
-\r
-    "Returns the URI of the given value."\r
-    uriOf :: a -> <ReadGraph> String\r
-  \r
-    "Reads the name of the value."\r
-    nameOf :: a -> <ReadGraph> String\r
-    possibleNameOf :: a -> <ReadGraph> Maybe String\r
-    \r
-    valueOf :: Serializable v => a -> <ReadGraph> v\r
-    \r
-    variantValueOf :: a -> <ReadGraph> Variant \r
-    \r
-    children :: a -> <ReadGraph> [a]\r
-    parent :: a -> <ReadGraph> a\r
-    possibleParent :: a -> <ReadGraph> Maybe a\r
-    \r
-    child :: a -> String -> <ReadGraph> a\r
-    possibleChild :: a -> String -> <ReadGraph> Maybe a\r
-    \r
-instance Browsable Resource where\r
-    fromUri = resource\r
-    uriOf = uriOfResource\r
-    nameOf r = relatedValue r L0.HasName\r
-    possibleNameOf r = possibleRelatedValue r L0.HasName\r
-    @inline\r
-    valueOf r = valueOf_ r binding\r
-    variantValueOf = variantValueOf_\r
-    \r
-    children r = r # L0.ConsistsOf\r
-    parent r = singleObject r L0.PartOf\r
-    possibleParent r = possibleObject r L0.PartOf\r
-    \r
-    possibleChild = possibleResourceChild\r
-    child r n = fromJust (possibleResourceChild r n)\r
-\r
-importJava "org.simantics.db.WriteOnlyGraph" where\r
-    markUndoPoint :: () -> <WriteGraph> ()\r
-\r
-importJava "org.simantics.db.WriteGraph" where\r
-    "Creates a new resource."\r
-    newResource :: () -> <WriteGraph> Resource\r
-    "Adds a statement to the semantic graph."\r
-    claim :: Resource -> Resource -> Resource -> <WriteGraph> ()\r
-    @JavaName claimLiteral\r
-    claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()\r
-    @JavaName claimLiteral\r
-    untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()\r
-    @JavaName claimValue\r
-    untypedClaimValue :: Resource -> a -> <WriteGraph> ()\r
-    @JavaName claimLiteral\r
-    claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()\r
-    "Removes a statement with the given subject, predicate and object"\r
-    deny :: Resource -> Resource -> Resource -> <WriteGraph> ()\r
-    @JavaName deny\r
-    denyByPredicate :: Resource -> Resource -> <WriteGraph> ()\r
-    @JavaName deny\r
-    denyAllStatements :: Resource -> <WriteGraph> ()\r
-    newClusterSet :: Resource -> <WriteGraph> ()\r
-    @JavaName denyValue\r
-    denyValue :: Resource -> <WriteGraph> ()\r
-\r
-claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()\r
-claimAssertion type_ predicate object = do\r
-    ass = newResource ()\r
-    claim ass L0.HasPredicate predicate\r
-    claim ass L0.HasObject object\r
-    claim type_ L0.Asserts ass\r
-\r
-"Sets the value of the literal that is an object with the given subject and predicate."\r
-@inline\r
-claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()\r
-claimRelatedValue s p v = claimRelatedValue_ s p v binding\r
-\r
-@inline\r
-claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()\r
-claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding\r
-\r
-importJava "org.simantics.layer0.utils.direct.GraphUtils" where\r
-    @JavaName getPossibleChild\r
-    possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)\r
-\r
-@deprecated "Use function children instead."\r
-resourceChildrenOf :: Resource -> <ReadGraph> [Resource]\r
-resourceChildrenOf r = r # L0.ConsistsOf\r
-\r
-importJava "org.simantics.db.common.utils.OrderedSetUtils" where\r
-    @JavaName add\r
-    addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean\r
-    \r
-    @JavaName set\r
-    setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean\r
-    \r
-    @JavaName getSingleOwnerList\r
-    parentOrderedSet :: Resource -> <ReadGraph> Resource\r
-    \r
-    @JavaName toList\r
-    elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]\r
-    \r
-importJava "org.simantics.db.common.utils.ListUtils" where\r
-    @JavaName toList\r
-    elementsOfList :: Resource -> <ReadGraph> [Resource]\r
-\r
-importJava "org.simantics.db.common.utils.CommonDBUtils" where\r
-    isParent :: Resource -> Resource -> <ReadGraph> Boolean\r
-    possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String\r
-    possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer\r
-    objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]\r
-    possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource\r
-\r
-importJava "org.simantics.db.common.utils.NameUtils" where\r
-    findFreshName :: String -> Resource -> <ReadGraph> String\r
-    findFreshEscapedName :: String -> Resource -> <ReadGraph> String\r
-\r
-"`subject # predicate` returns all objects with the given `subject` and `predicate`."\r
-(#) :: Resource -> Resource -> <ReadGraph> [Resource]\r
-subject # predicate = collectionToList $ objects_ subject predicate\r
-\r
-"statements subject predicate` returns all statements with the given subject and predicate."\r
-statements :: Resource -> Resource -> <ReadGraph> [Statement]\r
-statements subject predicate = collectionToList $ statements_ subject predicate\r
-\r
-importJava "org.simantics.scl.db.SCLFunctions" where\r
-    "Executes a read transaction and waits that it completes."\r
-    syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a\r
-    "Executes a write transaction and waits that it completes."\r
-    syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a\r
-    "Executes a delayed write transaction and waits that it completes."\r
-    delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a\r
-    \r
-    "Begins a read transaction and immediately returns."\r
-    asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()\r
-    "Begins a write transaction and immediately returns."\r
-    asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()\r
-    \r
-    virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a\r
-    virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a\r
-    \r
-    safeExec :: (() -> <Proc> a) -> <Proc> a\r
-    activateOnce :: Resource -> <Proc> ()\r
-    syncActivateOnce :: Resource -> <WriteGraph, Proc> ()\r
-    resourceFromId :: Long -> <ReadGraph> Resource\r
-    enableDependencies :: () -> <WriteGraph> ()\r
-    disableDependencies :: () -> <WriteGraph> ()\r
-    \r
-    unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b\r
-    unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b\r
-\r
-importJava "org.simantics.db.layer0.util.Layer0Utils" where\r
-    undo :: () -> <Proc> String\r
-    undoOperations :: Integer -> <Proc> String\r
-    redo :: () -> <Proc> String\r
-    queryDebugSupport :: String -> <Proc> String\r
-    queryListSupport :: String -> <Proc> String\r
-    addCommentMetadata :: String -> <WriteGraph> ()\r
-    sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]\r
-    makeSynchronous :: Boolean -> <ReadGraph> ()\r
-    listOntologies :: () -> <ReadGraph> [Resource]\r
-\r
-    @private\r
-    @JavaName copyTo\r
-    copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource\r
-    @JavaName getPossiblePredicateByName\r
-    possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource\r
-\r
-copyTo :: Resource -> Resource -> <WriteGraph> Resource\r
-copyTo targetContainer source = do\r
-    (collectionToList $ copyTo_ targetContainer source)!0\r
-\r
-importJava "org.simantics.db.common.utils.CommonDBUtils" where\r
-    selectClusterSet :: Resource -> <WriteGraph> () \r
-\r
-importJava "org.simantics.Simantics" where\r
-    @JavaName getProjectResource\r
-    currentProject :: () -> /*<Proc>*/ Resource\r
-\r
-// Move to somewhere more generic module\r
-importJava "java.util.Collection" where\r
-    data Collection a\r
-\r
-importJava "java.util.ArrayList" where\r
-    @JavaName "<init>"\r
-    collectionToList :: Collection a -> [a]\r
-/*@macro\r
-collectionToList :: Collection a -> [a]\r
-collectionToList x = Java.unsafeCoerce x\r
-*/ \r
-\r
-resourceToCollection :: a -> Collection a\r
-resourceToCollection x = Java.unsafeCoerce x\r
-\r
-newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource\r
-newEntity entitySpecs = do\r
-    entity = newResource ()\r
-    for entitySpecs (\spec -> spec entity)\r
-    entity\r
-\r
-updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource\r
-updateEntity entity entitySpecs = do\r
-    for entitySpecs (\spec -> spec entity)\r
-    entity\r
-\r
-hasStatement predicate object entity = \r
-    claim entity predicate object\r
-hasProperty relation value entity = \r
-    claimRelatedValue entity relation value\r
-hasPossibleProperty relation value entity =\r
-    match value with\r
-      Just v -> claimRelatedValue entity relation v\r
-      Nothing -> ()\r
-hasTypedProperty relation value t entity = \r
-    claimRelatedValueWithType entity relation t value        \r
-hasName (n :: String) = hasProperty L0.HasName n\r
-hasLabel (l :: String) = hasProperty L0.HasLabel l\r
-hasType t = hasStatement L0.InstanceOf t\r
-hasParent p = hasStatement L0.PartOf p\r
-\r
-importJava "org.simantics.databoard.util.URIStringUtils" where\r
-    @JavaName splitURISCL\r
-    splitURI :: String -> [String]\r
-    @JavaName escape\r
-    escapeURI :: String -> String\r
-    @JavaName unescape\r
-    unescapeURI :: String -> String\r
-\r
-@private\r
-importJava "org.simantics.db.layer0.request.ActiveModels" where\r
-    @JavaName getPossibleActiveModel\r
-    activeModel :: Resource -> <ReadGraph> Maybe Resource\r
-\r
-"Gives the current active model."\r
-currentModel :: <ReadGraph> Resource\r
-currentModel = match activeModel (currentProject ()) with\r
-    Just model -> model\r
-    Nothing -> fail "No active model."\r
-\r
-startUndoPoint :: String -> <WriteGraph> ()\r
-startUndoPoint string = do\r
-    markUndoPoint ()\r
-    addCommentMetadata (string)\r
-    ()\r
-\r
-@inline\r
-lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)\r
-lift1Read f x = syncRead (\_ -> f x)\r
-\r
-@inline\r
-lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)\r
-lift2Read f x y = syncRead (\_ -> f x y)\r
-\r
-@inline\r
-lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)\r
-lift3Read f x y z = syncRead (\_ -> f x y z)\r
-\r
-@inline\r
-lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)\r
-lift1Write f x = syncWrite (\_ -> f x)\r
-\r
-@inline\r
-lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)\r
-lift2Write f x y = syncWrite (\_ -> f x y)\r
-\r
-@inline\r
-lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)\r
-lift3Write f x y z = syncWrite (\_ -> f x y z)\r
-\r
-"""\r
-Returns a child Browsable of the specified parent that has that is the child\r
-of the specified parent with the specified name path.\r
-"""\r
-childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a\r
-childWithPath parent path = \r
-    foldl (\r name -> match possibleChild r name with\r
-              Just c -> c\r
-              Nothing -> fail ("Didn't find " + name + ".") \r
-          ) \r
-          parent path\r
-\r
-"""\r
-Like `childWithPath` but returns Maybe a which will be `Nothing` if no child\r
-was found.\r
-"""\r
-possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a\r
-possibleChildWithPath parent path = \r
-    foldl (\r name -> match r with\r
-              Just c -> possibleChild c name\r
-              Nothing -> Nothing \r
-          ) \r
-          (Just parent) path\r
+import "JavaBuiltin" as Java
+include "http://www.simantics.org/Layer0-1.1" as L0
+import "Map" as Map
+
+infixl 5 (#)
+effect ReadGraph
+    "graph"
+    "org.simantics.db.ReadGraph"
+
+effect WriteGraph
+    "graph"
+    "org.simantics.db.WriteGraph"
+    
+importJava "org.simantics.db.Resource" where
+    "A resource is a node in a semantic graph."
+    data Resource
+    
+    "Returns the 64-bit unique identifier of the resource."
+    @JavaName getResourceId
+    resourceId :: Resource -> Long
+    
+importJava "org.simantics.db.ReadGraph" where
+    data ReadGraphX
+    
+instance Ord Resource where
+    compare a b = compare (resourceId a) (resourceId b)
+  
+instance Show Resource where
+    show r = "#" + show (resourceId r)
+
+instance Show Statement where
+    show s = "#" + show (resourceId ( subjectOf s)) + "#" + show (resourceId ( predicateOf s)) + "#" + show (resourceId ( objectOf s))
+
+importJava "org.simantics.db.Statement" where
+    "A statement is an edge in a semantic graph."
+    data Statement
+    
+    @JavaName getSubject
+    subjectOf :: Statement -> Resource
+    @JavaName getPredicate
+    predicateOf :: Statement -> Resource
+    @JavaName getObject
+    objectOf :: Statement -> Resource
+    
+    isAsserted :: Statement -> Resource -> <ReadGraph> Boolean
+    
+importJava "org.simantics.db.ReadGraph" where
+    "Converts an absolute URI to a resource or returns `Nothing` if there is no such resource."
+    @JavaName getPossibleResource
+    possibleResource :: String -> <ReadGraph> (Maybe Resource)
+    
+    getDataType :: Resource -> <ReadGraph> Datatype
+
+    @JavaName getSupertypes
+    superTypesOf :: Resource -> <ReadGraph> Set.T Resource
+    
+    @private
+    @JavaName getURI
+    uriOfResource :: Resource -> <ReadGraph> String
+    
+    @JavaName getObjects
+    objects_ :: Resource -> Resource -> <ReadGraph> Collection Resource
+    
+    @JavaName getStatements
+    statements_ :: Resource -> Resource -> <ReadGraph> Collection Statement
+    
+    @JavaName hasStatement
+    existsStatement :: Resource -> Resource -> <ReadGraph> Boolean
+    
+    @JavaName hasStatement
+    existsStatement3 :: Resource -> Resource -> Resource -> <ReadGraph> Boolean
+
+    "Assumes that there is exactly one object with the given subject and predicate and returns it."
+    @JavaName getSingleObject
+    singleObject :: Resource -> Resource -> <ReadGraph> Resource
+
+    """
+    If there is exactly one object with the given `subject` and `predicate`,
+    `possibleObject subject predicate` returns it. Otherwise, it returns
+    `Nothing`.
+    """
+    @JavaName getPossibleObject
+    possibleObject :: Resource -> Resource -> <ReadGraph> Maybe Resource
+
+    "Assumes that there is exactly one statement with the given subject and predicate and returns it."
+    @JavaName getSingleStatement
+    singleStatement :: Resource -> Resource -> <ReadGraph> Statement
+
+    @JavaName getPossibleStatement
+    possibleStatement :: Resource -> Resource -> <ReadGraph> Maybe Statement
+
+    @JavaName getRelatedVariantValue
+    relatedVariantValue :: Resource -> Resource -> <ReadGraph> Variant
+
+    @JavaName getRelatedValue
+    relatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> a
+    
+    @JavaName getRelatedValue
+    untypedRelatedValue :: Resource -> Resource -> <ReadGraph> a
+
+    @JavaName getRelatedValue2
+    relatedValue2 :: Resource -> Resource -> <ReadGraph> a
+
+    @JavaName getRelatedVariantValue2
+    relatedVariantValue2 :: Resource -> Resource -> <ReadGraph> Variant
+    
+    @JavaName getPossibleRelatedValue
+    possibleRelatedValue_ :: Resource -> Resource -> Binding a -> <ReadGraph> Maybe a
+    
+    @JavaName getPossibleRelatedValue
+    untypedPossibleRelatedValue :: Resource -> Resource -> <ReadGraph> Maybe a
+    
+    @private
+    @JavaName getValue
+    valueOf_ :: Resource -> Binding a -> <ReadGraph> a
+    
+    @private
+    @JavaName getVariantValue
+    variantValueOf_ :: Resource -> <ReadGraph> Variant
+    
+    @JavaName getValue
+    untypedValueOf :: Resource -> <ReadGraph> Dynamic
+    
+    @JavaName getPossibleValue
+    untypedPossibleValueOf :: Resource -> <ReadGraph> Maybe Dynamic
+    
+    @JavaName getInverse
+    inverseOf :: Resource -> <ReadGraph> Resource
+    
+    @JavaName getSingleType
+    singleTypeOf :: Resource -> Resource -> <ReadGraph> Resource
+    
+    @JavaName getPossibleType
+    possibleTypeOf :: Resource -> Resource -> <ReadGraph> Maybe Resource
+    
+    "`isInstanceOf r t` returns true, if `r` is an instance of `t`"
+    isInstanceOf :: Resource -> Resource -> <ReadGraph> Boolean
+    isSubrelationOf :: Resource -> Resource -> <ReadGraph> Boolean
+    isInheritedFrom :: Resource -> Resource -> <ReadGraph> Boolean
+    
+    getRootLibrary :: () -> <ReadGraph> Resource
+
+importJava "org.simantics.db.layer0.util.ExtendedUris" where
+    "Converts an absolute URI to a resource"
+    @JavaName resolveAbsoluteUri    
+    resource :: String -> <ReadGraph> Resource
+    
+    "Converts a relative URI to a resource starting from the given resource"
+    @JavaName resolveRelativeUri
+    relativeResource :: Resource -> String -> <ReadGraph> Resource
+
+"Reads the value of a literal that is an object with the given subject and predicate"
+@inline
+relatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> a
+relatedValue s p = relatedValue_ s p binding
+
+@inline
+possibleRelatedValue :: Serializable a => Resource -> Resource -> <ReadGraph> Maybe a
+possibleRelatedValue s p = possibleRelatedValue_ s p binding
+
+class Browsable a where
+    fromUri :: String -> <ReadGraph> a
+
+    "Returns the URI of the given value."
+    uriOf :: a -> <ReadGraph> String
+  
+    "Reads the name of the value."
+    nameOf :: a -> <ReadGraph> String
+    possibleNameOf :: a -> <ReadGraph> Maybe String
+    
+    valueOf :: Serializable v => a -> <ReadGraph> v
+    
+    variantValueOf :: a -> <ReadGraph> Variant 
+    
+    children :: a -> <ReadGraph> [a]
+    parent :: a -> <ReadGraph> a
+    possibleParent :: a -> <ReadGraph> Maybe a
+    
+    child :: a -> String -> <ReadGraph> a
+    possibleChild :: a -> String -> <ReadGraph> Maybe a
+    
+instance Browsable Resource where
+    fromUri = resource
+    uriOf = uriOfResource
+    nameOf r = relatedValue r L0.HasName
+    possibleNameOf r = possibleRelatedValue r L0.HasName
+    @inline
+    valueOf r = valueOf_ r binding
+    variantValueOf = variantValueOf_
+    
+    children r = r # L0.ConsistsOf
+    parent r = singleObject r L0.PartOf
+    possibleParent r = possibleObject r L0.PartOf
+    
+    possibleChild = possibleResourceChild
+    child r n = fromJust (possibleResourceChild r n)
+
+importJava "org.simantics.db.WriteOnlyGraph" where
+    markUndoPoint :: () -> <WriteGraph> ()
+
+importJava "org.simantics.db.WriteGraph" where
+    "Creates a new resource."
+    newResource :: () -> <WriteGraph> Resource
+    "Adds a statement to the semantic graph."
+    claim :: Resource -> Resource -> Resource -> <WriteGraph> ()
+    @JavaName claimLiteral
+    claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
+    @JavaName claimLiteral
+    untypedClaimRelatedValue :: Resource -> Resource -> a -> <WriteGraph> ()
+    @JavaName claimValue
+    untypedClaimValue :: Resource -> a -> <WriteGraph> ()
+    @JavaName claimLiteral
+    claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> <WriteGraph> ()
+    "Removes a statement with the given subject, predicate and object"
+    deny :: Resource -> Resource -> Resource -> <WriteGraph> ()
+    @JavaName deny
+    denyByPredicate :: Resource -> Resource -> <WriteGraph> ()
+    @JavaName deny
+    denyAllStatements :: Resource -> <WriteGraph> ()
+    newClusterSet :: Resource -> <WriteGraph> ()
+    @JavaName denyValue
+    denyValue :: Resource -> <WriteGraph> ()
+
+claimAssertion :: Resource -> Resource -> Resource -> <WriteGraph> ()
+claimAssertion type_ predicate object = do
+    ass = newResource ()
+    claim ass L0.HasPredicate predicate
+    claim ass L0.HasObject object
+    claim type_ L0.Asserts ass
+
+"Sets the value of the literal that is an object with the given subject and predicate."
+@inline
+claimRelatedValue :: Serializable a => Resource -> Resource -> a -> <WriteGraph> ()
+claimRelatedValue s p v = claimRelatedValue_ s p v binding
+
+@inline
+claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> <WriteGraph> ()
+claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding
+
+importJava "org.simantics.layer0.utils.direct.GraphUtils" where
+    @JavaName getPossibleChild
+    possibleResourceChild :: Resource -> String -> <ReadGraph> (Maybe Resource)
+
+@deprecated "Use function children instead."
+resourceChildrenOf :: Resource -> <ReadGraph> [Resource]
+resourceChildrenOf r = r # L0.ConsistsOf
+
+importJava "org.simantics.db.common.utils.OrderedSetUtils" where
+    @JavaName add
+    addToOrderedSet :: Resource -> Resource -> <WriteGraph> Boolean
+    
+    @JavaName set
+    setOrderedSet :: Resource -> [Resource] -> <WriteGraph> Boolean
+    
+    @JavaName getSingleOwnerList
+    parentOrderedSet :: Resource -> <ReadGraph> Resource
+    
+    @JavaName toList
+    elementsOfOrderedSet :: Resource -> <ReadGraph> [Resource]
+    
+importJava "org.simantics.db.common.utils.ListUtils" where
+    @JavaName toList
+    elementsOfList :: Resource -> <ReadGraph> [Resource]
+    
+    @JavaName create
+    createList :: [Resource] -> <WriteGraph> Resource
+    
+    @javaName insertBack
+    insertBack :: Resource -> [Resource] -> <WriteGraph> ()
+    
+    @javaName removeElement
+    removeElement :: Resource -> Resource -> <WriteGraph> Boolean
+    
+    @javaName swapWithPrevious
+    swapWithPrevious :: Resource -> Resource -> <WriteGraph> Boolean
+
+    @javaName swapWithNext
+    swapWithNext :: Resource -> Resource -> <WriteGraph> Boolean
+
+
+importJava "org.simantics.db.common.utils.CommonDBUtils" where
+    isParent :: Resource -> Resource -> <ReadGraph> Boolean
+    possibleRelatedString :: Resource -> Resource -> <ReadGraph> Maybe String
+    possibleRelatedInteger :: Resource -> Resource -> <ReadGraph> Maybe Integer
+    objectsWithType :: Resource -> Resource -> Resource -> <ReadGraph> [Resource]
+    possibleObjectWithType :: Resource -> Resource -> Resource -> <ReadGraph> Maybe Resource
+
+importJava "org.simantics.db.common.utils.NameUtils" where
+    findFreshName :: String -> Resource -> <ReadGraph> String
+    findFreshEscapedName :: String -> Resource -> <ReadGraph> String
+
+"`subject # predicate` returns all objects with the given `subject` and `predicate`."
+(#) :: Resource -> Resource -> <ReadGraph> [Resource]
+subject # predicate = collectionToList $ objects_ subject predicate
+
+"statements subject predicate` returns all statements with the given subject and predicate."
+statements :: Resource -> Resource -> <ReadGraph> [Statement]
+statements subject predicate = collectionToList $ statements_ subject predicate
+
+importJava "org.simantics.scl.db.SCLFunctions" where
+    "Executes a read transaction and waits that it completes."
+    syncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> a
+    "Executes a write transaction and waits that it completes."
+    syncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> a
+    "Executes a delayed write transaction and waits that it completes."
+    delayedSyncWrite :: (() -> <Proc,WriteGraph> a) -> <Proc> a
+    
+    "Begins a read transaction and immediately returns."
+    asyncRead :: (() -> <Proc,ReadGraph> a) -> <Proc> ()
+    "Begins a write transaction and immediately returns."
+    asyncWrite :: (() -> <Proc,ReadGraph,WriteGraph> a) -> <Proc> ()
+    
+    virtualSyncWriteMem :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
+    virtualSyncWriteWS :: String -> (() -> <ReadGraph,WriteGraph> a) -> <WriteGraph> a
+    
+    safeExec :: (() -> <Proc> a) -> <Proc> a
+    activateOnce :: Resource -> <Proc> ()
+    syncActivateOnce :: Resource -> <WriteGraph, Proc> ()
+    resourceFromId :: Long -> <ReadGraph> Resource
+    enableDependencies :: () -> <WriteGraph> ()
+    disableDependencies :: () -> <WriteGraph> ()
+    
+    unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
+    unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
+
+    "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
+    subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
+    "Makes a new read request with given procedure for calculating the result. The request is always cached."
+    subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
+    "Tries to convert the given Dynamic value to a value with the inferred type"
+    possibleFromDynamic :: Typeable a => String -> Dynamic -> Maybe a
+
+importJava "org.simantics.db.layer0.util.Layer0Utils" where
+    undo :: () -> <Proc> String
+    undoOperations :: Integer -> <Proc> String
+    redo :: () -> <Proc> String
+    queryDebugSupport :: String -> <Proc> String
+    queryListSupport :: String -> <Proc> String
+    addCommentMetadata :: String -> <WriteGraph> ()
+    sortByCluster :: [a] -> (a->Resource) -> <ReadGraph> [a]
+    makeSynchronous :: Boolean -> <ReadGraph> ()
+    listOntologies :: () -> <ReadGraph> [Resource]
+    emptyTrashBin :: () -> <Proc> ()
+    purgeDatabase :: () -> <Proc> ()
+    prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
+
+    @private
+    @JavaName copyTo
+    copyTo_ :: Resource -> Resource -> <WriteGraph> Collection Resource
+    @JavaName getPossiblePredicateByName
+    possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
+
+copyTo :: Resource -> Resource -> <WriteGraph> Resource
+copyTo targetContainer source = do
+    (collectionToList $ copyTo_ targetContainer source)!0
+
+importJava "org.simantics.db.common.utils.CommonDBUtils" where
+    selectClusterSet :: Resource -> <WriteGraph> () 
+
+importJava "org.simantics.Simantics" where
+    @JavaName getProjectResource
+    currentProject :: () -> /*<Proc>*/ Resource
+
+// Move to somewhere more generic module
+importJava "java.util.Collection" where
+    data Collection a
+
+importJava "java.util.ArrayList" where
+    @JavaName "<init>"
+    collectionToList :: Collection a -> [a]
+/*@macro
+collectionToList :: Collection a -> [a]
+collectionToList x = Java.unsafeCoerce x
+*/ 
+
+resourceToCollection :: a -> Collection a
+resourceToCollection x = Java.unsafeCoerce x
+
+newEntity :: [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
+newEntity entitySpecs = do
+    entity = newResource ()
+    for entitySpecs (\spec -> spec entity)
+    entity
+
+updateEntity :: Resource -> [Resource -> <ReadGraph,WriteGraph> ()] -> <ReadGraph,WriteGraph> Resource
+updateEntity entity entitySpecs = do
+    for entitySpecs (\spec -> spec entity)
+    entity
+
+hasStatement predicate object entity = 
+    claim entity predicate object
+hasProperty relation value entity = 
+    claimRelatedValue entity relation value
+hasPossibleProperty relation value entity =
+    match value with
+      Just v -> claimRelatedValue entity relation v
+      Nothing -> ()
+hasTypedProperty relation value t entity = 
+    claimRelatedValueWithType entity relation t value        
+hasName (n :: String) = hasProperty L0.HasName n
+hasLabel (l :: String) = hasProperty L0.HasLabel l
+hasType t = hasStatement L0.InstanceOf t
+hasParent p = hasStatement L0.PartOf p
+
+importJava "org.simantics.databoard.util.URIStringUtils" where
+    @JavaName splitURISCL
+    splitURI :: String -> [String]
+    @JavaName escape
+    escapeURI :: String -> String
+    @JavaName unescape
+    unescapeURI :: String -> String
+
+@private
+importJava "org.simantics.db.layer0.request.ActiveModels" where
+    @JavaName getPossibleActiveModel
+    activeModel :: Resource -> <ReadGraph> Maybe Resource
+
+"Gives the current active model."
+currentModel :: <ReadGraph> Resource
+currentModel = match activeModel (currentProject ()) with
+    Just model -> model
+    Nothing -> fail "No active model."
+
+startUndoPoint :: String -> <WriteGraph> ()
+startUndoPoint string = do
+    markUndoPoint ()
+    addCommentMetadata (string)
+    ()
+
+@inline
+lift1Read :: (a -> <ReadGraph,Proc> b) -> (a -> <Proc> b)
+lift1Read f x = syncRead (\_ -> f x)
+
+@inline
+lift2Read :: (a -> b -> <ReadGraph,Proc> c) -> (a -> b -> <Proc> c)
+lift2Read f x y = syncRead (\_ -> f x y)
+
+@inline
+lift3Read :: (a -> b -> c -> <ReadGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
+lift3Read f x y z = syncRead (\_ -> f x y z)
+
+@inline
+lift1Write :: (a -> <WriteGraph,Proc> b) -> (a -> <Proc> b)
+lift1Write f x = syncWrite (\_ -> f x)
+
+@inline
+lift2Write :: (a -> b -> <WriteGraph,Proc> c) -> (a -> b -> <Proc> c)
+lift2Write f x y = syncWrite (\_ -> f x y)
+
+@inline
+lift3Write :: (a -> b -> c -> <WriteGraph,Proc> d) -> (a -> b -> c -> <Proc> d)
+lift3Write f x y z = syncWrite (\_ -> f x y z)
+
+"""
+Returns a child Browsable of the specified parent that has that is the child
+of the specified parent with the specified name path.
+"""
+childWithPath :: Browsable a => a -> [String] -> <ReadGraph> a
+childWithPath parent path = 
+    foldl (\r name -> match possibleChild r name with
+              Just c -> c
+              Nothing -> fail ("Didn't find " + name + ".") 
+          ) 
+          parent path
+
+"""
+Like `childWithPath` but returns Maybe a which will be `Nothing` if no child
+was found.
+"""
+possibleChildWithPath :: Browsable a => a -> [String] -> <ReadGraph> Maybe a
+possibleChildWithPath parent path = 
+    foldl (\r name -> match r with
+              Just c -> possibleChild c name
+              Nothing -> Nothing 
+          ) 
+          (Just parent) path