Search in sources :

Example 1 with Attribute

use of com.ibm.streams.operator.Attribute in project streamsx.topology by IBMStreams.

the class BOperatorInvocation method setParameter.

/*
    public BOperatorInvocation(GraphBuilder bt, String kind,
            Map<String, ? extends Object> params) {
        this(bt, Operator.class, params);
        json().put("kind", kind);
    }
    */
public void setParameter(String name, Object value) {
    Object jsonValue = value;
    String jsonType = null;
    if (value instanceof JSONAble) {
        value = ((JSONAble) value).toJSON();
    }
    if (value instanceof JSONObject) {
        JSONObject jo = ((JSONObject) value);
        if (jo.get("type") == null || jo.get("value") == null)
            throw new IllegalArgumentException("Illegal JSONObject " + jo);
        String type = (String) jo.get("type");
        Object val = (JSONObject) jo.get("value");
        if ("__spl_value".equals(type)) {
            /*
                 * The Value object is
                 * <pre><code>
                 * object {
                 *   type : "__spl_value"
                 *   value : object {
                 *     value : any. non-null. type appropriate for metaType
                 *     metaType : com.ibm.streams.operator.Type.MetaType.name() string
                 *   }
                 * }
                 * </code></pre>
                 */
            // unwrap and fall through to handling for the wrapped value
            JSONObject splValue = (JSONObject) val;
            value = splValue.get("value");
            jsonValue = value;
            String metaType = (String) splValue.get("metaType");
            if ("USTRING".equals(metaType) || "UINT8".equals(metaType) || "UINT16".equals(metaType) || "UINT32".equals(metaType) || "UINT64".equals(metaType)) {
                jsonType = metaType;
            }
        // fall through to handle jsonValue as usual 
        } else {
        // other kinds of JSONObject handled below
        }
    } else if (value instanceof Supplier<?>) {
        value = ((Supplier<?>) value).get();
        jsonValue = value;
    }
    if (value instanceof String) {
        op.setStringParameter(name, (String) value);
        if (jsonType == null)
            jsonType = MetaType.RSTRING.name();
    } else if (value instanceof Byte) {
        op.setByteParameter(name, (Byte) value);
        if (jsonType == null)
            jsonType = MetaType.INT8.name();
    } else if (value instanceof Short) {
        op.setShortParameter(name, (Short) value);
        if (jsonType == null)
            jsonType = MetaType.INT16.name();
    } else if (value instanceof Integer) {
        op.setIntParameter(name, (Integer) value);
        if (jsonType == null)
            jsonType = MetaType.INT32.name();
    } else if (value instanceof Long) {
        op.setLongParameter(name, (Long) value);
        if (jsonType == null)
            jsonType = MetaType.INT64.name();
    } else if (value instanceof Float) {
        op.setFloatParameter(name, (Float) value);
        jsonType = MetaType.FLOAT32.name();
    } else if (value instanceof Double) {
        op.setDoubleParameter(name, (Double) value);
        jsonType = MetaType.FLOAT64.name();
    } else if (value instanceof Boolean) {
        op.setBooleanParameter(name, (Boolean) value);
    } else if (value instanceof BigDecimal) {
        op.setBigDecimalParameter(name, (BigDecimal) value);
        // Need to maintain exact value
        jsonValue = value.toString();
    } else if (value instanceof Enum) {
        op.setCustomLiteralParameter(name, (Enum<?>) value);
        jsonValue = ((Enum<?>) value).name();
        jsonType = JParamTypes.TYPE_ENUM;
    } else if (value instanceof StreamSchema) {
        jsonValue = ((StreamSchema) value).getLanguageType();
        jsonType = JParamTypes.TYPE_SPLTYPE;
    } else if (value instanceof String[]) {
        String[] sa = (String[]) value;
        JSONArray a = new JSONArray(sa.length);
        for (String vs : sa) a.add(vs);
        jsonValue = a;
        op.setStringParameter(name, sa);
    } else if (value instanceof Attribute) {
        Attribute attr = (Attribute) value;
        jsonValue = attr.getName();
        jsonType = JParamTypes.TYPE_ATTRIBUTE;
        op.setAttributeParameter(name, attr.getName());
    } else if (value instanceof JSONObject) {
        JSONObject jo = (JSONObject) value;
        jsonType = (String) jo.get("type");
        jsonValue = (JSONObject) jo.get("value");
    } else {
        throw new IllegalArgumentException("Type for parameter " + name + " is not supported:" + value.getClass());
    }
    // Set the value as JSON
    JSONObject param = new JSONObject();
    param.put("value", jsonValue);
    if (jsonType != null)
        param.put("type", jsonType);
    jparams.put(name, param);
}
Also used : Attribute(com.ibm.streams.operator.Attribute) JSONArray(com.ibm.json.java.JSONArray) StreamSchema(com.ibm.streams.operator.StreamSchema) BigDecimal(java.math.BigDecimal) JSONObject(com.ibm.json.java.JSONObject) JSONAble(com.ibm.streamsx.topology.tuple.JSONAble) JSONObject(com.ibm.json.java.JSONObject) Supplier(com.ibm.streamsx.topology.function.Supplier)

Example 2 with Attribute

use of com.ibm.streams.operator.Attribute in project streamsx.topology by IBMStreams.

the class SPLStreams method toStringStream.

/**
     * Convert an {@link SPLStream} to a TStream&lt;String>
     * by taking a specific attribute.
     * The returned stream will contain a {@code String} tuple for
     * each tuple {@code T} on {@code stream}, the value of the
     * {@code String} tuple is {@code T.getString(attributeName)}.
     * A runtime error will occur if the attribute
     * can not be converted using {@code Tuple.getString(attributeName)}.
     * @param stream Stream to be converted to a TStream&lt;String>.
     * @return Stream that will contain a single attribute of tuples from {@code stream}
     */
public static TStream<String> toStringStream(SPLStream stream, String attributeName) {
    Attribute attribute = stream.getSchema().getAttribute(attributeName);
    if (attribute == null) {
        throw new IllegalArgumentException("Atttribute " + attributeName + " not present in SPL schema " + stream.getSchema().getLanguageType());
    }
    final int attributeIndex = attribute.getIndex();
    return stream.convert(new Function<Tuple, String>() {

        private static final long serialVersionUID = 1L;

        @Override
        public String apply(Tuple tuple) {
            return tuple.getString(attributeIndex);
        }
    });
}
Also used : Attribute(com.ibm.streams.operator.Attribute) Tuple(com.ibm.streams.operator.Tuple) OutputTuple(com.ibm.streams.operator.OutputTuple)

Aggregations

Attribute (com.ibm.streams.operator.Attribute)2 JSONArray (com.ibm.json.java.JSONArray)1 JSONObject (com.ibm.json.java.JSONObject)1 OutputTuple (com.ibm.streams.operator.OutputTuple)1 StreamSchema (com.ibm.streams.operator.StreamSchema)1 Tuple (com.ibm.streams.operator.Tuple)1 Supplier (com.ibm.streamsx.topology.function.Supplier)1 JSONAble (com.ibm.streamsx.topology.tuple.JSONAble)1 BigDecimal (java.math.BigDecimal)1