Ejemplo n.º 1
0
 def get_query(self, vrs, ancestor, mod_path):
     ci = utils.clips_instance(ancestor, mod_path)
     constraint = []
     if not self.value or self.value in vrs:
         if self.clsvar and self.clsvar not in vrs:
             if vrs[self.value]:
                 vrs[self.clsvar] = (
                            utils.clips_instance(*(vrs[self.value])),
                            [], ['class'])
             else:
                 vrs[self.clsvar] = (ancestor, mod_path, ['class'])
         return self.ob.get_query(vrs, ancestor, mod_path)
     else:
         vrs[self.value] = (ancestor, mod_path)
         if self.clsvar in vrs:
             if vrs[self.clsvar]:
                 return ['(eq (class %(var)s) %(cls)s)' % {
                                 'var': ci,
                      'cls': utils.clips_instance(*(vrs[self.clsvar]))}]
             else:
                 return ['(eq (class %(var)s) %(cls)s)' % {
                                 'var': ci,
                                 'cls': self.clsvar}]
         vrs[self.clsvar] = (ancestor, mod_path, ['class'])
         return ['''(or
                       (eq (class %(ci)s) %(cls)s)
                       (subclassp (class %(ci)s) %(cls)s))''' % {
                                 'ci': ci,
                                 'cls': self.cls.__name__}]
Ejemplo n.º 2
0
 def get_isc(self, queries, vrs, ancestor, mod_path):
     """
     get instance-set condition;
     modify (instance-set templates, instance-set queries)
     """
     ci = utils.clips_instance(ancestor, mod_path)
     if utils.varpat.match(self.value):
         if self.value in vrs:
             if vrs[self.value]:
                 queries.append('(eq %s %s)' % (ci,
                                  utils.clips_instance(*(vrs[self.value]))))
             else:
                 queries.append('(eq %s ?%s)' % (ci, self.value))
         else:
             vrs[self.value] = (ancestor, mod_path)
             queries.append('(eq (class %s) Duration)' % ci)
         return
     start = getattr(self, 'start', _m)
     if utils.varpat.match(str(start.value)) and not vrs.has_key(start.value):
         vrs[start.value] = (ancestor, mod_path+('start',))
     nstart = start.get_isc([], vrs, ancestor, mod_path)
     end = getattr(self, 'end', _m)
     if utils.varpat.match(str(end.value)) and not vrs.has_key(end.value):
         vrs[end.value] = (ancestor, mod_path + ('end',))
     nend = end.get_isc([], vrs, ancestor, mod_path)
     queries.append( '''
                (and (eq (class (send ?%(parent)s get-time)) Duration)
                     (<= (send (send ?%(parent)s get-time) get-start) %(start)s)
                     (or (= (send (send ?%(parent)s get-time) get-end) %(end)s)
                         (and (= %(end)s -1.0)
                              (>= (send (send ?%(parent)s get-time) get-end) (python-call ptime)))
                         (and (<> %(end)s -1.0)
                              (> (send (send ?%(parent)s get-time) get-end) %(end)s))))
         ''' % {'parent': ancestor, 'start': nstart, 'end': nend} )
Ejemplo n.º 3
0
    def get_slot_constraint(self, vrs):
        """
        return clips snippet to be used
        when the variable is predicate or subject
        and the sentence is in the tail of a rule.

        In case we are a Noun:

        In case we are a Verb:
        * in case the clsvar is vrs but not the var:
          * in case


          XXX dos cosas: aquí:

          (logical (object (is-a Fact) (subject ?P1&:(or (eq (class ?P1) Person) (subclassp (class ?P1) Person))) (predicate ?Y10&:(or (eq (class ?Y10) WfAction) (subclassp (class ?Y10) WfAction))&:(eq (send ?Y10 get-what) ?C1)) (time ?I1) (truth 1))) (logical (object (is-a Fact) (subject ?C1&:(eq ?C1 (send ?Y10 get-what)))
         se repite
        (eq ?C1 (send ?Y10 get-what))

        otra cosa: tanto en ClassVar como en ClassVarVar, al hacer get_slot_constraint (NO he mirado get_constraint, pero será igual) no se toma en cuanta cuando clsvar es var y está en vrs.

        ClassVar lo tiene que hacer es, en __call__, si no se le llama con una variable sino con argumantos, y es un verbo (en el caso de una cosa da igual, está unívocamente determinada por el nombre propio), tiene que pasar el clsvar para que verb lo tenga en cuenta cuando haga get_slot constraint (y posiblemente los demás)

        ClassVarVar lo tiene que tener en cuenta en get_slot_constraint cuando self value está en vrs, entonces tiene que pasarlo a self ob para que lo tenga en cuenta en get_var_slot_constraint


        HAY QUE PONERSELO TB A THING, TODO LO DEL _CLSVAR
        """
        if self.value in vrs:
            if self.clsvar and self.clsvar not in vrs:
                if vrs[self.value]:
                    vrs[self.clsvar] = (
                               utils.clips_instance(*(vrs[self.value])),
                               [], ['class'])
                else:
                    vrs[self.clsvar] = (self.value, [], ['class'])
            return self.ob.get_var_slot_constraint(vrs, self.value)
        else:
            vrs[self.value] = ()
            if self.clsvar in vrs:
                if vrs[self.clsvar]:
                    return '?%(var)s&:(eq (class ?%(var)s) %(cls)s)' % {
                                    'var': self.value,
                         'cls': utils.clips_instance(*(vrs[self.clsvar]))}
                else:
                    return '?%(var)s&:(eq (class ?%(var)s) %(cls)s)' % {
                                    'var': self.value,
                                    'cls': self.clsvar}
            vrs[self.clsvar] = (self.value, [], ['class'])
            return '''?%(var)s&:(or
                                 (eq (class ?%(var)s) %(cls)s)
                                 (subclassp (class ?%(var)s) %(cls)s))''' % {
                                    'var': self.value,
                                    'cls': self.cls.__name__}
Ejemplo n.º 4
0
 def get_query(self, vrs, ancestor, mod_path):
     ci = utils.clips_instance(ancestor, mod_path)
     constraint = []
     if self.value:
         if self.value in vrs:
             if vrs[self.value]:
                 if self.clsvar and self.clsvar not in vrs:
                     vrs[self.clsvar] = (
                            utils.clips_instance(*(vrs[self.value])),
                            [], ['class'])
                 v_ci = utils.clips_instance(*(vrs[self.value]))
                 constraint.append('(eq %s %s)' % (v_ci, ci))
             else:
                 if self.clsvar and self.clsvar not in vrs:
                     vrs[self.clsvar] = (ancestor, mod_path, ['class'])
                 constraint.append('(eq %s ?%s)' % (ci, self.value))
         else:
             vrs[self.value] = (ancestor, mod_path)
     else:
         if self.clsvar:
             if self.clsvar in vrs:
                 if vrs[self.clsvar]:
                     constraint.append(
                      '(eq (class %s) %s)' % (ci,
                          utils.clips_instance(*(vrs[self.clsvar]))))
                 else:
                     constraint.append(
                      '(eq (class %s) %s)' % (ci, self.clsvar))
             else:
                 vrs[self.clsvar] = (ancestor, mod_path, ('class',))
                 constraint.append(
                     '''(or (eq (class %(val)s) %(cls)s)
                              (subclassp (class %(val)s) %(cls)s))''' % {
                                        'val': ci,
                                        'cls': self.__class__.__name__})
         else:
             constraint.append(
                     '''(or (eq (class %(val)s) %(cls)s)
                              (subclassp (class %(val)s) %(cls)s))''' % {
                                        'val': ci,
                                        'cls': self.__class__.__name__})
         for mod,cls in self.mods.items():
             mod_o =  getattr(self, mod, _m)
             if mod_o is not _m:
                 constraint_meth = getattr(mod_o, 'get_query_cls',
                                           mod_o.get_query)
                 constraint += constraint_meth(vrs,
                                                ancestor,
                                              mod_path + (mod,))
     return constraint
Ejemplo n.º 5
0
 def get_query(self, vrs, ancestor, mod_path):
     ci = utils.clips_instance(ancestor, mod_path)
     if utils.varpat.match(self.value):
         constraint = self.get_var_query(vrs, ancestor, mod_path, ci)
     else:
         constraint = '(eq %s [%s])' % (ci, self.value)
     return [constraint]
Ejemplo n.º 6
0
 def get_query(self, vrs, ancestor, mod_path):
     ci = utils.clips_instance(ancestor, mod_path)
     if self.value in vrs:
         return self.ob.get_query(vrs, ancestor, mod_path)
     else:
         vrs[self.value] = (ancestor, mod_path)
         return ['''(or (eq %(ci)s %(cls)s)
                         (subclassp %(ci)s %(cls)s))''' % {
                                 'ci': ci,
                                 'cls': self.cls.__name__}]
Ejemplo n.º 7
0
 def get_var_slot_constraint(self, vrs, val):
     if self.value in vrs:
         if vrs[self.value]:
             return '?%(val)s&:(eq ?%(val)s %(var)s)' % {'val': val,
                                    'var': utils.clips_instance(*(vrs[self.value]))}
         else:
             return '?%s' % self.value
     else:
         vrs[self.value]= ()
         return class_constraint % {'val': self.value,
                                        'cls': self.__class__.__name__}
Ejemplo n.º 8
0
 def get_slot_constraint(self, vrs):
     """
     build rule CE constraint for clips
     for a slot constraint for a prop in a rule
     """
     if utils.varpat.match(self.value):
         if self.clsvar and self.clsvar not in vrs:
             if vrs[self.value]:
                 vrs[self.clsvar] = (
                            utils.clips_instance(*(vrs[self.value])),
                            [], ['class'])
             else:
                 vrs[self.clsvar] = (self.value, [], ['class'])
         return self.get_var_slot_constraint(vrs, self.value)
     newvar = utils._newvar()
     if self.clsvar:
         if self.clsvar in vrs:
             if vrs[self.clsvar]:
                 constraint = [
                  '?%(newvar)s&:(eq (class ?%(newvar)s) %(var)s)' % {
                      'newvar': newvar,
                      'var': utils.clips_instance(*(vrs[self.clsvar]))}]
             else:
                 constraint = [
                  '?%(newvar)s&:(eq (class ?%(newvar)s) %(var)s)' % {
                      'newvar': newvar,
                      'var': self.clsvar}]
         else:
             vrs[self.clsvar] = (newvar, [], ('class',))
             constraint = [class_constraint % {'val': newvar,
                                      'cls': self.__class__.__name__}]
     else:
         constraint = [class_constraint % {'val': newvar,
                                      'cls': self.__class__.__name__}]
     for mod,cls in self.mods.items():
         mod_o =  getattr(self, mod, _m)
         if mod_o is not _m:
             constraint_meth = getattr(mod_o, 'get_constraint_cls',
                                       mod_o.get_constraint)
             constraint.append(constraint_meth(vrs, newvar, (mod,)))
     return ''.join(constraint)
Ejemplo n.º 9
0
 def get_var_query(self, vrs, ancestor, mod_path, ci):
     constraint = ''
     if self.value in vrs:
         if vrs[self.value]:
             v_ci = utils.clips_instance(*(vrs[self.value]))
             constraint = '(eq %s %s)' % (v_ci, ci)
         else:
             constraint = '(eq %s ?%s)' % (ci, self.value)
     elif not isinstance(self, Number):
         constraint = '(or (eq (class %(ci)s) %(cls)s) (subclassp (class %(ci)s) %(cls)s))' % {'ci': ci, 'cls': self.__class__.__name__}
     vrs[self.value] = (ancestor, mod_path)
     return constraint
Ejemplo n.º 10
0
 def get_isc(self, queries, vrs, ancestor, mod_path):
     """
     """
     num = self._get_number(vrs)
     if not utils.varpat.match(num[1:]) or \
                    vrs.has_key(num[1:]):
         if vrs.has_key(num[1:]) and vrs[num[1:]]:
             num = utils.clips_instance(*(vrs[num[1:]]))
         queries.append( '''
            (or (and (eq (class ?%(parent)s:time) Duration)
                     (<= (send ?%(parent)s:time get-start) %(self)s)
                     (or (and (= (send ?%(parent)s:time get-end) -1.0)
                              (>= (python-call ptime) %(self)s))
                         (>= (send ?%(parent)s:time get-end) %(self)s)))
                (eq ?%(parent)s:time %(self)s))
         ''' % {'parent': ancestor, 'self': num} )
     return num
Ejemplo n.º 11
0
 def put(self, vrs, name=None):
     """
     put pred in clips as a make-instance action.
     """
     if self.value and utils.varpat.match(self.value):
         return self.put_var(vrs)
     slots = []
     for mod in self.mods:
         mod_o = getattr(self, mod, _m)
         if mod_o is not _m:
             put_meth = getattr(mod_o, 'clsput', mod_o.put)
             slots += [mod, put_meth(vrs)]
     slots = ' '.join(slots)
     if self.clsvar:
         if vrs[self.clsvar]:
             return '(add-pred %s %s)' % (
                 utils.clips_instance(*(vrs[self.clsvar])), slots)
         else:
             return '(add-pred %s %s)' % (self.clsvar, slots)
     return '(add-pred %s %s)' % (self.__class__.__name__, slots)
Ejemplo n.º 12
0
 def get_query_cls(self, vrs, ancestor, mod_path):
     ci = utils.clips_instance(ancestor, mod_path)
     return ['(eq %s %s)' % (self.__name__, ci)]
Ejemplo n.º 13
0
 def put_var(self, vrs):
     if self.value in vrs and vrs[self.value]:
         return utils.clips_instance(*(vrs[self.value]))
     return '?%s' % self.value