def testSortedGraph3(self): CBDNegator = CBD("negatorCBD", input_ports = ["inNegator"], output_ports = ["outNegator", "outInverter"]) negCbd = NegatorBlock(block_name="nC") invCBD = InverterBlock(block_name="iC") CBDNegator.addBlock(negCbd) CBDNegator.addBlock(invCBD) CBDNegator.addConnection("inNegator", "nC") CBDNegator.addConnection("inNegator", "iC") CBDNegator.addConnection("nC", "outNegator") CBDNegator.addConnection("iC", "outInverter") const = ConstantBlock(block_name="c", value=5.5) self.CBD.addBlock(const) add = AdderBlock(block_name="a") self.CBD.addBlock(add) self.CBD.addBlock(CBDNegator) self.CBD.addConnection("negatorCBD", "a", output_port_name = "outNegator") self.CBD.addConnection("negatorCBD", "a", output_port_name = "outInverter") self.CBD.addConnection("c", "negatorCBD", input_port_name = "inNegator") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() comps = [ x[0] for x in sortedGraph ] tester = self ag = lambda x,y: tester.assertTrue(comps.index(x) > comps.index(y)) self.assertEqual(len(sortedGraph), 7) ag(negCbd, const) ag(invCBD, const) ag(add, negCbd) ag(add, invCBD)
def testPIDControllerCBD(): cbd = CBD("CBD") cbd.addBlock(PIDControllerCBD("testPIDControllerCBD")) cbd.addBlock(ConstantBlock("ERROR_IN", 1)) cbd.addBlock(ConstantBlock("DELTA_IN", 1)) cbd.addConnection("ERROR_IN", "testPIDControllerCBD", input_port_name="ERROR") cbd.addConnection("DELTA_IN", "testPIDControllerCBD", input_port_name="DELTA") cbd.run(10)
def testSortedGraph2(self): CBDAdder = CBD("adderCBD", input_ports = ["in1", "in2"], output_ports = ["outAdd"]) addCBD = AdderBlock(block_name="aC") CBDAdder.addBlock(addCBD) CBDAdder.addConnection("in1", "aC") CBDAdder.addConnection("in2", "aC") CBDAdder.addConnection("aC", "outAdd") const1 = ConstantBlock(block_name="c1", value=5.5) const2 = ConstantBlock(block_name="c2", value=4.5) self.CBD.addBlock(const1) self.CBD.addBlock(const2) neg = NegatorBlock(block_name="n") self.CBD.addBlock(neg) self.CBD.addBlock(CBDAdder) self.CBD.addConnection("adderCBD", "n", output_port_name = "outAdd") self.CBD.addConnection("c1", "adderCBD", input_port_name = "in1") self.CBD.addConnection("c2", "adderCBD", input_port_name = "in2") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() comps = [ x[0] for x in sortedGraph ] tester = self ag = lambda x,y: tester.assertTrue(comps.index(x) > comps.index(y)) self.assertEqual(len(sortedGraph), 7) ag(addCBD, const1) ag(addCBD, const2) ag(neg, addCBD)
def testSortedGraph(self): CBDNegator = CBD("negatorCBD", input_ports=["inNegator"], output_ports=["outNegator"]) negCbd = NegatorBlock(block_name="nC") CBDNegator.addBlock(negCbd) CBDNegator.addConnection("inNegator", "nC") CBDNegator.addConnection("nC", "outNegator") const = ConstantBlock(block_name="c", value=5.5) self.CBD.addBlock(const) neg = NegatorBlock(block_name="n") self.CBD.addBlock(neg) self.CBD.addBlock(CBDNegator) self.CBD.addConnection("negatorCBD", "n", output_port_name="outNegator") self.CBD.addConnection("c", "negatorCBD", input_port_name="inNegator") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() self.assertEquals(len(sortedGraph), 5) self.assertEquals(sortedGraph[0][0], const) self.assertEquals(sortedGraph[2][0], negCbd) self.assertEquals(sortedGraph[4][0], neg)
def testInterCBD(self): CBDLittle1 = CBD("first_child", output_ports=["outCBD1"]) CBDLittle2 = CBD("second_child", input_ports=["inCBD2"]) self.CBD.addBlock(CBDLittle1) self.CBD.addBlock(CBDLittle2) self.CBD.addConnection("first_child", "second_child", input_port_name="inCBD2", output_port_name="outCBD1") CBDLittle1.addBlock(ConstantBlock(block_name="c1", value=2.0)) CBDLittle1.addConnection("c1", "outCBD1") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("second_child.inCBD2"), [2.0] * 5)
def testLinearStrongComponentWithNeg(self): CBDConstant1 = CBD("constantCBD1", output_ports=["outConstant1"]) CBDConstant1.addBlock(ConstantBlock(block_name="c", value=5)) CBDConstant1.addConnection("c", "outConstant1") CBDConstant2 = CBD("constantCBD2", output_ports=["outConstant2"]) CBDConstant2.addBlock(ConstantBlock(block_name="c", value=8)) CBDConstant2.addConnection("c", "outConstant2") CBDAdder1 = CBD("adder1CBD", input_ports=["in1Add1", "in2Add1"], output_ports=["outAdd1"]) CBDAdder1.addBlock(AdderBlock(block_name="a")) CBDAdder1.addConnection("in1Add1", "a") CBDAdder1.addConnection("in2Add1", "a") CBDAdder1.addConnection("a", "outAdd1") CBDAdder2 = CBD("adder2CBD", input_ports=["in1Add2", "in2Add2"], output_ports=["outAdd2"]) CBDAdder2.addBlock(AdderBlock(block_name="a")) CBDAdder2.addConnection("in1Add2", "a") CBDAdder2.addConnection("in2Add2", "a") CBDAdder2.addConnection("a", "outAdd2") CBDNegator = CBD("negatorCBD", input_ports=["inNeg"], output_ports=["outNeg"]) CBDNegator.addBlock(NegatorBlock(block_name="n")) CBDNegator.addConnection("inNeg", "n") CBDNegator.addConnection("n", "outNeg") self.CBD.addBlock(CBDConstant1) self.CBD.addBlock(CBDConstant2) self.CBD.addBlock(CBDAdder1) self.CBD.addBlock(CBDAdder2) self.CBD.addBlock(CBDNegator) self.CBD.addConnection("constantCBD1", "adder1CBD", input_port_name="in1Add1", output_port_name="outConstant1") self.CBD.addConnection("adder2CBD", "adder1CBD", input_port_name="in2Add1", output_port_name="outAdd2") self.CBD.addConnection("constantCBD2", "adder2CBD", input_port_name="in1Add2", output_port_name="outConstant2") self.CBD.addConnection("negatorCBD", "adder2CBD", input_port_name="in2Add2", output_port_name="outNeg") self.CBD.addConnection("adder1CBD", "negatorCBD", input_port_name="inNeg", output_port_name="outAdd1") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("adder1CBD.outAdd1"), [6.5] * 5) self.assertEquals(self._getSignal("adder2CBD.outAdd2"), [1.5] * 5) self.assertEquals(self._getSignal("negatorCBD.outNeg"), [-6.5] * 5)
class FlattenCBDTest(unittest.TestCase): def setUp(self): self.CBD = CBD("block_under_test") def _run(self, num_steps=1, step=1): self.CBD.run(num_steps, step) def _getSignal(self, blockname, output_port=None): foundBlocks = [ block for block in self.CBD.getBlocks() if block.getBlockName() == blockname ] signal = foundBlocks[0].getSignal(name_output=output_port) if len(foundBlocks) == 1: return [x.value for x in signal] else: raise Exception("No single block with name " + blockname + " found") def testInterCBD(self): CBDLittle1 = CBD("first_child", output_ports=["outCBD1"]) CBDLittle2 = CBD("second_child", input_ports=["inCBD2"]) self.CBD.addBlock(CBDLittle1) self.CBD.addBlock(CBDLittle2) self.CBD.addConnection("first_child", "second_child", input_port_name="inCBD2", output_port_name="outCBD1") CBDLittle1.addBlock(ConstantBlock(block_name="c1", value=2.0)) CBDLittle1.addConnection("c1", "outCBD1") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("second_child.inCBD2"), [2.0] * 5) def testLinearStrongComponentWithMult(self): CBDConstant1 = CBD("constantCBD1", output_ports=["outConstant1"]) CBDConstant1.addBlock(ConstantBlock(block_name="c", value=3)) CBDConstant1.addConnection("c", "outConstant1") CBDConstant2 = CBD("constantCBD2", output_ports=["outConstant2"]) CBDConstant2.addBlock(ConstantBlock(block_name="c", value=5)) CBDConstant2.addConnection("c", "outConstant2") CBDAdder = CBD("adderCBD", input_ports=["in1Add", "in2Add"], output_ports=["outAdd"]) CBDAdder.addBlock(AdderBlock(block_name="a")) CBDAdder.addConnection("in1Add", "a") CBDAdder.addConnection("in2Add", "a") CBDAdder.addConnection("a", "outAdd") CBDProduct = CBD("productCBD", input_ports=["in1Prod", "in2Prod"], output_ports=["outProd"]) CBDProduct.addBlock(ProductBlock(block_name="p")) CBDProduct.addConnection("in1Prod", "p") CBDProduct.addConnection("in2Prod", "p") CBDProduct.addConnection("p", "outProd") self.CBD.addBlock(CBDConstant1) self.CBD.addBlock(CBDConstant2) self.CBD.addBlock(CBDAdder) self.CBD.addBlock(CBDProduct) self.CBD.addConnection("constantCBD1", "adderCBD", input_port_name="in1Add", output_port_name="outConstant1") self.CBD.addConnection("productCBD", "adderCBD", input_port_name="in2Add", output_port_name="outProd") self.CBD.addConnection("adderCBD", "productCBD", input_port_name="in1Prod", output_port_name="outAdd") self.CBD.addConnection("constantCBD2", "productCBD", input_port_name="in2Prod", output_port_name="outConstant2") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("adderCBD.outAdd"), [-0.75] * 5) self.assertEquals(self._getSignal("productCBD.outProd"), [-3.75] * 5) def testLinearStrongComponentWithNeg(self): CBDConstant1 = CBD("constantCBD1", output_ports=["outConstant1"]) CBDConstant1.addBlock(ConstantBlock(block_name="c", value=5)) CBDConstant1.addConnection("c", "outConstant1") CBDConstant2 = CBD("constantCBD2", output_ports=["outConstant2"]) CBDConstant2.addBlock(ConstantBlock(block_name="c", value=8)) CBDConstant2.addConnection("c", "outConstant2") CBDAdder1 = CBD("adder1CBD", input_ports=["in1Add1", "in2Add1"], output_ports=["outAdd1"]) CBDAdder1.addBlock(AdderBlock(block_name="a")) CBDAdder1.addConnection("in1Add1", "a") CBDAdder1.addConnection("in2Add1", "a") CBDAdder1.addConnection("a", "outAdd1") CBDAdder2 = CBD("adder2CBD", input_ports=["in1Add2", "in2Add2"], output_ports=["outAdd2"]) CBDAdder2.addBlock(AdderBlock(block_name="a")) CBDAdder2.addConnection("in1Add2", "a") CBDAdder2.addConnection("in2Add2", "a") CBDAdder2.addConnection("a", "outAdd2") CBDNegator = CBD("negatorCBD", input_ports=["inNeg"], output_ports=["outNeg"]) CBDNegator.addBlock(NegatorBlock(block_name="n")) CBDNegator.addConnection("inNeg", "n") CBDNegator.addConnection("n", "outNeg") self.CBD.addBlock(CBDConstant1) self.CBD.addBlock(CBDConstant2) self.CBD.addBlock(CBDAdder1) self.CBD.addBlock(CBDAdder2) self.CBD.addBlock(CBDNegator) self.CBD.addConnection("constantCBD1", "adder1CBD", input_port_name="in1Add1", output_port_name="outConstant1") self.CBD.addConnection("adder2CBD", "adder1CBD", input_port_name="in2Add1", output_port_name="outAdd2") self.CBD.addConnection("constantCBD2", "adder2CBD", input_port_name="in1Add2", output_port_name="outConstant2") self.CBD.addConnection("negatorCBD", "adder2CBD", input_port_name="in2Add2", output_port_name="outNeg") self.CBD.addConnection("adder1CBD", "negatorCBD", input_port_name="inNeg", output_port_name="outAdd1") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("adder1CBD.outAdd1"), [6.5] * 5) self.assertEquals(self._getSignal("adder2CBD.outAdd2"), [1.5] * 5) self.assertEquals(self._getSignal("negatorCBD.outNeg"), [-6.5] * 5) def testInterInterCBD(self): """ +--------------------------------------------+ +----+ | +------+ | | |+--->|+------------------>| | | | 2 | || | + +--------------->+-------+ +----+ +----+ || +---------+ +-->| | | | | | |+>| | | +------+ | v | 5 | +------+ | | | | | +------+ +----++->| | | | * | | | | | | + | | | | | | | + +------> 0 +----++->| |+--->|+>| | | | | | | 2 | +------+ | +--+------+ +--------------+ | +------+ | | | | | +-----+ | ^ +----+ | | +--------------------+| | | | | | | | +-----+ || | 12 +->+-------+ | | | | | || | | | | +-->+---->| - +------->++ +-----+ | | | | | | | | | +-----+ | | | +--------------------+ | +--------------------------------------------+ """ CBDLittle1 = CBD("first_child", input_ports=["in1CBD1", "in2CBD1"], output_ports=["out1CBD1", "out2CBD1"]) CBDLittle2 = CBD("first_child_of_first_child", input_ports=["inCBD2"], output_ports=["outCBD2"]) self.CBD.addBlock(ConstantBlock(block_name="c1", value=2.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.0)) self.CBD.addBlock(ConstantBlock(block_name="c3", value=2.0)) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addBlock(AdderBlock(block_name="a2")) self.CBD.addBlock(CBDLittle1) self.CBD.addConnection("c3", "a") self.CBD.addConnection("c2", "a") self.CBD.addConnection("c1", "first_child", input_port_name="in1CBD1") self.CBD.addConnection("a", "first_child", input_port_name="in2CBD1") self.CBD.addConnection("first_child", "a2", output_port_name="out1CBD1") self.CBD.addConnection("first_child", "a2", output_port_name="out2CBD1") CBDLittle1.addBlock(ProductBlock(block_name="p")) CBDLittle1.addBlock(AdderBlock(block_name="a")) CBDLittle1.addBlock(CBDLittle2) CBDLittle1.addBlock(ConstantBlock(block_name="c", value=12.0)) CBDLittle1.addConnection("in1CBD1", "p") CBDLittle1.addConnection("in2CBD1", "p") CBDLittle1.addConnection("in1CBD1", "a") CBDLittle1.addConnection("first_child_of_first_child", "a", output_port_name="outCBD2") CBDLittle1.addConnection("p", "first_child_of_first_child", input_port_name="inCBD2") CBDLittle1.addConnection("c", "out1CBD1") CBDLittle1.addConnection("a", "out2CBD1") CBDLittle2.addBlock(NegatorBlock(block_name="n")) CBDLittle2.addConnection("inCBD2", "n") CBDLittle2.addConnection("n", "outCBD2") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("first_child.p"), [14.0] * 5) self.assertEquals( self._getSignal("first_child.first_child_of_first_child.n"), [-14.0] * 5) self.assertEquals(self._getSignal("first_child.in1CBD1"), [2.0] * 5) self.assertEquals(self._getSignal("first_child.a"), [-12.0] * 5) self.assertEquals(self._getSignal("a2"), [0.0] * 5)
def testLinearStrongComponentWithMult(self): CBDConstant1 = CBD("constantCBD1", output_ports=["outConstant1"]) CBDConstant1.addBlock(ConstantBlock(block_name="c", value=3)) CBDConstant1.addConnection("c", "outConstant1") CBDConstant2 = CBD("constantCBD2", output_ports=["outConstant2"]) CBDConstant2.addBlock(ConstantBlock(block_name="c", value=5)) CBDConstant2.addConnection("c", "outConstant2") CBDAdder = CBD("adderCBD", input_ports=["in1Add", "in2Add"], output_ports=["outAdd"]) CBDAdder.addBlock(AdderBlock(block_name="a")) CBDAdder.addConnection("in1Add", "a") CBDAdder.addConnection("in2Add", "a") CBDAdder.addConnection("a", "outAdd") CBDProduct = CBD("productCBD", input_ports=["in1Prod", "in2Prod"], output_ports=["outProd"]) CBDProduct.addBlock(ProductBlock(block_name="p")) CBDProduct.addConnection("in1Prod", "p") CBDProduct.addConnection("in2Prod", "p") CBDProduct.addConnection("p", "outProd") self.CBD.addBlock(CBDConstant1) self.CBD.addBlock(CBDConstant2) self.CBD.addBlock(CBDAdder) self.CBD.addBlock(CBDProduct) self.CBD.addConnection("constantCBD1", "adderCBD", input_port_name="in1Add", output_port_name="outConstant1") self.CBD.addConnection("productCBD", "adderCBD", input_port_name="in2Add", output_port_name="outProd") self.CBD.addConnection("adderCBD", "productCBD", input_port_name="in1Prod", output_port_name="outAdd") self.CBD.addConnection("constantCBD2", "productCBD", input_port_name="in2Prod", output_port_name="outConstant2") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("adderCBD.outAdd"), [-0.75] * 5) self.assertEquals(self._getSignal("productCBD.outProd"), [-3.75] * 5)
def testInterInterCBD(self): """ +--------------------------------------------+ +----+ | +------+ | | |+--->|+------------------>| | | | 2 | || | + +--------------->+-------+ +----+ +----+ || +---------+ +-->| | | | | | |+>| | | +------+ | v | 5 | +------+ | | | | | +------+ +----++->| | | | * | | | | | | + | | | | | | | + +------> 0 +----++->| |+--->|+>| | | | | | | 2 | +------+ | +--+------+ +--------------+ | +------+ | | | | | +-----+ | ^ +----+ | | +--------------------+| | | | | | | | +-----+ || | 12 +->+-------+ | | | | | || | | | | +-->+---->| - +------->++ +-----+ | | | | | | | | | +-----+ | | | +--------------------+ | +--------------------------------------------+ """ CBDLittle1 = CBD("first_child", input_ports=["in1CBD1", "in2CBD1"], output_ports=["out1CBD1", "out2CBD1"]) CBDLittle2 = CBD("first_child_of_first_child", input_ports=["inCBD2"], output_ports=["outCBD2"]) self.CBD.addBlock(ConstantBlock(block_name="c1", value=2.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.0)) self.CBD.addBlock(ConstantBlock(block_name="c3", value=2.0)) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addBlock(AdderBlock(block_name="a2")) self.CBD.addBlock(CBDLittle1) self.CBD.addConnection("c3", "a") self.CBD.addConnection("c2", "a") self.CBD.addConnection("c1", "first_child", input_port_name="in1CBD1") self.CBD.addConnection("a", "first_child", input_port_name="in2CBD1") self.CBD.addConnection("first_child", "a2", output_port_name="out1CBD1") self.CBD.addConnection("first_child", "a2", output_port_name="out2CBD1") CBDLittle1.addBlock(ProductBlock(block_name="p")) CBDLittle1.addBlock(AdderBlock(block_name="a")) CBDLittle1.addBlock(CBDLittle2) CBDLittle1.addBlock(ConstantBlock(block_name="c", value=12.0)) CBDLittle1.addConnection("in1CBD1", "p") CBDLittle1.addConnection("in2CBD1", "p") CBDLittle1.addConnection("in1CBD1", "a") CBDLittle1.addConnection("first_child_of_first_child", "a", output_port_name="outCBD2") CBDLittle1.addConnection("p", "first_child_of_first_child", input_port_name="inCBD2") CBDLittle1.addConnection("c", "out1CBD1") CBDLittle1.addConnection("a", "out2CBD1") CBDLittle2.addBlock(NegatorBlock(block_name="n")) CBDLittle2.addConnection("inCBD2", "n") CBDLittle2.addConnection("n", "outCBD2") self.CBD.flatten() self._run(5) self.assertEquals(self._getSignal("first_child.p"), [14.0] * 5) self.assertEquals( self._getSignal("first_child.first_child_of_first_child.n"), [-14.0] * 5) self.assertEquals(self._getSignal("first_child.in1CBD1"), [2.0] * 5) self.assertEquals(self._getSignal("first_child.a"), [-12.0] * 5) self.assertEquals(self._getSignal("a2"), [0.0] * 5)
def initializeFuncInt(self): #f(t) = 2*t CBDFunc = CBD("function", output_ports=["OUT1"]) CBDFunc.addBlock(TimeBlock(block_name="t")) CBDFunc.addBlock(ProductBlock(block_name="p")) CBDFunc.addBlock(ConstantBlock(block_name="c", value=2.0)) CBDFunc.addConnection("t", "p") CBDFunc.addConnection("c", "p") CBDFunc.addConnection("p", "OUT1") return CBDFunc
def testPlant(): cbd = CBD("CBD", output_ports=["vtrain","vpassenger","xtrain","xpassenger"]) cbd.addBlock(PlantCBD("Plant")) cbd.addBlock(ConstantBlock("F_TRACTION_IN", 2)) cbd.addBlock(ConstantBlock("DELTA_IN", 1)) cbd.addConnection("F_TRACTION_IN", "Plant", input_port_name="F_TRACTION") cbd.addConnection("DELTA_IN", "Plant", input_port_name="DELTA") cbd.addConnection("Plant", "vtrain", output_port_name="V_TRAIN") cbd.addConnection("Plant", "xtrain", output_port_name="X_TRAIN") cbd.addConnection("Plant", "vpassenger", output_port_name="V_PASSENGER") cbd.addConnection("Plant", "xpassenger", output_port_name="X_PASSENGER") cbd.run(20) p = plot(cbd, "vtrain") show(p) p = plot(cbd, "xtrain") show(p) p = plot(cbd, "vpassenger") show(p) p = plot(cbd, "xpassenger") show(p)
def setUp(self): self.CBD = CBD("CBD_for_block_under_test")
class BasicCBDTestCase(unittest.TestCase): def setUp(self): self.CBD = CBD("CBD_for_block_under_test") def _run(self, num_steps=1, delta_t=1.0): self.CBD.run(num_steps, delta_t) def _getSignal(self, blockname, output_port=None): foundBlocks = [ block for block in self.CBD.getBlocks() if block.getBlockName() == blockname ] numFoundBlocks = len(foundBlocks) if numFoundBlocks == 1: signal = foundBlocks[0].getSignal(name_output=output_port) return [x.value for x in signal] else: raise Exception( str(numFoundBlocks) + " blocks with name " + blockname + " found.\nExpected a single block.") def testConstantBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=3.3)) self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("c1"), [3.3] * NUM_DISCR_TIME_STEPS) def testNegatorBlockPos(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=6.0)) self.CBD.addBlock(NegatorBlock(block_name="n")) self.CBD.addConnection("c1", "n") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("n"), [-6.0] * NUM_DISCR_TIME_STEPS) def testNegatorBlockNeg(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=-6.0)) self.CBD.addBlock(NegatorBlock(block_name="n")) self.CBD.addConnection("c1", "n") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("n"), [6.0] * NUM_DISCR_TIME_STEPS) def testNegatorBlockZero(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=0.0)) self.CBD.addBlock(NegatorBlock(block_name="n")) self.CBD.addConnection("c1", "n") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("n"), [0.0] * NUM_DISCR_TIME_STEPS) def testInverterBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=5.0)) self.CBD.addBlock(InverterBlock(block_name="i1")) self.CBD.addBlock(InverterBlock(block_name="i2")) self.CBD.addConnection("c1", "i1") self.CBD.addConnection("i1", "i2") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("i1"), [0.2] * NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("i2"), [5.0] * NUM_DISCR_TIME_STEPS) def testAdderBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=2.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=6.0)) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addConnection("c1", "a") self.CBD.addConnection("c2", "a") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("a"), [8.0] * NUM_DISCR_TIME_STEPS) def testAdderBlock2(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=2.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=-6.0)) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addConnection("c1", "a") self.CBD.addConnection("c2", "a") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("a"), [-4.0] * NUM_DISCR_TIME_STEPS) def testProductBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=2.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.0)) self.CBD.addBlock(ProductBlock(block_name="p")) self.CBD.addConnection("c1", "p") self.CBD.addConnection("c2", "p") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("p"), [10.0] * 5) def testProductBlock2(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=1.0 / 2.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=4.0)) self.CBD.addBlock(ProductBlock(block_name="p")) self.CBD.addConnection("c1", "p") self.CBD.addConnection("c2", "p") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("p"), [2.0] * 5) def testGenericBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=2.2)) self.CBD.addBlock(GenericBlock(block_name="g", block_operator="ceil")) self.CBD.addConnection("c1", "g") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("g"), [3.0] * 5) def testRootBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=8.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=3.0)) self.CBD.addBlock(RootBlock(block_name="g")) self.CBD.addConnection("c1", "g") self.CBD.addConnection("c2", "g") self._run(1) self.assertEqual(self._getSignal("g"), [2.0]) def testRootBlock2(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=9.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=2.0)) self.CBD.addBlock(RootBlock(block_name="g")) self.CBD.addConnection("c1", "g") self.CBD.addConnection("c2", "g") self._run(1) self.assertEqual(self._getSignal("g"), [3.0]) def testModuloBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=8.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=3.0)) self.CBD.addBlock(ModuloBlock(block_name="g")) self.CBD.addConnection("c1", "g") self.CBD.addConnection("c2", "g") self._run(1) self.assertEqual(self._getSignal("g"), [2.0]) def testModuloBlock2(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=8.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=8.0)) self.CBD.addBlock(ModuloBlock(block_name="g")) self.CBD.addConnection("c1", "g") self.CBD.addConnection("c2", "g") self._run(1) self.assertEqual(self._getSignal("g"), [0.0]) def testPreviousValueDelayBlock(self): self.CBD.addBlock(ConstantBlock(block_name="ZeroConstant", value=0.0)) self.CBD.addBlock( SequenceBlock(block_name="seq", sequence=[0, 2, 4, 6, 8, 10, 12])) self.CBD.addBlock(DelayBlock(block_name="d")) self.CBD.addConnection("ZeroConstant", "d", input_port_name="IC") self.CBD.addConnection("seq", "d") self._run(7, 0.5) self.assertEqual(self._getSignal("d"), [0, 0, 2, 4, 6, 8, 10]) def testPreviousValueDelayBlock2(self): self.CBD.addBlock( SequenceBlock(block_name="FirstSeq", sequence=[2, 12, 22, 23, 32, 11, 91])) self.CBD.addBlock( SequenceBlock(block_name="SecSeq", sequence=[5, 5, 5, 5, 3, 3, 3])) self.CBD.addBlock(DelayBlock(block_name="prev")) self.CBD.addConnection(self.CBD.getBlockByName("FirstSeq"), "prev") self.CBD.addConnection(self.CBD.getBlockByName("SecSeq"), "prev", input_port_name="IC") self._run(7) self.assertEqual(self._getSignal("prev"), [5, 2, 12, 22, 23, 32, 11]) def testTimeBlock(self): self.CBD.addBlock(TimeBlock(block_name="t")) self._run(4) self.assertEqual(self._getSignal("t"), [0.0, 1.0, 2.0, 3.0]) def testSequenceBlock(self): self.CBD.addBlock( SequenceBlock(block_name="FirstSeq", sequence=[2, 2, 2, 3, 2, 1, 1])) self._run(7) self.assertEqual(self._getSignal("FirstSeq"), [2, 2, 2, 3, 2, 1, 1]) def testLoggingBlock(self): self.CBD.addBlock(ConstantBlock(block_name="One", value=1)) self.CBD.addBlock( LoggingBlock("Test", "Logging block test with level is error", level.ERROR)) self.CBD.addConnection("One", "Test") self._run(1) def testLinearStrongComponent(self): self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.5)) self.CBD.addBlock(ConstantBlock(block_name="c1", value=-5)) self.CBD.addBlock(AdderBlock(block_name="a1")) self.CBD.addBlock(AdderBlock(block_name="a3")) self.CBD.addBlock(AdderBlock(block_name="a2")) self.CBD.addConnection("a3", "a1") self.CBD.addConnection("c1", "a1") self.CBD.addConnection("a1", "a3") self.CBD.addConnection("a2", "a3") self.CBD.addConnection("c2", "a2") self.CBD.addConnection("a3", "a2") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("a1"), [-5.5] * 5) self.assertEqual(self._getSignal("a2"), [5.0] * 5) self.assertEqual(self._getSignal("a3"), [-0.5] * 5) def testLinearStrongComponentWithDelay(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=3.0)) self.CBD.addBlock(AdderBlock(block_name="sum")) self.CBD.addBlock(DelayBlock(block_name="delay")) self.CBD.addBlock(NegatorBlock(block_name="neg")) self.CBD.addConnection("c1", "sum") self.CBD.addConnection("neg", "sum") self.CBD.addConnection("sum", "delay", input_port_name="IC") self.CBD.addConnection("delay", "neg") self.CBD.addConnection("neg", "delay") self._run(1) self.assertEqual(self._getSignal("delay"), [1.5]) def testLinearStrongComponentWithMult(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=3)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5)) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addBlock(ProductBlock(block_name="p")) self.CBD.addConnection("c1", "a") self.CBD.addConnection("p", "a") self.CBD.addConnection("a", "p") self.CBD.addConnection("c2", "p") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("a"), [-0.75] * 5) self.assertEqual(self._getSignal("p"), [-3.75] * 5) def testLinearStrongComponentWithNeg(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=5)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=8)) self.CBD.addBlock(AdderBlock(block_name="a1")) self.CBD.addBlock(AdderBlock(block_name="a2")) self.CBD.addBlock(NegatorBlock(block_name="n")) self.CBD.addConnection("c1", "a1") self.CBD.addConnection("a2", "a1") self.CBD.addConnection("c2", "a2") self.CBD.addConnection("n", "a2") self.CBD.addConnection("a1", "n") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("a1"), [6.5] * 5) self.assertEqual(self._getSignal("a2"), [1.5] * 5) self.assertEqual(self._getSignal("n"), [-6.5] * 5) def testTwoLinearStrongComponent(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=3)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=2)) self.CBD.addBlock(ConstantBlock(block_name="c3", value=1.5)) self.CBD.addBlock(ConstantBlock(block_name="c4", value=1)) self.CBD.addBlock(AdderBlock(block_name="a1")) self.CBD.addBlock(AdderBlock(block_name="a2")) self.CBD.addBlock(AdderBlock(block_name="a3")) self.CBD.addBlock(AdderBlock(block_name="a4")) self.CBD.addBlock(AdderBlock(block_name="a5")) self.CBD.addBlock(ProductBlock(block_name="p")) self.CBD.addBlock(NegatorBlock(block_name="n1")) self.CBD.addBlock(NegatorBlock(block_name="n2")) self.CBD.addConnection("a3", "a1") self.CBD.addConnection("c1", "a1") self.CBD.addConnection("c2", "a2") self.CBD.addConnection("a3", "a2") self.CBD.addConnection("a1", "a3") self.CBD.addConnection("a2", "a3") self.CBD.addConnection("a3", "p") self.CBD.addConnection("c3", "p") self.CBD.addConnection("p", "n1") self.CBD.addConnection("n1", "a4") self.CBD.addConnection("a5", "a4") self.CBD.addConnection("c4", "a5") self.CBD.addConnection("n2", "a5") self.CBD.addConnection("a4", "n2") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("a1"), [-2.0] * 5) self.assertEqual(self._getSignal("a2"), [-3.0] * 5) self.assertEqual(self._getSignal("a3"), [-5.0] * 5) self.assertEqual(self._getSignal("a4"), [4.25] * 5) self.assertEqual(self._getSignal("a5"), [-3.25] * 5) self.assertEqual(self._getSignal("p"), [-7.5] * 5) self.assertEqual(self._getSignal("n1"), [7.5] * 5) self.assertEqual(self._getSignal("n2"), [-4.25] * 5) def testNonLinearStrongComponent(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=15)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=10)) self.CBD.addBlock(AdderBlock(block_name="a1")) self.CBD.addBlock(AdderBlock(block_name="a2")) self.CBD.addBlock(ProductBlock(block_name="p")) self.CBD.addConnection("c2", "a1") self.CBD.addConnection("p", "a1") self.CBD.addConnection("a1", "p") self.CBD.addConnection("a2", "p") self.CBD.addConnection("p", "a2") self.CBD.addConnection("c1", "a2") self.assertRaises(SystemExit, self._run, 5) def initializeFuncDerBas(self): #f(t) = 5*t CBDFunc = CBD("function", output_ports=["OUT1"]) CBDFunc.addBlock(TimeBlock(block_name="t")) CBDFunc.addBlock(ProductBlock(block_name="p")) CBDFunc.addBlock(ConstantBlock(block_name="c", value=5.0)) CBDFunc.addConnection("t", "p") CBDFunc.addConnection("c", "p") CBDFunc.addConnection("p", "OUT1") return CBDFunc def initializeFunc(self): #f(t) = (t-2)^3 CBDFunc = CBD("function", output_ports=["OUT1"]) CBDFunc.addBlock(TimeBlock(block_name="t")) CBDFunc.addBlock(ProductBlock(block_name="p")) CBDFunc.addBlock(ProductBlock(block_name="p2")) CBDFunc.addBlock(AdderBlock(block_name="a")) CBDFunc.addBlock(ConstantBlock(block_name="c", value=-2.0)) CBDFunc.addConnection("t", "a") CBDFunc.addConnection("c", "a") CBDFunc.addConnection("a", "p") CBDFunc.addConnection("a", "p") CBDFunc.addConnection("p", "p2") CBDFunc.addConnection("a", "p2") CBDFunc.addConnection("p2", "OUT1") return CBDFunc def initializeFunc2(self): #f(t) = (t-2)^4 CBDFunc = CBD("function", output_ports=["OUT1"]) CBDFunc.addBlock(TimeBlock(block_name="t")) CBDFunc.addBlock(ProductBlock(block_name="p")) CBDFunc.addBlock(ProductBlock(block_name="p2")) CBDFunc.addBlock(ProductBlock(block_name="p3")) CBDFunc.addBlock(AdderBlock(block_name="a")) CBDFunc.addBlock(ConstantBlock(block_name="c", value=-2.0)) CBDFunc.addConnection("t", "a") CBDFunc.addConnection("c", "a") CBDFunc.addConnection("a", "p") CBDFunc.addConnection("a", "p") CBDFunc.addConnection("p", "p2") CBDFunc.addConnection("a", "p2") CBDFunc.addConnection("p2", "p3") CBDFunc.addConnection("a", "p3") CBDFunc.addConnection("p3", "OUT1") return CBDFunc def initializeFuncInt(self): #f(t) = 2*t CBDFunc = CBD("function", output_ports=["OUT1"]) CBDFunc.addBlock(TimeBlock(block_name="t")) CBDFunc.addBlock(ProductBlock(block_name="p")) CBDFunc.addBlock(ConstantBlock(block_name="c", value=2.0)) CBDFunc.addConnection("t", "p") CBDFunc.addConnection("c", "p") CBDFunc.addConnection("p", "OUT1") return CBDFunc def testDerivatorBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c3", value=1.0)) self.CBD.addBlock(ConstantBlock(block_name="zero", value=0.0)) CBDFunc = self.initializeFuncDerBas() self.CBD.addBlock(CBDFunc) self.CBD.addBlock(DerivatorBlock(block_name="der")) self.CBD.addConnection("c3", "der", input_port_name="delta_t") self.CBD.addConnection("zero", "der", input_port_name="IC") self.CBD.addConnection("function", "der") self._run(5) self.assertEqual(self._getSignal("der"), [0.0] + [5.0] * 4) def testIntegratorBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=6.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=0.0)) self.CBD.addBlock(ConstantBlock(block_name="c3", value=0.001)) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addBlock(DelayBlock(block_name="d")) self.CBD.addBlock(IntegratorBlock(block_name="int")) self.CBD.addConnection("c3", "int", input_port_name="delta_t") self.CBD.addConnection("a", "int") self.CBD.addConnection("c2", "int", input_port_name="IC") self.CBD.addConnection("c1", "a") self.CBD.addConnection("d", "a") self.CBD.addConnection("a", "d") self.CBD.addConnection("c2", "d", input_port_name="IC") self._run(NUM_DISCR_TIME_STEPS) self.assertEqual(self._getSignal("int"), [ 0.0, 0.006, 0.018000000000000002, 0.036000000000000004, 0.060000000000000005 ]) def testDelayBlock(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=5.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=3.0)) self.CBD.addBlock(DelayBlock(block_name="d")) self.CBD.addConnection("c2", "d") self.CBD.addConnection("c1", "d", input_port_name="IC") self._run(4) self.assertEqual(self._getSignal("d"), [5.0, 3.0, 3.0, 3.0]) def testDelayBlock2(self): self.CBD.addBlock(ConstantBlock(block_name="c1", value=1.0)) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.0)) self.CBD.addBlock(DelayBlock(block_name="d")) self.CBD.addBlock(AdderBlock(block_name="a")) self.CBD.addConnection("c2", "a") self.CBD.addConnection("d", "a") self.CBD.addConnection("c1", "d", input_port_name="IC") self.CBD.addConnection("a", "d") self._run(5) self.assertEqual(self._getSignal("d"), [1.0, 6.0, 11.0, 16.0, 21.0])
class SortedGraphCBDTest(unittest.TestCase): def setUp(self): self.CBD = CBD("block_under_test") def _run(self, num_steps=1): self.CBD.run(num_steps) def testSortedGraph(self): CBDNegator = CBD("negatorCBD", input_ports = ["inNegator"], output_ports = ["outNegator"]) negCbd = NegatorBlock(block_name="nC") CBDNegator.addBlock(negCbd) CBDNegator.addConnection("inNegator", "nC") CBDNegator.addConnection("nC", "outNegator") const = ConstantBlock(block_name="c", value=5.5) self.CBD.addBlock(const) neg = NegatorBlock(block_name="n") self.CBD.addBlock(neg) self.CBD.addBlock(CBDNegator) self.CBD.addConnection("negatorCBD", "n", output_port_name = "outNegator") self.CBD.addConnection("c", "negatorCBD", input_port_name = "inNegator") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() self.assertEqual(len(sortedGraph), 5) self.assertEqual(sortedGraph[0][0], const) self.assertEqual(sortedGraph[2][0], negCbd) self.assertEqual(sortedGraph[4][0], neg) def testSortedGraph2(self): CBDAdder = CBD("adderCBD", input_ports = ["in1", "in2"], output_ports = ["outAdd"]) addCBD = AdderBlock(block_name="aC") CBDAdder.addBlock(addCBD) CBDAdder.addConnection("in1", "aC") CBDAdder.addConnection("in2", "aC") CBDAdder.addConnection("aC", "outAdd") const1 = ConstantBlock(block_name="c1", value=5.5) const2 = ConstantBlock(block_name="c2", value=4.5) self.CBD.addBlock(const1) self.CBD.addBlock(const2) neg = NegatorBlock(block_name="n") self.CBD.addBlock(neg) self.CBD.addBlock(CBDAdder) self.CBD.addConnection("adderCBD", "n", output_port_name = "outAdd") self.CBD.addConnection("c1", "adderCBD", input_port_name = "in1") self.CBD.addConnection("c2", "adderCBD", input_port_name = "in2") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() comps = [ x[0] for x in sortedGraph ] tester = self ag = lambda x,y: tester.assertTrue(comps.index(x) > comps.index(y)) self.assertEqual(len(sortedGraph), 7) ag(addCBD, const1) ag(addCBD, const2) ag(neg, addCBD) def testSortedGraph3(self): CBDNegator = CBD("negatorCBD", input_ports = ["inNegator"], output_ports = ["outNegator", "outInverter"]) negCbd = NegatorBlock(block_name="nC") invCBD = InverterBlock(block_name="iC") CBDNegator.addBlock(negCbd) CBDNegator.addBlock(invCBD) CBDNegator.addConnection("inNegator", "nC") CBDNegator.addConnection("inNegator", "iC") CBDNegator.addConnection("nC", "outNegator") CBDNegator.addConnection("iC", "outInverter") const = ConstantBlock(block_name="c", value=5.5) self.CBD.addBlock(const) add = AdderBlock(block_name="a") self.CBD.addBlock(add) self.CBD.addBlock(CBDNegator) self.CBD.addConnection("negatorCBD", "a", output_port_name = "outNegator") self.CBD.addConnection("negatorCBD", "a", output_port_name = "outInverter") self.CBD.addConnection("c", "negatorCBD", input_port_name = "inNegator") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() comps = [ x[0] for x in sortedGraph ] tester = self ag = lambda x,y: tester.assertTrue(comps.index(x) > comps.index(y)) self.assertEqual(len(sortedGraph), 7) ag(negCbd, const) ag(invCBD, const) ag(add, negCbd) ag(add, invCBD) def testSortedGraph4(self): CBDStrong = CBD("strongCBD", input_ports = ["inC1", "inC2"], output_ports = []) CBDStrong.addBlock(AdderBlock(block_name="a1")) CBDStrong.addBlock(AdderBlock(block_name="a3")) CBDStrong.addBlock(AdderBlock(block_name="a2")) CBDStrong.addConnection("a3", "a1") CBDStrong.addConnection("a1", "a3") CBDStrong.addConnection("a2", "a3") CBDStrong.addConnection("inC1", "a1") CBDStrong.addConnection("inC2", "a2") CBDStrong.addConnection("a3", "a2") self.CBD.addBlock(CBDStrong) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.5)) self.CBD.addBlock(ConstantBlock(block_name="c1", value=-5)) self.CBD.addConnection("c1", "strongCBD", input_port_name = "inC1") self.CBD.addConnection("c2", "strongCBD", input_port_name = "inC2") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() self.assertEqual(len(sortedGraph), 5) self.assertEqual(len(sortedGraph[4]), 3) def testSortedGraph5(self): CBDStrong = CBD("strongCBD", input_ports = ["inC1", "inC2", "inA"], output_ports = ["out1", "out2"]) CBDStrong.addBlock(AdderBlock(block_name="a1")) CBDStrong.addBlock(AdderBlock(block_name="a2")) CBDStrong.addConnection("inA", "a1") CBDStrong.addConnection("a1", "out1") CBDStrong.addConnection("a2", "out2") CBDStrong.addConnection("inC1", "a1") CBDStrong.addConnection("inC2", "a2") CBDStrong.addConnection("inA", "a2") self.CBD.addBlock(CBDStrong) self.CBD.addBlock(AdderBlock(block_name="a3")) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.5)) self.CBD.addBlock(ConstantBlock(block_name="c1", value=-5)) self.CBD.addConnection("c1", "strongCBD", input_port_name = "inC1") self.CBD.addConnection("c2", "strongCBD", input_port_name = "inC2") self.CBD.addConnection("a3", "strongCBD", input_port_name = "inA") self.CBD.addConnection("strongCBD", "a3", output_port_name = "out1") self.CBD.addConnection("strongCBD", "a3", output_port_name = "out2") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() self.assertEqual(len(sortedGraph), 5) self.assertEqual(len(sortedGraph[4]), 6)
def testSortedGraph5(self): CBDStrong = CBD("strongCBD", input_ports = ["inC1", "inC2", "inA"], output_ports = ["out1", "out2"]) CBDStrong.addBlock(AdderBlock(block_name="a1")) CBDStrong.addBlock(AdderBlock(block_name="a2")) CBDStrong.addConnection("inA", "a1") CBDStrong.addConnection("a1", "out1") CBDStrong.addConnection("a2", "out2") CBDStrong.addConnection("inC1", "a1") CBDStrong.addConnection("inC2", "a2") CBDStrong.addConnection("inA", "a2") self.CBD.addBlock(CBDStrong) self.CBD.addBlock(AdderBlock(block_name="a3")) self.CBD.addBlock(ConstantBlock(block_name="c2", value=5.5)) self.CBD.addBlock(ConstantBlock(block_name="c1", value=-5)) self.CBD.addConnection("c1", "strongCBD", input_port_name = "inC1") self.CBD.addConnection("c2", "strongCBD", input_port_name = "inC2") self.CBD.addConnection("a3", "strongCBD", input_port_name = "inA") self.CBD.addConnection("strongCBD", "a3", output_port_name = "out1") self.CBD.addConnection("strongCBD", "a3", output_port_name = "out2") depGraph = self.CBD._CBD__createDepGraph(0) sortedGraph = depGraph.getStrongComponents() self.assertEqual(len(sortedGraph), 5) self.assertEqual(len(sortedGraph[4]), 6)
class FlattenCBDTest(unittest.TestCase): def setUp(self): self.CBD = CBD("block_under_test") def _run(self, num_steps=1, step=1): self.CBD.run(num_steps, step) def _getSignal(self, blockname, output_port=None): foundBlocks = [ block for block in self.CBD.getBlocks() if block.getBlockName() == blockname ] signal = foundBlocks[0].getSignal(name_output=output_port) if len(foundBlocks) == 1: return [x.value for x in signal] else: raise Exception("No single block with name " + blockname + " found") def testCounter(self): self.CBD.addBlock(ConstantBlock(block_name="zero", value=0.0)) self.CBD.addBlock(DelayBlock(block_name="s")) self.CBD.addConnection("zero", "s", input_port_name="IC") self.CBD.addBlock(ConstantBlock(block_name="one", value=1.0)) self.CBD.addBlock(AdderBlock(block_name="plusOne")) self.CBD.addConnection("one", "plusOne") self.CBD.addConnection("s", "plusOne") self.CBD.addConnection("plusOne", "s") self._run(NUM_DISCR_TIME_STEPS) ScopeWindow([ self._getSignal("zero"), self._getSignal("one"), self._getSignal("s") ], ["zero", "one", "s"]) self.assertEquals(self._getSignal("s"), [float(x) for x in range(NUM_DISCR_TIME_STEPS)])
def setUp(self): self.CBD = CBD("block_under_test")
def initializeFunc2(self): #f(t) = (t-2)^4 CBDFunc = CBD("function", output_ports=["OUT1"]) CBDFunc.addBlock(TimeBlock(block_name="t")) CBDFunc.addBlock(ProductBlock(block_name="p")) CBDFunc.addBlock(ProductBlock(block_name="p2")) CBDFunc.addBlock(ProductBlock(block_name="p3")) CBDFunc.addBlock(AdderBlock(block_name="a")) CBDFunc.addBlock(ConstantBlock(block_name="c", value=-2.0)) CBDFunc.addConnection("t", "a") CBDFunc.addConnection("c", "a") CBDFunc.addConnection("a", "p") CBDFunc.addConnection("a", "p") CBDFunc.addConnection("p", "p2") CBDFunc.addConnection("a", "p2") CBDFunc.addConnection("p2", "p3") CBDFunc.addConnection("a", "p3") CBDFunc.addConnection("p3", "OUT1") return CBDFunc