Browse Source

Swapped src/CBD directory names to allow for setup.py

rparedis 4 years ago
parent
commit
ec562b52c7

+ 3 - 3
examples/EvenNumberGen/EvenNumberGen.py

@@ -2,9 +2,9 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py EvenNumberGen.xml -Svarg -F CBD -e EvenNumberGen
 
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.lib.interface.plotting import SignalPlotBlock
 
 
 class Counter(CBD):

+ 11 - 19
examples/EvenNumberGen/EvenNumberGen_experiment.py

@@ -2,36 +2,28 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py EvenNumberGen.xml -Svfarg -F CBD -e EvenNumberGen
 
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.simulator import Simulator
 from EvenNumberGen import *
 import matplotlib.pyplot as plt
-import matplotlib.animation as animation
 
 
 fig = plt.figure(figsize=(15, 5), dpi=100)
 ax = fig.add_subplot(1, 1, 1)
 
 cbd = EvenNumberGen("EvenNumberGen", ax)
-cbd.setRealTime(True)
+plotter = cbd.getBlockByName("plot")
 
-opened = True
-def handle_close(evt):
-	global opened
-	opened = False
-fig.canvas.mpl_connect('close_event', handle_close)
-
-# Set the animation to be updateable
-ani = animation.FuncAnimation(fig, lambda _: cbd.getBlockByName("plot").animate(), interval=100)
-
-plt.ion()
-plt.show()
-
-def term(_):
+def term(*_):
 	plt.draw()
 	plt.pause(0.01)
-	return not opened
+	return not plotter.is_opened()
 
 
 # Run the Simulation
-cbd.setTerminationCondition(term)
-cbd.run(100)
+sim = Simulator(cbd)
+sim.setRealTime(True)
+sim.setTerminationCondition(term)
+plotter.start_animation(fig)
+sim.run(100.0)
+plotter.end_animation()

+ 3 - 3
examples/Fibonacci/Fibonacci.py

@@ -2,9 +2,9 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py Fibonacci.xml -Svarg -F CBD -e FibonacciGen
 
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.lib.interface.plotting import SignalPlotBlock
 
 
 class InitialConditions(CBD):

+ 11 - 19
examples/Fibonacci/Fibonacci_experiment.py

@@ -2,36 +2,28 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py Fibonacci.xml -Svfarg -F CBD -e FibonacciGen
 
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.simulator import Simulator
 from Fibonacci import *
 import matplotlib.pyplot as plt
-import matplotlib.animation as animation
 
 
 fig = plt.figure(figsize=(15, 5), dpi=100)
 ax = fig.add_subplot(1, 1, 1)
 
 cbd = FibonacciGen("FibonacciGen", ax)
-cbd.setRealTime(True)
+plotter = cbd.getBlockByName("plot")
 
-opened = True
-def handle_close(evt):
-	global opened
-	opened = False
-fig.canvas.mpl_connect('close_event', handle_close)
-
-# Set the animation to be updateable
-ani = animation.FuncAnimation(fig, lambda _: cbd.getBlockByName("plot").animate(), interval=100)
-
-plt.ion()
-plt.show()
-
-def term(_):
+def term(*_):
 	plt.draw()
 	plt.pause(0.01)
-	return not opened
+	return not plotter.is_opened()
 
 
 # Run the Simulation
-cbd.setTerminationCondition(term)
-cbd.run(100)
+sim = Simulator(cbd)
+sim.setRealTime(True)
+sim.setTerminationCondition(term)
+plotter.start_animation(fig)
+sim.run(100.0)
+plotter.end_animation()

+ 3 - 3
examples/LCG/LCG.py

@@ -2,9 +2,9 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py LCG.xml -av -F CBD -e LCG -t 100
 
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.lib.interface.plotting import SignalPlotBlock
 
 
 class LCG(CBD):

+ 11 - 19
examples/LCG/LCG_experiment.py

@@ -2,35 +2,27 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py LCG.xml -av -F CBD -e LCG -t 100
 
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.simulator import Simulator
 from LCG import *
 import matplotlib.pyplot as plt
-import matplotlib.animation as animation
 
 fig = plt.figure(figsize=(15, 5), dpi=100)
 ax = fig.add_subplot(1, 1, 1)
 
 cbd = LCG("LCG", axes=ax)
-cbd.setRealTime(True)
+plotter = cbd.getBlockByName("plot")
 
-opened = True
-def handle_close(evt):
-	global opened
-	opened = False
-fig.canvas.mpl_connect('close_event', handle_close)
-
-# Set the animation to be updateable
-ani = animation.FuncAnimation(fig, lambda _: cbd.getBlockByName("plot").animate(), interval=100)
-
-plt.ion()
-plt.show()
-
-def term(_):
+def term(*_):
 	plt.draw()
 	plt.pause(0.01)
-	return not opened
+	return not plotter.is_opened()
 
 
 # Run the Simulation
-cbd.setTerminationCondition(term)
-cbd.run(100)
+sim = Simulator(cbd)
+sim.setRealTime(True)
+sim.setTerminationCondition(term)
+plotter.start_animation(fig)
+sim.run(100.0)
+plotter.end_animation()

+ 3 - 3
examples/SinGen/SinGen.py

@@ -2,9 +2,9 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py SinGen.xml -fav -F CBD -e SinGen -E delta=0.1
 
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.lib.interface.plotting import SignalPlotBlock
 
 DELTA_T = 0.1
 

+ 10 - 5
examples/SinGen/SinGen_experiment.py

@@ -2,7 +2,8 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py SinGen.xml -fav -F CBD -e SinGen -E delta=0.1
 
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.simulator import Simulator
 from SinGen import *
 import matplotlib.pyplot as plt
 
@@ -13,22 +14,26 @@ ax = fig.add_subplot(1, 1, 1)
 # ax = None
 
 cbd = SinGen("SinGen", ax)
-cbd.setRealTime(True)
 
 plotter = cbd.getBlockByName("plot")
 
-def term(_):
+def term(*_):
 	plt.draw()
 	plt.pause(0.01)
 	return not plotter.is_opened()
 
 
 # Run the Simulation
-cbd.setTerminationCondition(term)
+sim = Simulator(cbd)
+sim.setRealTime(True)
+sim.setDeltaT(DELTA_T)
+sim.setTerminationCondition(term)
 plotter.start_animation(fig)
-log = cbd.run(int(10/DELTA_T), delta_t=DELTA_T)
+sim.run(10.0)
 plotter.end_animation()
 
+log = sim.getDurationLog()
+
 fig2 = plt.figure()
 ax2 = fig2.subplots()
 ax2.plot([-1, len(log)], [DELTA_T, DELTA_T], c='red')

+ 3 - 3
examples/StateEventLocation/StateEventLocation.py

@@ -2,9 +2,9 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py StateEventLocation.xml -av -F CBD -e Experiment -t 100 -E delta=0.1
 
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.lib.interface.plotting import SignalPlotBlock
 
 DELTA_T = 0.1
 

+ 14 - 19
examples/StateEventLocation/StateEventLocation_experiment.py

@@ -2,7 +2,8 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py StateEventLocation.xml -fav -F CBD -e Experiment -t 100 -E delta=0.1
 
-from CBD.src.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.simulator import Simulator
 from StateEventLocation import *
 import matplotlib.pyplot as plt
 import matplotlib.animation as animation
@@ -14,28 +15,22 @@ ax1 = fig.add_subplot(1, 1, 1)
 ax2 = ax1.twinx()
 
 cbd = Experiment("Experiment", ax1, ax2)
-cbd.setRealTime(True)
 
-opened = True
-def handle_close(evt):
-	global opened
-	opened = False
-fig.canvas.mpl_connect('close_event', handle_close)
+plotter1 = cbd.getBlockByName("valueplot")
+plotter2 = cbd.getBlockByName("eventplot")
 
-# Set the animation to be updateable
-
-ani1 = animation.FuncAnimation(fig, lambda _: cbd.getBlockByName("valueplot").animate(), interval=100)
-ani2 = animation.FuncAnimation(fig, lambda _: cbd.getBlockByName("eventplot").animate(), interval=100)
-
-plt.ion()
-plt.show()
-
-def term(_):
+def term(*_):
 	plt.draw()
 	plt.pause(0.01)
-	return not opened
+	return not plotter1.is_opened() and plotter2.is_opened()
 
 
 # Run the Simulation
-cbd.setTerminationCondition(term)
-cbd.run(int(100.0/DELTA_T), delta_t=DELTA_T)
+sim = Simulator(cbd)
+sim.setRealTime(True)
+sim.setTerminationCondition(term)
+plotter1.start_animation(fig)
+plotter2.start_animation(fig)
+sim.run(100.0)
+plotter1.end_animation()
+plotter2.end_animation()

+ 0 - 22
CBD/src/CBD.py

@@ -194,28 +194,6 @@ class SequenceBlock(BaseBlock):
         self.appendToSignal(self.__sequence[curIteration])
 
 
-class Clock:
-    """
-    The clock of the simulation
-    delta_t is the timestep of the simulation
-    """
-    def __init__(self, delta_t):
-        self.__delta_t = delta_t
-        self.__time = 0.0
-
-    def getTime(self):
-        return self.__time
-
-    def step(self):
-        self.__time = self.__time + self.__delta_t
-
-    def setDeltaT(self, new_delta_t):
-        self.__delta_t = new_delta_t
-
-    def getDeltaT(self):
-        return self.__delta_t
-
-
 class CBD(BaseBlock):
     """
     The CBD class, contains an entire Causal Block Diagram

+ 1 - 1
CBD/src/CBD2LaTeX.py

@@ -6,7 +6,7 @@ a :func:`latex()` member function can be added to all blocks to
 overwrite the given representation. Blocks for which no LaTeX is
 defined will be ignored in the generation.
 """
-from CBD.src.CBD import CBD
+from .CBD import CBD
 import re
 
 def latexify(cbd):

+ 1 - 1
CBD/src/CBDDraw.py

@@ -1,4 +1,4 @@
-from CBD.src.CBD import *
+from .CBD import *
 def draw(cbd, filename, colors=None):
 	""" Output cbd as a dot script to filename.
 

CBD/__init__.py → src/CBD/__init__.py


CBD/src/depGraph.py → src/CBD/depGraph.py


CBD/src/lib/__init__.py → src/CBD/lib/__init__.py


+ 1 - 1
CBD/src/lib/interface/plotting.py

@@ -1,4 +1,4 @@
-from CBD.src.CBD import BaseBlock
+from CBD.CBD import BaseBlock
 import matplotlib.animation as animation
 import matplotlib.pyplot as plt
 

CBD/src/lib/micropython/AGV.drawio → src/CBD/lib/micropython/AGV.drawio


+ 2 - 1
CBD/src/lib/micropython/AGV.py

@@ -2,7 +2,8 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/drawio2star/__main__.py -F CBD -v -E delta=0.1 -e AGV -d CBD/micropython/ CBD/micropython/AGV.drawio
 
-from CBD.src.CBD import *
+from CBD.CBD import *
+from CBD.lib.std import *
 from ev3 import _PI as PI
 from ev3 import *
 

+ 1 - 1
CBD/src/lib/micropython/ev3.py

@@ -2,7 +2,7 @@
 
 These can (and should) be expanded for a complete library.
 """
-from CBD.src.CBD import BaseBlock
+from CBD.CBD import BaseBlock
 from pybricks.ev3devices import Motor, ColorSensor
 from pybricks.parameters import Port
 

+ 1 - 0
CBD/src/lib/micropython/main.py

@@ -4,4 +4,5 @@ DELTA_T = 0.1
 
 # Run the simulation
 model = AGV("AGV")
+# TODO: make use of simulator
 model.run(int(500 / DELTA_T), DELTA_T)

+ 2 - 2
CBD/src/lib/std.py

@@ -1,8 +1,8 @@
 """
 This file contains the standard library for CBD building blocks.
 """
-from CBD.src.CBD import BaseBlock, CBD, level
-from CBD.src import naivelog
+from CBD.CBD import BaseBlock, CBD, level
+from CBD import naivelog
 import math
 
 __all__ = ['ConstantBlock', 'NegatorBlock', 'InverterBlock', 'AdderBlock', 'ProductBlock', 'ModuloBlock',

CBD/src/naivelog.py → src/CBD/naivelog.py


+ 10 - 4
CBD/src/simulator.py

@@ -24,6 +24,7 @@ class Clock:
 	def getDeltaT(self):
 		return self.__delta_t
 
+
 class Simulator:
 	def __init__(self, model):
 		self.model = model
@@ -40,18 +41,20 @@ class Simulator:
 		# TODO: make this variable, given more solver implementations
 		self.__solver = GaussianJordanLinearSolver(self.__logger)
 
-	def run(self):
+	def run(self, term_time=None):
 		"""
 		Simulate the model!
 		"""
 		self.model.setClock(Clock(self.__deltaT))
 		self.__lasttime = time.time()
+		if term_time is None:
+			term_time = self.__termination_time
 		depGraph = None
 		sortedGraph = None
-		__duration_log = []
+		self.__duration_log = []
 
 		curIteration = 0
-		while self.getClock().getTime() < self.__termination_time:
+		while self.getClock().getTime() < term_time:
 			if self.__termination_condition is not None and \
 				self.__termination_condition(self.model, curIteration):
 				break
@@ -66,7 +69,7 @@ class Simulator:
 				# Time is computed w.r.t. last time a step was executed to
 				# account for delays due to processor scheduling
 				duration = time.time() - self.__lasttime
-				__duration_log.append(duration)
+				self.__duration_log.append(duration)
 				time.sleep(max(0.0, self.__deltaT - duration))
 				self.__lasttime = time.time()
 
@@ -87,6 +90,9 @@ class Simulator:
 	def setTerminationTime(self, term_time):
 		self.__termination_time = term_time
 
+	def getDurationLog(self):
+		return self.__duration_log
+
 	def __step(self, depGraph, sortedGraph, curIteration):
 		self.__computeBlocks(sortedGraph, depGraph, curIteration)
 		self.getClock().setDeltaT(self.__deltaT)

CBD/src/simulator2.py → src/CBD/simulator2.py


CBD/src/solver.py → src/CBD/solver.py


CBD/src/__init__.py → src/__init__.py


+ 15 - 0
src/setup.py

@@ -0,0 +1,15 @@
+from distutils.core import setup
+
+setup(name="CBD",
+      version="0.1.0",
+      description="Python CBD simulator",
+      author=", ".join([
+	      "Marc Provost",
+	      "Hans Vangheluwe <Hans.Vangheluwe@uantwerpen.be>",
+	      "Joachim Denil <Joachim.Denil@uantwerpen.be>",
+	      "Claudio Gomes",
+	      "Randy Paredis <Randy.Paredis@uantwerpen.be>"
+      ]),
+      url="http://msdl.cs.mcgill.ca/people/rparedis",
+      packages=['CBD', 'CBD.lib']
+      )

CBD/test/__init__.py → src/test/__init__.py


+ 3 - 3
CBD/test/basicCBDTest.py

@@ -4,9 +4,9 @@
 
 import unittest
 
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.simulator import Simulator
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.simulator import Simulator
 
 NUM_DISCR_TIME_STEPS = 5
 

+ 5 - 5
CBD/test/flattenCBDTest.py

@@ -3,10 +3,10 @@
 # Unit tests for the flatten function on the CBD
 
 import unittest
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.simulator import Simulator
-from CBD.test.basicCBDTest import BasicCBDTestCase
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.simulator import Simulator
+from test.basicCBDTest import BasicCBDTestCase
 
 class FlattenCBDTest(unittest.TestCase):
 	def setUp(self):
@@ -15,7 +15,7 @@ class FlattenCBDTest(unittest.TestCase):
 		
 	def _run(self, num_steps=1, step = 1):
 		self.sim.setDeltaT(step)
-		self.sim.setTerminationTime(num_steps / step)
+		self.sim.setTerminationTime(num_steps * step)
 		self.sim.run()
 			
 	def _getSignal(self, blockname, output_port = None):

+ 5 - 5
CBD/test/hierarchyCBDTest.py

@@ -3,10 +3,10 @@
 # Unit tests for the hierarchy functionality of the CBD
 
 import unittest
-from CBD.src.CBD import *
-from CBD.src.lib.std import *
-from CBD.src.simulator import Simulator
-from CBD.test.basicCBDTest import BasicCBDTestCase
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.simulator import Simulator
+from test.basicCBDTest import BasicCBDTestCase
 
 class HierarchyCBDTest(unittest.TestCase):
 	def setUp(self):
@@ -15,7 +15,7 @@ class HierarchyCBDTest(unittest.TestCase):
 		
 	def _run(self, num_steps=1, step = 1):
 		self.sim.setDeltaT(step)
-		self.sim.setTerminationTime(num_steps / step)
+		self.sim.setTerminationTime(num_steps * step)
 		self.sim.run()
 
 	def _getSignal(self, blockname, output_port = None):

+ 5 - 5
CBD/test/sortedGraphCBDTest.py

@@ -3,11 +3,11 @@
 # Unit tests for the sorting of a CBD.
 
 import unittest
-from CBD.src.CBD import *
-from CBD.src.depGraph import createDepGraph
-from CBD.src.lib.std import *
-from CBD.src.simulator import Simulator
-from CBD.test.basicCBDTest import BasicCBDTestCase
+from CBD.CBD import *
+from CBD.lib.std import *
+from CBD.depGraph import createDepGraph
+from CBD.simulator import Simulator
+from test.basicCBDTest import BasicCBDTestCase
 
 class SortedGraphCBDTest(unittest.TestCase):
 	def setUp(self):

+ 3 - 2
test.sh

@@ -1,2 +1,3 @@
-python3 -m unittest discover -v CBD.test "*.py"
-#python -m unittest discover -v CBD.test "*.py"
+cd src  # Otherwise, the imports won't work (CBD is the root directory)
+python3 -m unittest discover -v test "*.py"
+#python -m unittest discover -v test "*.py"

+ 4 - 0
whishlist.txt

@@ -21,10 +21,12 @@
   ---> Add LaTeX generation of the flattened CBDs!
   
 * Custom LaTeX representation for custom blocks?
+  ---> Can be defined by implementing a latex() function
 
 * Generate FMU (in the near future)
 
 * Restructure simulator based on Claudio's version
+  ---> Complete restructure: done
 
 * In the future: refactor simulator to use internal (graph) structure from ModelverseState (MvS).
   This is the first step to full integration with the Modelverse. drawio will then be 
@@ -32,3 +34,5 @@
 
 * currently, remove graphViz (or keep for dependency graph)
   ---> The CBD class now has a "gvDepGraph(curIt)" function, which generates a graphviz file for the depgraph
+
+* Remake docs + docs to online sphinx hosting