Commit a1b820d9 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Test random simulation with test-simu

parent 90025dc6
......@@ -22,6 +22,7 @@
import os
import ctypes
import itertools
import random
from functools import partial
from itertools import chain
......@@ -58,6 +59,60 @@ CleanName = lambda name: name.replace(UNICODE_SEP, '->')
UnicodeName = lambda name: name.replace('->', UNICODE_SEP)
# Set of functions used by the simulator to compute a random value for
# a given ASN.1 type, return a Value Notation string
def compute_random_value(asn1_ty, pool):
''' Top-level, type-dispatching function
pool is the set of types from process.dataview attribute '''
basic = opengeode.ogParser.find_basic_type(asn1_ty.type, pool)
if basic.kind.startswith('Integer'):
return rand_int(basic)
elif basic.kind == 'BooleanType':
return rand_bool(basic)
elif basic.kind.startswith('Real'):
return rand_real(basic)
elif basic.kind == 'EnumeratedType':
return rand_enum(basic)
elif basic.kind == 'ChoiceType':
return compute_random_choice(basic, pool)
elif basic.kind in ('SequenceType', 'SetType'):
return compute_random_sequence(basic, pool)
elif basic.kind in ('SequenceOfType', 'SetOfType'):
return compute_random_sequenceof(basic, pool)
# Random values for basic types
rand_int = lambda ty: str(random.randint(long(ty.Min), long(ty.Max)))
rand_real = lambda ty: str(random.uniform(float(ty.Min), float(ty.Max)))
rand_bool = lambda _: random.choice(['TRUE', 'FALSE'])
rand_enum = lambda ty: random.choice(ty.EnumValues.keys())
def compute_random_choice(asn1_ty, pool):
''' Select randomly a choice item and set a random value '''
choice = random.choice(asn1_ty.Children.keys())
value_ty = asn1_ty.Children[choice]
value = compute_random_value(value_ty, pool)
return '{}: {}'.format(choice, value)
def compute_random_sequence(asn1_ty, pool):
''' Compute randomly the values of SEQUENCE fields '''
res = []
for name, ty in asn1_ty.Children.viewitems():
res.append('{} {}'.format(name, compute_random_value(ty, pool)))
return '{{ {} }}'.format(', '.join(res))
def compute_random_sequenceof(asn1_ty, pool):
''' Compute a list of a random size and random values '''
size = random.randint(int(asn1_ty.Min), int(asn1_ty.Max))
elems = []
for _ in xrange(size):
elems.append(compute_random_value(asn1_ty, pool))
return '{{ {} }}'.format(', '.join(elems))
# Set of functions used by the simulator to compute the combination input
# parameters for each ASN.1 data type. Yield ASN.1 Value Notation strings
# ASN.1 types must be in the format generated by ASN1SCC Python backend
......@@ -69,7 +124,7 @@ def compute_combinations(asn1_ty, pool):
if basic.kind.startswith('Integer'):
for each in compute_integer_combinations(basic):
yield each
if basic.kind == 'BooleanType':
elif basic.kind == 'BooleanType':
for each in compute_boolean_combinations(basic):
yield each
elif basic.kind.startswith('Real'):
......@@ -193,6 +248,7 @@ class sdlHandler(QObject):
''' Startup: check if there are some SDL files in
the directory and try to parse them and build the widget '''
super(sdlHandler, self).__init__()
self.parent = parent
all_files = os.listdir('.')
pr_files = []
......@@ -584,18 +640,23 @@ class sdlHandler(QObject):
def random_step(self):
''' One step of random simulation '''
if self.sim_param['state'] == 'random':
for each in self.sim_param['periodic']:
if each in self.buttons:
# paramless TC
# tc with a param, use the Send button
# (first, set a random value, TODO)
for vals in self.param_tc_editors:
if vals['name'] == each:
QTimer().singleShot(1000, self.random_step)
if self.sim_param['state'] != 'random':
for name, asn1_ty in self.sim_param['periodic']:
if name not in self.active_tc:
if name in self.buttons:
# paramless TC
# tc with a param, use the Send button
for vals in self.param_tc_editors:
if vals['name'] == name:
arg = compute_random_value(asn1_ty, self.proc.dataview)
argpy = vn.fromValueNotationToPySide(name, arg)
QTimer().singleShot(1000, self.random_step)
def random_simulation(self):
''' Random simulator - read the config from the checker_table and
......@@ -605,12 +666,14 @@ class sdlHandler(QObject):
self.sim_param['random'] = []
self.sim_param['periodic'] = []
for row_nb in xrange(self.checker_table.rowCount()):
name = self.checker_table.item(row_nb, 0).text()
item = self.checker_table.item(row_nb, 0)
name = item.text()
asn1_ty =
cond = self.checker_table.cellWidget(row_nb, 1).currentText()
if cond == 'Random':
self.sim_param['random'].append((name, asn1_ty))
elif cond == 'Periodic':
self.sim_param['periodic'].append((name, asn1_ty))
def stop_simulation(self):
......@@ -775,8 +838,14 @@ class sdlHandler(QObject):
for idx, inp in enumerate(self.proc.input_signals):
name = inp['name']
sort = inp.get('type', None)
if sort:
typename = sort.ReferencedTypeName.replace('-', '_')
ty = self.proc.dataview[sort.ReferencedTypeName]
item = QTableWidgetItem(name)
# Associate the ASN.1 data type as hidden data
item.setData(Qt.UserRole, ty)
combo = QComboBox()
combo.addItems(('Manual', 'Random', 'Periodic'))
checker_table.setItem(idx, 0, item)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment