Changeset 47


Ignore:
Timestamp:
May 12, 2009, 6:04:01 PM (11 years ago)
Author:
marchulsman
Message:

Path code

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • container/capcon.py

    r46 r47  
    2525    _name = "prop"
    2626    _nfields= 0
    27 
    28     #returns value of active propertys of this class
    29     @classmethod
    30     def _getCurVar(cls,obj):
    31         p = Property._getProp(obj,cls)
    32         return p.value
    3327
    3428    #register property, capname parameter can contain name
     
    219213
    220214
     215
     216class PathNode(object):
     217    __slots__ = ['lncol','rncol','lactidx','ractidx','lnode','rnode','lskip','rskip']
     218
     219    def __init__(self,left,right):
     220        self.lncol = len(left._fields)
     221        self.rncol = len(right._fields)
     222        self.lactidx = left._actidx
     223        self.ractidx = right._actidx
     224        self.lskip = False
     225        self.rskip = False
     226
     227
     228#Path property, describing node in path
     229class PathProp(Property):
     230    _name = 'PathProp'
     231   
     232    @classmethod
     233    def _create_props(cls,obj,name,params):
     234       left = params['left']
     235       right = params['right']
     236       
     237       f = numpy.arange(len(obj._fields),dtype="int32") + 1
     238   
     239       vals = PathNode(left,right)
     240       vals.lnode = params['lnode']
     241       vals.rnode = params['rnode']
     242
     243       props = itypes_py.IH_FIELDANY | itypes_py.AC_KEEP | itypes_py.P_ACTIVE
     244
     245       newprops = itypes_py.createProps([name],[cls],[f],[vals],[props])
     246       return newprops
     247
     248
     249
    221250class VarCap(Capability):
    222251    class MTFields(type):
  • container/container.py

    r46 r47  
    169169            p = self._props
    170170            #find active capabilities
     171
    171172            newcaps = tuple((c for c in p[numpy.cast[bool](p.props & itypes_py.P_ACTIVE)].type if issubclass(c,capcon.Capability)))
    172173            if(newcaps):
     
    512513
    513514    #Returns active propertys/capabilities of a certain class cls
    514     #if not found, raises an RuntimeError
     515    #if not found, returns None
    515516    def _getProp(self,cls):
    516517        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"
    520         return n
     518            p = self._props
     519
     520            #somehow comparing with classes does not work in
     521            #numpy, have to do it elementwise ..
     522            tb = numpy.zeros((len(p),),dtype=bool)
     523            for i,t in enumerate(p.type):
     524                tb[i] = (t == cls)
     525           
     526            r = p[tb & numpy.cast[bool](p.props & itypes_py.P_ACTIVE)]
     527            if(len(r) > 0):
     528                return r
     529        return None
    521530
    522531
  • container/itypes_py.py

    r46 r47  
    6161
    6262#when is the property/capability active? (default: always)
    63 AC_FIELDANY = 1 << 9
    64 AC_FIELDALL = 1 << 10
    65 AC_CHECK = 1 << 11
    66 AC_KEEP = 1 << 12
     63AC_FIELDANY = 1 << 6
     64AC_FIELDALL = 1 << 7
     65AC_CHECK = 1 << 8
     66AC_KEEP = 1 << 9
    6767
    6868#flags
    69 FL_NOMODFIELD = 1 << 17 #no change field contents
    70 FL_NOROWCHANGE = 1 << 18 #no change in nr. of rows
     69FL_NOMODFIELD = 1 << 10#no change field contents
     70FL_NOROWCHANGE = 1 << 11#no change in nr. of rows
    7171
    7272#property props
    73 P_ACTIVENR = 19
     73P_ACTIVENR = 12
    7474P_ACTIVE = 1 << P_ACTIVENR  #property is active
    7575
     
    102102    active = [True] * rows
    103103       
    104     p = xnumpy.dimarray(transpose((pname,pidgen,ptype,pfields,nfields,pvalue,active,pihtype)),object,2,2)
     104    p = xnumpy.dimarray(transpose((pname,pidgen,ptype,pfields,nfields,pvalue,pihtype)),object,2,2)
    105105    p.colnames = _propfields_names
    106106    return p
  • container/opcon.py

    r46 r47  
    4242
    4343    #combines two sources, makes a path node property
    44     def _pathNodeProps(self,source):
     44    def _pathNodeProps(self,sources):
    4545        pr = []
    4646        nl = len(self._fields)
    47         nf = numpy.empty((nl,),dtype=int)
     47        nf = numpy.zeros((nl,),dtype=int)
    4848        pos = 0
    4949
    50         for source in sources:
     50        assert (len(sources) == 2), "Path Node should have two sources"
     51
     52       
     53        #filter props arrays on inhertiance
     54        path_ids = [-1,-1]
     55        for j,source in enumerate(sources):
    5156            if(not source._props is None):
    5257                p = source._props
    53                
     58                #determine which properties should be inherited
    5459                ih = numpy.cast[bool](p.props & (itypes_py.IH_FIELDALL | itypes_py.IH_FIELDANY | itypes_py.IH_ALWAYS))
    5560                check = numpy.cast[bool](p.props & itypes_py.IH_CHECK)
    5661                for pidx in utility.find(check):
    5762                    ih[pidx] = p[pidx].type.check(self,p[pidx])
     63
     64                #if any property is inherited
    5865                if(ih.any()):
    5966                    p = p.copy()[ih]
    60                     for i,fields in enumerate(p.fields):
     67                    #adapt fields
     68                    for prow in p:
    6169                        pf = nf.copy()
    62                         pf[pos:(pos + len(fields))] = fields
    63                         p.fields[i] = pf
     70                        pf[pos:(pos + len(prow.fields))] = prow.fields
     71                        prow.fields = pf
     72
     73                        #get active path id's, reset active flag
     74                        if(prow.type is capcon.PathProp):
     75                            if(prow.props & itypes_py.P_ACTIVE):
     76                                path_ids[j] = prow.id
     77                                prow.props &= ~itypes_py.P_ACTIVE
     78
    6479                    pr.append(p)
    65 
    66        
    67 
     80            pos = pos + source.ncol
     81       
     82        #find previous nodes, deactivate them
     83        #use id as node description
     84
     85        #create param array for new path node, linking to
     86        #earlier node ids
     87        params = dict()
     88        params['left'] = sources[0]
     89        params['right'] = sources[1]
     90        params['lnode'] = path_ids[0]
     91        params['rnode'] = path_ids[1]
     92
     93        #create path node
     94        pr.append(capcon.PathProp._create_props(self,"PathProp",params))
     95
     96        #combine props arrays, determine active props
    6897        self.__finishProps(*pr)
    6998       
     
    84113            #check for duplicate ids, while keeping ordering
    85114            ids = set()
    86             remove = numpy.empty((len(props),),dtype=bool)
     115            remove = numpy.zeros((len(props),),dtype=bool)
    87116            for i,id in enumerate(props.id):
    88117                if(id in ids):
     
    98127       
    99128        if(not props is None):
    100             props.props = props.props & ~P_ACTIVE
    101129            #check if props should be active
    102130            for i,prop in enumerate(props):
    103                 if(prop.props & itypes_py.AC_KEEP):
    104                     pass
    105                 elif(prop.props & itypes_py.AC_FIELDANY):
    106                     props[i].props |= ((sum(prop.fields > 0) > 0) << P_ACTIVENR)
    107                 elif(prop.props & itypes_py.AC_FIELDALL):
    108                     props[i].props |= ((sum(prop.fields > 0) == prop.nfields) << P_ACTIVENR)
    109                 elif(prop.props & itypes_py.AC_CHECK):
    110                     props[i].props |= ((prop.type.active_check(self,prop)) << P_ACTIVENR)
    111                 else:
    112                     props[i].props |= P_ACTIVE
    113        
     131                if(not (prop.props & itypes_py.AC_KEEP)):
     132                    props.props = props.props & ~itypes_py.P_ACTIVE
     133                    if(prop.props & itypes_py.AC_FIELDANY):
     134                        props[i].props |= ((sum(prop.fields > 0) > 0) << P_ACTIVENR)
     135                    elif(prop.props & itypes_py.AC_FIELDALL):
     136                        props[i].props |= ((sum(prop.fields > 0) == prop.nfields) << P_ACTIVENR)
     137                    elif(prop.props & itypes_py.AC_CHECK):
     138                        props[i].props |= ((prop.type.active_check(self,prop)) << P_ACTIVENR)
     139                    else:
     140                        props[i].props |= itypes_py.P_ACTIVE
     141
    114142        self._props = props
    115143        self._invar.update['_props'] = props
     
    283311    def __init__(self,left,right,operator="CROSS JOIN"):
    284312        self._fields = numpy.vstack((left._fields,right._fields))
     313        self._fields.colnames = left._fields.colnames
     314        self._actidx =  numpy.hstack((left._actidx, (right._actidx + len(left._fields))))
    285315       
    286316        if(operator == "LEFT JOIN"):
     
    294324            self._pathNodeProps((left,right))
    295325        MultiOpCon.__init__(self,left,right,operator)
     326
     327
    296328
    297329#only used during optimization
  • src/base_container.c

    r46 r47  
    561561{
    562562    PyObject *tmp;
     563    if(!(PyArray_Check(value) && ((PyArrayObject*)value)->nd == 1))
     564    {
     565        PyErr_SetString(PyExc_TypeError, "Active index shoulde be a one dimensional numpy object");
     566        return -1;
     567    }
    563568    tmp = self->actidx;
    564569    Py_INCREF(value);
Note: See TracChangeset for help on using the changeset viewer.