1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.history.csv;
14 import java.io.IOException;
15 import java.io.UnsupportedEncodingException;
16 import java.math.BigDecimal;
17 import java.net.URLDecoder;
18 import java.text.DecimalFormat;
19 import java.text.DecimalFormatSymbols;
20 import java.text.Format;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Locale;
26 import org.simantics.databoard.accessor.StreamAccessor;
27 import org.simantics.databoard.accessor.error.AccessorException;
28 import org.simantics.history.HistoryException;
29 import org.simantics.history.HistoryManager;
30 import org.simantics.history.util.HistoryExportUtil;
31 import org.simantics.history.util.ProgressMonitor;
32 import org.simantics.history.util.StreamIterator;
33 import org.simantics.history.util.ValueBand;
36 * CSV writer for history items.
38 * @author Toni Kalajainen
39 * @author Tuukka Lehtonen
41 public class CSVFormatter {
43 List<Item> items = new ArrayList<Item>();
44 double from = -Double.MAX_VALUE;
45 double end = Double.MAX_VALUE;
46 double startTime = 0.0;
47 double timeStep = 0.0;
48 ColumnSeparator columnSeparator;
49 DecimalSeparator decimalSeparator;
58 Formatter timeFormatter;
59 Formatter floatFormatter;
60 Formatter numberFormatter;
62 ExportInterpolation numberInterpolation = ExportInterpolation.LINEAR_INTERPOLATION;
64 public CSVFormatter() {
65 this.lineFeed = resolvePlatformLineFeed();
66 this.locale = Locale.getDefault(Locale.Category.FORMAT);
68 DecimalFormat defaultFormat = new DecimalFormat();
69 defaultFormat.setGroupingUsed(false);
70 setTimeFormat(defaultFormat);
71 setFloatFormat(defaultFormat);
72 setNumberFormat(defaultFormat);
76 * Add item to formatter
78 * @param historyItemId
80 * @param variableReference
83 public void addItem( HistoryManager history, String historyItemId, String label, String variableReference, String unit ) {
86 i.label = label!=null?label:"";
87 i.variableReference = variableReference!=null?variableReference:"";
88 i.variableReference = unescape(i.variableReference);
89 i.historyItemId = historyItemId;
91 if ( !items.contains(i) ) items.add( i );
94 private static String unescape(String url) {
96 return URLDecoder.decode(url, "UTF-8");
97 } catch (UnsupportedEncodingException e) {
103 * Sort items by variableId, label1, label2
106 Collections.sort(items);
109 public void setTimeRange( double from, double end ) {
114 public void setStartTime( double startTime ) {
115 this.startTime = startTime;
118 public void setTimeStep( double timeStep ) {
119 this.timeStep = timeStep;
122 void openHistory() throws HistoryException {
124 for (Item item : items) item.open();
125 } catch (HistoryException e) {
126 for (Item item : items) item.close();
131 void closeHistory() {
132 for (Item item : items) item.close();
136 * Reads visible data of all variables and formats as CSV lines (Comma
137 * Separated Values). Line Feed is \n, variable separator is \t, and
138 * decimal separator locale dependent.
140 * ReadData1 outputs separate time and value columns for each variable
142 * Variable1 Time | Variable1 Value | Variable2 Time | Variable2 Value
143 * 0.0 | 1.0 | 0.1 | 23423.0
147 * @throws HistoryException
148 * @throws IOException
151 public void formulate1( ProgressMonitor monitor, Appendable sb ) throws HistoryException, IOException {
152 if (items.isEmpty()) return;
153 boolean adaptComma = decimalSeparatorInLocale != decimalSeparator;
156 // Prepare columns: First time
157 for (Item item : items)
159 if (monitor.isCanceled())
161 if (item.stream.isEmpty()) continue;
162 Double firstTime = (Double) item.stream.getFirstTime( Bindings.DOUBLE );
163 if (from <= firstTime) {
164 item.time = firstTime;
166 item.time = (Double) item.stream.getFloorTime(Bindings.DOUBLE, from);
173 if (monitor.isCanceled())
175 boolean lastColumn = i == items.get( items.size()-1 );
176 sb.append(i.label + " Time");
177 sb.append( columnSeparator );
178 sb.append(i.label + " Value");
179 sb.append(lastColumn ? lineFeed : columnSeparator );
182 // Iterate until endTime is met for all variables
185 if (monitor.isCanceled())
191 boolean lastColumn = i == items.get( items.size()-1 );
193 if (i.time == null || i.time > end) {
195 sb.append( lastColumn ? columnSeparator+lineFeed : columnSeparator+columnSeparator);
202 String timeStr = format.format( i.time );
203 if ( adaptComma ) timeStr = timeStr.replace(decimalSeparatorInLocale, decimalSeparator);
204 sb.append( timeStr );
205 sb.append( columnSeparator );
208 i.value = i.stream.getValue(Bindings.DOUBLE, i.time);
209 if (i.value instanceof Number) {
210 String str = format.format( i.value );
211 if ( adaptComma ) str = str.replace(decimalSeparatorInLocale, decimalSeparator);
213 } else if (i.value instanceof Boolean) {
214 sb.append( (Boolean)i.value ? "1": "0");
216 sb.append( i.value.toString() );
218 sb.append(lastColumn ? lineFeed : columnSeparator);
221 i.time = (Double) i.stream.getHigherTime(Bindings.DOUBLE, i.time);
224 } while (readyColumns < items.size());
231 * Reads visible data of all variables and formats as CSV lines (Comma
232 * Separated Values). Line Feed is \n, variable separator is \t, and
233 * decimal separator locale dependent.
235 * ReadData2 outputs one shared time and one value column for each variable
237 * Time | Variable1 Label | Variable3 Label
238 * | Variable1 Id | Variable3 Id
239 * | Variable1 Unit | Variable3 Unit
244 * @throws HistoryException
245 * @throws IOException
247 public void formulate2( ProgressMonitor monitor, Appendable sb ) throws HistoryException, IOException
249 if ( items.isEmpty() ) return;
251 timeFormatter = evaluateFormatter(timeFormat, decimalSeparator);
252 floatFormatter = evaluateFormatter(floatFormat, decimalSeparator);
253 numberFormatter = evaluateFormatter(numberFormat, decimalSeparator);
257 // What is the time range of all items combined
258 double allFrom = Double.MAX_VALUE;
259 double allEnd = -Double.MAX_VALUE;
260 for (Item i : items) {
261 if (i.iter.isEmpty()) continue;
262 allFrom = Math.min(allFrom, i.iter.getFirstTime());
263 allEnd = Math.max(allEnd, i.iter.getLastTime());
267 for (int hl = 0; hl < 3; ++hl) {
269 case 0: sb.append("Time"); break;
270 case 1: sb.append("----"); break;
271 case 2: sb.append("Unit"); break;
273 sb.append( columnSeparator.preference );
276 boolean lastColumn = i == items.get( items.size()-1 );
279 sb.append(i.label != null ? i.label : "");
282 sb.append(i.variableReference != null ? i.variableReference : "");
285 sb.append(i.unit==null?"no unit":i.unit);
288 if (!lastColumn) sb.append( columnSeparator.preference );
290 sb.append( lineFeed );
294 boolean hasAnyValues = allFrom != Double.MAX_VALUE && allEnd != -Double.MAX_VALUE;
296 // Make intersection of actual data range (allFrom, allEnd) and requested data (from, end)
297 double _from = Double.MAX_VALUE, _end = -Double.MAX_VALUE;
299 _from = Math.max(allFrom, from);
300 _end = Math.min(allEnd, end);
303 if (!hasAnyValues) return;
305 // Iterate until endTime is met for all variables
310 // If resample is false then all samples are reported as is. The code achieves this by setting startTime to _from and timeStep to 0.0
316 // time = startTime + n*timeStep
318 // Sampling based on given startTime and timeStep
321 // Find the first sample time that contains data
322 double n = Math.max(0, Math.ceil((_from-startTime) / timeStep));
323 time = startTime + n*timeStep;
327 // Start sampling from startTime but make sure that it is not less than _from
328 if(startTime > _from) time = startTime;
335 // Must convert double times to String when initializing BigDecimal.
336 // Otherwise BigDecimal will pick up inaccuracies from beyond 15 precise digits
337 // thus making a mess of the time step calculations.
339 BigDecimal bigTime = new BigDecimal(String.valueOf(time));
340 BigDecimal bigTimeStep = new BigDecimal(String.valueOf(timeStep));
342 for (Item i : items) i.iter.gotoTime(time);
344 if ( monitor!=null && monitor.isCanceled() ) return;
347 String timeStr = timeFormatter.format( time );
348 //System.out.println("SAMPLING TIME: " + time);
349 sb.append( timeStr );
354 sb.append( columnSeparator.preference );
357 if ( i.iter.hasValidValue() ) {
358 Object value = i.iter.getValueBand().getValue();
359 if (value instanceof Number) {
360 if (value instanceof Float || value instanceof Double) {
361 switch (numberInterpolation) {
362 case PREVIOUS_SAMPLE:
363 sb.append( formatNumber(value) );
366 case LINEAR_INTERPOLATION:
367 if (time != i.iter.getValueBand().getTimeDouble() && i.iter.hasNext()) {
370 int currentIndex = i.iter.getIndex();
371 ValueBand band = i.iter.getValueBand();
372 //double t1 = band.getTimeDouble();
373 Number v1 = (Number) value;
374 double t12 = band.getEndTimeDouble();
376 double t2 = i.iter.getValueBand().getTimeDouble();
377 Number v2 = (Number) i.iter.getValueBand().getValue();
378 i.iter.gotoIndex(currentIndex);
380 double vs = v1.doubleValue();
382 vs = HistoryExportUtil.biglerp(t12, v1.doubleValue(), t2, v2.doubleValue(), time);
384 sb.append( formatDouble(vs) );
386 // Exact timestamp match, or last sample.
387 // Don't interpolate nor extrapolate.
388 sb.append( formatNumber(value) );
392 throw new UnsupportedOperationException("Unsupported interpolation: " + numberInterpolation);
395 sb.append( value.toString() );
397 } else if (value instanceof Boolean) {
398 sb.append( (Boolean)value ? "1": "0");
400 sb.append( value.toString() );
405 sb.append( lineFeed );
407 // Read next values, and the following times
408 if ( timeStep>0.0 ) {
409 bigTime = bigTime.add(bigTimeStep);
410 time = bigTime.doubleValue();
412 // Get smallest end time that is larger than current time
413 Double nextTime = null;
414 // System.out.println("time = "+time);
415 for (Item i : items) {
416 Double itemNextTime = i.iter.getNextTime( time );
417 // System.err.println(" "+i.label+" nextTime="+itemNextTime);
418 if ( itemNextTime == null ) continue;
419 if ( itemNextTime < time ) continue;
420 if ( nextTime == null || ( nextTime > itemNextTime && !itemNextTime.equals( time ) ) ) nextTime = itemNextTime;
422 if ( nextTime == null || nextTime.equals( time ) ) break;
426 boolean hasMore = false;
428 for (Item i : items) {
429 i.iter.proceedToTime(time);
430 if(contains(i, time)) hasMore = true;
435 } while (time<=_end);
441 private boolean contains(Item item, double time) {
442 double start = item.iter.getStartTime();
443 double end = item.iter.getEndTime();
444 // A special case, where start == end => accept
445 if(time == start) return true;
446 else if(time < start) return false;
447 else if(time >= end) return false;
451 private CharSequence formatNumber(Object value) {
452 return value instanceof Float
453 ? floatFormatter.format( value )
454 : numberFormatter.format( value );
457 private CharSequence formatDouble(double value) {
458 return numberFormatter.format( value );
461 public void setDecimalSeparator(DecimalSeparator separator) {
462 this.decimalSeparator = separator;
465 public void setColumnSeparator(ColumnSeparator separator) {
466 this.columnSeparator = separator;
469 public void setResample(boolean resample) {
470 this.resample = resample;
473 public void setLineFeed( String lf ) {
477 public void setTimeFormat(Format format) {
478 this.timeFormat = format;
481 public void setFloatFormat(Format format) {
482 this.floatFormat = format;
485 public void setNumberFormat(Format format) {
486 this.numberFormat = format;
489 public void setLocale(Locale locale) {
490 this.locale = locale;
493 public void setNumberInterpolation(ExportInterpolation interpolation) {
494 this.numberInterpolation = interpolation;
497 private static String resolvePlatformLineFeed() {
498 String osName = System.getProperty("os.name", "");
499 osName = osName.toLowerCase();
500 if (osName.contains("windows"))
505 private class Item implements Comparable<Item> {
507 String label; // Label
508 String variableReference; // Label
509 HistoryManager history; // History source for this item
510 String historyItemId;
514 StreamAccessor accessor; // Stream accessor
517 public void open() throws HistoryException {
518 accessor = history.openStream(historyItemId, "r");
519 iter = new StreamIterator( accessor );
522 public void close() {
523 if (accessor!=null) {
526 } catch (AccessorException e) {
534 public int compareTo(Item o) {
536 i = label.compareTo(o.label);
538 i = variableReference.compareTo(o.variableReference);
540 i = historyItemId.compareTo(o.historyItemId);
546 public int hashCode() {
548 code = 13*code + variableReference.hashCode();
549 code = 13*code + label.hashCode();
550 code = 13*code + historyItemId.hashCode();
551 code = 13*code + history.hashCode();
556 public boolean equals(Object obj) {
557 if ( obj == null ) return false;
558 if ( obj instanceof Item == false ) return false;
559 Item other = (Item) obj;
560 if ( !other.label.equals(label) ) return false;
561 if ( !other.variableReference.equals(variableReference) ) return false;
562 if ( !other.history.equals(history) ) return false;
563 if ( !other.historyItemId.equals(historyItemId) ) return false;
569 static interface Formatter {
570 String format(Object number);
573 static class NopFormatter implements Formatter {
574 private final Format format;
575 public NopFormatter(Format format) {
576 this.format = format;
578 public String format(Object number) {
579 return format.format(number);
583 static class ReplacingFormatter implements Formatter {
584 private final Format format;
585 private final char from;
586 private final char to;
587 public ReplacingFormatter(Format format, char from, char to) {
588 this.format = format;
592 public String format(Object number) {
593 return format.format(number).replace(from, to);
597 private Formatter evaluateFormatter(Format format, DecimalSeparator target) {
598 // Probe decimal separator
599 String onePointTwo = format.format(1.2);
600 System.out.println("formatted zeroPointOne: " + onePointTwo);
602 DecimalSeparator formatSeparator;
603 if (onePointTwo.indexOf('.') != -1) {
604 formatSeparator = DecimalSeparator.DOT;
605 } else if (onePointTwo.indexOf(',') != -1) {
606 formatSeparator = DecimalSeparator.COMMA;
608 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
609 formatSeparator = DecimalSeparator.fromChar(symbols.getDecimalSeparator());
612 switch (formatSeparator) {
616 return new NopFormatter(format);
618 return new ReplacingFormatter(format, ',', '.');
623 return new ReplacingFormatter(format, '.', ',');
625 return new NopFormatter(format);
628 return new NopFormatter(format);