Skip to content

Commit 5e6ea9e

Browse files
committed
Support for categorical variables in consequent
1 parent 5511b87 commit 5e6ea9e

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

simpful/simpful.py

+27-33
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,13 @@ def set_variable(self, name, value, verbose=False):
349349
"""
350350
if self._sanitize_input: name = self._sanitize(name)
351351
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:
353357
value = float(value)
358+
354359
self._variables[name] = value
355360
if verbose: print(" * Variable %s set to %f" % (name, value))
356361
except ValueError:
@@ -375,32 +380,8 @@ def set_constant(self, name, value, verbose=False):
375380
except ValueError:
376381
raise Exception("ERROR: specified value for "+name+" is not an integer or float: "+value)
377382

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-
391383
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
404385

405386
def add_rules_from_file(self, path, verbose=False):
406387
"""
@@ -589,29 +570,42 @@ def _replace_values(self, function, verbose=False):
589570
try:
590571
prestring = res_string[:res_string.find("{")]
591572
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)
593574
except:
594575
print("ERROR: missing curly brace in template, aborting.")
595576
exit()
596577

597578
variable = substring[2: substring.find("IS")].strip()
598579
case = substring[substring.find("IS")+2:substring.find("THEN")].strip()
599580
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))
601582

602583
# 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+
"""
604598
for k,v in self._replacement_dictionary.items():
605599
if k==variable and v==case:
606600
if verbose: print(" - case detected for", k,v)
607601
newstring = newstring+prestring+ str(value)
608602
res_string = res_string[res_string.find("}")+1:]
609603
detected = True
604+
"""
610605

611-
if detected: continue
606+
#if detected: continue
612607

613-
res_string = res_string[res_string.find("}")+1:]
614-
newstring += prestring + "0"
608+
615609

616610
newstring += res_string
617611
return newstring
@@ -664,7 +658,7 @@ def mediate(self, outputs, antecedent, results, ignore_errors=False, ignore_warn
664658
if self._check_templates() == TEMPLATES_ENGAGED:
665659
print(" * Replacing templates in function for '%s'" % res[0])
666660
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)
668662
print(" * Final version of the '%s' rule: %s" % (res[1], string_to_evaluate))
669663
#exit()
670664

0 commit comments

Comments
 (0)