Search in sources :

Example 91 with Or

use of org.wso2.siddhi.query.api.expression.condition.Or in project siddhi by wso2.

the class SetUpdateOrInsertInMemoryTableTestCase method updateFromTableTest5.

@Test
public void updateFromTableTest5() throws InterruptedException, SQLException {
    log.info("SET-InMemory-update-or-insert test case 5: assignment expression containing an output attribute " + "with a basic arithmatic operation.");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long); " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "update or insert into StockTable " + "set StockTable.price = price + 100 " + "   on StockTable.symbol == symbol ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    updateStockStream.send(new Object[] { "IBM", 100f, 100L });
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 92 with Or

use of org.wso2.siddhi.query.api.expression.condition.Or in project siddhi by wso2.

the class SetUpdateOrInsertInMemoryTableTestCase method updateFromTableTest7.

@Test
public void updateFromTableTest7() throws InterruptedException, SQLException {
    log.info("SET-InMemory-update-or-insert test case 7: Set clause should be optional.");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long); " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "update or insert into StockTable " + "   on StockTable.symbol == symbol ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    updateStockStream.send(new Object[] { "IBM", 100f, 100L });
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 93 with Or

use of org.wso2.siddhi.query.api.expression.condition.Or in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitAttribute_reference.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Variable visitAttribute_reference(@NotNull SiddhiQLParser.Attribute_referenceContext ctx) {
    // attribute_reference
    // : hash1='#'? name1=name ('['attribute_index1=attribute_index']')? (hash2='#' name2=name
    // ('['attribute_index2=attribute_index']')?)? '.'  attribute_name
    // | attribute_name
    // ;
    Variable variable = Expression.variable((String) visit(ctx.attribute_name()));
    if (ctx.name1 != null && ctx.name2 != null) {
        // Stream and Function
        variable.setStreamId(ctx.hash1 != null, (String) visit(ctx.name1));
        if (ctx.attribute_index1 != null) {
            variable.setStreamIndex((Integer) visit(ctx.attribute_index1));
        }
        variable.setFunctionId((String) visit(ctx.name2));
        if (ctx.attribute_index2 != null) {
            variable.setFunctionIndex((Integer) visit(ctx.attribute_index2));
        }
    } else if (ctx.name1 != null) {
        // name2 == null
        if (ctx.hash1 == null) {
            // Stream
            variable.setStreamId((String) visit(ctx.name1));
            if (ctx.attribute_index1 != null) {
                variable.setStreamIndex((Integer) visit(ctx.attribute_index1));
            }
        } else {
            // InnerStream or Function
            String name = (String) visit(ctx.name1);
            if (activeStreams.contains("#" + name)) {
                // InnerStream
                variable.setStreamId(true, name);
                if (ctx.attribute_index1 != null) {
                    variable.setStreamIndex((Integer) visit(ctx.attribute_index1));
                }
            } else {
                // Function
                variable.setFunctionId(name);
                if (ctx.attribute_index1 != null) {
                    variable.setFunctionIndex((Integer) visit(ctx.attribute_index1));
                }
            }
        }
    }
    populateQueryContext(variable, ctx);
    return variable;
}
Also used : Variable(org.wso2.siddhi.query.api.expression.Variable)

Example 94 with Or

use of org.wso2.siddhi.query.api.expression.condition.Or in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitQuery_output.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public OutputStream visitQuery_output(@NotNull SiddhiQLParser.Query_outputContext ctx) {
    if (ctx.INSERT() != null) {
        Source source = (Source) visit(ctx.target());
        if (ctx.UPDATE() != null && ctx.OR() != null) {
            if (source.isInnerStream) {
                throw newSiddhiParserException(ctx, "UPDATE OR INTO INSERT be only used with Tables!");
            }
            if (ctx.output_event_type() != null) {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            } else {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            }
        } else {
            if (ctx.output_event_type() != null) {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream, (OutputStream.OutputEventType) visit(ctx.output_event_type()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream);
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.DELETE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new DeleteStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new DeleteStream(source.streamId, (Expression) visit(ctx.expression()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else if (ctx.UPDATE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        if (ctx.output_event_type() != null) {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        } else {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.RETURN() != null) {
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new ReturnStream((OutputStream.OutputEventType) visit(ctx.output_event_type()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new ReturnStream();
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : UpdateOrInsertStream(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) Expression(org.wso2.siddhi.query.api.expression.Expression) DeleteStream(org.wso2.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream) InsertIntoStream(org.wso2.siddhi.query.api.execution.query.output.stream.InsertIntoStream) UpdateStream(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateStream) ReturnStream(org.wso2.siddhi.query.api.execution.query.output.stream.ReturnStream)

Example 95 with Or

use of org.wso2.siddhi.query.api.expression.condition.Or in project siddhi by wso2.

the class SimpleQueryTestCase method test7.

@Test
public void test7() {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[7+9.5 < price or 100 <= volume]#window.length(50) " + " " + "select symbol, avg(price) as avgPrice " + "group by symbol " + "having avgPrice!= 50 " + "insert into OutStockStream ;");
    AssertJUnit.assertNotNull(query);
    Query api = Query.query();
    api.from(InputStream.stream("StockStream").filter(Expression.or(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.LESS_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("volume")))).window("length", Expression.value(50)));
    api.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.NOT_EQUAL, Expression.value(50))));
    api.insertInto("OutStockStream");
    AssertJUnit.assertEquals(api, query);
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)170 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)156 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)152 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)149 TestUtil (org.wso2.siddhi.core.TestUtil)76 Event (org.wso2.siddhi.core.event.Event)66 ArrayList (java.util.ArrayList)51 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)44 HashMap (java.util.HashMap)38 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)30 IOException (java.io.IOException)24 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)24 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)21 Map (java.util.Map)18 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)17 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)16 File (java.io.File)13 List (java.util.List)13 Response (feign.Response)11 Attribute (org.wso2.charon3.core.attributes.Attribute)10