Search in sources :

Example 1 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class SiddhiDebuggerClient method start.

/**
 * Start the {@link SiddhiDebuggerClient} and configure the breakpoints.
 *
 * @param siddhiApp the Siddhi query
 * @param input         the user input as a whole text
 */
public void start(final String siddhiApp, String input) {
    SiddhiManager siddhiManager = new SiddhiManager();
    info("Deploying the siddhi app");
    final SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    // Add callbacks for all the streams
    final Set<String> streamNames = SiddhiCompiler.parse(siddhiApp).getStreamDefinitionMap().keySet();
    for (String streamName : streamNames) {
        final String stream = streamName;
        siddhiAppRuntime.addCallback(stream, new StreamCallback() {

            @Override
            public void receive(Event[] events) {
                info("@Receive: Stream: " + stream + ", Event: " + Arrays.deepToString(events));
            }
        });
    }
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    final InputFeeder inputFeeder = new InputFeeder(siddhiAppRuntime, input);
    System.out.println("Configure the breakpoints.\nYou can use the following commands:\n - " + ADD_BREAKPOINT + "<query name>:<IN/OUT>\n - " + REMOVE_BREAKPOINT + "<query name>:<IN/OUT>\n - " + START + "\n - " + STOP);
    printNextLine();
    final Scanner scanner = new Scanner(System.in, "UTF-8");
    while (scanner.hasNextLine()) {
        String userInput = scanner.nextLine().trim();
        String command = userInput.toLowerCase();
        if (command.startsWith(ADD_BREAKPOINT)) {
            if (!command.contains(QUERY_DELIMITER)) {
                error("Invalid add query. The query must be " + ADD_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
                printNextLine();
                continue;
            }
            String[] components = userInput.substring(ADD_BREAKPOINT.length(), userInput.length()).split(QUERY_DELIMITER);
            String queryName = components[0];
            String terminal = components[1].toLowerCase();
            if (IN.equals(terminal)) {
                siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
                info("Added a breakpoint at the IN terminal of " + queryName);
                printNextLine();
            } else if (OUT.equals(terminal)) {
                siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
                info("Added a breakpoint at the OUT terminal of " + queryName);
                printNextLine();
            } else {
                error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase() + ". Please try again");
                printNextLine();
            }
        } else if (command.startsWith(REMOVE_BREAKPOINT)) {
            if (!command.contains(QUERY_DELIMITER)) {
                error("Invalid add query. The query must be " + REMOVE_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
                printNextLine();
                continue;
            }
            String[] components = command.substring(ADD_BREAKPOINT.length(), command.length()).split(QUERY_DELIMITER);
            String queryName = components[0];
            String terminal = components[1];
            if (IN.equals(terminal)) {
                siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
                info("Removed the breakpoint at the IN terminal of " + queryName);
                printNextLine();
            } else if (OUT.equals(terminal)) {
                siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
                info("Removed the breakpoint at the OUT terminal of " + queryName);
                printNextLine();
            } else {
                error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase());
                printNextLine();
            }
        } else if (STOP.equals(command)) {
            inputFeeder.stop();
            siddhiAppRuntime.shutdown();
            break;
        } else if (START.equals(command)) {
            inputFeeder.start();
            info("Siddhi Debugger starts sending input to Siddhi");
            System.out.println("You can use the following commands:\n - " + NEXT + "\n - " + PLAY + "\n - " + STATE + ":<query name>\n - " + STOP);
            break;
        } else {
            error("Invalid command: " + command);
            printNextLine();
        }
    }
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            info("@Debug: Query: " + queryName + ", Terminal: " + queryTerminal + ", Event: " + event);
            printNextLine();
            while (scanner.hasNextLine()) {
                String command = scanner.nextLine().trim().toLowerCase();
                if (STOP.equals(command)) {
                    debugger.releaseAllBreakPoints();
                    debugger.play();
                    inputFeeder.stop();
                    siddhiAppRuntime.shutdown();
                    break;
                } else if (NEXT.equals(command)) {
                    debugger.next();
                    break;
                } else if (PLAY.equals(command)) {
                    debugger.play();
                    break;
                } else if (command.startsWith(STATE)) {
                    if (!command.contains(QUERY_DELIMITER)) {
                        error("Invalid get state request. The query must be " + STATE + ":<query " + "name>. Please try again");
                        printNextLine();
                        continue;
                    }
                    String[] components = command.split(QUERY_DELIMITER);
                    String requestQueryName = components[1];
                    Map<String, Object> state = debugger.getQueryState(requestQueryName.trim());
                    System.out.println("Query '" + requestQueryName + "' state : ");
                    for (Map.Entry<String, Object> entry : state.entrySet()) {
                        System.out.println("    '" + entry.getKey() + "' : " + entry.getValue());
                    }
                    printNextLine();
                    continue;
                } else {
                    error("Invalid command: " + command);
                    printNextLine();
                }
            }
        }
    });
    inputFeeder.join();
    if (inputFeeder.isRunning()) {
        info("Input feeder has sopped sending all inputs. If you want to stop the execution, use " + "the STOP command");
        printNextLine();
        while (scanner.hasNextLine()) {
            String command = scanner.nextLine().trim().toLowerCase();
            if (STOP.equals(command)) {
                inputFeeder.stop();
                siddhiAppRuntime.shutdown();
                break;
            } else {
                error("Invalid command: " + command);
                printNextLine();
            }
        }
    }
    scanner.close();
    info("Siddhi Debugger is stopped successfully");
}
Also used : Scanner(java.util.Scanner) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) Map(java.util.Map) SiddhiManager(org.wso2.siddhi.core.SiddhiManager)

Example 2 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class SelectiveStateEventPopulator method populateStateEvent.

public void populateStateEvent(ComplexEvent complexEvent) {
    StateEvent stateEvent = (StateEvent) complexEvent;
    for (StateMappingElement stateMappingElement : stateMappingElements) {
        int toPosition = stateMappingElement.getToPosition();
        stateEvent.setOutputData(getFromData(stateEvent, stateMappingElement.getFromPosition()), toPosition);
    // switch (toPosition[0]) {
    // case 0:
    // stateEvent.setPreOutputData(getFromData(stateEvent, stateMappingElement.getFromPosition()),
    // toPosition[1]);
    // break;
    // case 1:
    // stateEvent.setOutputData(getFromData(stateEvent, stateMappingElement.getFromPosition()),
    // toPosition[1]);
    // break;
    // default:
    // //will not happen
    // throw new IllegalStateException("To Position cannot be :" + toPosition[0]);
    // }
    }
}
Also used : StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 3 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class ConversionStreamEventChunk method convertAllStreamEvents.

private StreamEvent convertAllStreamEvents(ComplexEvent complexEvents, StreamEvent firstEvent) {
    streamEventConverter.convertComplexEvent(complexEvents, firstEvent);
    StreamEvent currentEvent = firstEvent;
    complexEvents = complexEvents.getNext();
    while (complexEvents != null) {
        StreamEvent nextEvent = streamEventPool.borrowEvent();
        streamEventConverter.convertComplexEvent(complexEvents, nextEvent);
        currentEvent.setNext(nextEvent);
        currentEvent = nextEvent;
        complexEvents = complexEvents.getNext();
    }
    return currentEvent;
}
Also used : MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 4 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class FindStoreQueryRuntime method executeSelector.

private Event[] executeSelector(StreamEvent streamEvents, MetaStreamEvent.EventType eventType) {
    ComplexEventChunk<StateEvent> complexEventChunk = new ComplexEventChunk<>(true);
    while (streamEvents != null) {
        StreamEvent streamEvent = streamEvents;
        streamEvents = streamEvents.getNext();
        streamEvent.setNext(null);
        StateEvent stateEvent = stateEventPool.borrowEvent();
        if (eventType == MetaStreamEvent.EventType.AGGREGATE) {
            stateEvent.addEvent(1, streamEvent);
        } else {
            stateEvent.addEvent(0, streamEvent);
        }
        complexEventChunk.add(stateEvent);
    }
    ComplexEventChunk outputComplexEventChunk = selector.execute(complexEventChunk);
    if (outputComplexEventChunk != null) {
        List<Event> events = new ArrayList<>();
        outputComplexEventChunk.reset();
        while (outputComplexEventChunk.hasNext()) {
            ComplexEvent complexEvent = outputComplexEventChunk.next();
            events.add(new Event(complexEvent.getTimestamp(), complexEvent.getOutputData()));
        }
        return events.toArray(new Event[0]);
    } else {
        return null;
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 5 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class MultiProcessStreamReceiver method receive.

@Override
public void receive(ComplexEvent complexEvent) {
    ComplexEvent aComplexEvent = complexEvent;
    while (aComplexEvent != null) {
        synchronized (this) {
            stabilizeStates();
            for (int anEventSequence : eventSequence) {
                StreamEventConverter aStreamEventConverter = streamEventConverters[anEventSequence];
                StreamEventPool aStreamEventPool = streamEventPools[anEventSequence];
                StreamEvent borrowedEvent = aStreamEventPool.borrowEvent();
                aStreamEventConverter.convertComplexEvent(aComplexEvent, borrowedEvent);
                process(anEventSequence, borrowedEvent);
            }
        }
        aComplexEvent = aComplexEvent.getNext();
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) StreamEventConverter(org.wso2.siddhi.core.event.stream.converter.StreamEventConverter) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool)

Aggregations

ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)59 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)29 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)26 ArrayList (java.util.ArrayList)21 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)17 Event (org.wso2.siddhi.core.event.Event)16 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)13 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)13 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)13 Test (org.testng.annotations.Test)12 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)12 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)10 AttributeProcessor (org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)5 Map (java.util.Map)4 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)4 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)3 Iterator (java.util.Iterator)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 Attribute (org.wso2.siddhi.query.api.definition.Attribute)2 HashMap (java.util.HashMap)1