rparedis vor 2 Jahren
Ursprung
Commit
256f8d445c
5 geänderte Dateien mit 61 neuen und 15 gelöschten Zeilen
  1. BIN
      CBD.zip
  2. 9 1
      src/pyCBD/Core.py
  3. 6 4
      src/pyCBD/converters/latexify/CBD2Latex.py
  4. 23 10
      src/pyCBD/depGraph.py
  5. 23 0
      src/pyCBD/lib/std.py

BIN
CBD.zip


+ 9 - 1
src/pyCBD/Core.py

@@ -132,6 +132,14 @@ class Port:
         """
         self.__history.pop()
 
+    def getDependencies(self, _):
+        if self.direction == Port.Direction.OUT:
+            return [self.getPreviousPortClosure()]
+        return []
+
+    def getPath(self, sep):
+        return self.block.getPath(sep) + sep + self.name
+
     @staticmethod
     def connect(source, target):
         """
@@ -511,7 +519,7 @@ class BaseBlock:
             # Connects output to output -- only in parent-child connection!
             target = self.getOutputPortByName(name_input)
         else:
-            raise ValueError("Could not connect ports!")
+            raise ValueError("Could not connect ports %s and %s!" % (name_input, name_output))
         Port.connect(source, target)
 
     def unlinkInput(self, name_input):

+ 6 - 4
src/pyCBD/converters/latexify/CBD2Latex.py

@@ -45,7 +45,8 @@ class CBD2Latex:
 								See Also:
 									:attr:`pyCBD.converters.latexify.functions.BLOCK_MAP`.
 		path_sep (str):         The separator to use for the paths. Defaults to :code:`"."`.
-		path_prefix (str):      The prefix to use for the paths. Defaults to the empty strig.
+		path_prefix (str):      The prefix to use for the paths. Defaults to the empty string.
+		ignore (iterable):      A list of block classes not to flatten.
 	"""
 
 	DEFAULT_CONFIG = {
@@ -59,18 +60,19 @@ class CBD2Latex:
 		"replace_par": True,
 		"type_formats": {},
 		"path_sep": '.',
-		"path_prefix": ''
+		"path_prefix": '',
+		"ignore": []
 	}
 	"""Default configuration setup."""
 
 	def __init__(self, model, **kwargs):
-		self.model = model.flattened()
 		self.config = self.DEFAULT_CONFIG
-
 		for k in kwargs:
 			if k in self.config:
 				self.config[k] = kwargs[k]
 
+		self.model = model.flattened(ignore=self.config["ignore"])
+
 		self.equations = []
 		self.outputs = [self._rename(self.model.getPath(self.config['path_sep']) + self.config['path_sep'] + x) for x in self.model.getSignals().keys()]
 		assert len(self.outputs) > 0, "Cannot create latex representation without outputs."

+ 23 - 10
src/pyCBD/depGraph.py

@@ -64,10 +64,18 @@ class DepGraph:
 	def __repr__(self):
 		repr = "Dependents: \n"
 		for dep in self.__dependents:
-			repr += dep.getBlockName() + ":" + str(self.__dependents[dep]) + "\n"
+			if isinstance(dep, Port):
+				name = dep.name
+			else:
+				name = dep.getBlockName()
+			repr += name + ":" + str(self.__dependents[dep]) + "\n"
 		repr += "Influencers: \n"
 		for infl in self.__influencers:
-			repr += infl.getBlockName() + ":" + str(self.__influencers[infl]) + "\n"
+			if isinstance(infl, Port):
+				name = infl.name
+			else:
+				name = infl.getBlockName()
+			repr += name + ":" + str(self.__influencers[infl]) + "\n"
 		return repr
 
 	def addMember(self, object):
@@ -292,33 +300,38 @@ def createDepGraph(model, curIteration, ignore_hierarchy=False):
 	Create a dependency graph of the CBD model.
 	Use the curIteration to differentiate between the first and other iterations
 	Watch out for dependencies with sub-models.
+
+	Args:
+		model:              The model for which the dependency graph must be computed.
+		curIteration:       The current iteration for the dependency graph.
+		ignore_hierarchy:   Assume the CBD model is flat and don't call this recursively.
 	"""
 	blocks = model.getBlocks()
 	depGraph = DepGraph(ignore_hierarchy)
 
 	for block in blocks:
 		depGraph.addMember(block)
-	# for port in model.getInputPorts() + model.getOutputPorts():
-	# 	depGraph.addMember(port)
+	for port in model.getInputPorts() + model.getOutputPorts():
+		depGraph.addMember(port)
 
 	def recSetInternalDependencies(blocks, curIteration):
 		"""
 		Recursive call for setting the internal dependencies of the graph.
 
 		Args:
-			blocks (iter):      The list of blocks to set as depencency
+			blocks (iter):      The list of blocks to set as dependency
 			curIteration (int): The current iteration number
 		"""
 		for block in blocks:
 			for dep in block.getDependencies(curIteration):
-				# if dep.block == model:
-				# 	depGraph.setDependency(block, dep, curIteration)
-				# else:
-				depGraph.setDependency(block, dep.block, curIteration)
+				if dep.block == model:
+					depGraph.setDependency(block, dep, curIteration)
+				else:
+					depGraph.setDependency(block, dep.block, curIteration)
 			if isinstance(block, CBD) and not ignore_hierarchy:
 				recSetInternalDependencies(block.getBlocks(), curIteration)
 
-	recSetInternalDependencies(blocks, curIteration)
+	recSetInternalDependencies(blocks + model.getOutputPorts(), curIteration)
 
 	return depGraph
 

+ 23 - 0
src/pyCBD/lib/std.py

@@ -101,6 +101,29 @@ class InverterBlock(BaseBlock):
 		self.appendToSignal(1.0 / input)
 
 
+class GainBlock(BaseBlock):
+	"""
+	The gain will output the input, multiplied by a constant.
+
+	Arguments:
+		block_name (str):   The name of the block.
+		value (numeric):    The value of the block.
+
+	:Input Ports:
+		**IN1** -- The value to be multiplied.
+
+	:Output Ports:
+		**OUT1** -- The multiplied value.
+	"""
+	def __init__(self, block_name, value):
+		BaseBlock.__init__(self, block_name, ["IN1"], ["OUT1"])
+		self._value = value
+
+	def compute(self, curIteration):
+		input = self.getInputSignal(curIteration, "IN1").value
+		self.appendToSignal(input * self._value)
+
+
 class AdderBlock(BaseBlock):
 	"""
 	The adderblock will add all the inputs.