Ejemplo n.º 1
0
    class Type(Enum):
        CURRENT = SiddhiLoader._loadType(
            "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.TypeProxy"
        )().CURRENT(),
        EXPIRED = SiddhiLoader._loadType(
            "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.TypeProxy"
        )().EXPIRED(),
        TIMER = SiddhiLoader._loadType(
            "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.TypeProxy"
        )().TIMER(),
        RESET = SiddhiLoader._loadType(
            "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.TypeProxy"
        )().RESET()

        @classmethod
        def _map_value(cls, type_proxy):
            type_value = None
            if type_proxy.isValueCurrent():
                type_value = ComplexEvent.Type.CURRENT
            elif type_proxy.isValueExpired():
                type_value = ComplexEvent.Type.EXPIRED
            elif type_proxy.isValueTimer():
                type_value = ComplexEvent.Type.TIMER
            elif type_proxy.isValueReset():
                type_value = ComplexEvent.Type.RESET
            else:
                raise TypeError("Unknown Complex Event Type")
            return ComplexEvent.Type(type_value)
Ejemplo n.º 2
0
 def __init__(self):
     '''
     ''Initialize a new SiddhiManager
     '''
     SiddhiLoader.loadLibrary()
     self._siddhi_manager_proxy = SiddhiLoader.siddhi_api_core_inst.initSiddhiManager(
     )
Ejemplo n.º 3
0
            def pollLoop():
                event_polling_started = False

                with self.pollLock:
                    event_polling_started = self.event_polling_started

                while event_polling_started:
                    with self.pollLock:
                        event = self.event_queue.getQueuedEvent()
                        if event is not None:
                            if event.isDebugEvent():
                                debug_callback = self.debugCallback
                                if debug_callback is not None:
                                    complexEvent = event.getComplexEvent(0)
                                    queryName = event.getString(1)
                                    queryTerminal = event.getQueryTerminal(2)
                                    debugger = event.getSiddhiDebugger(3)

                                    complexEvent = ComplexEvent._fromComplexEventProxy(complexEvent)
                                    queryTerminal = SiddhiDebugger.QueryTerminal._map_value(queryTerminal)
                                    debugger = SiddhiDebugger._fromSiddhiDebuggerProxy(debugger)

                                    debug_callback.debugEvent(complexEvent, queryName, queryTerminal, debugger)
                                elif event.isGCEvent():
                                    self.debugCallback = None  # Release reference held with callback since it has been
                                    # destroyed from Java Side

                    sleep(0.005)  # NOTE: Removing this sleep causes changing of Debug Callback to fail
                    # TODO: Investigate why removal of above sleep causes condition in above note

                # Requirement of Pyjnius to call detach before destruction of a thread
                SiddhiLoader._detachThread()
Ejemplo n.º 4
0
 def getOutputData(self):
     complex_event_static_proxy = SiddhiLoader._loadType(
         "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.ComplexEventProxy"
     )()
     output_data = unwrapData(
         complex_event_static_proxy.getOutputData(
             self._complex_event_proxy))
     return output_data
Ejemplo n.º 5
0
    class QueryTerminal(Enum):
        '''
        SiddhiDebugger allows to add breakpoints at the beginning and the end of a query.
        '''
        IN = SiddhiLoader._loadType(
            "org.wso2.siddhi.pythonapi.proxy.core.debugger.siddhi_debugger.QueryTerminalProxy")().IN()
        OUT = SiddhiLoader._loadType(
            "org.wso2.siddhi.pythonapi.proxy.core.debugger.siddhi_debugger.QueryTerminalProxy")().OUT()

        @classmethod
        def _map_value(cls, queryTerminalProxy):
            qt_value = None
            if queryTerminalProxy.isValueOut():
                qt_value = SiddhiDebugger.QueryTerminal.OUT
            elif queryTerminalProxy.isValueIn():
                qt_value = SiddhiDebugger.QueryTerminal.IN
            else:
                raise TypeError("Unknown QueryTerminal Value")
            return SiddhiDebugger.QueryTerminal(qt_value)
Ejemplo n.º 6
0
 def setExtension(self, name, clazz):
     '''
     Loads an extension into Siddhi Manager. The Extension Path must be already added via 
     SiddhiLoader.addExtensionPath.
     :param name: Name of extension
     :param clazz: Fully qualified class name of extension
     :return: 
     '''
     if isinstance(clazz, str):
         self._siddhi_manager_proxy.setExtension(
             name, SiddhiLoader._loadType(clazz))
     else:
         self._siddhi_manager_proxy.setExtension(name, clazz)
Ejemplo n.º 7
0
def wrapDataItem(d):
    '''
    Wraps a data item (int, long, string or float) , making it suitable to be transfered to Java Proxy
    :param d: 
    :return: 
    '''
    wrapped_data_proxy = SiddhiLoader._loadType(
        "org.wso2.siddhi.pythonapi.DataWrapProxy")
    wrapped_data = None
    if d is None:
        # Constructor for null type
        wrapped_data = wrapped_data_proxy(0, False, True)
    elif type(d) is LongType:
        # Consutrctor for Long Type
        wrapped_data = wrapped_data_proxy(d, True)
    elif type(d) is DoubleType:
        wrapped_data = wrapped_data_proxy(d, False, False, True)
    else:
        wrapped_data = wrapped_data_proxy(d)
    return wrapped_data
Ejemplo n.º 8
0
# "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.

import logging

from abc import ABCMeta, abstractmethod

from PySiddhi4 import SiddhiLoader
from PySiddhi4.core.event.Event import Event

from future.utils import with_metaclass

_query_callback_proxy = SiddhiLoader._loadType(
    "org.wso2.siddhi.pythonapi.proxy.core.query.output.callback.query_callback.QueryCallbackProxy"
)

_created_instances = [
]  # Hold references to prevent python from GCing Callbacks until Java does


class QueryCallback(with_metaclass(ABCMeta, object)):
    '''
    Callback to receive events from SiddhiAppRuntime. Should be extended by child class.
    Wrapper on org.wso2.siddhi.core.query.output.callback.QueryCallback
    '''
    def __init__(self):
        self._query_callback_proxy_inst = _query_callback_proxy()
        query_callback_self = self
Ejemplo n.º 9
0
# under the License.

from subprocess import call
import os
from PySiddhi4 import SiddhiLoader

# Download extension jars
path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                    os.path.join("Resources", "Extensions4"))
if os.name == "nt":  # For windows, shell=True is required
    call(["mvn", "install"], shell=True, cwd=path)
else:  # For linux, shell=True causes cwd to not function properly
    call(["mvn", "install"], cwd=path)
# Add extensions
extensions_path = os.path.join(path, "jars/*")
SiddhiLoader.addExtensionPath(extensions_path)

import unittest
import logging
from time import sleep

from PySiddhi4.DataTypes.LongType import LongType
from PySiddhi4.core.SiddhiManager import SiddhiManager
from PySiddhi4.core.query.output.callback.QueryCallback import QueryCallback
from PySiddhi4.core.util.EventPrinter import PrintEvent

logging.basicConfig(level=logging.INFO)

from unittest.case import TestCase

from Tests.Util.AtomicInt import AtomicInt
Ejemplo n.º 10
0
# 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.

from PySiddhi4 import SiddhiLoader

_event_printer_proxy = SiddhiLoader._loadType(
    "org.wso2.siddhi.pythonapi.proxy.core.util.EventPrinterProxy")


def PrintEvent(timestamp, inEvents, outEvents):
    '''
    Prints Stream Event to Log
    :param timestamp:
    :param inEvents:
    :param outEvents:
    :return:
    '''
    if inEvents is not None:
        inEvents = [event._event_proxy for event in inEvents]

    if outEvents is not None:
        outEvents = [event._event_proxy for event in outEvents]
Ejemplo n.º 11
0
#
#     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.

from PySiddhi4 import SiddhiLoader

from PySiddhi4.DataTypes import DataWrapper
from PySiddhi4.core.event import ComplexEvent

_event_class = SiddhiLoader._loadType("org.wso2.siddhi.core.event.Event")
_event_proxy_class = SiddhiLoader._loadType(
    "org.wso2.siddhi.pythonapi.proxy.core.event.event.EventProxy")
_event_proxy_class_inst = _event_proxy_class()


class Event(object):
    '''
    Wrapper on @org.wso2.siddhi.core.event.Event
    '''
    @classmethod
    def _fromEventProxy(cls, event_proxy):
        '''
        Internal Constructor to wrap around JAVA class Event
        :param event_proxy:
        :return:
Ejemplo n.º 12
0
#
#     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.

from abc import ABCMeta, abstractmethod
from future.utils import with_metaclass
from PySiddhi4 import SiddhiLoader

_siddhi_debugger_callback_proxy = SiddhiLoader._loadType(
    "org.wso2.siddhi.pythonapi.proxy.core.debugger.siddhi_debugger_callback.SiddhiDebuggerCallbackProxy"
)


class SiddhiDebuggerCallback(with_metaclass(ABCMeta, object)):
    '''
    Callback to get notification about the events passing through the break points.
    Should be extended by subclass.
    '''
    def __init__(self):
        _siddhi_debugger_callback_self = self
        self._siddhi_debugger_callback_proxy_inst = _siddhi_debugger_callback_proxy(
        )

    @abstractmethod
    def debugEvent(self, complexEvent, queryName, queryTerminal, debugger):
Ejemplo n.º 13
0
    def testTimeSeriesSimpleLinearRegression(self):
        logging.info("Simple Regression TestCase")
        SiddhiLoader.loadLibrary()

        siddhiManager = SiddhiManager()

        siddhiManager.setExtension(
            "timeseries:regress",
            "org.wso2.extension.siddhi.execution.timeseries.LinearRegressionStreamProcessor"
        )

        inputStream = "define stream InputStream (y int, x int);"
        siddhiApp = "@info(name = 'query1') from InputStream#timeseries:regress(1, 100, 0.95, y, x) " + \
                    "select * " + \
                    "insert into OutputStream;"
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(inputStream +
                                                                siddhiApp)
        self.betaZero = 0
        _self_shaddow = self

        class QueryCallbackImpl(QueryCallback):
            def receive(self, timestamp, inEvents, outEvents):
                PrintEvent(timestamp, inEvents, outEvents)
                _self_shaddow.count.addAndGet(len(inEvents))
                _self_shaddow.betaZero = inEvents[len(inEvents) - 1].getData(3)

        siddhiAppRuntime.addCallback("query1", QueryCallbackImpl())

        inputHandler = siddhiAppRuntime.getInputHandler("InputStream")
        siddhiAppRuntime.start()

        inputHandler.send([2500.00, 17.00])
        inputHandler.send([2600.00, 18.00])
        inputHandler.send([3300.00, 31.00])
        inputHandler.send([2475.00, 12.00])
        inputHandler.send([2313.00, 8.00])
        inputHandler.send([2175.00, 26.00])
        inputHandler.send([600.00, 14.00])
        inputHandler.send([460.00, 3.00])
        inputHandler.send([240.00, 1.00])
        inputHandler.send([200.00, 10.00])
        inputHandler.send([177.00, 0.00])
        inputHandler.send([140.00, 6.00])
        inputHandler.send([117.00, 1.00])
        inputHandler.send([115.00, 0.00])
        inputHandler.send([2600.00, 19.00])
        inputHandler.send([1907.00, 13.00])
        inputHandler.send([1190.00, 3.00])
        inputHandler.send([990.00, 16.00])
        inputHandler.send([925.00, 6.00])
        inputHandler.send([365.00, 0.00])
        inputHandler.send([302.00, 10.00])
        inputHandler.send([300.00, 6.00])
        inputHandler.send([129.00, 2.00])
        inputHandler.send([111.00, 1.00])
        inputHandler.send([6100.00, 18.00])
        inputHandler.send([4125.00, 19.00])
        inputHandler.send([3213.00, 1.00])
        inputHandler.send([2319.00, 38.00])
        inputHandler.send([2000.00, 10.00])
        inputHandler.send([1600.00, 0.00])
        inputHandler.send([1394.00, 4.00])
        inputHandler.send([935.00, 4.00])
        inputHandler.send([850.00, 0.00])
        inputHandler.send([775.00, 5.00])
        inputHandler.send([760.00, 6.00])
        inputHandler.send([629.00, 1.00])
        inputHandler.send([275.00, 6.00])
        inputHandler.send([120.00, 0.00])
        inputHandler.send([2567.00, 12.00])
        inputHandler.send([2500.00, 28.00])
        inputHandler.send([2350.00, 21.00])
        inputHandler.send([2317.00, 3.00])
        inputHandler.send([2000.00, 12.00])
        inputHandler.send([715.00, 1.00])
        inputHandler.send([660.00, 9.00])
        inputHandler.send([650.00, 0.00])
        inputHandler.send([260.00, 0.00])
        inputHandler.send([250.00, 1.00])
        inputHandler.send([200.00, 13.00])
        inputHandler.send([180.00, 6.00])

        sleep(1)

        self.assertEqual(50, self.count.get(), "No of events: ")
        self.assertTrue(
            573.1418421169493 - 0.001 < self.betaZero <
            573.1418421169493 + 0.001,
            "Beta0: " + str(573.1418421169493 - self.betaZero))

        siddhiAppRuntime.shutdown()
Ejemplo n.º 14
0
 def setOutputData(self, datum, index):
     complex_event_static_proxy = SiddhiLoader._loadType(
         "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.ComplexEventProxy"
     )()
     complex_event_static_proxy.setOutputData(self._complex_event_proxy,
                                              wrapData(datum), index)
Ejemplo n.º 15
0
 def getType(self):
     raw_type_proxy = self._complex_event_proxy.getType()
     type_proxy = SiddhiLoader._loadType(
         "org.wso2.siddhi.pythonapi.proxy.core.event.complex_event.TypeProxy"
     )(raw_type_proxy)
     return ComplexEvent.Type._map_value(type_proxy)
Ejemplo n.º 16
0
# specific language governing permissions and limitations
# under the License.

from multiprocessing import RLock

import logging

from abc import ABCMeta, abstractmethod

from PySiddhi4 import SiddhiLoader
from PySiddhi4.core.event.Event import Event

from future.utils import with_metaclass

_stream_callback_proxy = SiddhiLoader._loadType(
    "org.wso2.siddhi.pythonapi.proxy.core.stream.output.callback.stream_callback.StreamCallbackProxy"
)

_lock = RLock()

_created_instances = [
]  # Hold reference to prevent python from GC callback before java does


class StreamCallback(with_metaclass(ABCMeta, object)):
    '''
    StreamCallback is used to receive events from StreamJunction
    This class should be extended if one intends to get events from a Siddhi Stream.
    '''

    # __metaclass__ = ABCMeta
Ejemplo n.º 17
0
# 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.

from PySiddhi4 import SiddhiLoader
from PySiddhi4.DataTypes.DataWrapper import wrapData

input_handler_proxy = SiddhiLoader._loadType(
    "org.wso2.siddhi.pythonapi.proxy.core.stream.input.input_handler.InputHandlerProxy"
)


class InputHandler(object):
    '''
    Handles input to SiddhiAppRuntime.
    Wrapper on org.wso2.siddhi.core.stream.input.InputHandler
    '''
    def __init__(self):
        raise NotImplementedError(
            "Initialize InputHandler using SiddhiAppRuntime")

    def __new__(cls):
        bare_instance = object.__new__(cls)
        bare_instance.input_handler_proxy = None