Exemple #1
0
    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])
Exemple #2
0
    def subtraction(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.sub, args[1:], args[0])
Exemple #3
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]))
Exemple #4
0
    def multiplication(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.mul, args, IntegerType(1))
Exemple #5
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])
Exemple #6
0
    def logical_not(self, *args):
        if False in [isinstance(x, BooleanType) for x in args]:
            raise EvaluateException(
                'The \"not\" predicate takes only boolean parameters!')

        if len(args) != 1:
            raise EvaluateException(
                'The \"not\" predicate takes only one parameter!')

        return self.__boolean_converter(not args[0].content)
Exemple #7
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)
Exemple #8
0
    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)
Exemple #9
0
    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)
Exemple #10
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])
Exemple #11
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 = imp.load_source(mod_name, filename)
            spec = importlib.util.spec_from_file_location(mod_name, filename)
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)

        # If the file has .pyc extension, then it loads that module.
        elif file_ext.lower() == '.pyc':
            # py_mod = imp.load_compiled(mod_name, filename)
            spec = importlib.util.spec_from_file_location(mod_name, filename)
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)

        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)
Exemple #13
0
    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 get_method(self, name):
     try:
         # Returns the function which presents the specified name.
         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 +
                                 '!')
Exemple #15
0
    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
Exemple #16
0
    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])
    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]