add ParamIndex

This commit is contained in:
Emmanuel Garette 2021-02-17 22:32:28 +01:00
parent 3b2d96566b
commit e35a3cb19d
23 changed files with 254 additions and 69 deletions

View file

@ -104,51 +104,35 @@ class FamilyAnnotator(Walk):
def change_modes(self):
"""change the mode of variables
"""
# default is high level
new_family_mode = modes_level[-1]
for family in self.objectspace.space.variables.values():
self.change_family_mode(family,
new_family_mode,
first=True,
)
families = list(self.get_families())
for family in families:
self._set_default_mode(family)
families.reverse()
for family in families:
self._change_family_mode(family)
def change_family_mode(self,
def _set_default_mode(self,
family: 'self.objectspace.family',
new_family_mode: str,
first: bool=False,
) -> str:
if hasattr(family, 'mode'):
) -> None:
if hasattr(family, 'mode') and 'mode' in vars(family):
family_mode = family.mode
else:
family_mode = new_family_mode
if hasattr(family, 'variable'):
# change variable mode, but not if variables are not in a family
family_mode = None
if not hasattr(family, 'variable'):
return
for variable in family.variable.values():
if isinstance(variable, self.objectspace.family):
new_family_mode = self.change_family_mode(variable,
new_family_mode,
)
continue
if first:
if family_mode and 'mode' not in vars(variable):
variable.mode = family_mode
continue
if isinstance(variable, self.objectspace.leadership):
func = self._change_variable_mode_leader
func = self._set_default_mode_leader
else:
func = self._change_variabe_mode
func(variable,
family_mode,
)
if modes[new_family_mode] > modes[variable.mode]:
new_family_mode = variable.mode
if not first:
# set the lower variable mode to family
family.mode = new_family_mode
return new_family_mode
func = self._set_default_mode_variable
func(variable)
def _change_variabe_mode(self,
variable,
family_mode: str,
is_follower=False,
@staticmethod
def _set_default_mode_variable(variable: 'self.objectspace.variable',
) -> None:
# auto_save or auto_freeze variable is set to 'basic' mode
# if its mode is not defined by the user
@ -158,6 +142,57 @@ class FamilyAnnotator(Walk):
# mandatory variable without value is a basic variable
if variable.mandatory is True and not hasattr(variable, 'default'):
variable.mode = modes_level[0]
def _set_default_mode_leader(self,
leadership,
) -> None:
leader_mode = None
for follower in leadership.variable:
if follower.auto_save is True:
msg = _(f'leader/followers "{follower.name}" could not be auto_save')
raise DictConsistencyError(msg, 29, leadership.xmlfiles)
if follower.auto_freeze is True:
msg = f'leader/followers "{follower.name}" could not be auto_freeze'
raise DictConsistencyError(_(msg), 30, leadership.xmlfiles)
self._set_default_mode_variable(follower)
if leader_mode is None:
leader_mode = leadership.variable[0].mode
else:
# leader's mode is minimum level
if modes[leader_mode] > modes[follower.mode]:
follower.mode = leader_mode
leadership.mode = leader_mode
def _change_family_mode(self,
family: 'self.objectspace.family',
) -> None:
if hasattr(family, 'mode'):
family_mode = family.mode
else:
family_mode = modes_level[1]
min_variable_mode = modes_level[-1]
# change variable mode, but not if variables are not in a family
if hasattr(family, 'variable'):
for variable in family.variable.values():
if not isinstance(variable, self.objectspace.family):
if isinstance(variable, self.objectspace.leadership):
func = self._change_variable_mode_leader
else:
func = self._change_variable_mode
func(variable,
family_mode,
)
if modes[min_variable_mode] > modes[variable.mode]:
min_variable_mode = variable.mode
if hasattr(family, 'mode'):
# set the lower variable mode to family
family.mode = min_variable_mode
def _change_variable_mode(self,
variable,
family_mode: str,
is_follower=False,
) -> None:
# none basic variable in high level family has to be in high level
if modes[variable.mode] < modes[family_mode] and \
(not is_follower or variable.mode != modes_level[0]):
@ -168,27 +203,13 @@ class FamilyAnnotator(Walk):
family_mode: str,
) -> None:
is_follower = False
leader_mode = None
for follower in leadership.variable:
if follower.auto_save is True:
msg = _(f'leader/followers "{follower.name}" could not be auto_save')
raise DictConsistencyError(msg, 29, leadership.xmlfiles)
if follower.auto_freeze is True:
msg = f'leader/followers "{follower.name}" could not be auto_freeze'
raise DictConsistencyError(_(msg), 30, leadership.xmlfiles)
self._change_variabe_mode(follower,
self._change_variable_mode(follower,
family_mode,
is_follower,
)
if leader_mode is None:
leader_mode = leadership.variable[0].mode
leadership.variable[0].mode = None
else:
# leader's mode is minimum level
if modes[leader_mode] > modes[follower.mode]:
follower.mode = leader_mode
is_follower = True
leadership.mode = leader_mode
leadership.variable[0].mode = None
def dynamic_families(self):
"""link dynamic families to object

View file

@ -53,6 +53,11 @@ class GroupAnnotator:
cache_paths[group.leader] = leader_fam_path
follower_names = list(group.follower.keys())
leader = self.objectspace.paths.get_variable(group.leader)
if '.' not in leader_fam_path:
# it's a namespace
ori_leader_family = self.objectspace.space.variables[leader_fam_path]
else:
# it's a sub family
ori_leader_family = self.objectspace.paths.get_family(leader_fam_path,
leader.namespace,
)
@ -121,10 +126,7 @@ class GroupAnnotator:
leadership_path,
leader_space,
)
leader_family = self.objectspace.paths.get_family(ori_leader_family.path,
ori_leader_family.namespace,
)
leader_family.variable[variable.name] = leader_space
ori_leader_family.variable[variable.name] = leader_space
leader_space.variable.append(variable)
self.objectspace.paths.set_leader(variable.namespace,
ori_leader_family.path,

View file

@ -48,7 +48,7 @@ class ParamAnnotator:
variable_type = self.valid_type_validation(obj)
for param_idx, param in enumerate(obj.param):
if hasattr(param, 'text'):
if param.type == 'suffix':
if param.type in ['suffix', 'index']:
msg = _(f'"{param.type}" parameter must not have a value')
raise DictConsistencyError(msg, 28, obj.xmlfiles)
elif param.type == 'nil':
@ -81,9 +81,16 @@ class ParamAnnotator:
if param.type == 'suffix':
for target in obj.target:
if not self.objectspace.paths.variable_is_dynamic(target.name.path):
msg = _(f'"suffix" parameter cannot be set with target "{target.name}"'
msg = _(f'"{param.type}" parameter cannot be set with target "{target.name}"'
f' which is not a dynamic variable')
raise DictConsistencyError(msg, 53, obj.xmlfiles)
elif param.type == 'index':
for target in obj.target:
if not self.objectspace.paths.is_follower(target.name.path):
msg = _(f'"{param.type}" parameter cannot be set with target "{target.name}"'
f' which is not a follower variable')
raise DictConsistencyError(msg, 60, obj.xmlfiles)
pass
elif param.type == 'nil':
param.text = None
elif param.type == 'string':

View file

@ -72,7 +72,7 @@
<!ATTLIST file group CDATA "root">
<!ATTLIST file filelist CDATA #IMPLIED>
<!ATTLIST file redefine (True|False) "False">
<!ATTLIST file templating (creole|none) "creole">
<!ATTLIST file templating (none|creole) "creole">
<!ELEMENT override EMPTY>
<!ATTLIST override source CDATA #IMPLIED>
@ -125,7 +125,7 @@
<!ATTLIST condition apply_on_fallback (True|False) #IMPLIED>
<!ELEMENT param (#PCDATA)>
<!ATTLIST param type (string|number|nil|boolean|variable|information|suffix) "string">
<!ATTLIST param type (string|number|nil|boolean|variable|information|suffix|index) "string">
<!ATTLIST param name CDATA #IMPLIED>
<!ATTLIST param propertyerror (True|False) "True">
<!ATTLIST param optional (True|False) "False">

View file

@ -136,6 +136,15 @@ class Path:
leadership = self.get_family(variable['leader'], variable['variableobj'].namespace)
return leadership.variable[0].path == path
def is_follower(self, path):
"""Is the variable is a follower
"""
variable = self._get_variable(path)
if not variable['leader']:
return False
leadership = self.get_family(variable['leader'], variable['variableobj'].namespace)
return leadership.variable[0].path != path
# Variable
def add_variable(self, # pylint: disable=R0913
namespace: str,

View file

@ -253,6 +253,8 @@ class Common:
return f'ParamInformation("{param.text}", None)'
if param.type == 'suffix':
return 'ParamSuffix()'
if param.type == 'index':
return 'ParamIndex()'
raise Exception(f'unknown type {param.type}') # pragma: no cover
@staticmethod

View file

@ -0,0 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<variables>
<family name="general">
<variable name="mode_conteneur_actif1" type="string" description="No change">
<value>non</value>
</variable>
</family>
</variables>
<constraints>
<fill name="calc_val">
<param type="variable">mode_conteneur_actif1</param>
<target optional="True">mode_conteneur_actif</target>
</fill>
</constraints>
</rougail>
<!-- vim: ts=4 sw=4 expandtab
-->

View file

@ -0,0 +1 @@
{"rougail.general.mode_conteneur_actif1": "non"}

View file

@ -0,0 +1,17 @@
from importlib.machinery import SourceFileLoader
from importlib.util import spec_from_loader, module_from_spec
loader = SourceFileLoader('func', 'tests/dictionaries/../eosfunc/test.py')
spec = spec_from_loader(loader.name, loader)
func = module_from_spec(spec)
loader.exec_module(func)
for key, value in dict(locals()).items():
if key != ['SourceFileLoader', 'func']:
setattr(func, key, value)
try:
from tiramisu3 import *
except:
from tiramisu import *
option_3 = StrOption(name="mode_conteneur_actif1", doc="No change", default="non", properties=frozenset({"mandatory", "normal"}))
option_2 = OptionDescription(name="general", doc="general", children=[option_3], properties=frozenset({"normal"}))
option_1 = OptionDescription(name="rougail", doc="rougail", children=[option_2])
option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[option_1])

View file

@ -0,0 +1,15 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<variables>
<variable name="my_variable"/>
</variables>
<constraints>
<check name="valid_entier">
<param name="mini" type="number">0</param>
<param name="maxi" type="number">100</param>
<target optional="True">int</target>
</check>
</constraints>
</rougail>
<!-- vim: ts=4 sw=4 expandtab
-->

View file

@ -0,0 +1 @@
{"rougail.my_variable": null}

View file

@ -0,0 +1,16 @@
from importlib.machinery import SourceFileLoader
from importlib.util import spec_from_loader, module_from_spec
loader = SourceFileLoader('func', 'tests/dictionaries/../eosfunc/test.py')
spec = spec_from_loader(loader.name, loader)
func = module_from_spec(spec)
loader.exec_module(func)
for key, value in dict(locals()).items():
if key != ['SourceFileLoader', 'func']:
setattr(func, key, value)
try:
from tiramisu3 import *
except:
from tiramisu import *
option_2 = StrOption(name="my_variable", doc="my_variable", properties=frozenset({"normal"}))
option_1 = OptionDescription(name="rougail", doc="rougail", children=[option_2])
option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[option_1])

View file

@ -0,0 +1,21 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<variables>
<variable name="leader" multi="True">
<value>a</value>
<value>b</value>
<value>c</value>
</variable>
<variable name="follower1" type="number"/>
</variables>
<constraints>
<fill name="calc_val">
<param type="index"/>
<target>follower1</target>
</fill>
<group leader="leader">
<follower>follower1</follower>
</group>
</constraints>
</rougail>

View file

@ -0,0 +1 @@
{"rougail.leader.leader": [{"rougail.leader.leader": "a", "rougail.leader.follower1": 0}, {"rougail.leader.leader": "b", "rougail.leader.follower1": 1}, {"rougail.leader.leader": "c", "rougail.leader.follower1": 2}]}

View file

@ -0,0 +1,18 @@
from importlib.machinery import SourceFileLoader
from importlib.util import spec_from_loader, module_from_spec
loader = SourceFileLoader('func', 'tests/dictionaries/../eosfunc/test.py')
spec = spec_from_loader(loader.name, loader)
func = module_from_spec(spec)
loader.exec_module(func)
for key, value in dict(locals()).items():
if key != ['SourceFileLoader', 'func']:
setattr(func, key, value)
try:
from tiramisu3 import *
except:
from tiramisu import *
option_3 = StrOption(name="leader", doc="leader", multi=True, default=['a', 'b', 'c'], properties=frozenset({"mandatory"}))
option_4 = IntOption(name="follower1", doc="follower1", multi=True, default=Calculation(func.calc_val, Params((ParamIndex()))), properties=frozenset({"normal"}))
option_2 = Leadership(name="leader", doc="leader", children=[option_3, option_4], properties=frozenset({"normal"}))
option_1 = OptionDescription(name="rougail", doc="rougail", children=[option_2])
option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[option_1])

View file

@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<variables>
<family name="base" mode="expert">
<family name="subfamily">
<variable name="variable"/>
</family>
</family>
</variables>
</rougail>
<!-- vim: ts=4 sw=4 expandtab
-->

View file

@ -0,0 +1 @@
{"rougail.base.subfamily.variable": null}

View file

@ -0,0 +1,18 @@
from importlib.machinery import SourceFileLoader
from importlib.util import spec_from_loader, module_from_spec
loader = SourceFileLoader('func', 'tests/dictionaries/../eosfunc/test.py')
spec = spec_from_loader(loader.name, loader)
func = module_from_spec(spec)
loader.exec_module(func)
for key, value in dict(locals()).items():
if key != ['SourceFileLoader', 'func']:
setattr(func, key, value)
try:
from tiramisu3 import *
except:
from tiramisu import *
option_4 = StrOption(name="variable", doc="variable", properties=frozenset({"expert"}))
option_3 = OptionDescription(name="subfamily", doc="subfamily", children=[option_4], properties=frozenset({"expert"}))
option_2 = OptionDescription(name="base", doc="base", children=[option_3], properties=frozenset({"expert"}))
option_1 = OptionDescription(name="rougail", doc="rougail", children=[option_2])
option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[option_1])

View file

@ -60,3 +60,7 @@ def device_type(*args, **kwargs):
def calc_list(*args, **kwargs):
return list(args)
def test_index(index):
return index