]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.db/scl/Simantics/Variables.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.db / scl / Simantics / Variables.scl
diff --git a/bundles/org.simantics.scl.db/scl/Simantics/Variables.scl b/bundles/org.simantics.scl.db/scl/Simantics/Variables.scl
new file mode 100644 (file)
index 0000000..c54048a
--- /dev/null
@@ -0,0 +1,423 @@
+include "Simantics/Model"\r
+include "Simantics/DB"\r
+import "Simantics/DB" as DB\r
+\r
+importJava "java.lang.Object" where\r
+    @JavaName equals\r
+    """\r
+Function **variableEquals** compares the given input variables and returns True if they are the same.\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    my_variable_1 = moduleVariable "PO01"\r
+    my_variable_2 = moduleVariable "PO02"\r
+    variableEquals my_variable_1 my_variable_2\r
+    \r
+    > False\r
+    \r
+    """\r
+    variableEquals :: Variable -> Variable -> Boolean\r
+    @JavaName hashCode\r
+    variableHash :: Variable -> Integer\r
+\r
+instance Eq Variable where\r
+    (==) = variableEquals\r
+    \r
+instance Hashable Variable where\r
+    hashP = hashP . variableHash\r
+\r
+importJava "org.simantics.db.layer0.variable.Variables" where\r
+    @JavaName getVariable\r
+    """\r
+Function **variable** converts a variable URI to a variable.\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    model_id = model "Model"\r
+    my_component = getComponent model_id "PO01"\r
+    my_uri = uriOf my_component\r
+    my_variable = variable my_uri\r
+    my_variable\r
+    \r
+    > <variable>\r
+    """\r
+    variable :: String -> <ReadGraph> Variable\r
+    \r
+    @JavaName getVariable\r
+    """\r
+Function **resourceVariable** converts a resource to a corresponding variable.\r
+\r
+Example:\r
+    \r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    model_id = model "Model"\r
+    my_resource = getComponent model_id "PO01"\r
+    my_variable = resourceVariable my_resource\r
+    my_variable\r
+    \r
+    > <variable>\r
+    """\r
+    resourceVariable :: Resource -> <ReadGraph> Variable\r
+    \r
+    @JavaName getModel\r
+    """\r
+Function **modelOfVariable** returns the model, in which the given input variable is located, as **Resource**\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    my_variable = moduleVariable "PO01"\r
+    my_model = modelOfVariable my_variable\r
+    my_model\r
+    \r
+    > #376833\r
+    \r
+    // use function nameOf, which works for resources, to print out the name of the model\r
+    nameOf(my_model)\r
+    \r
+    > "Model"\r
+\r
+    """\r
+    modelOfVariable :: Variable -> <ReadGraph> Model\r
+\r
+    @JavaName getModel\r
+    modelResourceOfVariable :: Variable -> <ReadGraph> Resource\r
+    \r
+    @JavaName possibleActiveVariable\r
+    possibleActiveVariable :: Variable -> <ReadGraph> Maybe Variable\r
+\r
+    @JavaName switchPossibleContext\r
+    switchPossibleContext :: Variable -> Resource -> <ReadGraph> Maybe Variable\r
+    \r
+    @JavaName getIndexRoot\r
+    instanceIndexRoot :: Variable -> <ReadGraph> Resource\r
+    \r
+    createValueAccessor :: (Variable -> <ReadGraph> a) -> (Variable -> Binding b -> <ReadGraph> b) -> (Variable -> c -> <WriteGraph> ()) -> (Variable -> d -> Binding d -> <WriteGraph> ()) -> (Variable -> <ReadGraph> Datatype) -> ValueAccessor\r
+    \r
+importJava "org.simantics.db.layer0.function.All" where\r
+\r
+    standardGetValue1 :: Variable -> <ReadGraph> a\r
+    standardGetValue2 :: Variable -> Binding a -> <ReadGraph> a\r
+    standardSetValue2 :: Variable -> a -> <WriteGraph> ()\r
+    standardSetValue3 :: Variable -> a -> Binding a -> <WriteGraph> ()\r
+    standardGetDatatype :: Variable -> <ReadGraph> Datatype\r
+\r
+importJava "org.simantics.db.layer0.variable.VariableMap" where\r
+    data VariableMap\r
+\r
+importJava "org.simantics.db.layer0.variable.ValueAccessor" where\r
+    data ValueAccessor\r
+\r
+importJava "org.simantics.db.layer0.variable.Variable" where\r
+    data Variable\r
+    \r
+    @JavaName getProperties\r
+    properties_ :: Variable -> <ReadGraph> Collection Variable\r
+    @JavaName getChildren\r
+    children_ :: Variable -> <ReadGraph> Collection Variable\r
+    \r
+    @JavaName getValue\r
+    value_ :: Variable -> Binding a -> <ReadGraph> a\r
+    \r
+    @JavaName getDatatype\r
+    datatype :: Variable -> <ReadGraph> Datatype\r
+    \r
+    @JavaName getValue\r
+    untypedValue :: Variable -> <ReadGraph> a\r
+    \r
+    @JavaName getPossibleValue\r
+    possibleVariableValue_ :: Variable -> Binding a -> <ReadGraph> Maybe a\r
+    \r
+    @JavaName getPossibleValue\r
+    untypedPossibleVariableValue :: Variable -> <ReadGraph> Maybe a\r
+    \r
+    @private\r
+    @JavaName getChild\r
+    child_ :: Variable -> String -> <ReadGraph> Variable\r
+    \r
+    @private\r
+    @JavaName getPossibleChild\r
+    possibleChild_ :: Variable -> String -> <ReadGraph> Maybe Variable\r
+\r
+    browse :: Variable -> String -> <ReadGraph> Variable\r
+    browsePossible :: Variable -> String -> <ReadGraph> Maybe Variable\r
+    \r
+    @JavaName getProperty\r
+    """\r
+Function **property** return the wanted property as **Variable**\r
+\r
+**Input 1:** Module which property we want to obtain as **Variable**\r
+\r
+**Input 2:** Name of the property as **String**\r
+\r
+**Output:** wanted property as **Variable**\r
+\r
+Example\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    my_property_variable = property (moduleVariable "PO01") "PO11_PRESSURE"\r
+    my_property_variable\r
+    \r
+    > <variable>\r
+    """\r
+    property :: Variable -> String -> <ReadGraph> Variable\r
+    \r
+    @JavaName getPossibleProperty\r
+    possibleProperty :: Variable -> String -> <ReadGraph> Maybe Variable\r
+    \r
+    @JavaName getPropertyValue\r
+    propertyValue_ :: Variable -> String -> Binding a -> <ReadGraph> a\r
+\r
+    @JavaName getPropertyValue\r
+    untypedPropertyValue :: Variable -> String -> <ReadGraph> a\r
+\r
+    @JavaName getPossiblePropertyValue\r
+    possiblePropertyValue_ :: Variable -> String -> Binding a -> <ReadGraph> Maybe a\r
+    \r
+    @JavaName getPossiblePropertyValue\r
+    untypedPossiblePropertyValue :: Variable -> String -> <ReadGraph> Maybe a\r
+    \r
+    @JavaName getName\r
+\r
+    """\r
+Function **name** return the name of the input variable as string\r
+\r
+Example:\r
+    \r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    name (moduleVariable "PO01")\r
+    \r
+    > "PO01"\r
+    """\r
+    \r
+    name :: Variable -> <ReadGraph> String\r
+    @JavaName getURI\r
+    """\r
+Function **uri** return the uri of given variable. The uri is fetched from the active **Experiment**\r
+\r
+**Input 1:** wanted variable which uri is needed as **Variable**\r
+\r
+**Output:** uri of the variable as **String**\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    uri (moduleVariable "PO01")\r
+\r
+    >"http://Projects/Development%20Project/Model/Experiment/8ee6b693-891b-438e-a597-9e15a2634e8b/NewGenericDiagram/PO01"\r
+\r
+    """\r
+    uri :: Variable -> <ReadGraph> String\r
+    \r
+    @JavaName getParent\r
+    """\r
+Function **variableParent** returns the name of the parent variable as **Variable**\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    my_variable = moduleVariable "PO01"\r
+    variableParent my_variable\r
+    \r
+    > <variable>\r
+    \r
+    name (variableParent my_variable)\r
+\r
+    > "NewGenericDiagram"\r
+    \r
+    """\r
+    variableParent :: Variable -> <ReadGraph> Variable\r
+\r
+    @JavaName getRepresents\r
+    \r
+    """\r
+Function **represents** returns the resource of the given input variable\r
+\r
+**Input 1:** **Variable** which resource is wanted to be obtainend\r
+\r
+**Output:** **Resource** of the given input variable.\r
+\r
+Example: Find out the resource of given variable\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    represents (moduleVariable "PO01")\r
+\r
+       > #426013\r
+\r
+    """\r
+    represents :: Variable -> <ReadGraph> Resource\r
+    \r
+    @JavaName getPossibleRepresents\r
+    possibleRepresents :: Variable -> <ReadGraph> Maybe Resource\r
+    \r
+    @JavaName setValue\r
+    setValue :: Variable -> a -> <WriteGraph> ()\r
+    \r
+    @JavaName setPropertyValue\r
+    setPropertyValue_ :: Variable -> String -> a -> Binding a -> <WriteGraph> ()\r
+    \r
+    @JavaName getType\r
+    """\r
+Function **getType** returns the type of the input variable as resource\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    getType (moduleVariable "PO01")\r
+    \r
+    > #275837\r
+    \r
+    nameOf(getType (moduleVariable "PO01"))\r
+    \r
+    > "POINT_QF"\r
+    """\r
+    getType :: Variable -> <ReadGraph> Resource\r
+    \r
+    @JavaName getPossibleType\r
+    getPossibleType :: Variable -> <ReadGraph> Maybe Resource\r
+        \r
+    getPredicateResource :: Variable -> <ReadGraph> Resource\r
+    \r
+    getIndexRoot :: Variable -> <ReadGraph> Resource\r
+\r
+value :: Serializable a => Typeable a => Variable -> <ReadGraph> a\r
+value var = value_ var binding \r
+\r
+possibleVariableValue :: Serializable a => Typeable a => Variable -> <ReadGraph> Maybe a\r
+possibleVariableValue var = possibleVariableValue_ var binding  \r
+\r
+"""\r
+Function **propertyValue** finds the value of given property.\r
+\r
+Example: Find out the value of point PO01 attribute PO11_PRESSURE\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    propertyValue (moduleVariable "PO01") "PO11_PRESSURE" :: Double\r
+\r
+       > 0.75\r
+\r
+"""\r
+\r
+propertyValue :: Serializable a => Typeable a => Variable -> String -> <ReadGraph> a\r
+propertyValue var prop = propertyValue_ var prop binding\r
+\r
+possiblePropertyValue :: Serializable a => Typeable a => Variable -> String -> <ReadGraph> Maybe a\r
+possiblePropertyValue var prop = possiblePropertyValue_ var prop binding\r
+\r
+"""\r
+Function **setPropertyValue** sets wanted to value to given Variable property.\r
+\r
+**Input 1:** Wanted module as **Variable**\r
+\r
+**Input 2:** Wanted property name as **String**\r
+\r
+**Output:** No output, given value is inserted to property \r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    setPropertyValue (moduleVariable "PO01") "PO11_PRESSURE" 0.5\r
+"""\r
+\r
+setPropertyValue :: Serializable a => Variable -> String -> a -> <WriteGraph> ()\r
+setPropertyValue subject prediate value = setPropertyValue_ subject prediate value binding\r
+\r
+"""\r
+Function **modelVariableOfVariable** returns the model, in which the given input Variable is located, as **Variable**\r
+\r
+Example:\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    my_model_variable = modelVariableOfVariable (moduleVariable "PO01")\r
+    my_model_variable\r
+\r
+    > <variable>\r
+    \r
+    name my_model_variable\r
+    \r
+    > "Model"\r
+"""\r
+\r
+modelVariableOfVariable :: Variable -> <ReadGraph> Variable     \r
+modelVariableOfVariable var = variable $ uriOf $ toResource $ modelOfVariable var\r
+\r
+uniqueChild :: Model -> Resource -> String -> <ReadGraph> Variable\r
+uniqueChild model typet childName = do\r
+    typeName = DB.nameOf typet\r
+    query = "Types: " + typeName + " AND Name: " + childName\r
+    moduleResources = searchByQuery (toResource model) query\r
+    variable $ uriOf $ moduleResources ! 0\r
+\r
+"""\r
+Function **properties** returns a list, which contains of the properties of the input variable as **Variable**\r
+\r
+Example 1: print out all the properties of certain point as they are shown in Variable Debugger.\r
+    \r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    point_properties_list = properties (moduleVariable "PO01")\r
+    //print out the names of the properties\r
+    for point_properties_list (\x -> print(name x) )\r
+    \r
+    > IncludedInSimulation\r
+    > PO11_PRESSURE\r
+    > IsDesynchronized\r
+    > PO11_PROPERTY_CALC\r
+    > PO11_EPM_ROU\r
+    > PO11_ELEV_FROM_BOT\r
+    ...\r
+\r
+Example 2: print out only the properties of a point, which name starts as "PO11". Else do nothing.\r
+\r
+    import "Simantics/Variables"\r
+    import "Apros/Module"\r
+    point_properties_list = properties (moduleVariable "PO01")\r
+    for point_properties_list (\x -> do\r
+        //print out the names of the properties, which name start as "PO11"\r
+        if (take 4 (name x) == "PO11") then do\r
+            print(name x)\r
+            ()\r
+        else do\r
+            ()\r
+    )\r
+    \r
+    > PO11_PRESSURE\r
+    > PO11_PROPERTY_CALC\r
+    > PO11_EPM_ROU\r
+    > PO11_ELEV_FROM_BOT\r
+    > PO11_NODE_VELOCITY_CALC\r
+    ...\r
+\r
+"""\r
+properties = collectionToList . properties_\r
+\r
+instance Show Variable where\r
+    show _ = "<variable>"\r
+    \r
+instance Browsable Variable where\r
+    fromUri = variable\r
+    uriOf = uri\r
+    nameOf = name\r
+    possibleNameOf v = Just (name v)\r
+    children v = collectionToList (children_ v)\r
+    parent = variableParent\r
+    possibleParent v = Just (variableParent v)\r
+    valueOf v = untypedValue v\r
+    variantValueOf v = createVariant (datatype v) (untypedValue v :: Dynamic)\r
+    child = child_\r
+    possibleChild = possibleChild_
\ No newline at end of file