def __init__(self, predicate, arguments, *args): """ :param predicate: :param arguments: :param args: :return: """ super(Literal, self).__init__() self.predicate = predicate self.arguments = tuple(arguments) if len(args) == 0: from spf.mr.lambda_.logic_language_services import LogicLanguageServices type_comparator = LogicLanguageServices.get_type_comparator() type_repository = LogicLanguageServices.get_type_repository() elif len(args) == 2: type_comparator = args[0] type_repository = args[1] else: raise RuntimeError("Wrong number of arguments") if not predicate.get_type().is_complex(): raise LogicalExpressionRuntimeError("Predicate must have a complex type, not %s" % predicate.get_type()) literal_typing = Literal.compute_literal_typing( self.predicate.get_type(), [arg.get_type() for arg in self.arguments], type_comparator, type_repository ) self.type_ = None if literal_typing is None else literal_typing[0]
def read(cls, string, mapping=None, type_repository=None, type_comparator=None): if mapping is None: mapping = {} if type_repository is None: type_repository = LogicLanguageServices.get_type_repository() if type_comparator is None: type_comparator = LogicLanguageServices.get_type_comparator() flat_string = cls.WHITE_SPACE.sub(string, " ") try: for reader in cls.readers: if reader.is_valid(flat_string): return reader.read(flat_string, mapping, type_repository, type_comparator, cls) raise AttributeError("Invalid logical expression syntax: %s" % string) except Exception, e: cls.LOG.error("Logic expression syntax error: %s" % flat_string) raise e
def of(func, arg): """ :param func: :param arg: :return: """ if (not func.get_type().is_complex() or not LogicLanguageServices.get_type_comparator().verfiy_arg_type( func.get_type().get_domain(), arg.get_type())): return None elif isinstance(func, Lambda): lambda_ = func variable = lambda_.get_argument() visitor = ApplyAndSimplify(arg, variable) visitor.visit(lambda_.get_body()) return visitor.temp_return elif isinstance(func, Literal): return Simplify.of(ApplyAndSimplify.literal_application(func, arg)) elif isinstance(func, Term): return Simplify.of(ApplyAndSimplify.term_application(func, arg)) else: raise LogicalExpressionRuntimeError('Impossible condition: unhandled logical expression object.')