Search in sources :

Example 16 with ListNumber

use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.

the class VType method toVType.

/**
 * Converts a standard java type to VTypes. Returns null if no conversion
 * is possible.
 * <p>
 * Types are converted as follow:
 * <ul>
 *   <li>Boolean -&gt; VBoolean</li>
 *   <li>Number -&gt; corresponding VNumber</li>
 *   <li>String -&gt; VString</li>
 *   <li>number array -&gt; corresponding VNumberArray</li>
 *   <li>ListNumber -&gt; corresponding VNumberArray</li>
 *   <li>List -&gt; if all elements are String, VStringArray</li>
 * </ul>
 *
 * @param javaObject the value to wrap
 * @param alarm the alarm
 * @param time the time
 * @param display the display
 * @return the new VType value
 */
public static VType toVType(Object javaObject, Alarm alarm, Time time, Display display) {
    if (javaObject instanceof Number) {
        return VNumber.create((Number) javaObject, alarm, time, display);
    } else if (javaObject instanceof String) {
        return VString.create((String) javaObject, alarm, time);
    } else if (javaObject instanceof Boolean) {
        // newVBoolean((Boolean) javaObject, alarm, time);
        return null;
    } else if (javaObject instanceof byte[] || javaObject instanceof short[] || javaObject instanceof int[] || javaObject instanceof long[] || javaObject instanceof float[] || javaObject instanceof double[]) {
        return VNumberArray.create(ListNumbers.toListNumber(javaObject), alarm, time, display);
    } else if (javaObject instanceof ListNumber) {
        return VNumberArray.create((ListNumber) javaObject, alarm, time, display);
    } else if (javaObject instanceof String[]) {
        // newVStringArray(Arrays.asList((String[]) javaObject), alarm, time);
        return null;
    } else if (javaObject instanceof List) {
        boolean matches = true;
        List list = (List) javaObject;
        for (Object object : list) {
            if (!(object instanceof String)) {
                matches = false;
            }
        }
        if (matches) {
            @SuppressWarnings("unchecked") List<String> newList = (List<String>) list;
            // newVStringArray(Collections.unmodifiableList(newList), alarm, time);
            return null;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : ListNumber(org.diirt.util.array.ListNumber) ListNumber(org.diirt.util.array.ListNumber) List(java.util.List)

Example 17 with ListNumber

use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.

the class VTableFactory method join.

public static VTable join(VTable... tables) {
    if (tables.length == 0) {
        return null;
    }
    if (tables.length == 1) {
        return tables[0];
    }
    // Find columns to join
    Map<String, int[]> commonColumnsIndexes = null;
    for (int nTable = 0; nTable < tables.length; nTable++) {
        VTable vTable = tables[nTable];
        if (commonColumnsIndexes == null) {
            commonColumnsIndexes = new HashMap<>();
            for (int i = 0; i < vTable.getColumnCount(); i++) {
                int[] indexes = new int[tables.length];
                indexes[0] = i;
                commonColumnsIndexes.put(vTable.getColumnName(i), indexes);
            }
        } else {
            commonColumnsIndexes.keySet().retainAll(columnNames(vTable));
            for (int i = 0; i < vTable.getColumnCount(); i++) {
                if (commonColumnsIndexes.keySet().contains(vTable.getColumnName(i))) {
                    commonColumnsIndexes.get(vTable.getColumnName(i))[nTable] = i;
                }
            }
        }
    }
    if (commonColumnsIndexes.isEmpty()) {
        throw new UnsupportedOperationException("Case not implemented yet");
    }
    List<EqualValueFilter> filters = new ArrayList<>();
    for (Map.Entry<String, int[]> entry : commonColumnsIndexes.entrySet()) {
        int[] indexes = entry.getValue();
        filters.add(new EqualValueFilter(Arrays.asList(tables), indexes));
    }
    // Find rows
    boolean done = false;
    List<BufferInt> rowIndexes = new ArrayList<>();
    for (int i = 0; i < tables.length; i++) {
        rowIndexes.add(new BufferInt());
        if (tables[i].getRowCount() == 0) {
            done = true;
        }
    }
    int[] currentIndexes = new int[tables.length];
    while (!done) {
        boolean match = true;
        for (EqualValueFilter filter : filters) {
            match = match && filter.filterRow(currentIndexes);
        }
        if (match) {
            for (int i = 0; i < currentIndexes.length; i++) {
                rowIndexes.get(i).addInt(currentIndexes[i]);
            }
        }
        boolean needsIncrement = true;
        int offset = currentIndexes.length - 1;
        while (needsIncrement) {
            currentIndexes[offset]++;
            if (currentIndexes[offset] == tables[offset].getRowCount()) {
                currentIndexes[offset] = 0;
                offset--;
                if (offset == -1) {
                    done = true;
                    needsIncrement = false;
                }
            } else {
                needsIncrement = false;
            }
        }
    }
    List<String> columnNames = new ArrayList<>();
    List<Class<?>> columnTypes = new ArrayList<>();
    List<Object> columnData = new ArrayList<>();
    for (int nColumn = 0; nColumn < tables[0].getColumnCount(); nColumn++) {
        columnNames.add(tables[0].getColumnName(nColumn));
        Class<?> type = tables[0].getColumnType(nColumn);
        if (type.isPrimitive()) {
            columnTypes.add(type);
            columnData.add(ListNumbers.listView((ListNumber) tables[0].getColumnData(nColumn), rowIndexes.get(0)));
        } else {
            columnTypes.add(type);
            columnData.add(createView((List<?>) tables[0].getColumnData(nColumn), rowIndexes.get(0)));
        }
    }
    for (int i = 1; i < tables.length; i++) {
        VTable vTable = tables[i];
        for (int nColumn = 0; nColumn < vTable.getColumnCount(); nColumn++) {
            if (!commonColumnsIndexes.containsKey(vTable.getColumnName(nColumn))) {
                columnNames.add(vTable.getColumnName(nColumn));
                Class<?> type = vTable.getColumnType(nColumn);
                if (type.isPrimitive()) {
                    columnTypes.add(type);
                    columnData.add(ListNumbers.listView((ListNumber) vTable.getColumnData(nColumn), rowIndexes.get(i)));
                } else {
                    columnTypes.add(type);
                    columnData.add(createView((List<?>) vTable.getColumnData(nColumn), rowIndexes.get(i)));
                }
            }
        }
    }
    return ValueFactory.newVTable(columnTypes, columnNames, columnData);
}
Also used : ArrayList(java.util.ArrayList) VString(org.diirt.vtype.VString) ListNumber(org.diirt.util.array.ListNumber) BufferInt(org.diirt.util.array.BufferInt) VTable(org.diirt.vtype.VTable) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ListNumber

use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.

the class VTableFactory method validateTable.

public static void validateTable(VTable vTable) {
    for (int i = 0; i < vTable.getColumnCount(); i++) {
        Class<?> type = null;
        String name = null;
        Object data = null;
        try {
            type = vTable.getColumnType(i);
        } catch (RuntimeException ex) {
            throw new IllegalArgumentException("Can't get column " + i + " type", ex);
        }
        try {
            name = vTable.getColumnName(i);
        } catch (RuntimeException ex) {
            throw new IllegalArgumentException("Can't get column " + i + " name", ex);
        }
        try {
            data = vTable.getColumnData(i);
        } catch (RuntimeException ex) {
            throw new IllegalArgumentException("Can't get column " + i + " data", ex);
        }
        if (String.class.equals(type)) {
            if (!(data instanceof List)) {
                throw new IllegalArgumentException("Data for column " + i + " (" + name + ") is not a List<String> (" + data + ")");
            }
        } else if (type.equals(double.class) || type.equals(float.class) || type.equals(long.class) || type.equals(int.class) || type.equals(short.class) || type.equals(byte.class)) {
            if (!(data instanceof ListNumber)) {
                throw new IllegalArgumentException("Data for column " + i + " (" + name + ") is not a ListNumber (" + data + ")");
            }
        } else if (type.equals(Instant.class)) {
            if (!(data instanceof List)) {
                throw new IllegalArgumentException("Data for column " + i + " (" + name + ") is not a List<Timestamp> (" + data + ")");
            }
        } else {
            throw new IllegalArgumentException("Column type " + type.getSimpleName() + " not supported");
        }
    }
}
Also used : ListNumber(org.diirt.util.array.ListNumber) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) VString(org.diirt.vtype.VString)

Example 19 with ListNumber

use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.

the class HistogramOfFormulaFunction method calculate.

@Override
public Object calculate(List<Object> args) {
    VNumberArray numberArray = (VNumberArray) args.get(0);
    if (numberArray == null) {
        return null;
    }
    // If no change, return previous
    if (previousValue == numberArray) {
        return previousResult;
    }
    Statistics stats = StatisticsUtil.statisticsOf(numberArray.getData());
    int nBins = 100;
    Range aggregatedRange = Ranges.aggregateRange(stats.getRange(), previousXRange);
    Range xRange;
    if (Ranges.overlap(aggregatedRange, stats.getRange()) >= 0.75) {
        xRange = aggregatedRange;
    } else {
        xRange = stats.getRange();
    }
    IteratorNumber newValues = numberArray.getData().iterator();
    double minValueRange = xRange.getMinimum();
    double maxValueRange = xRange.getMaximum();
    ListNumber xBoundaries = ListNumbers.linearListFromRange(minValueRange, maxValueRange, nBins + 1);
    String unit = numberArray.getUnits();
    int[] binData = new int[nBins];
    double maxCount = 0;
    while (newValues.hasNext()) {
        double value = newValues.nextDouble();
        // Check value in range
        if (xRange.contains(value)) {
            int bin = (int) Math.floor(xRange.normalize(value) * nBins);
            if (bin == nBins) {
                bin--;
            }
            binData[bin]++;
            if (binData[bin] > maxCount) {
                maxCount = binData[bin];
            }
        }
    }
    if (previousMaxCount > maxCount && previousMaxCount < maxCount * 2.0) {
        maxCount = previousMaxCount;
    }
    previousMaxCount = maxCount;
    previousXRange = xRange;
    previousValue = numberArray;
    previousResult = newVNumberArray(new ArrayInt(binData), new ArrayInt(nBins), Arrays.asList(newDisplay(xBoundaries, unit)), numberArray, numberArray, newDisplay(0.0, 0.0, 0.0, "count", NumberFormats.format(0), maxCount, maxCount, maxCount, Double.NaN, Double.NaN));
    return previousResult;
}
Also used : VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber) Range(org.diirt.util.Range) ArrayInt(org.diirt.util.array.ArrayInt) Statistics(org.diirt.util.Statistics) IteratorNumber(org.diirt.util.array.IteratorNumber)

Example 20 with ListNumber

use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.

the class ValueUtil method getStringArray.

/**
 * Get string array from pv.
 *  @param value Value of a PV
 *  @return String array.
 *          For string array, it's the actual strings.
 *          For numeric arrays, the numbers are formatted as strings.
 *          For enum array, the labels are returned.
 *          For scalar PVs, an array with a single string is returned.
 */
public static final String[] getStringArray(final VType value) {
    if (value instanceof VStringArray) {
        final List<String> list = ((VStringArray) value).getData();
        return list.toArray(new String[list.size()]);
    } else if (value instanceof VDoubleArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) text[i] = Double.toString(list.getDouble(i));
        return text;
    } else if (value instanceof VNumberArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) text[i] = Long.toString(list.getLong(i));
        return text;
    } else if (value instanceof VEnumArray) {
        final List<String> labels = ((VEnumArray) value).getLabels();
        final ListInt list = ((VEnumArray) value).getIndexes();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) {
            final int index = list.getInt(i);
            if (index >= 0 && index <= labels.size())
                text[i] = labels.get(index);
            else
                text[i] = "<" + index + ">";
        }
        return text;
    }
    return new String[] { getString(value) };
}
Also used : VDoubleArray(org.diirt.vtype.VDoubleArray) VNumberArray(org.diirt.vtype.VNumberArray) VEnumArray(org.diirt.vtype.VEnumArray) ListNumber(org.diirt.util.array.ListNumber) ListInt(org.diirt.util.array.ListInt) ArrayList(java.util.ArrayList) List(java.util.List) VStringArray(org.diirt.vtype.VStringArray)

Aggregations

ListNumber (org.diirt.util.array.ListNumber)29 VNumberArray (org.diirt.vtype.VNumberArray)13 List (java.util.List)10 VString (org.diirt.vtype.VString)9 ArrayList (java.util.ArrayList)6 ArrayDouble (org.diirt.util.array.ArrayDouble)5 ArrayInt (org.diirt.util.array.ArrayInt)5 ListInt (org.diirt.util.array.ListInt)5 VEnumArray (org.diirt.vtype.VEnumArray)5 VNumber (org.diirt.vtype.VNumber)4 VTable (org.diirt.vtype.VTable)4 VType (org.diirt.vtype.VType)4 Test (org.junit.Test)4 AbstractList (java.util.AbstractList)3 ListDouble (org.diirt.util.array.ListDouble)3 VEnum (org.diirt.vtype.VEnum)3 VStringArray (org.diirt.vtype.VStringArray)3 BufferedImage (java.awt.image.BufferedImage)2 Background (javafx.scene.layout.Background)2 BackgroundFill (javafx.scene.layout.BackgroundFill)2