Example #1
0
def test_from_json():
    '''basic test of JSON deserialization'''
    s_json = '''{
      "__class__": "sysdiag.System",
      "__sysdiagclass__": "System",
      "name": "my syst",
      "params": {},
      "ports": [],
      "subsystems": [],
      "wires": []
    }'''
    s = sysdiag.json_load(s_json)
    assert_is(type(s), sysdiag.System)
    assert_equal(s.name, "my syst")

    # Test a port:
    p_json = '''{
      "__class__": "sysdiag.Port",
      "__sysdiagclass__": "Port",
      "name": "my port",
      "type": ""
    }'''
    p = sysdiag.json_load(p_json)
    assert_is(type(p), sysdiag.Port)
    assert_equal(p.name, "my port")

    # Test a wire:
    w_json = '''{
Example #2
0
def test_json():
    '''basic test of JSON serialization/deserialization'''
    # PI controller block (example of Laplace tranfer)
    K = 1
    Ti = 0.1
    ctrl = blocks.TransferFunction('controller', [1, K*Ti],[0, Ti])
    s_json = '''{
      "__class__": "blocks.TransferFunction",
      "__sysdiagclass__": "System",
      "name": "controller",
      "params": {
        "den": [
          0,
          0.1
        ],
        "num": [
          1,
          0.1
        ]
      },
      "ports": [],
      "subsystems": [],
      "wires": []
    }'''
    # Compare JSON without whitespaces:
    assert_equal(ctrl.json_dump().replace(' ',''), s_json.replace(' ',''))
    
    import sysdiag
    ctrl1 = sysdiag.json_load(s_json)
    assert_is(type(ctrl1), blocks.TransferFunction)
    assert_equal(ctrl1.name, "controller")
    assert_equal(ctrl, ctrl1)
Src = blocks.Source('src', root)
K = 2; Ti = .2
Ctrl = blocks.TransferFunction('controller', [1, K*Ti],[0, Ti], root) # PI control
Plant = blocks.TransferFunction('plant', [1], [0, 1], root) # integrator
Cmp = blocks.Summation('compare', ops = ['+','-'], parent = root)

Out = blocks.Sink(parent=root)

w0 = blocks.connect_systems(Src, Cmp, d_pname='in0')
w1 = blocks.connect_systems(Cmp, Ctrl)
w2 = blocks.connect_systems(Ctrl, Plant)
w3 = blocks.connect_systems(Plant, Cmp, d_pname='in1')

w4 = blocks.connect_systems(Plant, Out)
assert w3 == w4

print(root)

print('Compute incidence matrix')
inc_mat = blocks.incidence_matrix(root)
print(inc_mat)


# Serialize/Deserialize:
r_json = root.json_dump()
import sysdiag
r1 = sysdiag.json_load(r_json)
print('Serialize/Deserialize:')
print('root == r1: {:s}'.format(str(root == r1)))