]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 import "Simantics/DB"
2 import "Simantics/Variables"
3 import "Simantics/Model"
4 import "Simantics/Entity"
5 import "Simantics/Subscription"
6
7 import "http://www.simantics.org/Charts-1.2" as CHART
8
9 type Chart = Resource
10 type ChartGroup = Resource
11 type ChartItem = Resource
12
13 importJava "org.simantics.charts.ui.SCLChart" where
14     @JavaName createNewChart
15     """
16     Creates a new Chart to the default Charts folder under the given Model parameter. Returns the created Chart.
17     """
18     createChart :: Model -> <Proc> Chart
19
20     @JavaName createNewChart
21     """
22     Creates a new Chart under the given ChartGroup parameter. Returns the created Chart.
23     """
24     createChartInGroup :: ChartGroup -> <Proc> Chart
25
26     @JavaName createNewChartGroup
27     """
28     Creates a new ChartGroup under the given Model parameter. Returns the created ChartGroup.
29     """
30     createChartGroup :: Model -> <Proc> ChartGroup
31
32     @JavaName addChartItems
33     """
34     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.
35     """
36     addChartItems :: Chart -> Variable -> <WriteGraph> ChartItem 
37
38     @JavaName linkSubToChart
39     """
40     Links the given Subscription to the given chart and returns the created ChartItem.
41     """
42     linkSubToChart :: Subscription -> Chart -> <WriteGraph> ChartItem
43
44 """
45 Browses the given Model for its Charts and then returns them in a list.
46 """
47 chartsOf :: Model -> <ReadGraph> [Chart]
48 chartsOf model = recurse (toResource model)
49   where
50     recurse r = do
51         cs = resourceChildrenOf r 
52         charts = map fromResource $ filter isChart cs
53         chartGrp = filter isChartGroup cs
54         charts + concatMap recurse chartGrp
55     isChart r = isInstanceOf r CHART.TimeSeriesChart
56     isChartGroup r = isInstanceOf r CHART.ChartGroup
57
58 //-----------------------------------------------------------------------------
59 // Support for exporting subscription data in CSV format
60
61 import "UI/Progress" as Progress
62 import "File"
63
64 importJava "org.simantics.history.csv.DecimalSeparator" where
65     data DecimalSeparator
66
67     @JavaName fromPreference
68     """
69     Possible arguments are:
70     
71         Dot (.)
72         Comma (,)
73     """
74     decimalSeparatorFromString :: String -> <Proc> DecimalSeparator
75
76 importJava "org.simantics.history.csv.ColumnSeparator" where
77     data ColumnSeparator
78
79     @JavaName fromPreference
80     """
81     Possible arguments are: 
82     
83         Comma (,)
84         Tabulator (\\t)
85         Semicolon (;)
86         Colon (:)
87         Space ( )
88     """
89     columnSeparatorFromString :: String -> <Proc> ColumnSeparator
90
91 importJava "org.simantics.history.csv.ExportInterpolation" where
92     data ExportInterpolation
93
94     @JavaName fromPreference
95     """
96     Possible arguments are:
97     
98         Linear Interpolation (lerp)
99         Previous Sample (previous)
100     """
101     exportInterpolationFromString :: String -> <Proc> ExportInterpolation
102
103 importJava "org.simantics.charts.ui.CSVExportPlan" where
104     @FieldNames [startTime, timeStep, decimalSeparator, columnSeparator, resample, samplingMode, timeDigits, floatDigits, doubleDigits]
105     
106     """
107     Example of construction:
108     
109         plan = CSVExportPlan {
110             startTime = 0.0,
111             timeStep = 1.0,
112             decimalSeparator = decimalSeparatorFromString ".",
113             columnSeparator = columnSeparatorFromString ",",
114             resample = True,
115             samplingMode = exportInterpolationFromString "lerp",
116             timeDigits = 7,
117             floatDigits = 9,
118             doubleDigits = 15
119         }
120     """
121     data CSVExportPlan = CSVExportPlan {
122         startTime :: Double,
123         timeStep :: Double,
124         decimalSeparator :: DecimalSeparator,
125         columnSeparator :: ColumnSeparator,
126         resample :: Boolean,
127         samplingMode :: ExportInterpolation ,
128         timeDigits :: Integer,
129         floatDigits :: Integer,
130         doubleDigits :: Integer
131     }
132     
133     @JavaName setItems
134     @private
135     setCSVExportPlanItems :: CSVExportPlan -> [Resource] -> <Proc> ()
136
137 """
138 Example of construction:
139
140     plan = SubscriptionCSVExportPlan {
141         modelName = "Model",
142         filePath = "D:/folder/output.csv",
143         subscriptionNames = ["Default"],
144         exportPlan = CSVExportPlan {}
145     }
146 """
147 data SubscriptionCSVExportPlan = SubscriptionCSVExportPlan {
148     modelName :: String,
149     subscriptionNames :: [String],
150     filePath :: String,
151     exportPlan :: CSVExportPlan
152 }
153
154 @private
155 modelNameOf SubscriptionCSVExportPlan { modelName } = modelName
156 @private
157 subscriptionNamesOf SubscriptionCSVExportPlan { subscriptionNames } = subscriptionNames
158 @private
159 filePathOf SubscriptionCSVExportPlan { filePath } = filePath
160 @private
161 exportPlanOf SubscriptionCSVExportPlan { exportPlan } = exportPlan
162
163 importJava "org.simantics.charts.ui.CSVExporter" where
164     @JavaName doExport
165     @private
166     exportSubscriptionsCSVInternal :: Progress.ProgressMonitor -> File -> CSVExportPlan -> <Proc> ()
167
168 """
169 Exports subscription data as CSV values in a similar manner as the CSV Exporter provided by the user interface
170
171 Example of usage:
172
173     exportSubscriptionsCSV SubscriptionCSVExportPlan {
174         modelName = "Model",
175         filePath = "D:/folder/output.csv",
176         subscriptionNames = ["Default"],
177         exportPlan = CSVExportPlan {
178             startTime = 0.0,
179             timeStep = 1.0,
180             decimalSeparator = decimalSeparatorFromString ".",
181             columnSeparator = columnSeparatorFromString ",",
182             resample = True,
183             samplingMode = exportInterpolationFromString "lerp",
184             timeDigits = 7,
185             floatDigits = 9,
186             doubleDigits = 15
187         }
188     }
189 """
190 exportSubscriptionsCSV :: SubscriptionCSVExportPlan -> <Proc> ()
191 exportSubscriptionsCSV subscriptionExportPlan = do
192     items = syncRead (\_ -> resolveSubscriptionItems (modelNameOf subscriptionExportPlan) (subscriptionNamesOf subscriptionExportPlan))
193     csvExportPlan = exportPlanOf subscriptionExportPlan
194     setCSVExportPlanItems csvExportPlan items
195     exportSubscriptionsCSVInternal (Progress.createNullProgressMonitor ()) (file (filePathOf subscriptionExportPlan)) csvExportPlan
196
197 @private
198 resolveSubscriptionItems :: String -> [String] -> <Proc, ReadGraph> [Resource]
199 resolveSubscriptionItems modelName subscriptionNames = concatMap itemsOf (filter nameFilter subscriptions)
200     where
201         nameFilter sub = elem (relatedValue2 sub L0.HasLabel) subscriptionNames
202         subscriptions = (objectsWithType (model modelName) L0.ConsistsOf MOD.Subscription)
203         itemsOf subscription = objectsWithType subscription L0.ConsistsOf MOD.Subscription.Item