<commit_before>/*
 * Copyright (c)  2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.siddhi.distribution.editor.core.internal;

import io.siddhi.core.SiddhiAppRuntime;
import io.siddhi.core.debugger.SiddhiDebugger;
import io.siddhi.core.stream.input.InputHandler;
import io.siddhi.distribution.editor.core.exception.InvalidExecutionStateException;
import io.siddhi.distribution.editor.core.exception.NoSuchStreamException;
import io.siddhi.distribution.editor.core.util.DebugCallbackEvent;
import io.siddhi.distribution.editor.core.util.SourceEditorUtils;
import io.siddhi.query.api.definition.Attribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Class for debugger runtime implementation.
 */
public class DebugRuntime {

    private String siddhiAppName;
    private Mode mode = Mode.STOP;
    private transient String siddhiApp;
    private transient SiddhiAppRuntime siddhiAppRuntime;
    private transient SiddhiDebugger debugger;
    private transient LinkedBlockingQueue<DebugCallbackEvent> callbackEventsQueue;
    private Throwable caughtException;
    private static final Logger log = LoggerFactory.getLogger(DebugRuntime.class);

    public DebugRuntime(String siddhiAppName, String siddhiApp) {
        this.siddhiAppName = siddhiAppName;
        this.siddhiApp = siddhiApp;
        callbackEventsQueue = new LinkedBlockingQueue<>(10);
        createRuntime(siddhiApp);
    }

    public DebugRuntime(String siddhiAppName, String siddhiApp, Map<String, String> variableMap) {
        this.siddhiAppName = siddhiAppName;
        this.siddhiApp = siddhiApp;
        callbackEventsQueue = new LinkedBlockingQueue<>(10);
        createRuntime(SourceEditorUtils.populateSiddhiAppWithVars(variableMap, siddhiApp));
    }

    public String getSiddhiAppName() {

        return siddhiAppName;
    }

    public Mode getMode() {

        return mode;
    }

    public void setMode(Mode mode) {

        this.mode = mode;
    }

    public SiddhiDebugger getDebugger() {

        return debugger;
    }

    public String getSiddhiApp() {
        return siddhiApp;
    }

    public void start() {
        if (Mode.STOP.equals(mode)) {
            try {
                siddhiAppRuntime.start();
                mode = Mode.RUN;
            } catch (Throwable e) {
                mode = Mode.FAULTY;
                String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
                log.error(errorMessage, e);
                throw new InvalidExecutionStateException(errorMessage, e);
            }
        } else {
            String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
            log.error(errorMessage, caughtException);
            throw new InvalidExecutionStateException(errorMessage);
        }
    }

    public void debug() {

        if (Mode.STOP.equals(mode)) {
            try {
                debugger = siddhiAppRuntime.debug();
                debugger.setDebuggerCallback((event, queryName, queryTerminal, debugger) -> {
                    String[] queries = getQueries().toArray(new String[getQueries().size()]);
                    int queryIndex = Arrays.asList(queries).indexOf(queryName);
                    callbackEventsQueue.add(new DebugCallbackEvent(queryName, queryIndex, queryTerminal, event));
                });
                mode = Mode.DEBUG;
            } catch (Throwable e) {
                String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
                log.error(errorMessage, e);
                mode = Mode.FAULTY;
                throw new InvalidExecutionStateException(errorMessage, e);
            }
        } else {
            String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
            log.error(errorMessage, caughtException);
            throw new InvalidExecutionStateException(errorMessage);
        }
    }

    public void stop() {

        if (debugger != null) {
            debugger.releaseAllBreakPoints();
            debugger.play();
            debugger = null;
        }
        if (siddhiAppRuntime != null) {
            siddhiAppRuntime.shutdown();
            siddhiAppRuntime = null;
        }
        callbackEventsQueue.clear();
        createRuntime(siddhiApp);
    }

    public void reload(String siddhiApp) {
<commit_msg>Stop app runtime recreation when siddhi app is stopped<commit_after>/*
 * Copyright (c)  2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.siddhi.distribution.editor.core.internal;

import io.siddhi.core.SiddhiAppRuntime;
import io.siddhi.core.debugger.SiddhiDebugger;
import io.siddhi.core.stream.input.InputHandler;
import io.siddhi.distribution.editor.core.exception.InvalidExecutionStateException;
import io.siddhi.distribution.editor.core.exception.NoSuchStreamException;
import io.siddhi.distribution.editor.core.util.DebugCallbackEvent;
import io.siddhi.distribution.editor.core.util.SourceEditorUtils;
import io.siddhi.query.api.definition.Attribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Class for debugger runtime implementation.
 */
public class DebugRuntime {

    private String siddhiAppName;
    private Mode mode = Mode.STOP;
    private transient String siddhiApp;
    private transient SiddhiAppRuntime siddhiAppRuntime;
    private transient SiddhiDebugger debugger;
    private transient LinkedBlockingQueue<DebugCallbackEvent> callbackEventsQueue;
    private Throwable caughtException;
    private static final Logger log = LoggerFactory.getLogger(DebugRuntime.class);

    public DebugRuntime(String siddhiAppName, String siddhiApp) {
        this.siddhiAppName = siddhiAppName;
        this.siddhiApp = siddhiApp;
        callbackEventsQueue = new LinkedBlockingQueue<>(10);
        createRuntime(siddhiApp);
    }

    public DebugRuntime(String siddhiAppName, String siddhiApp, Map<String, String> variableMap) {
        this.siddhiAppName = siddhiAppName;
        this.siddhiApp = siddhiApp;
        callbackEventsQueue = new LinkedBlockingQueue<>(10);
        createRuntime(SourceEditorUtils.populateSiddhiAppWithVars(variableMap, siddhiApp));
    }

    public String getSiddhiAppName() {

        return siddhiAppName;
    }

    public Mode getMode() {

        return mode;
    }

    public void setMode(Mode mode) {

        this.mode = mode;
    }

    public SiddhiDebugger getDebugger() {

        return debugger;
    }

    public String getSiddhiApp() {
        return siddhiApp;
    }

    public void start() {
        if (Mode.STOP.equals(mode)) {
            try {
                siddhiAppRuntime.start();
                mode = Mode.RUN;
            } catch (Throwable e) {
                mode = Mode.FAULTY;
                String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
                log.error(errorMessage, e);
                throw new InvalidExecutionStateException(errorMessage, e);
            }
        } else {
            String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
            log.error(errorMessage, caughtException);
            throw new InvalidExecutionStateException(errorMessage);
        }
    }

    public void debug() {

        if (Mode.STOP.equals(mode)) {
            try {
                debugger = siddhiAppRuntime.debug();
                debugger.setDebuggerCallback((event, queryName, queryTerminal, debugger) -> {
                    String[] queries = getQueries().toArray(new String[getQueries().size()]);
                    int queryIndex = Arrays.asList(queries).indexOf(queryName);
                    callbackEventsQueue.add(new DebugCallbackEvent(queryName, queryIndex, queryTerminal, event));
                });
                mode = Mode.DEBUG;
            } catch (Throwable e) {
                String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
                log.error(errorMessage, e);
                mode = Mode.FAULTY;
                throw new InvalidExecutionStateException(errorMessage, e);
            }
        } else {
            String errorMessage = "Siddhi App '" + siddhiAppName + "' is in faulty state.";
            log.error(errorMessage, caughtException);
            throw new InvalidExecutionStateException(errorMessage);
        }
    }

    public void stop() {

        if (debugger != null) {
            debugger.releaseAllBreakPoints();
            debugger.play();
            debugger = null;
        }
        if (siddhiAppRuntime != null) {
            siddhiAppRuntime.shutdown();
            mode = Mode.STOP;
        }
        callbackEventsQueue.clear();
    }

    public void reload(String siddhiApp) {

{'fraction_non_alphanumeric': 0.09012875536480687, 'fraction_numerical': 0.003316426063207179, 'mean_word_length': 2.596282006313574, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2279220', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2784, 'n_words': 705}