Changeset 297

Show
Ignore:
Timestamp:
01/30/08 16:49:59 (10 months ago)
Author:
johnbywater
Message:

Just checkin' in terrible but functional bitemporal property spike solution before refactoring.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/dm/db.py

    r296 r297  
    743743            logger.debug(message) 
    744744        self.domainObject.id = self.id 
    745         recordedTime = timepoint.recorded 
    746745        for metaAttr in self.meta.attributes: 
    747746            dbName = metaAttr.dbName 
     
    752751                ) 
    753752                logger.debug(message) 
    754             if metaAttr.dom.isTemporal: 
     753            if domName == 'parent' and metaAttr.isDomainObjectRef: 
     754                # Continue if it's already loaded.... 
     755                if self.parent and self.parent.domainObject: 
     756                    mapperValue = self.parent.domainObject 
     757                    self.domainObject.parent = mapperValue 
     758                    message = "Continuing with loaded parent: %s" % ( 
     759                        self.domainObject.parent 
     760                    ) 
     761                    logger.debug(message) 
     762                    continue 
     763            if metaAttr.dom.isTemporal or metaAttr.dom.isBitemporalActual: 
    755764                r = metaAttr.dom.createTemporalCollection(self.domainObject) 
     765                if metaAttr.dom.isBitemporalActual: 
     766                    r.sortOnName = 'dateCreated' 
     767                    referenceTime = timepoint.actual 
     768                    mostRecents = r.findDomainObjects( 
     769                        __loadedList__=loadedList, 
     770                        __dateCreatedOnOrBefore__=referenceTime 
     771                    ) 
     772                    mostRecent = r.findFirstDomainObject( 
     773                        __loadedList__=loadedList, 
     774                        __dateCreatedOnOrBefore__=referenceTime 
     775                    ) 
     776                    #print "LOAD: Bitemporal actual %s at %s" % ([i for i in mostRecents], referenceTime) 
     777                    if mostRecent: 
     778                        print "!!!!!! Bitemporal actuals for recorded time: %s" % timepoint.recorded 
     779                        print "" 
     780                        print [i for i in mostRecents] 
     781                        print "" 
     782                        print "!!!!!! Bitemporal latest for actual time: %s" % timepoint.actual 
     783                        print "" 
     784                        print mostRecent 
     785                        print "" 
     786                        print "!!!!!!" 
     787 
     788                elif metaAttr.dom.isBitemporal: 
     789                    referenceTime = timepoint.recorded 
     790                    #print "LOAD: Bitemporal recorded at %s " % referenceTime 
     791                else: 
     792                    referenceTime = timepoint.recorded 
     793                    #print "LOAD: Temporal recorded at %s" % referenceTime 
    756794                mostRecent = r.findFirstDomainObject( 
    757795                    __loadedList__=loadedList, 
    758                     __dateCreatedOnOrBefore__=recordedTime 
     796                    __dateCreatedOnOrBefore__=referenceTime 
    759797                ) 
    760798                if mostRecent: 
    761799                    mappedValue = mostRecent.recordedValue 
    762800                else: 
     801                    #print "LOAD: No record, creating initial value..." 
    763802                    mappedValue = metaAttr.dom.createInitialValue( 
    764803                        self.domainObject 
    765804                    ) 
     805                #print "LOAD: Temporal '%s' value: %s" % (metaAttr.dom.name, mappedValue) 
    766806            elif metaAttr.isDomainObjectRef: 
    767807                mapper = getattr(self, dbName) 
     
    798838        isChanged = False 
    799839        for metaAttr in self.meta.attributes: 
    800             if metaAttr.dom.isTemporal
     840            if metaAttr.dom.isTemporal or metaAttr.dom.isBitemporalActual
    801841                domValue = getattr(self.domainObject, metaAttr.domName) 
    802842                r = metaAttr.dom.createTemporalCollection(self.domainObject) 
     
    804844                loadedList[self.domainObject] = self.domainObject 
    805845                mostRecent = r.findFirstDomainObject(__loadedList__=loadedList) 
    806                 #print "SAVE: Dom value (%s)" % (domValue) 
     846                print "SAVE: Attribute '%s' value: %s" % (metaAttr.dom.name, domValue) 
     847                if (r.ownerName != 'parent') or (r.owner != self.domainObject): 
     848                    msg = "r: %s %s %s" % (r, r.ownerName, r.owner) 
     849                    raise Exception, msg 
    807850                if mostRecent != None: 
    808                     #print "SAVE: Most recent (%s)" % (mostRecent.recordedValue) 
    809                     if domValue != mostRecent.recordedValue: 
    810                         #print "SAVE: Stale version record. Creating new..." 
    811                         r.create(recordedValue=domValue, 
    812                             __loadedList__=loadedList) 
     851                    print "SAVE: Recorded value: %s" % mostRecent.recordedValue 
     852                    if metaAttr.dom.isBitemporal or (domValue != mostRecent.recordedValue): 
     853                        if metaAttr.dom.isBitemporal: 
     854                            print "SAVE: Creating new bitemporal history." 
     855                            newRecorded = r.create(__loadedList__=loadedList) 
     856                            #newRecorded.parent = self.domainObject 
     857                            #newRecorded.save() 
     858                            print "SAVE: %s" % newRecorded 
     859                            actualsMeta = mostRecent.meta.attributeNames['recordedValue'] 
     860                            newActuals = actualsMeta.createTemporalCollection(newRecorded) 
     861                            print "SAVE: Copying old bitemporal actual records...." 
     862                            oldActuals = actualsMeta.createTemporalCollection(mostRecent) 
     863                            #msg = "newActuals: %s %s %s" % (newActuals, newActuals.ownerName, newActuals.owner) 
     864                            #raise Exception, msg 
     865 
     866                            for oldActual in oldActuals: 
     867                                print "SAVE: Copying old bitemporal actual: %s" % oldActual.recordedValue 
     868                                newActual = newActuals.create( 
     869                                    recordedValue=oldActual.recordedValue, 
     870                                    dateCreated=oldActual.dateCreated, 
     871                                    __loadedList__=loadedList 
     872                                ) 
     873                                print "SAVE: %s" % newActual 
     874                            print "SAVE: Setting new bitemporal actual.." 
     875                            newRecorded.recordedValue = domValue 
     876                            newRecorded.save() 
     877                        elif metaAttr.dom.isBitemporalActual: 
     878                            print "SAVE: Creating new bitemporal actual: %s" % domValue 
     879                            newActual = r.create( 
     880                                recordedValue=domValue, 
     881                                dateCreated=timepoint.actual, 
     882                                __loadedList__=loadedList 
     883                            ) 
     884                            print "SAVE: %s" % newActual 
     885                        else: 
     886                            print "SAVE: Creating new temporal: %s" % domValue 
     887                            r.create( 
     888                                recordedValue=domValue, 
     889                                __loadedList__=loadedList 
     890                            ) 
    813891                    else: 
    814                         pass # print "SAVE: Fresh version record." 
     892                        print "SAVE: Fresh version record. No change needed." 
    815893                else: 
    816                     #print "SAVE: No version record. Creating new..." 
    817                     r.create(recordedValue=domValue,__loadedList__=loadedList) 
     894                    print "SAVE: No version record. Creating first." 
     895                    #r.ownerName = 'parent' 
     896                    #r.owner = self.domainObject 
     897                    if metaAttr.dom.isBitemporal: 
     898                        first = r.create( 
     899                            __loadedList__=loadedList 
     900                        ) 
     901                        first.recordedValue = domValue 
     902                        #first.parent = self.domainObject 
     903                        #first.save() 
     904                        print "SAVE: Created first bitemporal recorded record: %s" % first 
     905 
     906                    elif metaAttr.dom.isBitemporalActual: 
     907                        first = r.create( 
     908                            recordedValue=domValue, 
     909                            dateCreated=timepoint.actual, 
     910                            __loadedList__=loadedList 
     911                        ) 
     912                        print "SAVE: Created first bitemporal actual record: %s" % first 
     913                    else: 
     914                        first = r.create( 
     915                            recordedValue=domValue, 
     916                            __loadedList__=loadedList 
     917                        ) 
     918                        print "SAVE: Created first temporal record: %s" % first 
     919                     
    818920            elif metaAttr.isDomainObjectRef: 
    819921                domainObject = getattr(self.domainObject, metaAttr.domName) 
  • trunk/src/dm/dom/meta.py

    r296 r297  
    9494    def createDomainClass(self, baseClass): 
    9595        className = self.name 
    96         classAttrs = {'meta': self} 
     96        classAttrs = { 
     97            'meta': self, 
     98            'isUnique': self.isUnique, 
     99            'isCached': self.isCached, 
     100        } 
    97101        return self.createClass(className, baseClass, classAttrs) 
    98102 
     
    110114            raise Exception(msg) 
    111115        return self.attributeNames[attrName] 
    112  
    113  
    114 class TemporalCollection(object): 
    115  
    116     def __init__(self, register): 
    117         self.register = register 
    118  
    119     def __get__(self, obj, type=None): 
    120         print "Got __get__()" 
    121  
    122     def __set__(self, obj, value): 
    123         print "Got __set__(%s)" % value 
    124116 
    125117 
     
    156148        if self.isBitemporal: 
    157149            self.isTemporal = True 
    158         self.isBitemporal = False  # Cut this out for the moment. 
     150        self.isBitemporalActual = False 
    159151        self.temporalDomainClass = None 
    160152 
     
    184176    def setTemporalDomainClass(self, domainClass): 
    185177        self.temporalDomainClass = domainClass 
    186         self.temporalDomainClass.registerKeyName = 'dateCreated' 
     178        self.temporalDomainClass.registerKeyName = 'id' 
     179        self.temporalDomainClass.isUnique = False 
    187180        self.temporalDomainClass.isCached = True 
    188         self.temporalDomainClass.sortOnName = 'id' 
     181        if self.isBitemporalActual: 
     182            self.temporalDomainClass.sortOnName = 'dateCreated' 
     183        else: 
     184            self.temporalDomainClass.sortOnName = 'id' 
    189185        self.temporalDomainClass.sortAscending = False 
    190186 
    191187    def createTemporalCollection(self, domainObject): 
     188        if not domainObject: 
     189            raise Exception, "Need a domain object! %s" % domainObject 
    192190        register = self.temporalDomainClass.createRegister() 
     191        register.owner = domainObject 
    193192        register.ownerName = 'parent' 
    194         register.owner = domainObject 
    195193        return register 
    196194         
     
    265263             attrMeta.isTemporal = True 
    266264             attrMeta.isBitemporal = False 
     265             attrMeta.isBitemporalActual = True 
    267266        else: 
    268267             attrMeta.isTemporal = False 
     268        attrMeta.isRequired = self.isRequired 
    269269        return attrMeta 
    270270 
  • trunk/src/dm/dom/registry.py

    r296 r297  
    8484        for name in classRegister: 
    8585            self.checkHasAsForHasManys(classRegister[name]) 
    86         self.createTemporalAttributePersistence(domainClass.meta) 
    87  
    88     def createTemporalAttributePersistence(self, domainClassMeta): 
    8986        temporalAttrs = [] 
    90         for a in domainClassMeta.attributes: 
     87        for a in domainClass.meta.attributes: 
    9188            if a.isTemporal: 
    92                 new = self.createTemporalDomainClass(domainClassMeta, a) 
     89                temporalMeta = domainClass.meta 
     90                if a.isBitemporal: 
     91                    new = self.createBitemporalDomainClass(temporalMeta, a) 
     92                else: 
     93                    new = self.createTemporalDomainClass(temporalMeta, a) 
    9394                a.setTemporalDomainClass(new) 
    9495 
     96    def createBitemporalDomainClass(self, classMeta, attrMeta): 
     97        temporalClassName = self.createTemporalDomainClassName(classMeta.name, attrMeta.name) 
     98        temporalMetaObject = DomainObject.metaClass(temporalClassName) 
     99        temporalMetaObject.recordedValue = attrMeta.duplicateTemporal() 
     100        temporalMetaObject.parent = HasA(classMeta.name, isRequired=True) 
     101        temporalMetaObject.dateCreated = DateTime(isIndexed=True, 
     102            isRequired=True, default=mx.DateTime.now) 
     103        temporalMetaObject.isUnique = False 
     104        temporalMetaObject.isCached = True 
     105        return self.createDomainClass(temporalMetaObject) 
     106 
    95107    def createTemporalDomainClass(self, classMeta, attrMeta): 
    96         temporalClassName = classMeta.name + '_t_' + attrMeta.name 
    97         metaTemporalObject = DomainObject.metaClass(temporalClassName) 
    98         metaTemporalObject.recordedValue = attrMeta.duplicateTemporal() 
    99         metaTemporalObject.parent = HasA(classMeta.name, isRequired=True) 
    100         metaTemporalObject.dateCreated = DateTime(isIndexed=True, 
     108        temporalClassName = self.createTemporalDomainClassName(classMeta.name, attrMeta.name) 
     109        temporalMetaObject = DomainObject.metaClass(temporalClassName) 
     110        temporalMetaObject.recordedValue = attrMeta.duplicateTemporal() 
     111        temporalMetaObject.parent = HasA(classMeta.name, isRequired=True) 
     112        temporalMetaObject.dateCreated = DateTime(isIndexed=True, 
    101113            isRequired=True, default=mx.DateTime.now) 
    102         return self.createDomainClass(metaTemporalObject) 
     114        temporalMetaObject.isUnique = False 
     115        temporalMetaObject.isCached = True 
     116        return self.createDomainClass(temporalMetaObject) 
     117 
     118    def createTemporalDomainClassName(self, className, attrName): 
     119        return className + '_t_' + attrName 
    103120 
    104121    def setMetaAttributesFromClass(self, domainClass, domainClassMeta): 
  • trunk/src/dm/dom/temporaltest.py

    r296 r297  
    77import mx.DateTime 
    88from time import sleep 
     9from dm.dom.temporal import TemporalProperty, BitemporalProperty 
     10from dm.dom.temporal import TemporalCollection, BitemporalCollection 
    911 
    1012def suite(): 
    1113    suites = [ 
    12         unittest.makeSuite(TestTemporal), 
     14        unittest.makeSuite(TestTemporal),  # Example 
     15        unittest.makeSuite(TestTemporalProperty), 
     16        unittest.makeSuite(TestBitemporalProperty), 
    1317    ] 
    1418    return unittest.TestSuite(suites) 
     
    1721# Todo: Separate out the testing of indexes from testing of temporal attribute. 
    1822 
    19 class Temporal(SimpleNamedObject): 
     23class Temporal(SimpleNamedObject):  # Example 
    2024    "Temporally attributed domain object." 
    2125 
     
    2428    description = String(default='', isTemporal=True) 
    2529    firstkiss = DateTime(isTemporal=True, default=mx.DateTime.now()) 
    26     state = HasA('State', isTemporal=True, isRequired=None) 
    27     haircolor = String(default='', isBitemporal=True) 
    28  
    29  
    30 class TestTemporal(TestCase): 
     30    state = HasA('State', isTemporal=True, isRequired=False) 
     31    haircolor = String(default='My Natural Color', isBitemporal=True) 
     32 
     33 
     34class TestTemporalProperty(TestCase): 
     35 
     36    def setUp(self): 
     37        super(TestTemporalProperty, self).setUp() 
     38        self.collection = TemporalProperty.createRegister() 
     39 
     40    def tearDown(self): 
     41        self.collection = None 
     42 
     43    def test_collection(self): 
     44        self.failUnlessEqual(type(self.collection), TemporalCollection) 
     45         
     46         
     47class TestBitemporalProperty(TestCase): 
     48 
     49    def setUp(self): 
     50        super(TestBitemporalProperty, self).setUp() 
     51        self.collection = BitemporalProperty.createRegister() 
     52 
     53    def tearDown(self): 
     54        self.collection = None 
     55 
     56    def test_collection(self): 
     57        self.failUnlessEqual(type(self.collection), BitemporalCollection) 
     58         
     59         
     60 
     61 
     62class TestTemporal(TestCase):  # Example 
    3163    "TestCase for the Temporal class." 
    3264 
     
    127159        temporal.haircolor = haircolor2 
    128160        temporal.save() 
     161        print "Sleeping now!" 
     162        import time 
     163        time.sleep(5) 
    129164        #  - check present is most recent 
    130165        self.timepoint.reset() 
     
    160195        self.failUnlessEqual(temporal.haircolor, haircolor3) 
    161196 
     197        #raise Exception, str(temporal) 
     198 
    162199        # Make changes to the actual time. 
    163  
    164  
    165         return 
    166  
    167  
    168200        self.timepoint.reset() 
    169201        temporal = self.temporals[self.fixtureName] 
     
    177209        self.timepoint.reset() 
    178210        temporal = self.temporals[self.fixtureName] 
     211        print "Sleeping... %s" % temporal.haircolor 
     212        sleep(5) 
    179213        self.failUnlessEqual(temporal.haircolor, haircolor3) 
    180214        self.timepoint.actual = yearsAgo