formatted_io2 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
27
# Import classes for input/output channels
from cis_interface.interface.CisInterface import (
    CisAsciiTableInput, CisAsciiTableOutput)

# Initialize input/output channels
in_channel = CisAsciiTableInput('inputA')
out_channel = CisAsciiTableOutput('outputA', '%6s\t%d\t%f\n')

# 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("Model A: No more input.")
        break
    name, count, size = msg

    # Print received message
    print('Model A: %s, %d, %f' % (name, count, size))

    # Send output to output channel
    # If there is an error, the flag will be False
    flag = out_channel.send(name, count, size)
    if not flag:
        raise RuntimeError("Model A: Error sending output.")
 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
# Import classes for input/output channels
from cis_interface.interface.CisInterface import (
    CisAsciiTableInput, CisAsciiTableOutput)

# Initialize input/output channels
in_channel = CisAsciiTableInput('inputB')
out_channel = CisAsciiTableOutput('outputB', '%6s\t%d\t%f\n')

# 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("Model B: No more input.")
        break
    name, count, size = msg

    # Print received message
    print('Model B: %s, %d, %f' % (name, count, size))

    # Send output to output channel
    # If there is an error, the flag will be False
    flag = out_channel.send(name, count, size)
    if not flag:
        raise RuntimeError("Model B: Error sending output.")

Model YAML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
models:
  - name: python_modelA
    language: python
    args: ./src/formatted_io2_modelA.py
    inputs: inputA
    outputs: outputA

  - name: python_modelB
    language: python
    args: ./src/formatted_io2_modelB.py
    inputs: inputB
    outputs: outputB

connections:
  - input: outputA  # Connection between model A output & model B input
    output: inputB
  - input: ./Input/input.txt  # Connection between file and model A input
    output: inputA
    filetype: table
  - input: outputB  # Connection between model B output and file
    output: ./output.txt
    filetype: table

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
32
% Initialize input/output channels 
in_channel = CisInterface('CisAsciiTableInput', 'inputA');
out_channel = CisInterface('CisAsciiTableOutput', 'outputA', '%6s\t%d\t%f\n');

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('Model A: No more input.');
    break;
  end;
  name = msg{1};
  count = msg{2};
  size = msg{3};

  % Print received message
  fprintf('Model A: %s, %d, %f\n', name, count, size);

  % Send output to output channel
  % If there is an error, the flag will be False
  flag = out_channel.send(name, count, size);
  if (~flag)
    error('Model A: Error sending output.');
    break;
  end;
  
end;
 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
% Initialize input/output channels 
in_channel = CisInterface('CisAsciiTableInput', 'inputB');
out_channel = CisInterface('CisAsciiTableOutput', 'outputB', '%6s\t%d\t%f\n');

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('Model B: No more input.');
    break;
  end;
  name = msg{1};
  count = msg{2};
  size = msg{3};

  % Print received message
  fprintf('Model B: %s, %d, %f\n', name, count, size);

  % Send output to output channel
  % If there is an error, the flag will be False
  flag = out_channel.send(name, count, size);
  if (~flag)
    error('Model B: Error sending output.');
    break;
  end;
  
end;

Model YAML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
models:
  - name: matlab_modelA
    language: matlab
    args: ./src/formatted_io2_modelA.m
    inputs: inputA
    outputs: outputA

  - name: matlab_modelB
    language: matlab
    args: ./src/formatted_io2_modelB.m
    inputs: inputB
    outputs: outputB

connections:
  - input: outputA  # Connection between model A output & model B input
    output: inputB
  - input: ./Input/input.txt  # Connection between file and model A input
    output: inputA
    filetype: table
  - input: outputB  # Connection between model B output and file
    output: ./output.txt
    filetype: table

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
44
45
46
47
#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
  cisAsciiTableInput_t in_channel = cisAsciiTableInput("inputA");
  cisAsciiTableOutput_t out_channel = cisAsciiTableOutput("outputA", "%6s\t%d\t%f\n");

  // Declare resulting variables and create buffer for received message
  int flag = 1;
  size_t name_siz = MYBUFSIZ;
  char name[MYBUFSIZ];
  int count = 0;
  double size = 0.0;

  // Loop until there is no longer input or the queues are closed
  while (flag >= 0) {
    name_siz = MYBUFSIZ; // Reset to size of the buffer
  
    // Receive input from input channel
    // If there is an error, the flag will be negative
    // Otherwise, it is the size of the received message
    flag = cisRecv(in_channel, &name, &name_siz, &count, &size);
    if (flag < 0) {
      printf("Model A: No more input.\n");
      break;
    }

    // Print received message
    printf("Model A: %s, %d, %f\n", name, count, size);

    // Send output to output channel
    // If there is an error, the flag will be negative
    flag = cisSend(out_channel, name, name_siz, count, size);
    if (flag < 0) {
      printf("Model A: Error sending output.\n");
      break;
    }

  }
  
  return 0;
}

 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
44
45
46
47
#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
  cisAsciiTableInput_t in_channel = cisAsciiTableInput("inputB");
  cisAsciiTableOutput_t out_channel = cisAsciiTableOutput("outputB", "%6s\t%d\t%f\n");

  // Declare resulting variables and create buffer for received message
  int flag = 1;
  size_t name_siz = MYBUFSIZ;
  char name[MYBUFSIZ];
  int count = 0;
  double size = 0.0;

  // Loop until there is no longer input or the queues are closed
  while (flag >= 0) {
    name_siz = MYBUFSIZ; // Reset to size of the buffer
  
    // Receive input from input channel
    // If there is an error, the flag will be negative
    // Otherwise, it is the size of the received message
    flag = cisRecv(in_channel, &name, &name_siz, &count, &size);
    if (flag < 0) {
      printf("Model B: No more input.\n");
      break;
    }

    // Print received message
    printf("Model B: %s, %d, %f\n", name, count, size);

    // Send output to output channel
    // If there is an error, the flag will be negative
    flag = cisSend(out_channel, name, name_siz, count, size);
    if (flag < 0) {
      printf("Model B: Error sending output.\n");
      break;
    }

  }
  
  return 0;
}

Model YAML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
models:
  - name: c_modelA
    language: c
    args: ./src/formatted_io2_modelA.c
    inputs: inputA
    outputs: outputA

  - name: c_modelB
    language: c
    args: ./src/formatted_io2_modelB.c
    inputs: inputB
    outputs: outputB

connections:
  - input: outputA  # Connection between model A output & model B input
    output: inputB
  - input: ./Input/input.txt  # Connection between file and model A input
    output: inputA
    filetype: table
  - input: outputB  # Connection between model B output and file
    output: ./output.txt
    filetype: table

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
44
45
46
47
#include <iostream>
// Include methods for input/output channels
#include "CisInterface.hpp"

#define MYBUFSIZ 1000

int main(int argc, char *argv[]) {
  // Initialize input/output channels
  CisAsciiTableInput in_channel("inputA");
  CisAsciiTableOutput out_channel("outputA", "%6s\t%d\t%f\n");

  // Declare resulting variables and create buffer for received message
  int flag = 1;
  size_t name_siz = MYBUFSIZ;
  size_t * const p_name_siz = &name_siz; // Required in C++ to get name size
  char name[MYBUFSIZ];
  int count = 0;
  double size = 0.0;

  // Loop until there is no longer input or the queues are closed
  while (flag >= 0) {
    name_siz = MYBUFSIZ;  // Reset to buffer size
  
    // Receive input from input channel
    // If there is an error, the flag will be negative
    // Otherwise, it is the size of the received message
    flag = in_channel.recv(4, &name, &name_siz, &count, &size);
    if (flag < 0) {
      std::cout << "Model A: No more input." << std::endl;
      break;
    }

    // Print received message
    std::cout << "Model A: " << name << ", " << count << ", " << size << std::endl;

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

  }
  
  return 0;
}
 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
44
45
46
47
#include <iostream>
// Include methods for input/output channels
#include "CisInterface.hpp"

#define MYBUFSIZ 1000

int main(int argc, char *argv[]) {
  // Initialize input/output channels
  CisAsciiTableInput in_channel("inputB");
  CisAsciiTableOutput out_channel("outputB", "%6s\t%d\t%f\n");

  // Declare resulting variables and create buffer for received message
  int flag = 1;
  size_t name_siz = MYBUFSIZ;
  size_t * const p_name_siz = &name_siz; // Required in C++ to get name size
  char name[MYBUFSIZ];
  int count = 0;
  double size = 0.0;

  // Loop until there is no longer input or the queues are closed
  while (flag >= 0) {
    name_siz = MYBUFSIZ;  // Reset to buffer size
  
    // Receive input from input channel
    // If there is an error, the flag will be negative
    // Otherwise, it is the size of the received message
    flag = in_channel.recv(4, &name, &name_siz, &count, &size);
    if (flag < 0) {
      std::cout << "Model B: No more input." << std::endl;
      break;
    }

    // Print received message
    std::cout << "Model B: " << name << ", " << count << ", " << size << std::endl;

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

  }
  
  return 0;
}

Model YAML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
models:
  - name: cpp_modelA
    language: c++
    args: ./src/formatted_io2_modelA.cpp
    inputs: inputA
    outputs: outputA

  - name: cpp_modelB
    language: c++
    args: ./src/formatted_io2_modelB.cpp
    inputs: inputB
    outputs: outputB

connections:
  - input: outputA  # Connection between model A output & model B input
    output: inputB
  - input: ./Input/input.txt  # Connection between file and model A input
    output: inputA
    filetype: table
  - input: outputB  # Connection between model B output and file
    output: ./output.txt
    filetype: table