Title: [commits] (vajda) [11102] - debugged dynamic source changes in IntersectionCollection
Revision
11102
Author
vajda
Date
2006-07-09 08:35:01 -0700 (Sun, 09 Jul 2006)

Log Message

- debugged dynamic source changes in IntersectionCollection
- added testIntersectionSourceChanges unit test

Modified Paths

Added Paths

Diff

Modified: trunk/chandler/parcels/osaf/pim/collections.py (11101 => 11102)

--- trunk/chandler/parcels/osaf/pim/collections.py	2006-07-08 17:00:59 UTC (rev 11101)
+++ trunk/chandler/parcels/osaf/pim/collections.py	2006-07-09 15:35:01 UTC (rev 11102)
@@ -223,6 +223,7 @@
         if op in ('add', 'remove'):
             view = self.itsView
             source = view[sourceId]
+            name = source.__collection__
 
             if op == 'add':
                 set = self._sourcesChanged_()
@@ -231,8 +232,8 @@
                 assert actualSource is not None
                 for uuid in source.iterkeys():
                     view._notifyChange(sourceChanged, 'add', 'collection',
-                                       source, source.__collection__,
-                                       False, uuid, actualSource)
+                                       source, name, False, uuid, actualSource)
+
             elif op == 'remove':
                 set = getattr(self, self.__collection__)
                 sourceChanged = set.sourceChanged
@@ -240,8 +241,7 @@
                 assert actualSource is not None
                 for uuid in source.iterkeys():
                     view._notifyChange(sourceChanged, 'remove', 'collection',
-                                       source, source.__collection__,
-                                       False, uuid, actualSource)
+                                       source, name, False, uuid, actualSource)
                 set = self._sourcesChanged_()
 
     def addSource(self, source):
@@ -285,7 +285,19 @@
 
         super(SingleSourceWrapperCollection, self).__init__(*args, **kwds)
 
+    def _sourcesChanged_(self):
+        
+        source = self.source
 
+        if source is None:
+            set = EmptySet()
+        else:
+            set = Set(source)
+
+        setattr(self, self.__collection__, set)
+        return set
+
+
 class DifferenceCollection(WrapperCollection):
     """
     A ContentCollection containing the set theoretic difference of two
@@ -362,6 +374,44 @@
 
     schema.kindInfo(displayName=u"IntersectionCollection")
 
+    def _sourcesChanged(self, op, item, attribute, sourceId):
+
+        if op in ('add', 'remove'):
+            view = self.itsView
+            source = view[sourceId]
+            name = self.__collection__
+            _collectionChanged = self._collectionChanged
+
+            if op == 'add':
+                set = getattr(self, name)
+                wasEmpty = isinstance(set, EmptySet)
+                sourceSet = getattr(source, source.__collection__)
+                for uuid in set.iterkeys():
+                    if uuid not in sourceSet:
+                        view._notifyChange(_collectionChanged,
+                                           'remove', 'collection', name, uuid)
+                set = self._sourcesChanged_()
+                if wasEmpty:
+                    for uuid in set.iterkeys():
+                        view._notifyChange(_collectionChanged,
+                                           'add', 'collection', name, uuid)
+
+            elif op == 'remove':
+                wasEmpty = isinstance(getattr(self, name), EmptySet)
+                set = self._sourcesChanged_()
+                sourceSet = getattr(source, source.__collection__)
+                if isinstance(set, EmptySet):
+                    if not wasEmpty:
+                        for uuid in sourceSet.iterkeys():
+                            view._notifyChange(_collectionChanged,
+                                               'remove', 'collection',
+                                               name, uuid)
+                else:
+                    for uuid in set.iterkeys():
+                        if uuid not in sourceSet:
+                            view._notifyChange(_collectionChanged,
+                                               'add', 'collection', name, uuid)
+
     def _sourcesChanged_(self):
 
         sources = self.sources
@@ -665,7 +715,7 @@
             # they drop the trash. So we artificially insert it back
             set = Difference(source, trash)
         else:
-            set = Set(self.source)
+            set = Set(source)
 
         setattr(self, self.__collection__, set)
         return set

Modified: trunk/chandler/parcels/osaf/pim/tests/TestCollections.py (11101 => 11102)

--- trunk/chandler/parcels/osaf/pim/tests/TestCollections.py	2006-07-08 17:00:59 UTC (rev 11101)
+++ trunk/chandler/parcels/osaf/pim/tests/TestCollections.py	2006-07-09 15:35:01 UTC (rev 11102)
@@ -17,6 +17,7 @@
 from osaf.pim.collections import *
 from osaf import pim
 from repository.persistence.DBRepository import DBRepository
+from repository.tests.RepositoryTestCase import RepositoryTestCase
 from i18n.tests import uw
 
 class NotifyHandler(schema.Item):
@@ -80,7 +81,7 @@
         rep.create(**kwds)
         view = rep.view
 
-        if view.findPath("//Schema") is None:
+        if view.getRoot("Schema") is None:
             view.loadPack(os.path.join(self.chandlerDir, 'repository', 'packs', 'schema.pack'))
             view.loadPack(os.path.join(self.chandlerDir, 'repository', 'packs', 'chandler.pack'))
         self.view = view
@@ -641,6 +642,131 @@
         self.assert_(note in all)
 
 
+class TestCollections(RepositoryTestCase):
+
+    def setUp(self):
+
+        super(TestCollections, self).setUp()
+
+        view = self.rep.view
+        cineguidePack = os.path.join(self.testdir, 'data', 'packs',
+                                     'cineguide.pack')
+        view.loadPack(cineguidePack)
+
+
+    def testIntersectionSourceChanges(self):
+
+        view = self.rep.view
+
+        k = view['CineGuide']['KHepburn']
+        k.movies.addIndex('n', 'numeric')
+
+        c2 = ListCollection("c2", view)
+        c3 = ListCollection("c3", view)
+        c4 = ListCollection("c4", view)
+        c8 = ListCollection("c8", view)
+
+        i = 0
+        for m in k.movies:
+            if i % 2 == 0:
+                c2.append(m)
+            if i % 3 == 0:
+                c3.append(m)
+            if i % 4 == 0:
+                c4.append(m)
+            if i % 8 == 0:
+                c8.append(m)
+            i += 1
+
+        ci = IntersectionCollection("ci", view, sources = [c2, c3, c4])
+
+        cw = SingleSourceWrapperCollection("cw", view, source = ci)
+        cw.addIndex('n', 'numeric')
+
+        i = 0
+        for m in k.movies:
+            if i % 12 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        ci.removeSource(c4)
+        i = 0
+        for m in k.movies:
+            if i % 6 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        ci.addSource(c4)
+        i = 0
+        for m in k.movies:
+            if i % 12 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        ci.addSource(c8)
+        i = 0
+        for m in k.movies:
+            if i % 24 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        ci.removeSource(c2)
+        i = 0
+        for m in k.movies:
+            if i % 24 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        ci.removeSource(c8)
+        i = 0
+        for m in k.movies:
+            if i % 12 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        ci.removeSource(c4)
+        for m in k.movies:
+            self.assert_(m not in ci)
+            self.assert_(m not in cw)
+
+        ci.addSource(c2)
+        i = 0
+        for m in k.movies:
+            if i % 6 == 0:
+                self.assert_(m in ci)
+                self.assert_(m in cw)
+            else:
+                self.assert_(m not in ci)
+                self.assert_(m not in cw)
+            i += 1
+
+        self.assert_(view.check())
+
+
 if __name__ == "__main__":
 #    import hotshot
 #    profiler = hotshot.Profile('/tmp/TestItems.hotshot')

Added: trunk/chandler/repository/tests/data/packs/cineguide/schema/attributes/collections.attr (11101 => 11102)

--- trunk/chandler/repository/tests/data/packs/cineguide/schema/attributes/collections.attr	2006-07-08 17:00:59 UTC (rev 11101)
+++ trunk/chandler/repository/tests/data/packs/cineguide/schema/attributes/collections.attr	2006-07-09 15:35:01 UTC (rev 11102)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+
+<item>
+    <name>collections</name>
+    <kind type="path">//Schema/Core/Attribute</kind>
+
+    <attribute name="cardinality">list</attribute>
+    <attribute name="otherName">inclusions</attribute>
+</item>

Modified: trunk/chandler/repository/tests/data/packs/cineguide/schema/kinds/Movie.kind (11101 => 11102)

--- trunk/chandler/repository/tests/data/packs/cineguide/schema/kinds/Movie.kind	2006-07-08 17:00:59 UTC (rev 11101)
+++ trunk/chandler/repository/tests/data/packs/cineguide/schema/kinds/Movie.kind	2006-07-09 15:35:01 UTC (rev 11102)
@@ -22,6 +22,7 @@
     <ref alias="delta" type="path">/CineGuide/Attributes/delta</ref>
     <ref alias="set" type="path">/CineGuide/Attributes/set</ref>
     <ref alias="appearsIn" type="path">/CineGuide/Attributes/appearsIn</ref>
+    <ref alias="collections" type="path">/CineGuide/Attributes/collections</ref>
   </ref>
 
   <attribute name="displayAttribute">title</attribute>




_______________________________________________
Commits mailing list
[email protected]
http://lists.osafoundation.org/mailman/listinfo/commits

Reply via email to