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);
}
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<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<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);
}
});
}
Aggregations