X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.db%2Fscl%2FSimantics%2FDB.scl;h=df556086ed6ab61d0aa1458f5d6da06e24955172;hp=ae092cfa8d62aafe6cd203d29e20e9a7f9885903;hb=aea3e7b117a8398471f10c31844efffc8026f815;hpb=cb5fc8d606d8b322563e9345c441eecfa7f01753 diff --git a/bundles/org.simantics.scl.db/scl/Simantics/DB.scl b/bundles/org.simantics.scl.db/scl/Simantics/DB.scl index ae092cfa8..df556086e 100644 --- a/bundles/org.simantics.scl.db/scl/Simantics/DB.scl +++ b/bundles/org.simantics.scl.db/scl/Simantics/DB.scl @@ -1,445 +1,477 @@ -import "JavaBuiltin" as Java -include "http://www.simantics.org/Layer0-1.1" as L0 - -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 - -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 -> (Maybe Resource) - - getDataType :: Resource -> Datatype - - @JavaName getSupertypes - superTypesOf :: Resource -> Set.T Resource - - @private - @JavaName getURI - uriOfResource :: Resource -> String - - @JavaName getObjects - objects_ :: Resource -> Resource -> Collection Resource - - @JavaName getStatements - statements_ :: Resource -> Resource -> Collection Statement - - @JavaName hasStatement - existsStatement :: Resource -> Resource -> Boolean - - @JavaName hasStatement - existsStatement3 :: Resource -> Resource -> Resource -> Boolean - - "Assumes that there is exactly one object with the given subject and predicate and returns it." - @JavaName getSingleObject - singleObject :: Resource -> Resource -> 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 -> Maybe Resource - - "Assumes that there is exactly one statement with the given subject and predicate and returns it." - @JavaName getSingleStatement - singleStatement :: Resource -> Resource -> Statement - - @JavaName getRelatedVariantValue - relatedVariantValue :: Resource -> Resource -> Variant - - @JavaName getRelatedValue - relatedValue_ :: Resource -> Resource -> Binding a -> a - - @JavaName getRelatedValue - untypedRelatedValue :: Resource -> Resource -> a - - @JavaName getRelatedValue2 - relatedValue2 :: Resource -> Resource -> a - - @JavaName getRelatedVariantValue2 - relatedVariantValue2 :: Resource -> Resource -> Variant - - @JavaName getPossibleRelatedValue - possibleRelatedValue_ :: Resource -> Resource -> Binding a -> Maybe a - - @JavaName getPossibleRelatedValue - untypedPossibleRelatedValue :: Resource -> Resource -> Maybe a - - @private - @JavaName getValue - valueOf_ :: Resource -> Binding a -> a - - @private - @JavaName getVariantValue - variantValueOf_ :: Resource -> Variant - - @JavaName getValue - untypedValueOf :: Resource -> Dynamic - - @JavaName getPossibleValue - untypedPossibleValueOf :: Resource -> Maybe Dynamic - - @JavaName getInverse - inverseOf :: Resource -> Resource - - @JavaName getSingleType - singleTypeOf :: Resource -> Resource -> Resource - - @JavaName getPossibleType - possibleTypeOf :: Resource -> Resource -> Maybe Resource - - "`isInstanceOf r t` returns true, if `r` is an instance of `t`" - isInstanceOf :: Resource -> Resource -> Boolean - isSubrelationOf :: Resource -> Resource -> Boolean - isInheritedFrom :: Resource -> Resource -> Boolean - - getRootLibrary :: () -> Resource - -importJava "org.simantics.db.layer0.util.ExtendedUris" where - "Converts an absolute URI to a resource" - @JavaName resolveAbsoluteUri - resource :: String -> Resource - - "Converts a relative URI to a resource starting from the given resource" - @JavaName resolveRelativeUri - relativeResource :: Resource -> String -> Resource - -"Reads the value of a literal that is an object with the given subject and predicate" -@inline -relatedValue :: Serializable a => Resource -> Resource -> a -relatedValue s p = relatedValue_ s p binding - -@inline -possibleRelatedValue :: Serializable a => Resource -> Resource -> Maybe a -possibleRelatedValue s p = possibleRelatedValue_ s p binding - -class Browsable a where - fromUri :: String -> a - - "Returns the URI of the given value." - uriOf :: a -> String - - "Reads the name of the value." - nameOf :: a -> String - possibleNameOf :: a -> Maybe String - - valueOf :: Serializable v => a -> v - - variantValueOf :: a -> Variant - - children :: a -> [a] - parent :: a -> a - possibleParent :: a -> Maybe a - - child :: a -> String -> a - possibleChild :: a -> String -> 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 :: () -> () - -importJava "org.simantics.db.WriteGraph" where - "Creates a new resource." - newResource :: () -> Resource - "Adds a statement to the semantic graph." - claim :: Resource -> Resource -> Resource -> () - @JavaName claimLiteral - claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> () - @JavaName claimLiteral - untypedClaimRelatedValue :: Resource -> Resource -> a -> () - @JavaName claimValue - untypedClaimValue :: Resource -> a -> () - @JavaName claimLiteral - claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> () - "Removes a statement with the given subject, predicate and object" - deny :: Resource -> Resource -> Resource -> () - @JavaName deny - denyByPredicate :: Resource -> Resource -> () - @JavaName deny - denyAllStatements :: Resource -> () - newClusterSet :: Resource -> () - @JavaName denyValue - denyValue :: Resource -> () - -claimAssertion :: Resource -> Resource -> Resource -> () -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 -> () -claimRelatedValue s p v = claimRelatedValue_ s p v binding - -@inline -claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> () -claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding - -importJava "org.simantics.layer0.utils.direct.GraphUtils" where - @JavaName getPossibleChild - possibleResourceChild :: Resource -> String -> (Maybe Resource) - -@deprecated "Use function children instead." -resourceChildrenOf :: Resource -> [Resource] -resourceChildrenOf r = r # L0.ConsistsOf - -importJava "org.simantics.db.common.utils.OrderedSetUtils" where - @JavaName add - addToOrderedSet :: Resource -> Resource -> Boolean - - @JavaName set - setOrderedSet :: Resource -> [Resource] -> Boolean - - @JavaName getSingleOwnerList - parentOrderedSet :: Resource -> Resource - - @JavaName toList - elementsOfOrderedSet :: Resource -> [Resource] - -importJava "org.simantics.db.common.utils.ListUtils" where - @JavaName toList - elementsOfList :: Resource -> [Resource] - -importJava "org.simantics.db.common.utils.CommonDBUtils" where - isParent :: Resource -> Resource -> Boolean - possibleRelatedString :: Resource -> Resource -> Maybe String - possibleRelatedInteger :: Resource -> Resource -> Maybe Integer - objectsWithType :: Resource -> Resource -> Resource -> [Resource] - possibleObjectWithType :: Resource -> Resource -> Resource -> Maybe Resource - -importJava "org.simantics.db.common.utils.NameUtils" where - findFreshName :: String -> Resource -> String - findFreshEscapedName :: String -> Resource -> String - -"`subject # predicate` returns all objects with the given `subject` and `predicate`." -(#) :: Resource -> Resource -> [Resource] -subject # predicate = collectionToList $ objects_ subject predicate - -"statements subject predicate` returns all statements with the given subject and predicate." -statements :: Resource -> Resource -> [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 :: (() -> a) -> a - "Executes a write transaction and waits that it completes." - syncWrite :: (() -> a) -> a - "Executes a delayed write transaction and waits that it completes." - delayedSyncWrite :: (() -> a) -> a - - "Begins a read transaction and immediately returns." - asyncRead :: (() -> a) -> () - "Begins a write transaction and immediately returns." - asyncWrite :: (() -> a) -> () - - virtualSyncWriteMem :: String -> (() -> a) -> a - virtualSyncWriteWS :: String -> (() -> a) -> a - - safeExec :: (() -> a) -> a - activateOnce :: Resource -> () - syncActivateOnce :: Resource -> () - resourceFromId :: Long -> Resource - enableDependencies :: () -> () - disableDependencies :: () -> () - - unaryQuery :: (a -> b) -> a -> b - unaryQueryCached :: (a -> b) -> a -> b - -importJava "org.simantics.db.layer0.util.Layer0Utils" where - undo :: () -> String - undoOperations :: Integer -> String - redo :: () -> String - queryDebugSupport :: String -> String - queryListSupport :: String -> String - addCommentMetadata :: String -> () - sortByCluster :: [a] -> (a->Resource) -> [a] - makeSynchronous :: Boolean -> () - listOntologies :: () -> [Resource] - - @private - @JavaName copyTo - copyTo_ :: Resource -> Resource -> Collection Resource - @JavaName getPossiblePredicateByName - possiblePredicateByName :: Resource -> String -> Maybe Resource - -copyTo :: Resource -> Resource -> Resource -copyTo targetContainer source = do - (collectionToList $ copyTo_ targetContainer source)!0 - -importJava "org.simantics.db.common.utils.CommonDBUtils" where - selectClusterSet :: Resource -> () - -importJava "org.simantics.Simantics" where - @JavaName getProjectResource - currentProject :: () -> /**/ Resource - -// Move to somewhere more generic module -importJava "java.util.Collection" where - data Collection a - -importJava "java.util.ArrayList" where - @JavaName "" - 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 -> ()] -> Resource -newEntity entitySpecs = do - entity = newResource () - for entitySpecs (\spec -> spec entity) - entity - -updateEntity :: Resource -> [Resource -> ()] -> 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 -> Maybe Resource - -"Gives the current active model." -currentModel :: Resource -currentModel = match activeModel (currentProject ()) with - Just model -> model - Nothing -> fail "No active model." - -startUndoPoint :: String -> () -startUndoPoint string = do - markUndoPoint () - addCommentMetadata (string) - () - -@inline -lift1Read :: (a -> b) -> (a -> b) -lift1Read f x = syncRead (\_ -> f x) - -@inline -lift2Read :: (a -> b -> c) -> (a -> b -> c) -lift2Read f x y = syncRead (\_ -> f x y) - -@inline -lift3Read :: (a -> b -> c -> d) -> (a -> b -> c -> d) -lift3Read f x y z = syncRead (\_ -> f x y z) - -@inline -lift1Write :: (a -> b) -> (a -> b) -lift1Write f x = syncWrite (\_ -> f x) - -@inline -lift2Write :: (a -> b -> c) -> (a -> b -> c) -lift2Write f x y = syncWrite (\_ -> f x y) - -@inline -lift3Write :: (a -> b -> c -> d) -> (a -> b -> c -> 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] -> 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] -> Maybe a -possibleChildWithPath parent path = - foldl (\r name -> match r with - Just c -> possibleChild c name - Nothing -> Nothing - ) - (Just parent) path +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 -> 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 -> (Maybe Resource) + + getDataType :: Resource -> Datatype + + @JavaName getSupertypes + superTypesOf :: Resource -> Set.T Resource + + @private + @JavaName getURI + uriOfResource :: Resource -> String + + @JavaName getObjects + objects_ :: Resource -> Resource -> Collection Resource + + @JavaName getStatements + statements_ :: Resource -> Resource -> Collection Statement + + @JavaName hasStatement + existsStatement :: Resource -> Resource -> Boolean + + @JavaName hasStatement + existsStatement3 :: Resource -> Resource -> Resource -> Boolean + + "Assumes that there is exactly one object with the given subject and predicate and returns it." + @JavaName getSingleObject + singleObject :: Resource -> Resource -> 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 -> Maybe Resource + + "Assumes that there is exactly one statement with the given subject and predicate and returns it." + @JavaName getSingleStatement + singleStatement :: Resource -> Resource -> Statement + + @JavaName getPossibleStatement + possibleStatement :: Resource -> Resource -> Maybe Statement + + @JavaName getRelatedVariantValue + relatedVariantValue :: Resource -> Resource -> Variant + + @JavaName getRelatedValue + relatedValue_ :: Resource -> Resource -> Binding a -> a + + @JavaName getRelatedValue + untypedRelatedValue :: Resource -> Resource -> a + + @JavaName getRelatedValue2 + relatedValue2 :: Resource -> Resource -> a + + @JavaName getRelatedVariantValue2 + relatedVariantValue2 :: Resource -> Resource -> Variant + + @JavaName getPossibleRelatedValue + possibleRelatedValue_ :: Resource -> Resource -> Binding a -> Maybe a + + @JavaName getPossibleRelatedValue + untypedPossibleRelatedValue :: Resource -> Resource -> Maybe a + + @private + @JavaName getValue + valueOf_ :: Resource -> Binding a -> a + + @private + @JavaName getVariantValue + variantValueOf_ :: Resource -> Variant + + @JavaName getValue + untypedValueOf :: Resource -> Dynamic + + @JavaName getPossibleValue + untypedPossibleValueOf :: Resource -> Maybe Dynamic + + @JavaName getInverse + inverseOf :: Resource -> Resource + + @JavaName getSingleType + singleTypeOf :: Resource -> Resource -> Resource + + @JavaName getPossibleType + possibleTypeOf :: Resource -> Resource -> Maybe Resource + + "`isInstanceOf r t` returns true, if `r` is an instance of `t`" + isInstanceOf :: Resource -> Resource -> Boolean + isSubrelationOf :: Resource -> Resource -> Boolean + isInheritedFrom :: Resource -> Resource -> Boolean + + getRootLibrary :: () -> Resource + +importJava "org.simantics.db.layer0.util.ExtendedUris" where + "Converts an absolute URI to a resource" + @JavaName resolveAbsoluteUri + resource :: String -> Resource + + "Converts a relative URI to a resource starting from the given resource" + @JavaName resolveRelativeUri + relativeResource :: Resource -> String -> Resource + +"Reads the value of a literal that is an object with the given subject and predicate" +@inline +relatedValue :: Serializable a => Resource -> Resource -> a +relatedValue s p = relatedValue_ s p binding + +@inline +possibleRelatedValue :: Serializable a => Resource -> Resource -> Maybe a +possibleRelatedValue s p = possibleRelatedValue_ s p binding + +class Browsable a where + fromUri :: String -> a + + "Returns the URI of the given value." + uriOf :: a -> String + + "Reads the name of the value." + nameOf :: a -> String + possibleNameOf :: a -> Maybe String + + valueOf :: Serializable v => a -> v + + variantValueOf :: a -> Variant + + children :: a -> [a] + parent :: a -> a + possibleParent :: a -> Maybe a + + child :: a -> String -> a + possibleChild :: a -> String -> 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 :: () -> () + +importJava "org.simantics.db.WriteGraph" where + "Creates a new resource." + newResource :: () -> Resource + "Adds a statement to the semantic graph." + claim :: Resource -> Resource -> Resource -> () + @JavaName claimLiteral + claimRelatedValue_ :: Resource -> Resource -> a -> Binding a -> () + @JavaName claimLiteral + untypedClaimRelatedValue :: Resource -> Resource -> a -> () + @JavaName claimValue + untypedClaimValue :: Resource -> a -> () + @JavaName claimLiteral + claimRelatedValueWithType_ :: Resource -> Resource -> Resource -> a -> Binding a -> () + "Removes a statement with the given subject, predicate and object" + deny :: Resource -> Resource -> Resource -> () + @JavaName deny + denyByPredicate :: Resource -> Resource -> () + @JavaName deny + denyAllStatements :: Resource -> () + newClusterSet :: Resource -> () + @JavaName denyValue + denyValue :: Resource -> () + +claimAssertion :: Resource -> Resource -> Resource -> () +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 -> () +claimRelatedValue s p v = claimRelatedValue_ s p v binding + +@inline +claimRelatedValueWithType :: Serializable a => Resource -> Resource -> Resource -> a -> () +claimRelatedValueWithType s p t v = claimRelatedValueWithType_ s p t v binding + +importJava "org.simantics.layer0.utils.direct.GraphUtils" where + @JavaName getPossibleChild + possibleResourceChild :: Resource -> String -> (Maybe Resource) + +@deprecated "Use function children instead." +resourceChildrenOf :: Resource -> [Resource] +resourceChildrenOf r = r # L0.ConsistsOf + +importJava "org.simantics.db.common.utils.OrderedSetUtils" where + @JavaName add + addToOrderedSet :: Resource -> Resource -> Boolean + + @JavaName set + setOrderedSet :: Resource -> [Resource] -> Boolean + + @JavaName getSingleOwnerList + parentOrderedSet :: Resource -> Resource + + @JavaName toList + elementsOfOrderedSet :: Resource -> [Resource] + +importJava "org.simantics.db.common.utils.ListUtils" where + @JavaName toList + elementsOfList :: Resource -> [Resource] + + @JavaName create + createList :: [Resource] -> Resource + + @javaName insertBack + insertBack :: Resource -> [Resource] -> () + + @javaName removeElement + removeElement :: Resource -> Resource -> Boolean + + @javaName swapWithPrevious + swapWithPrevious :: Resource -> Resource -> Boolean + + @javaName swapWithNext + swapWithNext :: Resource -> Resource -> Boolean + + +importJava "org.simantics.db.common.utils.CommonDBUtils" where + isParent :: Resource -> Resource -> Boolean + possibleRelatedString :: Resource -> Resource -> Maybe String + possibleRelatedInteger :: Resource -> Resource -> Maybe Integer + objectsWithType :: Resource -> Resource -> Resource -> [Resource] + possibleObjectWithType :: Resource -> Resource -> Resource -> Maybe Resource + +importJava "org.simantics.db.common.utils.NameUtils" where + findFreshName :: String -> Resource -> String + findFreshEscapedName :: String -> Resource -> String + +"`subject # predicate` returns all objects with the given `subject` and `predicate`." +(#) :: Resource -> Resource -> [Resource] +subject # predicate = collectionToList $ objects_ subject predicate + +"statements subject predicate` returns all statements with the given subject and predicate." +statements :: Resource -> Resource -> [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 :: (() -> a) -> a + "Executes a write transaction and waits that it completes." + syncWrite :: (() -> a) -> a + "Executes a delayed write transaction and waits that it completes." + delayedSyncWrite :: (() -> a) -> a + + "Begins a read transaction and immediately returns." + asyncRead :: (() -> a) -> () + "Begins a write transaction and immediately returns." + asyncWrite :: (() -> a) -> () + + virtualSyncWriteMem :: String -> (() -> a) -> a + virtualSyncWriteWS :: String -> (() -> a) -> a + + safeExec :: (() -> a) -> a + activateOnce :: Resource -> () + syncActivateOnce :: Resource -> () + resourceFromId :: Long -> Resource + enableDependencies :: () -> () + disableDependencies :: () -> () + + unaryQuery :: (a -> b) -> a -> b + unaryQueryCached :: (a -> b) -> a -> 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 :: ( a) -> a + "Makes a new read request with given procedure for calculating the result. The request is always cached." + subqueryC :: ( a) -> 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 :: () -> String + undoOperations :: Integer -> String + redo :: () -> String + queryDebugSupport :: String -> String + queryListSupport :: String -> String + addCommentMetadata :: String -> () + sortByCluster :: [a] -> (a->Resource) -> [a] + makeSynchronous :: Boolean -> () + listOntologies :: () -> [Resource] + emptyTrashBin :: () -> () + purgeDatabase :: () -> () + prettyPrintResource :: Resource -> Boolean -> String + + @private + @JavaName copyTo + copyTo_ :: Resource -> Resource -> Collection Resource + @JavaName getPossiblePredicateByName + possiblePredicateByName :: Resource -> String -> Maybe Resource + +copyTo :: Resource -> Resource -> Resource +copyTo targetContainer source = do + (collectionToList $ copyTo_ targetContainer source)!0 + +importJava "org.simantics.db.common.utils.CommonDBUtils" where + selectClusterSet :: Resource -> () + +importJava "org.simantics.Simantics" where + @JavaName getProjectResource + currentProject :: () -> /**/ Resource + +// Move to somewhere more generic module +importJava "java.util.Collection" where + data Collection a + +importJava "java.util.ArrayList" where + @JavaName "" + 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 -> ()] -> Resource +newEntity entitySpecs = do + entity = newResource () + for entitySpecs (\spec -> spec entity) + entity + +updateEntity :: Resource -> [Resource -> ()] -> 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 -> Maybe Resource + +"Gives the current active model." +currentModel :: Resource +currentModel = match activeModel (currentProject ()) with + Just model -> model + Nothing -> fail "No active model." + +startUndoPoint :: String -> () +startUndoPoint string = do + markUndoPoint () + addCommentMetadata (string) + () + +@inline +lift1Read :: (a -> b) -> (a -> b) +lift1Read f x = syncRead (\_ -> f x) + +@inline +lift2Read :: (a -> b -> c) -> (a -> b -> c) +lift2Read f x y = syncRead (\_ -> f x y) + +@inline +lift3Read :: (a -> b -> c -> d) -> (a -> b -> c -> d) +lift3Read f x y z = syncRead (\_ -> f x y z) + +@inline +lift1Write :: (a -> b) -> (a -> b) +lift1Write f x = syncWrite (\_ -> f x) + +@inline +lift2Write :: (a -> b -> c) -> (a -> b -> c) +lift2Write f x y = syncWrite (\_ -> f x y) + +@inline +lift3Write :: (a -> b -> c -> d) -> (a -> b -> c -> 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] -> 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] -> Maybe a +possibleChildWithPath parent path = + foldl (\r name -> match r with + Just c -> possibleChild c name + Nothing -> Nothing + ) + (Just parent) path