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 -> Chart @JavaName createNewChart """ Creates a new Chart under the given ChartGroup parameter. Returns the created Chart. """ createChartInGroup :: ChartGroup -> Chart @JavaName createNewChartGroup """ Creates a new ChartGroup under the given Model parameter. Returns the created ChartGroup. """ createChartGroup :: Model -> 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 -> ChartItem @JavaName linkSubToChart """ Links the given Subscription to the given chart and returns the created ChartItem. """ linkSubToChart :: Subscription -> Chart -> ChartItem """ Browses the given Model for its Charts and then returns them in a list. """ chartsOf :: Model -> [Chart] chartsOf model = recurse model where recurse r = do cs = children r charts = 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 -> DecimalSeparator importJava "org.simantics.history.csv.ColumnSeparator" where data ColumnSeparator @JavaName fromPreference """ Possible arguments are: Comma (,) Tabulator (\\t) Semicolon (;) Colon (:) Space ( ) """ columnSeparatorFromString :: String -> ColumnSeparator importJava "org.simantics.history.csv.ExportInterpolation" where data ExportInterpolation @JavaName fromPreference """ Possible arguments are: Linear Interpolation (lerp) Previous Sample (previous) """ exportInterpolationFromString :: String -> 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] -> () """ 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 -> () """ 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 -> () 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] -> [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