2.1 Designing your own module - rayhan-ferdous/ProvMod GitHub Wiki

From definition, a module has some parameters (that are internal attributes of it) and inputs (that are data-flow coming from other sources).

First fix your module's parameters and inputs with their types. Then you can inherit the Module class from the library and override the body method, to create a new module class. Note, the parent class Module already has an attribute named P in it. This should be your list of arguments (including inputs).

Example

Create a module that takes a list L of integers as input, and returns maximum and minimum numbers withing a certain index i and j, where 0 <= i, j < length(L).

  • We are gonna name this module as Max_Min
  • We assume its parameter & input mapping is, Max_Min(L, i, j)
  • Therefore, its input is L == P[0] : Object
  • And, it's parameters are i == P[1] : int and j == P[2] : int
  • Its outputs are 2 distinct integers, so of Object type

Implementation

import ProvModel as pm
import Operators as op

# creating a module with 2 outputs max and min of a list

# first inherit parent class Module from the model
class Max_Min(pm.Module):
    # override the body method
    def body(self):
        # no need to pass and define any parameter or input
        # from our design, we assume that there are 2 parameters and 1 input as attributes
        # So, we already have attribute list P in the parent class Module
        # we assign a name to the attributes
        i = self.P[1]
        j = self.P[2]
        # P[0] is a Object type data-flow, so we take its ref to seek the value
        L = self.P[0].ref

        subL = L[i:j]

        x = max(subL)
        y = min(subL)
        
        # x and y are the corresponding max and min values of the sublist
        # the output type could be of only certain types from the model
        # here they are of Object type
        r1 = pm.Object(x)
        r2 = pm.Object(y)
        
        # We can easily pass multiple data-flows as outputs
        return r1, r2

Use

# a list L
L = [1,3,5,8,7,4,2,3,6,4,8,9,1,2,0]

# the input data-flow as Object type
d_in = pm.Object(L)

# the module instance as m with parameters and input
m = Max_Min(d_in, 2, 6)

# a support for multiple output
d_out1, d_out2 = m.run()

# test the values of output
# as d_out is a Object instance, we print its ref to show the containing value
print d_out1.ref, d_out2.ref