示例#1
0
def single_process_publication_example_1():
    """
    The application in this example consists of single process.
    The process has a single source and no actuator.
    The single source generates 1, 2, 3, 4, .....
    The compute function multiplies this sequence by 10
    and puts the result in the file called test.dat
    num_steps is the number of values output by the source.
    For example, if num_steps is 4 and test.dat is empty before the
    function is called then, test.dat will contain 10, 20, 30, 40
    on separate lines.

    The steps for creating a process are:
    (1) Define the sources.
        In this example we have two sources, source_0 and source_1
    (2) Define the actuators.
        In this example we have no actuators.
    (3) Define compute_func
    (4) Create the process by calling distributed_process()

    Final step
    After creating all processes, specify the connections between
    processes and run the application by calling run_multiprocess.

    """

    # STEP 1: DEFINE SOURCES
    def source(out_stream):
        """
        A simple source which outputs 1, 2, 3,... on
        out_stream.
        """
        def generate_sequence(state):
            return state + 1, state + 1

        # Return an agent which takes 10 steps, and
        # sleeps for 0.1 seconds between successive steps, and
        # puts the next element of the sequence in stream s,
        # and starts the sequence with value 0. The elements on
        # out_stream will be 1, 2, 3, ...
        return source_func_to_stream(func=generate_sequence,
                                     out_stream=out_stream,
                                     time_interval=0.1,
                                     num_steps=4,
                                     state=0)

    # STEP 2: DEFINE ACTUATORS
    # This example has no actuators

    # STEP 3: DEFINE COMPUTE_FUNC
    def f(in_streams, out_streams):
        map_element(func=lambda x: 10 * x,
                    in_stream=in_streams[0],
                    out_stream=out_streams[0])

    # STEP 4: CREATE PROCESSES
    # This process has a single input stream that we call 'in' and it
    # has no output streams. We connect the source to the input stream
    # called 'in'.
    proc_0 = distributed_process(compute_func=f,
                                 in_stream_names=['in'],
                                 out_stream_names=['out'],
                                 connect_sources=[('in', source)],
                                 connect_actuators=[],
                                 name='proc_0')

    # STEP 3: DEFINE COMPUTE_FUNC
    def g(in_streams, out_streams):
        def print_element(v):
            print 'stream element is ', v

        sink_element(func=print_element, in_stream=in_streams[0])

    # STEP 4: CREATE PROCESSES
    proc_1 = distributed_process(compute_func=g,
                                 in_stream_names=['in'],
                                 out_stream_names=[],
                                 connect_sources=[],
                                 connect_actuators=[],
                                 name='proc_1')

    # FINAL STEP: RUN APPLICATION
    # Since this application has a single process it has no
    # connections between processes.
    vm_0 = VM(processes=[proc_0],
              connections=[],
              publishers=[(proc_0, 'out', 'sequence')])

    vm_1 = VM(processes=[proc_1],
              connections=[],
              subscribers=[(proc_1, 'in', 'sequence')])
    vm_0.start()
    vm_1.start()
    vm_0.join()
    vm_1.join()
示例#2
0
def two_process_publication_example_1():
    """
    The application in this example consists of two VMs. It is a
    small extension of def single_process_publication_example_1(). The
    first VM has two processes, proc_0 and proc_1. The second VM has two
    processes, proc_2 and proc_3.

    THE FIRST VM

    PROC_0
    proc_0 has a single source and no actuator.
    The single source generates 1, 2, 3, 4, .....
    num_steps is the number of values output by the source.
    The compute function has a single in_stream and a single
    out_stream. commpute_func merely passes its in_stream to its
    out_stream. 

    PROC_1
    proc_1 has no sources or actuators.
    Its compute function has a single in_stream and a single
    out_stream. compute_func multiples its input elements by 10 and
    puts the results on out_stream.

    THE SECOND VM

    PROC_2
    proc_2 has no sources or actuators. Its compute function has a
    single in_stream and a single out_stream. compute_func multiplies
    elements in in_stream by 2 and places results on out_stream.

    PROC_3
    proc_2 has no sources or actuators. Its compute function has a
    single in_stream and no out_stream. compute_func prints its
    in_stream. 

    The steps for creating a process are:
    (1) Define the sources.
        In this example we have two sources, source_0 and source_1
    (2) Define the actuators.
        In this example we have no actuators.
    (3) Define compute_func
    (4) Create the process by calling distributed_process()
    (5) Create a VM fter creating all processes in the VM. Do this by
        specifying the connections between processes within the VM and
        by specifying the streams published by the VM and the streams 
        subscribed to by the VM.
    (6) Start the VMs.
    (7) Join the VMs. Skip this step if a VM is persistent.

    """

    #------------------------
    #------------------------
    # VM_0
    #------------------------
    #------------------------

    #------------------------
    # proc_0 in VM_0
    #------------------------

    # STEP 1: DEFINE SOURCES
    def source(out_stream):
        """
        A simple source which outputs 1, 2, 3,... on
        out_stream.
        """
        def generate_sequence(state):
            return state + 1, state + 1

        # Return an agent which takes 10 steps, and
        # sleeps for 0.1 seconds between successive steps, and
        # puts the next element of the sequence in stream s,
        # and starts the sequence with value 0. The elements on
        # out_stream will be 1, 2, 3, ...
        return source_func_to_stream(func=generate_sequence,
                                     out_stream=out_stream,
                                     time_interval=0.1,
                                     num_steps=4,
                                     state=0)

    # STEP 2: DEFINE ACTUATORS
    # This example has no actuators

    # STEP 3: DEFINE COMPUTE_FUNC
    def f(in_streams, out_streams):
        map_element(func=lambda x: x,
                    in_stream=in_streams[0],
                    out_stream=out_streams[0])

    # STEP 4: CREATE PROCESSES
    # This process has a single input stream that we call 'in' and it
    # has no output streams. We connect the source to the input stream
    # called 'in'.
    proc_0 = distributed_process(compute_func=f,
                                 in_stream_names=['in'],
                                 out_stream_names=['out'],
                                 connect_sources=[('in', source)],
                                 connect_actuators=[],
                                 name='proc_0')

    #------------------------
    # proc_1 in VM_0
    #------------------------
    # STEP 1: DEFINE SOURCES
    # skip this step since proc_1 has no sources.

    # STEP 2: DEFINE ACTUATORS
    # # skip this step since proc_1 has no actuators.

    # STEP 3: DEFINE COMPUTE_FUNC
    def g(in_streams, out_streams):
        map_element(func=lambda x: 10 * x,
                    in_stream=in_streams[0],
                    out_stream=out_streams[0])

    # STEP 4: CREATE PROCESSES
    proc_1 = distributed_process(compute_func=g,
                                 in_stream_names=['in'],
                                 out_stream_names=['out'],
                                 connect_sources=[],
                                 connect_actuators=[],
                                 name='proc_1')

    # STEP 5: CREATE VM
    vm_0 = VM(processes=[proc_0, proc_1],
              connections=[(proc_0, 'out', proc_1, 'in')],
              publishers=[(proc_1, 'out', 'sequence')])

    #------------------------
    #------------------------
    # VM_1
    #------------------------
    #------------------------

    #------------------------
    # proc_2 in VM_1
    #------------------------
    # STEP 1: DEFINE SOURCES
    # skip this step since proc_1 has no sources.

    # STEP 2: DEFINE ACTUATORS
    # # skip this step since proc_1 has no actuators.

    # STEP 3: DEFINE COMPUTE_FUNC
    def h(in_streams, out_streams):
        map_element(func=lambda x: 2 * x,
                    in_stream=in_streams[0],
                    out_stream=out_streams[0])

    # STEP 4: CREATE PROCESSES
    proc_2 = distributed_process(compute_func=h,
                                 in_stream_names=['in'],
                                 out_stream_names=['out'],
                                 connect_sources=[],
                                 connect_actuators=[],
                                 name='proc_2')

    #------------------------
    # proc_3 in VM_1
    #------------------------
    # STEP 1: DEFINE SOURCES
    # skip this step since proc_1 has no sources.

    # STEP 2: DEFINE ACTUATORS
    # # skip this step since proc_1 has no actuators.

    # STEP 3: DEFINE COMPUTE_FUNC
    def pr(in_streams, out_streams):
        def print_element(v):
            print 'stream element is ', v

        sink_element(func=print_element, in_stream=in_streams[0])

    # STEP 4: CREATE PROCESSES
    proc_3 = distributed_process(compute_func=pr,
                                 in_stream_names=['in'],
                                 out_stream_names=[],
                                 connect_sources=[],
                                 connect_actuators=[],
                                 name='proc_3')

    # STEP 5: CREATE VM
    vm_1 = VM(processes=[proc_2, proc_3],
              connections=[(proc_2, 'out', proc_3, 'in')],
              subscribers=[(proc_2, 'in', 'sequence')])

    # STEP 6: START PROCESSES
    vm_0.start()
    vm_1.start()

    # STEP 7: JOIN PROCESSES
    vm_0.join()
    vm_1.join()