Пример #1
0
	def setUp(self):
		self.rs = RootScope()
		self.rs.importModule('numpy', '', ['sin', 'cos', 'tan'])
		self.rs.importModule('numpy.linalg', 'LA', ['eig'])
		self.rs.addSymbols({'a': 3, 'b': 6})
		self.cs1 = self.rs.createChildScope({'a': 71})
		self.cs2 = self.rs.createChildScope({'a': 14})
Пример #2
0
	def compute(self):
		# Create the root scope
		self.rootScope = RootScope()
		# Sections with global scope
		globalSections = filter(lambda s: not s['hasScope'], self.modelData['board']['layouts'])
		# Sections with isolated scope
		isolatedSections = filter(lambda s: s['hasScope'], self.modelData['board']['layouts'])
		
		## Process global sections
		# Set all the variables in the global scope and collect all the formulas
		for section in globalSections:
			self.preProcessSection(section, self.rootScope)
		# Create the root scope processor
		formulaProcessor = FormulaBlockProcessor(self.rootScope)
		# Run the formula evaluator
		formulaProcessor.process()
		# Write back results to fields
		for section in globalSections:
			self.postProcessSection(section, self.rootScope)
		
		# Compute child scopes
		for section in isolatedSections:
			scope = self.rootScope.createChildScope()
			self.preProcessSection(section, scope)
			formulaProcessor = FormulaBlockProcessor(scope)
			formulaProcessor.process()
			self.postProcessSection(section, scope)
Пример #3
0
class TestModelScope(unittest.TestCase):
	def setUp(self):
		self.rs = RootScope()
		self.rs.importModule('numpy', '', ['sin', 'cos', 'tan'])
		self.rs.importModule('numpy.linalg', 'LA', ['eig'])
		self.rs.addSymbols({'a': 3, 'b': 6})
		self.cs1 = self.rs.createChildScope({'a': 71})
		self.cs2 = self.rs.createChildScope({'a': 14})
		
	def tearDown(self):
		pass
		
	def testDefinitions(self):
		self.assertEqual(self.rs.getSymbolValue('a'), 3)
		self.assertEqual(self.rs.getSymbolValue('b'), 6)
		self.assertEqual(self.cs1.getSymbolValue('a'), 71)
		self.assertEqual(self.cs1.getSymbolValue('b'), 6)
		self.assertEqual(self.cs2.getSymbolValue('a'), 14)
		self.assertEqual(self.cs2.getSymbolValue('b'), 6)
	
	def testImports(self):
		self.assertEqual(self.cs1.getSymbolValue('sin'), np.sin)
		self.assertEqual(self.cs1.getSymbolValue('cos'), np.cos)
		self.assertEqual(self.cs1.getSymbolValue('LA')['eig'], np.linalg.eig)
		with self.assertRaises(UndefinedSymbolError):
			self.cs1.getSymbolValue('tanh')
	
	def testSetSymbols(self):
		self.rs.setSymbolValue('x', 7)
		self.cs1.setSymbolValue('y', 10)
		self.assertEqual(self.cs1.getSymbolValue('x'), 7)
		self.assertEqual(self.cs1.getSymbolValue('y'), 10)
		
					
	def testPrint(self):	
		print json.dumps(self.rs, cls = ScopeEncoder, indent = 4)
Пример #4
0
class ModelCalculator(object):
	def __init__(self, modelData):
		self.modelData = modelData

	def compute(self):
		# Create the root scope
		self.rootScope = RootScope()
		# Sections with global scope
		globalSections = filter(lambda s: not s['hasScope'], self.modelData['board']['layouts'])
		# Sections with isolated scope
		isolatedSections = filter(lambda s: s['hasScope'], self.modelData['board']['layouts'])
		
		## Process global sections
		# Set all the variables in the global scope and collect all the formulas
		for section in globalSections:
			self.preProcessSection(section, self.rootScope)
		# Create the root scope processor
		formulaProcessor = FormulaBlockProcessor(self.rootScope)
		# Run the formula evaluator
		formulaProcessor.process()
		# Write back results to fields
		for section in globalSections:
			self.postProcessSection(section, self.rootScope)
		
		# Compute child scopes
		for section in isolatedSections:
			scope = self.rootScope.createChildScope()
			self.preProcessSection(section, scope)
			formulaProcessor = FormulaBlockProcessor(scope)
			formulaProcessor.process()
			self.postProcessSection(section, scope)

		#print json.dumps(self.rootScope, cls = ScopeEncoder)
					
	def preProcessSection(self, section, scope):
		""" Collects all the variables and formula blocks"""			
		for field in section['fields']:
			fieldName = field['name']
			if (field['type'] == 'stem.FormulasField'):
				scope.addFormulaBlock(section['title'], field['name'], field['value'])
			elif (field['type'] == 'stem.TextField'):
				pass
			elif (field['type'] in ['stem.ScalarField', 'stem.BoolField', 'stem.ChoiceField', 'stem.TableField']):
				if (fieldName in scope.fields):
					duplicateField = scope.fields[fieldName]
					raise E.FieldError(u'Duplicate field (duplicate found in section {})'.format(duplicateField.section['title']),
									section['title'], field['name'])
				pField = Field(field, section = section)
				scope.fields[fieldName] = pField					 
				scope.setSymbolValue(fieldName, pField.parseValue(field['value']))
			else:
				raise E.FieldError("Unknown field type {}".format(field['type']), section['title'], field['name'])

	def postProcessSection(self, section, scope):
		""" Set values of the calculated fields"""
		for field in section['fields']:
			if (field['type'] == 'stem.ScalarField' or field['type'] == 'stem.TableField'):
				fieldName = field['name']
				field['value'] = scope.fields[fieldName].serializeValue(
					scope.getSymbolValue(fieldName, searchImports = False)
				)