gs_lesson3 Example — cis_interface 0.3.0 documentation

# gs_lesson3 Example

## Python Version

Model Code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    # Import classes for input/output channels
    from cis_interface.interface.CisInterface import CisInput, CisOutput

    # Initialize input/output channels
    in_channel = CisInput('input')
    out_channel = CisOutput('output')

    # Loop until there is no longer input or the queues are closed
    while True:

        # Receive input from input channel
        # If there is an error, the flag will be False
        flag, msg = in_channel.recv()
        if not flag:
            print("No more input.")
            break

        # Print received message
        print(msg)

        # Send output to output channel
        # If there is an error, the flag will be False
        flag = out_channel.send(msg)
        if not flag:
            print("Error sending output.")
            break

Model YAML:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    models:
      - name: python_model
        language: python  # Runs the python script using default Python
        args: ./src/gs_lesson3.py
        inputs:
          - input
        outputs:
          - output

    connections:
      - input_file: ./Input/input.txt
        output: input
      - input: output
        output: ./output.txt

## C++ Version

Model Code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    #include <iostream>
    // Include methods for input/output channels
    #include "CisInterface.hpp"

    #define MYBUFSIZ 1000

    int main(int argc, char *argv[]) {
      // Initialize input/output channels
      CisInput in_channel("input");
      CisOutput out_channel("output");

      // Declare resulting variables and create buffer for received message
      int flag = 1;
      char buf[MYBUFSIZ];

      // Loop until there is no longer input or the queues are closed
      while (flag >= 0) {

        // Receive input from input channel
        // If there is an error or the queue is closed, the flag will be negative
        // Otherwise, it is the size of the received message
        flag = in_channel.recv(buf, MYBUFSIZ);
        if (flag < 0) {
          std::cout << "No more input." << std::endl;
          break;
        }

        // Print received message
        std::cout << buf << std::endl;

        // Send output to output channel
        // If there is an error, the flag will be negative
        flag = out_channel.send(buf, flag);
        if (flag < 0) {
          std::cout << "Error sending output." << std::endl;
          break;
        }

      }

      return 0;
    }

Model YAML:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    models:
      - name: cpp_model
        language: c++  # Compiles the C code with necessary cis_interface libraries
        args: ./src/gs_lesson3.cpp
        inputs:
          - input
        outputs:
          - output

    connections:
      - input_file: ./Input/input.txt
        output: input
      - input: output
        output: ./output.txt

## C Version

Model Code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    #include <stdio.h>
    // Include methods for input/output channels
    #include "CisInterface.h"

    #define MYBUFSIZ 1000

    int main(int argc, char *argv[]) {
      // Initialize input/output channels
      cisInput_t in_channel = cisInput("input");
      cisOutput_t out_channel = cisOutput("output");

      // Declare resulting variables and create buffer for received message
      int flag = 1;
      char buf[MYBUFSIZ];

      // Loop until there is no longer input or the queues are closed
      while (flag >= 0) {

        // Receive input from input channel
        // If there is an error or the queue is closed, the flag will be negative
        // Otherwise, it is the size of the received message
        flag = cis_recv(in_channel, buf, MYBUFSIZ);
        if (flag < 0) {
          printf("No more input.\n");
          break;
        }

        // Print received message
        printf("%s\n", buf);

        // Send output to output channel
        // If there is an error, the flag will be negative
        flag = cis_send(out_channel, buf, flag);
        if (flag < 0) {
          printf("Error sending output.\n");
          break;
        }

      }

      return 0;
    }

Model YAML:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    models:
      - name: c_model
        language: c  # Compiles the C code with necessary cis_interface libraries
        args: ./src/gs_lesson3.c
        inputs:
          - input
        outputs:
          - output

    connections:
      - input_file: ./Input/input.txt
        output: input
      - input: output
        output: ./output.txt

## Matlab Version

Model Code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    % Initialize input/output channels
    in_channel = CisInterface('CisInput', 'input');
    out_channel = CisInterface('CisOutput', 'output');

    flag = true;

    % Loop until there is no longer input or the queues are closed
    while flag

      % Receive input from input channel
      % If there is an error, the flag will be False.
      [flag, msg] = in_channel.recv();
      if (~flag)
        disp('No more input.');
        break
      end

      % Print received message
      fprintf('%s\n', char(msg));

      % Send output to output channel
      % If there is an error, the flag will be False
      flag = out_channel.send(msg);
      if (~flag)
        disp('Error sending output.');
        break
      end

    end

    exit(0);

Model YAML:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    models:
      - name: matlab_model
        language: matlab  # Runs the script using a Matlab engine
        args: ./src/gs_lesson3.m
        inputs:
          - input
        outputs:
          - output

    connections:
      - input_file: ./Input/input.txt
        output: input
      - input: output
        output: ./output.txt