def _specializeField(self, specializingPath=None): if specializingPath is None: specializingPath = SpecializingPath(memory=Memory()) # we retrieve the field definition domain domain = self.field.domain # and check it exists if domain is None: raise Exception( "No definition domain specified for field '{0}', cannnot parse the content against it." .format(self.field.name)) # we create a first VariableParser and uses it to parse the domain variableSpecializer = VariableSpecializer(domain) resultSpecializingPaths = variableSpecializer.specialize( specializingPath) for resultSpecializingPath in resultSpecializingPaths: assignedData = bitarray('') if resultSpecializingPath.isDataAvailableForVariable( self.field.domain): assignedData = resultSpecializingPath.getDataAssignedToVariable( self.field.domain) else: resultSpecializingPath.addResult(self.field.domain, assignedData) self._logger.debug( "FieldSpecializer Result: {0}".format(assignedData)) resultSpecializingPath.addResultToField(self.field, assignedData) return resultSpecializingPaths
def _specializeFieldWithChildren(self, specializingPath=None): if specializingPath is None: specializingPath = SpecializingPath(memory=Memory()) resultPaths = [specializingPath] for child in self.field.fields: fs = FieldSpecializer(child, presets=self.presets) tmpResultPaths = [] for path in resultPaths: tmpResultPaths.extend(fs.specialize(path)) resultPaths = tmpResultPaths for resultPath in resultPaths: value = None for child in self.field.fields: childResult = resultPath.getDataAssignedToVariable( child.domain) if value is None: value = childResult.copy() else: value += childResult.copy() resultPath.addResult(self.field.domain, value) resultPath.addResultToField(self.field, value) return resultPaths
def specializeSymbol(self, symbol): """This method generates a message based on the provided symbol definition.""" if symbol is None: raise Exception("Specified symbol is None") self._logger.debug("Specifies symbol '{0}'.".format(symbol.name)) # this variable host all the specialization path specializingPaths = [SpecializingPath(memory=self.memory)] for field in symbol.fields: self._logger.debug("Specializing field {0}".format(field.name)) fieldDomain = field.domain if fieldDomain is None: raise Exception("Cannot specialize field '{0}' since it defines no domain".format(fieldDomain)) fs = FieldSpecializer(field, presets = self.presets) newSpecializingPaths = [] for specializingPath in specializingPaths: newSpecializingPaths.extend(fs.specialize(specializingPath)) specializingPaths = newSpecializingPaths if len(specializingPaths) > 1: self._logger.info("TODO: multiple valid paths found when specializing this message.") if len(specializingPaths) == 0: raise Exception("Cannot specialize this symbol.") retainedPath = specializingPaths[0] generatedContent = None # let's configure the generated content for field in symbol.fields: # TODO: only support one level of children... must be improved if len(field.fields) > 0: d = None for child in field.fields: if d is None: d = retainedPath.getDataAssignedToVariable(child.domain).copy() else: d += retainedPath.getDataAssignedToVariable(child.domain).copy() else: d = retainedPath.getDataAssignedToVariable(field.domain) if generatedContent is None: generatedContent = d.copy() else: generatedContent += d.copy() retainedPath.generatedContent = generatedContent self._logger.debug("Specialized message: {0}".format(TypeConverter.convert(retainedPath.generatedContent, BitArray, ASCII))) self.memory = retainedPath.memory return retainedPath
def specialize(self, specializingPath=None): """Execute the specialize operation""" if specializingPath is None: specializingPath = SpecializingPath(memory=Memory()) self._logger.debug("Specialize field {0}".format(self.field.name)) # does an arbitrary value is specified ? if self.arbitraryValue is not None: specializingPath.addResult(self.field.domain, self.arbitraryValue) specializingPath.addResultToField(self.field, self.arbitraryValue) return [specializingPath] # does current field has children if len(self.field.fields) > 0: return self._specializeFieldWithChildren(specializingPath) else: return self._specializeField(specializingPath)