Search in sources :

Example 11 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class SubscribeSPLDictTest method testSubscribeSPLDictMap.

@Test
public void testSubscribeSPLDictMap() throws Exception {
    Topology topology = new Topology("testSubscribeMap");
    // Publish a stream with all the SPL types supported by Python including sets
    SPLStream tuples = PythonFunctionalOperatorsTest.testTupleStream(topology, true);
    tuples = addStartupDelay(tuples);
    tuples.publish("pytest/spl/map");
    SPLStream viaSPL = SPL.invokeOperator("spl.relational::Functor", tuples, tuples.getSchema(), null);
    // Python that subscribes to the SPL tuple stream and then republishes as Json.
    includePythonApp(topology, "spl_map_json.py", "spl_map_json::spl_map_json");
    TStream<JSONObject> viaPythonJson = topology.subscribe("pytest/spl/map/result", JSONObject.class);
    SPLStream viaPythonJsonSpl = JSONStreams.toSPL(viaPythonJson.isolate());
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(viaPythonJsonSpl, TUPLE_COUNT);
    Condition<Long> expectedCountSpl = tester.tupleCount(viaSPL, TUPLE_COUNT);
    Condition<List<Tuple>> viaSPLResult = tester.tupleContents(viaSPL);
    Condition<List<Tuple>> viaPythonResult = tester.tupleContents(viaPythonJsonSpl);
    complete(tester, allConditions(expectedCount, expectedCountSpl), 60, TimeUnit.SECONDS);
    System.out.println(expectedCount.getResult());
    System.out.println(expectedCountSpl.getResult());
    assertTrue(expectedCount.valid());
    assertTrue(expectedCountSpl.valid());
    List<Tuple> splResults = viaSPLResult.getResult();
    List<Tuple> pyJsonResults = viaPythonResult.getResult();
    assertEquals(TUPLE_COUNT, splResults.size());
    assertEquals(TUPLE_COUNT, pyJsonResults.size());
    for (int i = 0; i < TUPLE_COUNT; i++) {
        Tuple spl = splResults.get(i);
        JSONObject json = (JSONObject) JSON.parse(pyJsonResults.get(i).getString("jsonString"));
        System.out.println(spl);
        System.out.println(pyJsonResults.get(i).getString("jsonString"));
        assertEquals(spl.getBoolean("b"), ((Boolean) json.get("b")).booleanValue());
        assertEquals(spl.getInt("i8"), ((Number) json.get("i8")).intValue());
        assertEquals(spl.getInt("i16"), ((Number) json.get("i16")).intValue());
        assertEquals(spl.getInt("i32"), ((Number) json.get("i32")).intValue());
        assertEquals(spl.getLong("i64"), ((Number) json.get("i64")).longValue());
        assertEquals(spl.getString("r"), json.get("r").toString());
        assertEquals(spl.getDouble("f32"), ((Number) json.get("f32")).doubleValue(), 0.1);
        assertEquals(spl.getDouble("f64"), ((Number) json.get("f64")).doubleValue(), 0.1);
        {
            List<?> ex = spl.getList("li32");
            JSONArray pya = (JSONArray) json.get("li32");
            assertEquals(ex.size(), pya.size());
            for (int j = 0; j < ex.size(); j++) {
                assertEquals(ex.get(j), ((Number) pya.get(j)).intValue());
            }
        }
        {
            Set<?> ex = spl.getSet("si32");
            JSONArray pya = (JSONArray) json.get("si32");
            assertEquals(ex.size(), pya.size());
            setAssertHelper(ex, pya);
        }
    }
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Set(java.util.Set) TreeSet(java.util.TreeSet) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) JSONObject(com.ibm.json.java.JSONObject) List(java.util.List) Tuple(com.ibm.streams.operator.Tuple) PythonFunctionalOperatorsTest(com.ibm.streamsx.topology.test.splpy.PythonFunctionalOperatorsTest) Test(org.junit.Test)

Example 12 with JSONObject

use of com.ibm.json.java.JSONObject 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 13 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class Topology method getJSONConfig.

private JSONObject getJSONConfig(JSONObject jsonConfig, String key) {
    if (isSPLConfig(key)) {
        JSONObject splConfig = (JSONObject) jsonConfig.get("spl");
        if (splConfig == null) {
            splConfig = new JSONObject();
            jsonConfig.put("spl", splConfig);
            return splConfig;
        }
    }
    return jsonConfig;
}
Also used : JSONObject(com.ibm.json.java.JSONObject)

Example 14 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class Topology method subscribe.

/**
     * Declare a stream that is a subscription to {@code topic}.
     * A topic is published using {@link TStream#publish(String)}.
     * Subscribers are matched to published streams when the {@code topic}
     * is an exact match and the type of the stream ({@code T},
     * {@code tupleTypeClass}) is an exact match.
     * <BR>
     * Publish-subscribe is a many to many relationship,
     * multiple streams from multiple applications may
     * be published on the same topic and type. Multiple
     * subscribers may subscribe to a topic and type.
     * <BR>
     * A subscription will match all publishers using the
     * same topic and tuple type. Tuples on the published
     * streams will appear on the returned stream, as
     * a single stream.
     * <BR>
     * The subscription is dynamic, the returned stream
     * will subscribe to a matching stream published by
     * a newly submitted application (a job), and stops a
     * subscription when an running job is cancelled.
     * <P>
     * Publish-subscribe only works when the topology is
     * submitted to a {@link com.ibm.streamsx.topology.context.StreamsContext.Type#DISTRIBUTED}
     * context. This allows different applications (or
     * even within the same application) to communicate
     * using published streams.
     * </P>
     * <P>
     * If {@code tupleTypeClass} is {@code JSONObject.class} then the
     * subscription is the generic IBM Streams schema for JSON
     * ({@link JSONSchemas#JSON}). Streams of type {@code JSONObject}
     * are always published and subscribed using the generic schema
     * to allow interchange between applications implemented in
     * different languages.
     * </P>
     * @param topic Topic to subscribe to.
     * @param tupleTypeClass Type to subscribe to.
     * @return Stream the will contain tuples from matching publishers.
     * 
     * @see TStream#publish(String)
     * @see SPLStreams#subscribe(TopologyElement, String, com.ibm.streams.operator.StreamSchema)
     */
public <T> TStream<T> subscribe(String topic, Class<T> tupleTypeClass) {
    checkTopicFilter(topic);
    if (JSONObject.class.equals(tupleTypeClass)) {
        @SuppressWarnings("unchecked") TStream<T> json = (TStream<T>) SPLStreams.subscribe(this, topic, JSONSchemas.JSON).toJSON();
        return json;
    }
    StreamSchema mappingSchema = Schemas.getSPLMappingSchema(tupleTypeClass);
    SPLStream splImport;
    // Subscribed as an SPL Stream.
    if (Schemas.usesDirectSchema(tupleTypeClass)) {
        splImport = SPLStreams.subscribe(this, topic, mappingSchema);
    } else {
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("class", tupleTypeClass.getName());
        params.put("streamType", mappingSchema);
        splImport = SPL.invokeSource(this, "com.ibm.streamsx.topology.topic::SubscribeJava", params, mappingSchema);
    }
    return new StreamImpl<T>(this, splImport.output(), tupleTypeClass);
}
Also used : HashMap(java.util.HashMap) StreamImpl(com.ibm.streamsx.topology.internal.core.StreamImpl) JSONObject(com.ibm.json.java.JSONObject) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 15 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class BInputPort method complete.

/**
     * Add this port information and its connections to output ports, by name.
     */
@Override
public JSONObject complete() {
    final JSONObject json = json();
    BUtils.addPortInfo(json, port);
    JSONArray conns = new JSONArray();
    for (StreamConnection c : port().getConnections()) {
        conns.add(c.getOutput().getName());
    }
    json.put("connections", conns);
    return json;
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray) StreamConnection(com.ibm.streams.flow.declare.StreamConnection)

Aggregations

JSONObject (com.ibm.json.java.JSONObject)56 JSONArray (com.ibm.json.java.JSONArray)19 Topology (com.ibm.streamsx.topology.Topology)18 Test (org.junit.Test)18 TestTopology (com.ibm.streamsx.topology.test.TestTopology)14 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)6 Tester (com.ibm.streamsx.topology.tester.Tester)5 JsonObject (com.google.gson.JsonObject)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 OrderedJSONObject (com.ibm.json.java.OrderedJSONObject)2 StreamConnection (com.ibm.streams.flow.declare.StreamConnection)2 StreamSchema (com.ibm.streams.operator.StreamSchema)2 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)2 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Attribute (com.ibm.streams.operator.Attribute)1