Search in sources :

Example 56 with VType

use of org.diirt.vtype.VType in project org.csstudio.display.builder by kasemir.

the class FormatOptionHandlerTest method testHexParse.

@Test
public void testHexParse() throws Exception {
    final VType number = ValueFactory.newVDouble(65535.0, display);
    // Parse hex as hex
    Object parsed = FormatOptionHandler.parse(number, "0xFF", FormatOption.HEX);
    System.out.println(parsed);
    assertThat(parsed, instanceOf(Number.class));
    assertThat(((Number) parsed).intValue(), equalTo(255));
    // Parse hex when using default format
    parsed = FormatOptionHandler.parse(number, "0x7777", FormatOption.DEFAULT);
    System.out.println(parsed);
    assertThat(parsed, instanceOf(Number.class));
    assertThat(((Number) parsed).intValue(), equalTo(30583));
    // Parse hex without '0x' identifier when using hex format
    parsed = FormatOptionHandler.parse(number, "0xFFFF", FormatOption.HEX);
    System.out.println(parsed);
    assertThat(parsed, instanceOf(Number.class));
    assertThat(((Number) parsed).intValue(), equalTo(65535));
}
Also used : VType(org.diirt.vtype.VType) ListNumber(org.diirt.util.array.ListNumber) Test(org.junit.Test)

Example 57 with VType

use of org.diirt.vtype.VType in project org.csstudio.display.builder by kasemir.

the class FormatOptionHandlerTest method testSexagesimalParser.

@Test
public void testSexagesimalParser() throws Exception {
    final VType number = ValueFactory.newVDouble(0.0);
    assertEquals(12.5824414, (Double) FormatOptionHandler.parse(number, "12:34:56.789", FormatOption.SEXAGESIMAL), 0.0000001);
    assertEquals(Math.PI, (Double) FormatOptionHandler.parse(number, "12:00:00.000", FormatOption.SEXAGESIMAL_HMS), 0.0000001);
    assertEquals(12.5824414, (Double) FormatOptionHandler.parse(number, "48:03:40.989", FormatOption.SEXAGESIMAL_HMS), 0.0000001);
    assertEquals(Math.PI, (Double) FormatOptionHandler.parse(number, "180:00:00.000", FormatOption.SEXAGESIMAL_DMS), 0.0000001);
    assertEquals(12.5824414, (Double) FormatOptionHandler.parse(number, "720:55:14.837", FormatOption.SEXAGESIMAL_DMS), 0.0000001);
}
Also used : VType(org.diirt.vtype.VType) Test(org.junit.Test)

Example 58 with VType

use of org.diirt.vtype.VType in project org.csstudio.display.builder by kasemir.

the class CSVSampleImporter method importValues.

/**
 * {@inheritDoc}
 */
@Override
public List<VType> importValues(final InputStream input) throws Exception {
    // To be reentrant, need per-call parsers
    final DateFormat date_parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    final Pattern pattern = Pattern.compile(// YYYY/MM/DD HH:MM:SS.SSSSSSSSS   value  ignore
    "\\s*([0-9][0-9][0-9][0-9][-/][0-9][0-9][-/][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\.[0-9]*)[ \\t,]+([-+0-9.,eE]+)\\s*.*");
    final Pattern statisticsPattern = Pattern.compile(// YYYY/MM/DD HH:MM:SS.SSSSSSSSS   value    negativeError    positiveError    ignore
    "\\s*([0-9][0-9][0-9][0-9][-/][0-9][0-9][-/][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\.[0-9]*)[ \\t,]+([-+0-9.,eE]+)[ \\t,]+([-+0-9.,eE]+)[ \\t,]+([-+0-9.,eE]+)\\s*.*");
    final List<VType> values = new ArrayList<VType>();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    String line;
    char groupingSeparator = DecimalFormatSymbols.getInstance().getGroupingSeparator();
    char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
    boolean statistics = true;
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        // Skip empty lines, comments
        if (line.length() <= 0 || line.startsWith("#"))
            continue;
        statistics = true;
        // Locate time and value
        // Is statistical data?
        Matcher matcher = statisticsPattern.matcher(line);
        if (!matcher.matches()) {
            // Not statistical data, try normal
            matcher = pattern.matcher(line);
            if (!matcher.matches()) {
                logger.log(Level.INFO, "Ignored input: {0}", line);
                continue;
            }
            statistics = false;
        }
        // Parse
        // Date may use '-' or '/' as separator. Force '-'
        String date_text = matcher.group(1).replace('/', '-');
        // Can only parse up to millisecs, so limit length
        if (date_text.length() > 23)
            date_text = date_text.substring(0, 23);
        final Date date = date_parser.parse(date_text);
        // Double.parseDouble only parses numbers in format #.#... or #.#...#E0, meaning
        // that you cannot have any grouping separators, and the decimal separator must be '.'
        // First remove all grouping separators, then replace the decimal separator with a '.'
        final double number = Double.parseDouble(remove(matcher.group(2), groupingSeparator).replace(decimalSeparator, '.'));
        final Instant time = TimestampHelper.fromMillisecs(date.getTime());
        if (statistics) {
            final double min = Double.parseDouble(remove(matcher.group(3), groupingSeparator).replace(decimalSeparator, '.'));
            final double max = Double.parseDouble(remove(matcher.group(4), groupingSeparator).replace(decimalSeparator, '.'));
            values.add(new ArchiveVStatistics(time, AlarmSeverity.NONE, "", meta_data, number, number - min, number + max, 0, 1));
        } else {
            values.add(new ArchiveVNumber(time, AlarmSeverity.NONE, "", meta_data, number));
        }
    }
    reader.close();
    return values;
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ArchiveVNumber(org.csstudio.archive.vtype.ArchiveVNumber) Instant(java.time.Instant) ArchiveVStatistics(org.csstudio.archive.vtype.ArchiveVStatistics) ArrayList(java.util.ArrayList) Date(java.util.Date) VType(org.diirt.vtype.VType) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BufferedReader(java.io.BufferedReader) SimpleDateFormat(java.text.SimpleDateFormat)

Example 59 with VType

use of org.diirt.vtype.VType in project org.csstudio.display.builder by kasemir.

the class PlainExportJob method performExport.

/**
 * {@inheritDoc}
 */
@Override
protected void performExport(final IProgressMonitor monitor, final PrintStream out) throws Exception {
    int count = 0;
    for (ModelItem item : model.getItems()) {
        // Item header
        if (count > 0)
            out.println();
        printItemInfo(out, item);
        // Get data
        monitor.subTask(NLS.bind("Fetching data for {0}", item.getName()));
        final ValueIterator values = createValueIterator(item);
        // Dump all values
        out.println(comment + Messages.TimeColumn + Messages.Export_Delimiter + formatter.getHeader());
        long line_count = 0;
        while (values.hasNext() && !monitor.isCanceled()) {
            final VType value = values.next();
            final String time = TimestampHelper.format(VTypeHelper.getTimestamp(value));
            out.println(time + Messages.Export_Delimiter + formatter.format(value));
            ++line_count;
            if (++line_count % PROGRESS_UPDATE_LINES == 0)
                monitor.subTask(NLS.bind("{0}: Wrote {1} samples", item.getName(), line_count));
        }
        ++count;
    }
}
Also used : VType(org.diirt.vtype.VType) ModelItem(org.csstudio.trends.databrowser3.model.ModelItem) ValueIterator(org.csstudio.archive.reader.ValueIterator)

Example 60 with VType

use of org.diirt.vtype.VType in project org.csstudio.display.builder by kasemir.

the class FormulaItem method compute.

/**
 * Evaluate formula for each input sample
 *  <p>
 *  Iterates over the input samples in a manner of spreadsheet or
 *  staircase-interpolation: An input with a time stamp is valid
 *  until there's a sample with a greater time stamp.
 */
private void compute() {
    final List<PlotSample> result = new ArrayList<PlotSample>();
    final Display display = ValueFactory.displayNone();
    try {
        // Prevent changes to formula & inputs
        synchronized (this) {
            // 'Current' value for each input or null when no more
            // In computation loop, values is actually moved to the _next_
            // value
            final VType[] values = new VType[inputs.length];
            // 'Current' numeric min/val/max of values
            final double[] min = new double[inputs.length];
            final double[] val = new double[inputs.length];
            final double[] max = new double[inputs.length];
            // Determine first sample for each input
            boolean more_input = false;
            for (int i = 0; i < values.length; i++) {
                // Initially, none have any data
                min[i] = val[i] = max[i] = Double.NaN;
                // Is there an initial value for any input?
                values[i] = inputs[i].first();
                if (values[i] != null)
                    more_input = true;
            }
            // Compute result for each 'line in the spreadsheet'
            Instant time;
            while (more_input) {
                // Find oldest time stamp of all the inputs
                time = null;
                for (int i = 0; i < values.length; i++) {
                    if (values[i] == null)
                        continue;
                    final Instant sample_time = VTypeHelper.getTimestamp(values[i]);
                    if (time == null || sample_time.compareTo(time) < 0)
                        time = sample_time;
                }
                if (time == null) {
                    // No input left with any data
                    more_input = false;
                    break;
                }
                // 'time' now defines the current spreadsheet line.
                // Set min/max/val to sample from each input for that time.
                // This might move values[i] resp. the inputs' iterators
                // to the 'next' sample
                boolean have_min_max = true;
                for (int i = 0; i < values.length; i++) {
                    if (// No more data
                    values[i] == null) {
                        min[i] = val[i] = max[i] = Double.NaN;
                        have_min_max = false;
                    } else if (VTypeHelper.getTimestamp(values[i]).compareTo(time) <= 0) {
                        // Input is valid before-and-up-to 'time'
                        if (values[i] instanceof VStatistics) {
                            final VStatistics mmv = (VStatistics) values[i];
                            min[i] = mmv.getMin();
                            val[i] = mmv.getAverage();
                            max[i] = mmv.getMax();
                        } else {
                            min[i] = max[i] = Double.NaN;
                            val[i] = VTypeHelper.toDouble(values[i]);
                            // Use NaN for any non-number
                            if (Double.isInfinite(val[i]))
                                val[i] = Double.NaN;
                            have_min_max = false;
                        }
                        // Move to next input sample
                        values[i] = inputs[i].next();
                    } else {
                        // Just update the have_min_max flag
                        if (Double.isNaN(min[i]) || Double.isNaN(max[i]))
                            have_min_max = false;
                    }
                }
                // Set variables[] from val to get res_val
                for (int i = 0; i < values.length; i++) variables[i].setValue(val[i]);
                // Evaluate formula for these inputs
                final double res_val = formula.eval();
                final VType value;
                if (have_min_max) {
                    // Set variables[] from min
                    for (int i = 0; i < values.length; i++) variables[i].setValue(min[i]);
                    final double res_min = formula.eval();
                    // Set variables[] from max
                    for (int i = 0; i < values.length; i++) variables[i].setValue(max[i]);
                    final double res_max = formula.eval();
                    value = new ArchiveVStatistics(time, AlarmSeverity.NONE, Messages.Formula, display, res_val, res_min, res_max, 0.0, 1);
                } else {
                    // No min/max.
                    if (Double.isNaN(res_val))
                        value = new ArchiveVNumber(time, AlarmSeverity.INVALID, Messages.Formula, display, res_val);
                    else
                        value = new ArchiveVNumber(time, AlarmSeverity.NONE, ArchiveVType.STATUS_OK, display, res_val);
                }
                result.add(new PlotSample(Messages.Formula, value));
            }
        }
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error computing " + this, ex);
    }
    // Update PlotSamples
    samples.set(result);
}
Also used : ArchiveVNumber(org.csstudio.archive.vtype.ArchiveVNumber) Instant(java.time.Instant) ArchiveVStatistics(org.csstudio.archive.vtype.ArchiveVStatistics) ArrayList(java.util.ArrayList) VType(org.diirt.vtype.VType) ArchiveVType(org.csstudio.archive.vtype.ArchiveVType) VStatistics(org.diirt.vtype.VStatistics) ArchiveVStatistics(org.csstudio.archive.vtype.ArchiveVStatistics) Display(org.diirt.vtype.Display)

Aggregations

VType (org.diirt.vtype.VType)69 Test (org.junit.Test)17 IPV (org.csstudio.simplepv.IPV)8 ArrayList (java.util.ArrayList)7 Display (org.diirt.vtype.Display)7 VEnum (org.diirt.vtype.VEnum)7 IWidgetPropertyChangeHandler (org.csstudio.opibuilder.properties.IWidgetPropertyChangeHandler)6 ListNumber (org.diirt.util.array.ListNumber)6 IFigure (org.eclipse.draw2d.IFigure)6 Instant (java.time.Instant)5 ModelItem (org.csstudio.trends.databrowser3.model.ModelItem)5 VNumberArray (org.diirt.vtype.VNumberArray)5 VString (org.diirt.vtype.VString)5 List (java.util.List)4 RuntimePV (org.csstudio.display.builder.runtime.pv.RuntimePV)4 RuntimePVListener (org.csstudio.display.builder.runtime.pv.RuntimePVListener)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 ValueIterator (org.csstudio.archive.reader.ValueIterator)3 ArchiveVNumber (org.csstudio.archive.vtype.ArchiveVNumber)3 WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)3