1 package org.simantics.simulation.export;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.simantics.Simantics;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.binding.mutable.Variant;
11 import org.simantics.databoard.util.Bean;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.export.core.ExportContext;
14 import org.simantics.export.core.error.ExportException;
15 import org.simantics.export.core.intf.ExportClass;
16 import org.simantics.export.core.manager.Content;
17 import org.simantics.history.History;
18 import org.simantics.history.HistoryException;
19 import org.simantics.history.HistoryManager;
20 import org.simantics.history.ItemManager;
21 import org.simantics.history.util.subscription.SamplingFormat;
22 import org.simantics.simulation.experiment.IExperiment;
23 import org.simantics.simulation.experiment.IHistoryExperiment;
24 import org.simantics.simulation.export.CSVItemsQuery.CSVItem;
25 import org.simantics.simulation.project.ExperimentManager;
26 import org.simantics.simulation.project.IExperimentManager;
27 import org.simantics.utils.datastructures.MapList;
29 public class ExportSubscriptionCSV extends ExperimentExportClass implements ExportClass {
32 public void export(List<Content> contents,
34 ExportContext context,
36 IProgressMonitor monitor,
37 MapList<Content, Content> attachmentMap
38 ) throws ExportException {
40 // Flush all experiments, just in case.
41 IExperimentManager em = Simantics.getProject().getHint( ExperimentManager.KEY_EXPERIMENT_MANAGER );
43 for (IExperiment exp : em.getExperiments()) {
44 if ( exp instanceof IHistoryExperiment ) {
45 IHistoryExperiment he = (IHistoryExperiment) exp;
48 } catch (HistoryException e) {
54 CSVWriter writer = (CSVWriter) writer_;
55 List<HistoryManager> historiesToClose = new ArrayList<HistoryManager>();
57 List<ModelRef> models = ExperimentExportClass.getResult(context, options, true);
59 // Label format is the following
60 // "MODEL EXPERIMENT/RUN ITEM"
61 // MODEL is omited, if itemCount <= 1
62 // RUN is omited, if runCount <= 1
64 int modelCount = models.size();
65 for (ModelRef model : models) runCount += model.enabledRunCount();
67 List<CSVItem> items = new ArrayList<CSVItem>();
68 for (Content content : contents) {
69 items.addAll( context.session.syncRequest( new CSVItemsQuery( content.url ) ) );
73 for (ModelRef model : models) {
74 for (ExperimentRef experiment : model.experiments) {
75 if ( experiment.runs.isEmpty() ) continue;
76 for (RunRef run : experiment.runs) {
77 if ( run.historyFolder == null || !run.historyFolder.exists() ) continue;
79 HistoryManager history = History.openFileHistory( run.historyFolder );
80 historiesToClose.add( history );
81 ItemManager im = new ItemManager( history.getItems() );
83 for ( CSVItem item : items ) {
84 if ( !item.modelUri.equals( model.uri ) ) continue;
85 StringBuilder labelBuilder = new StringBuilder();
86 labelBuilder.append( item.label );
88 labelBuilder.append(" (");
89 labelBuilder.append(model.label);
90 labelBuilder.append(")");
93 labelBuilder.append(" (");
94 labelBuilder.append( experiment.label );
95 labelBuilder.append( "\\" );
96 labelBuilder.append( run.label );
97 labelBuilder.append(")");
100 List<Bean> historyItems = im.search("variableId", item.variableReference);
101 Collections.sort(historyItems, SamplingFormat.INTERVAL_COMPARATOR);
102 if (items.isEmpty()) continue;
103 Bean config = historyItems.get(0);
104 String historyId = (String) config.getFieldUnchecked("id");
106 writer.addItem(history, historyId, labelBuilder.toString(), item.variableReference, item.unit);
113 } catch (DatabaseException e) {
114 throw new ExportException( e );
115 } catch (HistoryException e) {
116 throw new ExportException( e );
117 } catch (BindingException e) {
118 throw new ExportException( e );
120 for (HistoryManager history : historiesToClose) history.close();
126 public List<String> validate(String contentUri, ExportContext context, Variant options) {
127 return Collections.emptyList();