]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/scl/Simantics/Chart.scl
Add SCL support for exporting subscription data as CSV
[simantics/platform.git] / bundles / org.simantics.charts / scl / Simantics / Chart.scl
index 9f1c02c9861ef4c2b4900b89015cd409f359853a..3972fd95c9317f58730eac5a193037a6ed840775 100644 (file)
-import "Simantics/DB"\r
-import "Simantics/Variables"\r
-import "Simantics/Model"\r
-import "Simantics/Entity"\r
-import "Simantics/Subscription"\r
-\r
-import "http://www.simantics.org/Charts-1.2" as CHART\r
-\r
-type Chart = Resource\r
-type ChartGroup = Resource\r
-type ChartItem = Resource\r
-\r
-importJava "org.simantics.charts.ui.SCLChart" where\r
-    @JavaName createNewChart\r
-    """\r
-    Creates a new Chart to the default Charts folder under the given Model parameter. Returns the created Chart.\r
-    """\r
-    createChart :: Model -> <Proc> Chart\r
-\r
-    @JavaName createNewChart\r
-    """\r
-    Creates a new Chart under the given ChartGroup parameter. Returns the created Chart.\r
-    """\r
-    createChartInGroup :: ChartGroup -> <Proc> Chart\r
-\r
-    @JavaName createNewChartGroup\r
-    """\r
-    Creates a new ChartGroup under the given Model parameter. Returns the created ChartGroup.\r
-    """\r
-    createChartGroup :: Model -> <Proc> ChartGroup\r
-\r
-    @JavaName addChartItems\r
-    """\r
-    Creates new chartItem with the given Variable to the given Chart and returns the created ChartItem. New Subscription item is created at the same time.\r
-    """\r
-    addChartItems :: Chart -> Variable -> <WriteGraph> ChartItem \r
-\r
-    @JavaName linkSubToChart\r
-    """\r
-    Links the given Subscription to the given chart and returns the created ChartItem.\r
-    """\r
-    linkSubToChart :: Subscription -> Chart -> <WriteGraph> ChartItem\r
-\r
-"""\r
-Browses the given Model for its Charts and then returns them in a list.\r
-"""\r
-chartsOf :: Model -> <ReadGraph> [Chart]\r
-chartsOf model = recurse (toResource model)\r
-  where\r
-    recurse r = do\r
-        cs = resourceChildrenOf r \r
-        charts = map fromResource $ filter isChart cs\r
-        chartGrp = filter isChartGroup cs\r
-        charts + concatMap recurse chartGrp\r
-    isChart r = isInstanceOf r CHART.TimeSeriesChart\r
-    isChartGroup r = isInstanceOf r CHART.ChartGroup\r
+import "Simantics/DB"
+import "Simantics/Variables"
+import "Simantics/Model"
+import "Simantics/Entity"
+import "Simantics/Subscription"
+
+import "http://www.simantics.org/Charts-1.2" as CHART
+
+type Chart = Resource
+type ChartGroup = Resource
+type ChartItem = Resource
+
+importJava "org.simantics.charts.ui.SCLChart" where
+    @JavaName createNewChart
+    """
+    Creates a new Chart to the default Charts folder under the given Model parameter. Returns the created Chart.
+    """
+    createChart :: Model -> <Proc> Chart
+
+    @JavaName createNewChart
+    """
+    Creates a new Chart under the given ChartGroup parameter. Returns the created Chart.
+    """
+    createChartInGroup :: ChartGroup -> <Proc> Chart
+
+    @JavaName createNewChartGroup
+    """
+    Creates a new ChartGroup under the given Model parameter. Returns the created ChartGroup.
+    """
+    createChartGroup :: Model -> <Proc> ChartGroup
+
+    @JavaName addChartItems
+    """
+    Creates new chartItem with the given Variable to the given Chart and returns the created ChartItem. New Subscription item is created at the same time.
+    """
+    addChartItems :: Chart -> Variable -> <WriteGraph> ChartItem 
+
+    @JavaName linkSubToChart
+    """
+    Links the given Subscription to the given chart and returns the created ChartItem.
+    """
+    linkSubToChart :: Subscription -> Chart -> <WriteGraph> ChartItem
+
+"""
+Browses the given Model for its Charts and then returns them in a list.
+"""
+chartsOf :: Model -> <ReadGraph> [Chart]
+chartsOf model = recurse (toResource model)
+  where
+    recurse r = do
+        cs = resourceChildrenOf r 
+        charts = map fromResource $ filter isChart cs
+        chartGrp = filter isChartGroup cs
+        charts + concatMap recurse chartGrp
+    isChart r = isInstanceOf r CHART.TimeSeriesChart
+    isChartGroup r = isInstanceOf r CHART.ChartGroup
+
+//-----------------------------------------------------------------------------
+// Support for exporting subscription data in CSV format
+
+import "UI/Progress" as Progress
+import "File"
+
+importJava "org.simantics.history.csv.DecimalSeparator" where
+    data DecimalSeparator
+
+    @JavaName fromPreference
+    """
+    Possible arguments are:
+    
+        Dot (.)
+        Comma (,)
+    """
+    decimalSeparatorFromString :: String -> <Proc> DecimalSeparator
+
+importJava "org.simantics.history.csv.ColumnSeparator" where
+    data ColumnSeparator
+
+    @JavaName fromPreference
+    """
+    Possible arguments are: 
+    
+        Comma (,)
+        Tabulator (\\t)
+        Semicolon (;)
+        Colon (:)
+        Space ( )
+    """
+    columnSeparatorFromString :: String -> <Proc> ColumnSeparator
+
+importJava "org.simantics.history.csv.ExportInterpolation" where
+    data ExportInterpolation
+
+    @JavaName fromPreference
+    """
+    Possible arguments are:
+    
+        Linear Interpolation (lerp)
+        Previous Sample (previous)
+    """
+    exportInterpolationFromString :: String -> <Proc> ExportInterpolation
+
+importJava "org.simantics.charts.ui.CSVExportPlan" where
+    @FieldNames [startTime, timeStep, decimalSeparator, columnSeparator, resample, samplingMode, timeDigits, floatDigits, doubleDigits]
+    
+    """
+    Example of construction:
+    
+        plan = CSVExportPlan {
+            startTime = 0.0,
+            timeStep = 1.0,
+            decimalSeparator = decimalSeparatorFromString ".",
+            columnSeparator = columnSeparatorFromString ",",
+            resample = True,
+            samplingMode = exportInterpolationFromString "lerp",
+            timeDigits = 7,
+            floatDigits = 9,
+            doubleDigits = 15
+        }
+    """
+    data CSVExportPlan = CSVExportPlan {
+        startTime :: Double,
+        timeStep :: Double,
+        decimalSeparator :: DecimalSeparator,
+        columnSeparator :: ColumnSeparator,
+        resample :: Boolean,
+        samplingMode :: ExportInterpolation ,
+        timeDigits :: Integer,
+        floatDigits :: Integer,
+        doubleDigits :: Integer
+    }
+    
+    @JavaName setItems
+    @private
+    setCSVExportPlanItems :: CSVExportPlan -> [Resource] -> <Proc> ()
+
+"""
+Example of construction:
+
+    plan = SubscriptionCSVExportPlan {
+        modelName = "Model",
+        filePath = "D:/folder/output.csv",
+        subscriptionNames = ["Default"],
+        exportPlan = CSVExportPlan {}
+    }
+"""
+data SubscriptionCSVExportPlan = SubscriptionCSVExportPlan {
+    modelName :: String,
+    subscriptionNames :: [String],
+    filePath :: String,
+    exportPlan :: CSVExportPlan
+}
+
+@private
+modelNameOf SubscriptionCSVExportPlan { modelName } = modelName
+@private
+subscriptionNamesOf SubscriptionCSVExportPlan { subscriptionNames } = subscriptionNames
+@private
+filePathOf SubscriptionCSVExportPlan { filePath } = filePath
+@private
+exportPlanOf SubscriptionCSVExportPlan { exportPlan } = exportPlan
+
+importJava "org.simantics.charts.ui.CSVExporter" where
+    @JavaName doExport
+    @private
+    exportSubscriptionsCSVInternal :: Progress.ProgressMonitor -> File -> CSVExportPlan -> <Proc> ()
+
+"""
+Exports subscription data as CSV values in a similar manner as the CSV Exporter provided by the user interface
+
+Example of usage:
+
+    exportSubscriptionsCSV SubscriptionCSVExportPlan {
+        modelName = "Model",
+        filePath = "D:/folder/output.csv",
+        subscriptionNames = ["Default"],
+        exportPlan = CSVExportPlan {
+            startTime = 0.0,
+            timeStep = 1.0,
+            decimalSeparator = decimalSeparatorFromString ".",
+            columnSeparator = columnSeparatorFromString ",",
+            resample = True,
+            samplingMode = exportInterpolationFromString "lerp",
+            timeDigits = 7,
+            floatDigits = 9,
+            doubleDigits = 15
+        }
+    }
+"""
+exportSubscriptionsCSV :: SubscriptionCSVExportPlan -> <Proc> ()
+exportSubscriptionsCSV subscriptionExportPlan = do
+    items = syncRead (\_ -> resolveSubscriptionItems (modelNameOf subscriptionExportPlan) (subscriptionNamesOf subscriptionExportPlan))
+    csvExportPlan = exportPlanOf subscriptionExportPlan
+    setCSVExportPlanItems csvExportPlan items
+    exportSubscriptionsCSVInternal (Progress.createNullProgressMonitor ()) (file (filePathOf subscriptionExportPlan)) csvExportPlan
+
+@private
+resolveSubscriptionItems :: String -> [String] -> <Proc, ReadGraph> [Resource]
+resolveSubscriptionItems modelName subscriptionNames = concatMap itemsOf (filter nameFilter subscriptions)
+    where
+        nameFilter sub = elem (relatedValue2 sub L0.HasLabel) subscriptionNames
+        subscriptions = (objectsWithType (model modelName) L0.ConsistsOf MOD.Subscription)
+        itemsOf subscription = objectsWithType subscription L0.ConsistsOf MOD.Subscription.Item