Collection items

March 8th, 2006 at 11:42 am (2 years, 2 months ago) by Andi Vajda under chandlerdb

A collection item is an item that wraps an abstract set of items or bi-directional item references.

A collection item is said to wrap a set of items because it delegates the API of that set value. All methods defined on this value’s implementation class that aren’t also defined on the collection item’s implementation class are available directly on the collection item.

The collection item’s set value is stored in an attribute whose name is declared on its implementation class via the collection attribute. That attribute is declared like any other on the collection item’s kind or via Chandler’s schema API.

The collection item’s implementation class must at least be a subclass of the repository’s Collection class and must be setup with the CollectionClass metaclass. When the set of items is intended to be an abstract set it must be initialized in the collection item’s implementation class’ constructor.
For example, using the schema API:

  • Defining a collection item class wrapping a simple bi-directional item reference collection.
    from repository.item.Collection import Collection
    from application import schema
    
    class GreenCollection(Collection):
      __metaclass__ = schema.CollectionClass
      __collection__ = 'bunch'
    
      # declare a collection attribute of bi-refs of cardinality 'list'
      bunch = schema.Sequence(otherName='collections', initialValue=[])
      ...
    
  • Defining a collection item class wrapping an abstract union set.
    from repository.item.Collection import Collection
    from repository.item.Sets import Union
    from application import schema
    
    class BlueCollection(Collection):
      __metaclass__ = schema.CollectionClass
      __collection__ = 'plastic'
    
      # declare a simple abstract set of items attribute
      plastic = schema.One(schema.TypeReference('//Schema/Core/AbstractSet'))
    
      # in this example, a and b are collection items
      def __init__(self, a, b, *args, **kwds):
          super(BlueCollection, self).__init__(*args, **kwds)
          self.plastic = Union((a, a.__collection__), (b, b.__collection__))
      ...
    
  • Defining a collection item wrapping class an abstract intersection set of bi-directional item references.
    from repository.item.Collection import Collection
    from repository.item.Sets import Intersection
    from application import schema
    
    class RedCollection(Collection):
      __metaclass__ = schema.CollectionClass
      __collection__ = 'paper'
    
      # declare a collection attribute of bi-refs of cardinality 'set'
      paper = schema.Many(otherName='inPaper')
    
      # in this example, a and b are collection items
      def __init__(self, a, b, *args, **kwds):
          super(RefCollection, self).__init__(*args, **kwds)
          self.paper = Intersection((a, a.__collection__), (b, b.__collection__))
      ...
    
    This class wraps a bi-directional set. When an item appears in the intersection set, the collection item will also appear in the item’s inPaper attribute which, like any other attribute, must be declared on the item’s kind. A collection item may be added or set explicitely to inPaper if and only if the collection item supports such explicit addition by implementing an add(item) method.
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Reddit

Leave a Reply