Changeset 46


Ignore:
Timestamp:
May 12, 2009, 11:42:12 AM (11 years ago)
Author:
marchulsman
Message:

Work on capabilities

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • container/capcon.py

    r42 r46  
    1616            dups.add(cap)
    1717            ncaps.append(cap)
    18     caps = tuple(ncaps)
    19            
    20     bases = tuple(caps) + (cls,)
     18    ncaps.append(cls)
     19    bases = tuple(ncaps)       
    2120    if(not bases in concaptypes):
    2221        concaptypes[bases] = type(cls.__name__ + str(concapseqgen()),bases,{})
     
    2625    _name = "prop"
    2726    _nfields= 0
    28    
    29 
    30        
    31    
     27
     28    #returns value of active propertys of this class
    3229    @classmethod
    3330    def _getCurVar(cls,obj):
    3431        p = Property._getProp(obj,cls)
    3532        return p.value
    36            
    37        
    38 
     33
     34    #register property, capname parameter can contain name
    3935    @classmethod
    4036    def _register(cls,obj,params):
     
    4642       newprops = cls._create_props(obj,name,params)
    4743
    48        if('_props' in obj.__dict__):
     44       if(not obj._props is None):
    4945           for p in obj._props:
    5046               if(p.type == newprops.type and id(p.value) == id(newprops.value) and p.name == newprops.name):
     
    5248           newprops = numpy.vstack((newprops,obj._props))
    5349           newprops.colnames = itypes_py._propfields_names
    54        else:
    55            newprops = newprops
    5650
    5751       obj._props = newprops
    58 
    59 
     52   
     53    #create new property array
    6054    @classmethod
    6155    def _create_props(cls,obj,name,params):
     
    6357       return newprops
    6458
    65 
    66     def __getattr__(self,name):
    67         try:
    68             return super(Property,self).__getattr__(name)
    69         except AttributeError,e:
    70             caps = self.__class__.__bases__[:-1]
    71             for cap in caps:
    72                 if(cap._nfields > 0 and name in cap._fnames):
    73                     idx = cap._fnames.index(name)
    74                     d = self._getCapFields(cap)[idx]
    75                     if(d != -1):
    76                        self.__dict__[name] = opcon.SelectCon(self, d) 
    77                        return self.__dict__[name]
    78             raise e   
    79              
    8059
    8160class Capability(Property):
    8261    _name = "cap"
    83     def _getCapFields(self,cls):
    84         n = self._getProp(cls)
    85         fields = numpy.zeros((cls._nfields,),dtype="int32") - 1
    86 
    87         for i,f in enumerate(n.fields):
    88             if(f >= 0):
    89                 fields[f] = i
    90         return tuple(fields)
    91    
    92 
     62   
     63    #overloads property._register
    9364    @classmethod
    9465    def _register(cls,obj,params):
    9566       super(Capability,cls)._register(obj,params)     
    96        
     67     
     68       #FIXME: make more efficient/sane
     69       #obtain real class
    9770       ocls = obj.__class__
    9871       if(len(ocls.__bases__) > 1):
     
    10174           rcls = ocls
    10275       
     76       #obtain capabilities
    10377       curcaps = ocls.__bases__[0:-1]
     78       
     79       #add current capabilities to base classes
    10480       obj.__class__ = getConCapType((cls,) + curcaps,rcls)
    10581   
     82   
     83    #match fields in capability with fields in container
    10684    @classmethod
    10785    def _find_fields(cls,obj,params):
     
    11997                   raise RuntimeError, "Cannot find field %s" % local_name
    12098        return fields
    121    
     99
     100   
     101    #create new property array, matching fields
    122102    @classmethod
    123103    def _create_props(cls,obj,name,params):
    124104       fields = cls._find_fields(obj,params)
    125        newprops = itypes_py.createProps([name],[cls],[fields],None,[itypes_py.IH_FIELDALL])
    126        return newprops
    127 
    128 
     105       newprops = itypes_py.createProps([name],[cls],None,None,[itypes_py.IH_FIELDALL])
     106       return newprops
     107
     108    #overloads getattr
    129109    def __getattr__(self,name):
    130110        try:
    131             return super(Capability,self).__getattr__(name)
     111            return super(Property,self).__getattr__(name)
    132112        except AttributeError,e:
     113            #if parameter not found in regular way, try
     114            #capability fields
    133115            caps = self.__class__.__bases__[:-1]
    134116            for cap in caps:
     
    136118                    idx = cap._fnames.index(name)
    137119                    d = self._getCapFields(cap)[idx]
    138                     if(d != -1):
    139                         self.__dict__[name] = opcon.SelectCon(self, d) 
    140                         return self.__dict__[name]
     120                    if(not d is None):
     121                       self.__dict__[name] = opcon.SelectCon(self, d) 
     122                       return self.__dict__[name]
    141123            raise e   
    142    
    143    
     124             
     125    #returns list of active field names (includes capability
     126    #fields) for use by tab-completion
    144127    def _getAttributeNames(self):
    145128        res = super(Capability,self)._getAttributeNames()
     
    150133                res += [cap._fnames[i] for i,fidx in enumerate(d) if fidx >=0]
    151134        return res
    152 
    153          
    154     def _getAttributeNames(self):
    155      try:
    156         res = super(Capability,self)._getAttributeNames()
    157         caps = self.__class__.__bases__[:-1]
    158         for cap in caps:
    159             if(cap._nfields > 0 and len(cap._fnames) > 0):
    160                 d = self._getCapFields(cap)
    161                 res += [cap._fnames[i] for i,fidx in enumerate(d) if fidx >=0]
    162         return res
    163      except:
    164         traceback.print_exc()
     135   
     136    #returns an array with indexes of fields of this property
     137    #(array corresponds to _fnames array)
     138    def _getCapFields(self,cls):
     139        n = self._getProp(cls)
     140        fields = [None] * cls._nfields
     141        #there can be multiple propertys for this class
     142        for prop in n:
     143            #for each field position
     144            for i,field in enumerate(n.fields):
     145                #see if its part of this property
     146                if(f > 0):
     147                    #if so add it to fields structure
     148                    if(fields[f] is None):
     149                        fields[f] = i
     150                    elif(isinstance(fields[f],list)):
     151                        fields[f].append(i)
     152                    else:
     153                        fields[f] = [fields[f],i]
     154        return tuple(fields)
     155 
     156         
    165157
    166158class ConSetCap(Capability):
  • container/container.py

    r43 r46  
    166166    #Set class of this object based on capabilities
    167167    def _set_props_class(self):
    168         if('_props' in self.__dict__):
     168        if(not self._props is None):
    169169            p = self._props
    170170            #find active capabilities
    171             newcaps = tuple((c for c in p[p.active == True].type if issubclass(c,capcon.Capability)))
     171            newcaps = tuple((c for c in p[numpy.cast[bool](p.props & itypes_py.P_ACTIVE)].type if issubclass(c,capcon.Capability)))
    172172            if(newcaps):
    173173                self.__class__ = capcon.getConCapType(newcaps,self.__class__)
     
    450450            for sem in self.modify_sems:
    451451                sem.signal()
    452    
     452   
     453    #returns list of active field names (for use by
     454    #tab-completion mechanism)
    453455    def _getAttributeNames(self):
    454456        return list(self._activefields.name)
     
    507509    def back(self):
    508510        return opcon.BackCon(self)
    509    
     511   
     512
     513    #Returns active propertys/capabilities of a certain class cls
     514    #if not found, raises an RuntimeError
    510515    def _getProp(self,cls):
    511         if('_props' in self.__dict__):
    512             n = []
    513             for p in self._props:
    514                 if(p.type == cls and p.active):
    515                     n.append(p)
    516             n = numpy.vstack(n)
    517             n.colnames = self._props.colnames
    518             if(len(n.shape) > 1):
    519                 n = n[0]
    520         else:
    521             raise RuntimeError,"_props not found"
     516        if(not self._props is None):
     517            n = p[(p.type == cls) & numpy.cast[bool](p.props & itypes_py.P_ACTIVE)]
     518        else:
     519            raise RuntimeError,"property not found"
    522520        return n
    523521
  • container/itypes_py.py

    r43 r46  
    6464AC_FIELDALL = 1 << 10
    6565AC_CHECK = 1 << 11
     66AC_KEEP = 1 << 12
    6667
    6768#flags
     
    104105    p.colnames = _propfields_names
    105106    return p
    106 
    107 empty_props = xnumpy.dimarray([],object)
    108 empty_props.shape = (len(_propfields_names),0)
    109 empty_props.colnames = _propfields_names
    110107
    111108
  • container/opcon.py

    r45 r46  
    88
    99class OpCon(container.Container):
    10 
    11 
    1210    #inherits props if no field is inherited from sources
    1311    def _noFieldProps(self,*sources):
    1412        pr = []
    1513        for source in sources:
    16             if(source._props.shape[1] > 0):
    17                 p = source._props.copy()
    18                 p.fields = -1 * numpy.abs(p.fields)
    19 
     14            if(not source._props is None):
     15                p = source._props
    2016                ih = numpy.cast[bool](p.props & itypes_py.IH_ALWAYS)
    2117                check = numpy.cast[bool](p.props & itypes_py.IH_CHECK)
     
    2319                    ih[pidx] = p[pidx].type.check(self,p[pidx])
    2420
    25                 pr.append(p)
     21                if(ih.any()):
     22                    p = p.copy()[ih]
     23                    p.fields = map(numpy.abs,p.fields)
     24                    p.fields = -1 * p.fields
     25                    pr.append(p)
    2626        self.__finishProps(*pr)
    2727   
    2828    #inherits props if active field have changed
    2929    def _actFieldChgProps(self,source):
    30         props = source._props.copy()
    31 
    32         #make all active fields positive and all inactive negative
    33         if(props.shape[1] > 0):
     30        if(not source._props is None):
     31            props = source._props.copy()
     32
     33            #make all active fields positive and all inactive negative
    3434            v = numpy.empty((len(self._fields),),dtype=int)
    3535            v[:] = -1
    3636            v[self._actidx] = 1
    37             for i,fields in enumerate(props.fields):
    38                 props.fields[i] = numpy.abs(fields) * v
    39 
    40         self.__finishProps(props)
    41    
     37            props.fields = map(numpy.abs,props.fields)
     38            props.fields = props.fields * v
     39            self.__finishProps(props)
     40   
     41   
     42
     43    #combines two sources, makes a path node property
     44    def _pathNodeProps(self,source):
     45        pr = []
     46        nl = len(self._fields)
     47        nf = numpy.empty((nl,),dtype=int)
     48        pos = 0
     49
     50        for source in sources:
     51            if(not source._props is None):
     52                p = source._props
     53               
     54                ih = numpy.cast[bool](p.props & (itypes_py.IH_FIELDALL | itypes_py.IH_FIELDANY | itypes_py.IH_ALWAYS))
     55                check = numpy.cast[bool](p.props & itypes_py.IH_CHECK)
     56                for pidx in utility.find(check):
     57                    ih[pidx] = p[pidx].type.check(self,p[pidx])
     58                if(ih.any()):
     59                    p = p.copy()[ih]
     60                    for i,fields in enumerate(p.fields):
     61                        pf = nf.copy()
     62                        pf[pos:(pos + len(fields))] = fields
     63                        p.fields[i] = pf
     64                    pr.append(p)
     65
     66       
     67
     68        self.__finishProps(*pr)
     69       
     70
    4271    #inherit props if only row content has changed
    4372    def _rowChgProps(self,source):
     
    6695            props = psources[0]
    6796        else:
    68             props = itypes_py.empty_props
    69        
    70         if(props.shape[1] > 0):
     97            props = None
     98       
     99        if(not props is None):
    71100            props.props = props.props & ~P_ACTIVE
    72101            #check if props should be active
    73102            for i,prop in enumerate(props):
    74                 if(prop.props & itypes_py.AC_FIELDANY):
     103                if(prop.props & itypes_py.AC_KEEP):
     104                    pass
     105                elif(prop.props & itypes_py.AC_FIELDANY):
    75106                    props[i].props |= ((sum(prop.fields > 0) > 0) << P_ACTIVENR)
    76107                elif(prop.props & itypes_py.AC_FIELDALL):
     
    252283    def __init__(self,left,right,operator="CROSS JOIN"):
    253284        self._fields = numpy.vstack((left._fields,right._fields))
    254         self._fields.lastid = self._fields.id
    255285       
    256286        if(operator == "LEFT JOIN"):
     
    261291            self._fields.type = [itypes_py.createType(object)] * (left.ncol + right.ncol)
    262292           
    263         self._fields.id = [itypes_py.idgen() for i in range(left.ncol + right.ncol)]
    264        
    265293        if(left._invar.register(self,(right._invar,operator))):
    266             self.deriveMultipleProps((left,right),(None,None),(0,left.ncol),left.ncol+right.ncol,True)
     294            self._pathNodeProps((left,right))
    267295        MultiOpCon.__init__(self,left,right,operator)
    268296
  • container/srccon.py

    r43 r46  
    6060        self._actidx = numpy.arange(len(fields)).view(xnumpy.XArray)
    6161        self._invar = container.Invar()
    62         self._props = itypes_py.empty_props
    6362
    6463        if(not data is None):
  • src/base_container.c

    r37 r46  
    497497    if(self->props == NULL)
    498498    {
    499         PyErr_SetString(PyExc_TypeError, "BaseContainer object not properly initialized (no props).");
    500         return NULL;
     499        Py_RETURN_NONE;
    501500    }   
    502501    Py_INCREF(self->props);
     
    507506{
    508507    PyObject *tmp;
    509     if(!(PyArray_Check(value) && ((PyArrayObject*)value)->nd == 2))
     508    if(!(value == Py_None || (PyArray_Check(value) && ((PyArrayObject*)value)->nd == 2)))
    510509    {
    511510        PyErr_SetString(PyExc_TypeError, "Props objects should be a two dimensional numpy object");
  • todo.txt

    r44 r46  
    66- replace _fields en _actidx in container with _type
    77
     8- for a left/right join, field with NULL values we have to use the
     9  object type to be able to store the NULL. Any other options?
    810
     11
Note: See TracChangeset for help on using the changeset viewer.