def from_clips(cls, instance): if not isinstance(instance, clips._clips_wrap.Instance): instance = clips.FindInstance(instance) cls = utils.get_class(str(instance.Class.Name)) kwargs = {} for mod,mcls in cls.mods.items(): cmod = instance.GetSlot(mod) if cmod and str(cmod) != 'nil': mcls = utils.get_class(mcls) kwargs[mod] = mcls.from_clips(cmod) return cls(**kwargs)
def __init__(cls, classname, bases, newdict): if classname == 'Exists': # due to the ordering of things in clps.py utils.register(classname, cls) return slots = ['(slot %s (type %s) (visibility public) (pattern-match reactive))' % (mod, issubclass(utils.get_class(modclass), Number) and \ 'NUMBER' or 'INSTANCE') for mod,modclass in cls.mods.items()] slots = ' '.join(slots) parents = ' '.join([base.__name__ for base in bases]) clp = '(defclass %s (is-a %s) %s)' % (classname, parents, slots) for mod,modclass in cls.mods.items(): if isinstance(modclass, type): cls.mods[mod] = modclass.__name__ modification = ['%s a %s' % (mod, modclass.lower()) for mod, modclass in cls.mods.items()] verb_def = '%s is %s withsubject %s' % (classname.lower(), parents.lower().replace(' ', ', '), cls.subject.__name__.lower()) if modification: verb_def += ' andcanbe ' + ', '.join(modification) verb_def += '.' utils.to_history(verb_def) for kls in bases: if getattr(kls, 'mods', False): cls.mods.update(kls.mods) super(Verb, cls).__init__(classname, bases, newdict, clp=clp)
def from_clips(cls, instance): if isinstance(instance, clips._clips_wrap.InstanceName): return cls(str(instance)) if not isinstance(instance, clips._clips_wrap.Instance): instance = clips.FindInstance(instance) clsname = str(instance.Class.Name) cls = utils.get_class(clsname) return cls(str(instance))
def from_clips(cls, instance): if not isinstance(instance, clips._clips_wrap.Instance): instance = clips.FindInstance(instance) clsname = str(instance.Class.Name) cls = utils.get_class(clsname) if clsname == 'Namable': return cls(str(instance)) else: return cls.from_clips(instance)
def from_clips(cls, instance): ''' build nl instance starting from a clips instance or instance name ''' if not isinstance(instance, clips._clips_wrap.Class): instance = clips.FindClass(instance) clsname = str(instance.Name) return utils.get_class(clsname)
def __new__(cls, classname, bases=None, newdict=None): if utils.varpat.match(classname): if bases: return ClassVar(classname, bases) elif getattr(cls, 'cls', False): return ClassVar(classname, cls.cls) return ClassVar(classname, utils.get_class('Namable')) cls.value = '' return super(Word, cls).__new__(cls, classname, bases, newdict)
def get_symbol(sym): ''' ''' try: return utils.get_class(sym) except KeyError: try: return ask_obj(Thing(sym))[0] except IndexError: return sym
def __new__(cls, classname, bases=None, newdict=None): if utils.varpat.match(classname): if bases: return ClassVar(classname, bases) elif getattr(cls, 'cls', False): return ClassVar(classname, cls.cls) return ClassVar(classname, utils.get_class('Namable')) if not classpat.match(classname): raise ValueError('Ilegal name for class: ' + classname) cls.value = '' return super(Word, cls).__new__(cls, classname, bases, newdict)
def __init__(self, *args, **kwargs): #if args and kwargs: # raise ValueError('You cannot instantiate a verb (%s) with both ' # 'unnamed and named arguments' % # self.__class__.__name__) if len(args) > 1: raise ValueError('You cannot instantiate a verb (%s) with more ' 'than one unnamed argument' % self.__class__.__name__) if args and args[0] and not utils.varpat.match(args[0]): raise ValueError('You cannot instantiate a verb (%s) with an ' 'unnamed argument that does not correspond ' 'to a variable' % self.__class__.__name__) for kw in kwargs: if kw not in self.mods and kw != '_clsvar': raise KeyError('Unknown kwarg for %s: %s' % (self.__class__.__name__, kw)) self.value = args and args[0] or '' self.clsvar = kwargs.get('_clsvar', '') for mod,cls in self.mods.items(): if kwargs.get(mod, _m) is not _m: if isinstance(kwargs[mod], basestring) or \ isinstance(kwargs[mod], int) or \ isinstance(kwargs[mod], float): setattr(self, mod, utils.get_class(cls)(kwargs[mod])) else: if not isinstance(kwargs[mod], utils.get_class(cls)) and \ not (isinstance(utils.get_class(cls), Word) and (isinstance(kwargs[mod], ClassVarVar))) and \ not (issubclass(utils.get_class(cls), Word) and isinstance(kwargs[mod], ClassVar)): raise ValueError('The %s arg to %s must be of ' 'type %s' % (mod, self.__class__.__name__, cls)) setattr(self, mod, kwargs[mod])
def ask_obj(sentence): ''' retrieve sentences in clips matching the given sentence. Can use variables. ''' clps, templs = get_instances(sentence) sens = [] if clps: if isinstance(sentence, Thing): for ins in clps: sens.append(Namable.from_clips(ins)) elif isinstance(sentence, Fact): for ins in clps: i = clips.FindInstance(ins) if issubclass(utils.get_class(str(i.Class.Name)), Fact): sens.append(Fact.from_clips(ins)) return sens
def __init__(cls, classname, bases, newdict): if classname == 'Exists': # due to the ordering of things in clps.py utils.register(classname, cls) return slots = ['(slot %s (type %s) (visibility public) (pattern-match reactive))' % (mod, issubclass(utils.get_class(modclass), Number) and \ '?VARIABLE' or 'INSTANCE') for mod,modclass in cls.mods.items()] slots = ' '.join(slots) clp = '(defclass %s (is-a %s) %s)' % (classname, bases[0].__name__, slots) for mod,modclass in cls.mods.items(): if isinstance(modclass, type): cls.mods[mod] = modclass.__name__ for kls in bases: if getattr(kls, 'mods', _m): cls.mods.update(kls.mods) super(Verb, cls).__init__(classname, bases, newdict, clp=clp)
def __init__(self, subj, pred, t='now', truth=1): self.truth = truth if isinstance(pred, str): self.predicate = Exists(pred) else: if not isinstance(pred, Exists) and \ not isinstance(pred, ClassVarVar): raise ValueError('The predicate of a fact has to be a verb') self.predicate = pred if isinstance(subj, str): if utils.varpat.match(subj): self.subject = self.pred.subject(subj) else: from nl import kb try: self.subject = kb.ask_obj(Thing(subj))[0] except IndexError: self.subject = utils.get_class(subj) if not self.subject: raise ValueError('If Fact receives a string as subject' 'It has to be a variable or a thing' 'already defined in the knowledge base') else: if not isinstance(subj, Thing) and \ not isinstance(subj, Word) and \ not isinstance(subj, ClassVar) and \ not isinstance(subj, ClassVarVar): raise ValueError('Not a proper subject for fact') self.subject = subj subj = self.subject if not isinstance(subj, ClassVar) and \ not isinstance(subj, ClassVarVar) and \ (not isinstance(self.predicate, ClassVar) and not isinstance(self.predicate, ClassVarVar) and not isinstance(subj, self.predicate.subject)): raise ValueError('Not a proper subject for %s' % self.predicate.__class__.__name__) if isinstance(t, Namable): self.time = t else: self.time = Instant(t)
def __new__(cls, classname, bases=None, newdict=None): if utils.varpat.match(classname) and not bases: cls.cls = utils.get_class('Exists') return Word.__new__(cls, classname, bases, newdict)
def tonl(self): if self.value: if issubclass(self.cls, utils.get_class('Exists')): return '[%s %s]' % (self.clsvar, self.value) return '%s(%s)' % (self.value, self.clsvar) return self.ob.tonl()
def __call__(self, var='', **kwargs): if isinstance(var, basestring) and utils.varpat.match(var): return ClassVarVar(self.value, self.cls, var, **kwargs) if issubclass(self.cls, utils.get_class('Exists')): return self.cls(var, _clsvar=self.value, **kwargs) return self.cls(var, **kwargs)
def __init__(self, var, cls=None): self.value = var self.cls = cls and cls or utils.get_class('Namable') self.ob = self.cls(self.value)