def abs(self, *args): if not len(args) is 1: raise EvaluateException('Abs requires 1 parameter!') elif not isinstance(args[0], NumberType): raise EvaluateException( 'Abs requires all parameters to be numbers!') return op.abs(args[0])
def symcat(self, *args): if not len(args) > 0: raise EvaluateException('Symcat requires at least 1 parameter!' + ' (' + str(len(args)) + ' given).') elif False in [isinstance(x, SymbolType) for x in args]: raise EvaluateException('Symcat requires 2 symbols!') return reduce(SymbolType.__add__, args[1:], args[0])
def addition(self, *args): if not len(args) > 1: raise EvaluateException('+ requires at least 2 parameters!' + ' (' + str(len(args)) + ' given).') elif False in [isinstance(x, NumberType) for x in args]: raise EvaluateException('+ requires all parameters to be numbers!') return reduce(op.add, args, IntegerType(0))
def strlen(self, *args): if not len(args) is 1: raise EvaluateException('Strlen requires 1 parameter!' + ' (' + str(len(args)) + ' given).') elif not isinstance(args[0], StringType): raise EvaluateException('Strlen requires 1 string!') return IntegerType(StringType.str_length(args[0]))
def strindex(self, *args): if not len(args) is 2: raise EvaluateException('Strindex requires 2 parameters!' + ' (' + str(len(args)) + ' given).') elif not isinstance(args[0], StringType) or not isinstance( args[1], StringType): raise EvaluateException('Strindex requires 2 strings!') return args[0].str_index(args[1].content)
def substr(self, *args): if not len(args) is 3: raise EvaluateException('Substr requires 3 parameters!' + ' (' + str(len(args)) + ' given).') elif not isinstance(args[0], StringType) or not isinstance( args[1], IntegerType) or not isinstance(args[2], IntegerType): raise EvaluateException('Substr requires 1 string and 2 integers!') return args[0].sub_string(args[1].content, args[2].content)
def maximum(self, *args): if not len(args) > 1: raise EvaluateException('Max requires at least 2 parameters!' + ' (' + str(len(args)) + ' given).') elif False in [isinstance(x, NumberType) for x in args]: raise EvaluateException( 'Max requires all parameters to be numbers!') return max(args)
def power(self, *args): if not len(args) > 1: raise EvaluateException('** requires at least 2 parameters!' + ' (' + str(len(args)) + ' given).') elif False in [isinstance(x, NumberType) for x in args]: raise EvaluateException( '** requires all parameters to be numbers!') return reduce(op.pow, args[1:], args[0])
def module(self, *args): if not len(args) > 1: raise EvaluateException('% requires at least 2 parameters!' + ' (' + str(len(args)) + ' given).') elif False in [isinstance(x, NumberType) for x in args]: raise EvaluateException('% requires all parameters to be numbers!') elif 0 in [x.content for x in args[1:]]: raise EvaluateException('module by zero!') return reduce(op.mod, args[1:], args[0])
def randint(self, *args): if not len(args) is 2: raise EvaluateException('Randint requires 2 parameters!' + ' (' + str(len(args)) + ' given).') elif not isinstance(args[0], IntegerType) or not isinstance( args[1], IntegerType): raise EvaluateException('Randint requires 2 integers!') step = 1 if args[0] > args[1]: step = -1 return IntegerType( random.randrange(args[0].content, args[1].content, step))
def load_file(self, filename): # Saves the name and the extension of the file. It assumes that the class # to be loaded has the same name of the module (.py or .pyc file). mod_name, file_ext = os.path.splitext(os.path.split(filename)[-1]) py_mod = None # If the file has .py extension, then it loads that module. if file_ext.lower() == '.py': py_mod = importlib.import_module(filename) # If the file has .pyc extension, then it loads that module. elif file_ext.lower() == '.pyc': py_mod = importlib.import_module(filename) else: raise EvaluateException('Unable to find the file ' + filename + '!') # Builds the list of the classes contained in the module. classes = [ u for (v, u) in getmembers(py_mod, isclass) if v.startswith(mod_name) ] # Loads the classes contained in the module. for c in classes: self.load_class(c)
def add_fact(self, fact): # If the Working Memory doesn't contain a fact with # the name equal to the one of the specified fact, # then it adds that fact to the Working Memory # and it returns the generated WME. fact.values = [ x.content if isinstance(x, VariableType) else x for x in fact.values ] if False in [isinstance(x, BaseType) for x in fact.values]: raise EvaluateException('The fact "' + str(fact.name) + '" contains a null variable!') if not self.__facts[fact]: wme = WME(self.__counter, fact) self.__facts[fact] = True self.__wmes[self.__counter] = wme self.__counter += 1 return wme # Otherwise, if the Working Memory already contains # a fact with the name equal to the one of the # specified fact, then it doesn't add any fact # to the Working Memory. else: return None
def visitGlobalVariableType(self, node): value = self.__environment.get_global_variable(node) if value is None: raise EvaluateException('The global variable ' + node.name + ' has not been instanced!') else: node.content = value return node
def build_tests(self, is_last, alpha_variables, tests): var_globals = [ GlobalVariableType(x) for x in self.environment.global_variables.keys() ] # For every variable in the set composed by the union # of the variables of the specified alpha memory # and the global variables, then it saves # the tests which involve these variables # to the specified tests set. #TODO changed here for v in set(list(alpha_variables) + list(var_globals)): if v in tests: self.__tests.update(tests[v]) del tests[v] # It removes the set of the identified local test # from the set of the tests specified as parameter. for t in self.__tests: for v in t.test_variables: if v in tests: tests[v].remove(t) # If the flag which specifies if the considered node # is the last one which contains the test is set to True, # then, if there are still tests in the set, then it # raises an exception, because it means that the # test contains variables not declared in the alpha memory. if is_last: # Rimuove tutti gli insiemi di test vuoti rimanenti. # It removes all the remaining empty sets of tests. for k in tests.keys(): if not tests[k]: del tests[k] if tests: raise EvaluateException( 'There are tests with invalid variables!')
def get_method(self, name): predicate = Predicates() special_function = SpecialFunctions() predicates = { "eq": predicate.equal, "neq": predicate.not_equal, "<": predicate.less_than, "<=": predicate.less_equal, ">": predicate.greater_than, ">=": predicate.greater_equal, "and": predicate.logical_and, "or": predicate.logical_or, "not": predicate.not_equal } special_functions = { "printout": special_function.printout, "assert": special_function.assertion, "retract": special_function.retract, "bind": special_function.bind, "test": special_function.test, "strategy": special_function.strategy } try: if name in special_functions: return special_functions[name] elif name in predicates: return predicates[name] else: return self.__map[name] except KeyError: # Raises an exception in the case in which the function is not present. raise EvaluateException('Unable to find the function ' + name + '!')
def test(self, *args): if False in [isinstance(x, BooleanType) for x in args[1:]]: raise EvaluateException( 'The \"test\" predicate takes only boolean parameters!') return args[1]
def logical_or(self, *args): if False in [isinstance(x, BooleanType) for x in args]: raise EvaluateException('The \"or\" predicate takes only boolean parameters!') return self.__boolean_converter(True in [x.content for x in args])