@@ -349,8 +349,13 @@ def set_variable(self, name, value, verbose=False):
349
349
"""
350
350
if self ._sanitize_input : name = self ._sanitize (name )
351
351
try :
352
- if type (value )== int :
352
+ if isinstance (value ,str ):
353
+ self ._templates_enabled = TEMPLATES_ENGAGED
354
+ elif isinstance (value ,bool ):
355
+ self ._templates_enabled = TEMPLATES_ENGAGED
356
+ else :
353
357
value = float (value )
358
+
354
359
self ._variables [name ] = value
355
360
if verbose : print (" * Variable %s set to %f" % (name , value ))
356
361
except ValueError :
@@ -375,32 +380,8 @@ def set_constant(self, name, value, verbose=False):
375
380
except ValueError :
376
381
raise Exception ("ERROR: specified value for " + name + " is not an integer or float: " + value )
377
382
378
-
379
- def set_input_templates (self , replacement_dictionary = {}):
380
- """
381
- Sets the current status of categorical values using a template system. The replacement
382
- rules are specified as a dictionary. EXPERIMENTAL.
383
- """
384
- self ._replacement_dictionary = replacement_dictionary
385
- if self ._check_templates () == TEMPLATES_ENGAGED :
386
- print ( " * Templates replacement set to:" , self ._replacement_dictionary )
387
- else :
388
- print ( "WARNING: templates replacement set but no templates were found in the functions" )
389
-
390
-
391
383
def _check_templates (self ):
392
- # replacement dictionary filled and templates used in functions
393
- if len (self ._replacement_dictionary )> 0 and self ._templates_enabled :
394
- return TEMPLATES_ENGAGED
395
-
396
- # no replacement set but templates are used in functions (not good)
397
- elif len (self ._replacement_dictionary )== 0 and self ._templates_enabled :
398
- return TEMPLATES_MISSING_INFO
399
- else :
400
-
401
- # replacement not set or templates are disabled
402
- return TEMPLATES_DISENGAGED
403
-
384
+ return self ._templates_enabled
404
385
405
386
def add_rules_from_file (self , path , verbose = False ):
406
387
"""
@@ -589,29 +570,42 @@ def _replace_values(self, function, verbose=False):
589
570
try :
590
571
prestring = res_string [:res_string .find ("{" )]
591
572
substring = res_string [res_string .find ("{" )+ 1 :res_string .find ("}" )]
592
- if verbose : print ("Pre- and sub-strings:" , prestring , substring )
573
+ if verbose : print (" Pre- and sub-strings:" , prestring , substring )
593
574
except :
594
575
print ("ERROR: missing curly brace in template, aborting." )
595
576
exit ()
596
577
597
578
variable = substring [2 : substring .find ("IS" )].strip ()
598
579
case = substring [substring .find ("IS" )+ 2 :substring .find ("THEN" )].strip ()
599
580
value = substring [substring .find ("THEN" )+ 4 :].strip ()
600
- if verbose : print ("Analysing rule: IF %s IS %s THEN %s" % (variable , case , value ))
581
+ if verbose : print (" * Analysing rule: IF %s IS %s THEN %s" % (variable , case , value ))
601
582
602
583
# checking everything all the time is not a good idea, optimize later
603
- detected = False
584
+ #detected = False
585
+
586
+
587
+ candidate = self ._variables [variable ]
588
+ if candidate == case :
589
+ newstring = newstring + prestring + str (value )
590
+ res_string = res_string [res_string .find ("}" )+ 1 :]
591
+ if verbose : print (" - case detected for '%s IS %s" % (variable , case ))
592
+ else :
593
+ res_string = res_string [res_string .find ("}" )+ 1 :]
594
+ newstring += prestring + "0"
595
+ if verbose : print (" - case NOT detected for '%s IS %s'" % (variable , case ))
596
+
597
+ """
604
598
for k,v in self._replacement_dictionary.items():
605
599
if k==variable and v==case:
606
600
if verbose: print(" - case detected for", k,v)
607
601
newstring = newstring+prestring+ str(value)
608
602
res_string = res_string[res_string.find("}")+1:]
609
603
detected = True
604
+ """
610
605
611
- if detected : continue
606
+ # if detected: continue
612
607
613
- res_string = res_string [res_string .find ("}" )+ 1 :]
614
- newstring += prestring + "0"
608
+
615
609
616
610
newstring += res_string
617
611
return newstring
@@ -664,7 +658,7 @@ def mediate(self, outputs, antecedent, results, ignore_errors=False, ignore_warn
664
658
if self ._check_templates () == TEMPLATES_ENGAGED :
665
659
print (" * Replacing templates in function for '%s'" % res [0 ])
666
660
print (" name of function: '%s'" % res [1 ])
667
- string_to_evaluate = self ._replace_values (string_to_evaluate )
661
+ string_to_evaluate = self ._replace_values (string_to_evaluate , verbose = verbose )
668
662
print (" * Final version of the '%s' rule: %s" % (res [1 ], string_to_evaluate ))
669
663
#exit()
670
664
0 commit comments