More on resource allocations
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@109 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
		
							parent
							
								
									fa2d52a342
								
							
						
					
					
						commit
						9b690b62b3
					
				
					 5 changed files with 90 additions and 35 deletions
				
			
		| 
						 | 
					@ -22,6 +22,7 @@ loops interface definitions.
 | 
				
			||||||
$Id$
 | 
					$Id$
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from zope.interface import Interface
 | 
				
			||||||
from zope.app.container.interfaces import IOrderedContainer
 | 
					from zope.app.container.interfaces import IOrderedContainer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from zope.schema import TextLine
 | 
					from zope.schema import TextLine
 | 
				
			||||||
| 
						 | 
					@ -70,18 +71,18 @@ class ITask(IOrderedContainer):
 | 
				
			||||||
            target resource types given.
 | 
					            target resource types given.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def allocateResource(resource, allocType=None):
 | 
					    def allocateResource(resource, allocType='standard'):
 | 
				
			||||||
        """ Allocate resource to self. A special allocation type may be given.
 | 
					        """ Allocate resource to self. A special allocation type may be given.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def createAndAllocateResource(resourceType='Resource', allocType='standard',
 | 
					    def createAndAllocateResource(resourceType=None, allocType='standard',
 | 
				
			||||||
                                  container=None, name=None):
 | 
					                                  container=None, name=None):
 | 
				
			||||||
        """ Allocate resource to self. A special allocation type may be given.
 | 
					        """ Allocate resource to self. A special allocation type may be given.
 | 
				
			||||||
            Additional properties may be given as keyword parameters.
 | 
					 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def deallocateResource(resource):
 | 
					    def deallocateResource(resource, allocTypes=None):
 | 
				
			||||||
        """ Deallocate from self the resource allocated.
 | 
					        """ Deallocate from self the resource given. If no allocTypes
 | 
				
			||||||
 | 
					            given all allocations to resource will be removed.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def allocatedUserIds():
 | 
					    def allocatedUserIds():
 | 
				
			||||||
| 
						 | 
					@ -89,9 +90,9 @@ class ITask(IOrderedContainer):
 | 
				
			||||||
            Used by catalog index 'allocUserIds'.
 | 
					            Used by catalog index 'allocUserIds'.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getAllocType(resource):
 | 
					    def getAllocTypes(resource):
 | 
				
			||||||
        """ Return the allocation type for the resource given. Raise a
 | 
					        """ Return the allocation types with which the resource given
 | 
				
			||||||
            ValueException if resource is not allocated to self.
 | 
					            is allocated to self.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getAllAllocTypes():
 | 
					    def getAllAllocTypes():
 | 
				
			||||||
| 
						 | 
					@ -111,3 +112,9 @@ class IResource(IOrderedContainer):
 | 
				
			||||||
            source task types given.
 | 
					            source task types given.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class IResourceConstraint(Interface):
 | 
				
			||||||
 | 
					    """ A ResourceConstraint governs which Resource objects may be
 | 
				
			||||||
 | 
					        allocated to a Task object.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										17
									
								
								resource.py
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								resource.py
									
										
									
									
									
								
							| 
						 | 
					@ -24,6 +24,7 @@ $Id$
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from zope.interface import implements
 | 
					from zope.interface import implements
 | 
				
			||||||
from zope.app.container.ordered import OrderedContainer
 | 
					from zope.app.container.ordered import OrderedContainer
 | 
				
			||||||
 | 
					from zope.app import zapi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from interfaces import IResource
 | 
					from interfaces import IResource
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -52,13 +53,23 @@ class Resource(OrderedContainer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Helper methods:
 | 
					    # Helper methods:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _updateAllocations(self, task, allocType):
 | 
					    def _addAllocation(self, task, allocType):
 | 
				
			||||||
        #called by Task.allocateResource
 | 
					        #called by Task.allocateResource
 | 
				
			||||||
        tList = self._allocations.get(allocType, [])
 | 
					        tList = self._allocations.get(allocType, [])
 | 
				
			||||||
        tList.append(task)
 | 
					        tList.append(task)
 | 
				
			||||||
        self._allocations[allocType] = tList
 | 
					        self._allocations[allocType] = tList
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _createResourceName(self):
 | 
					    def _removeAllocations(self, task, allocTypes):
 | 
				
			||||||
 | 
					        #called by Task.deallocateResource
 | 
				
			||||||
 | 
					        allocs = self._allocations
 | 
				
			||||||
 | 
					        for at in allocTypes or allocs.keys():
 | 
				
			||||||
 | 
					            if task in allocs[at]:
 | 
				
			||||||
 | 
					                allocs[at].remove(task)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _createResourceName(self, container=None):
 | 
				
			||||||
        prefix = 'rsc'
 | 
					        prefix = 'rsc'
 | 
				
			||||||
        last = max([ int(n[len(prefix):]) for n in self.__parent__.keys() ] or [1])
 | 
					        container = container or zapi.getParent(self)
 | 
				
			||||||
 | 
					        last = max([ int(n[len(prefix):]) for n in container.keys() ] or [1])
 | 
				
			||||||
        return prefix + str(last+1)
 | 
					        return prefix + str(last+1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
							
								
								
									
										48
									
								
								task.py
									
										
									
									
									
								
							
							
						
						
									
										48
									
								
								task.py
									
										
									
									
									
								
							| 
						 | 
					@ -24,10 +24,11 @@ $Id$
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from zope.interface import implements
 | 
					from zope.interface import implements
 | 
				
			||||||
from zope.app.container.ordered import OrderedContainer
 | 
					from zope.app.container.ordered import OrderedContainer
 | 
				
			||||||
 | 
					from zope.app import zapi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from resource import Resource
 | 
				
			||||||
from interfaces import ITask
 | 
					from interfaces import ITask
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
class Task(OrderedContainer):
 | 
					class Task(OrderedContainer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    implements(ITask)
 | 
					    implements(ITask)
 | 
				
			||||||
| 
						 | 
					@ -54,9 +55,9 @@ class Task(OrderedContainer):
 | 
				
			||||||
        task._parentTasks.append(self)
 | 
					        task._parentTasks.append(self)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def createSubtask(self, taskType=None, container=None, name=None):
 | 
					    def createSubtask(self, taskType=None, container=None, name=None):
 | 
				
			||||||
        container = container or self.__parent__
 | 
					        container = container or zapi.getParent(self)
 | 
				
			||||||
        task = Task()
 | 
					        task = Task()
 | 
				
			||||||
        name = name or self._createTaskName()
 | 
					        name = name or task._createTaskName(container)
 | 
				
			||||||
        container[name] = task
 | 
					        container[name] = task
 | 
				
			||||||
        self.assignSubtask(task)
 | 
					        self.assignSubtask(task)
 | 
				
			||||||
        return task
 | 
					        return task
 | 
				
			||||||
| 
						 | 
					@ -65,37 +66,47 @@ class Task(OrderedContainer):
 | 
				
			||||||
        self._subtasks.remove(task)
 | 
					        self._subtasks.remove(task)
 | 
				
			||||||
        task._parentTasks.remove(self)
 | 
					        task._parentTasks.remove(self)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # resources:
 | 
					    # resource allocations:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getAllocatedResources(self, allocTypes=None, resTypes=None):
 | 
					    def getAllocatedResources(self, allocTypes=None, resTypes=None):
 | 
				
			||||||
        from sets import Set
 | 
					        from sets import Set
 | 
				
			||||||
        allocs = self._resourceAllocs
 | 
					        allocs = self._resourceAllocs
 | 
				
			||||||
        res = Set()
 | 
					        res = Set()
 | 
				
			||||||
        for at in allocs.keys():
 | 
					        for at in allocTypes or allocs.keys():
 | 
				
			||||||
            if allocTypes is None or at in allocTypes:
 | 
					 | 
				
			||||||
            res.union_update(allocs[at])
 | 
					            res.union_update(allocs[at])
 | 
				
			||||||
 | 
					        if resTypes:
 | 
				
			||||||
 | 
					            res = [ r for r in res if r in resTypes ]
 | 
				
			||||||
        return tuple(res)
 | 
					        return tuple(res)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def allocateResource(self, resource, allocType=None):
 | 
					    def allocateResource(self, resource, allocType='standard'):
 | 
				
			||||||
        allocType = allocType or 'standard'
 | 
					 | 
				
			||||||
        allocs = self._resourceAllocs
 | 
					        allocs = self._resourceAllocs
 | 
				
			||||||
        rList = allocs.get(allocType, [])
 | 
					        rList = allocs.get(allocType, [])
 | 
				
			||||||
 | 
					        if resource not in rList:
 | 
				
			||||||
            rList.append(resource)
 | 
					            rList.append(resource)
 | 
				
			||||||
        allocs[allocType] = rList
 | 
					        allocs[allocType] = rList
 | 
				
			||||||
        resource._updateAllocations(self, allocType)
 | 
					        resource._addAllocation(self, allocType)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def createAndAllocateResource(self, resourceType='Resource', allocType='standard',
 | 
					    def createAndAllocateResource(self, resourceType=None, allocType='standard',
 | 
				
			||||||
                                  container=None, id=None):
 | 
					                                  container=None, name=None):
 | 
				
			||||||
        return None
 | 
					        container = container or zapi.getParent(self)
 | 
				
			||||||
 | 
					        resource = Resource()
 | 
				
			||||||
 | 
					        name = name or resource._createResourceName(container)
 | 
				
			||||||
 | 
					        container[name] = resource
 | 
				
			||||||
 | 
					        self.allocateResource(resource, allocType)
 | 
				
			||||||
 | 
					        return resource
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def deallocateResource(self, resource):
 | 
					    def deallocateResource(self, resource, allocTypes=None):
 | 
				
			||||||
        pass
 | 
					        allocs = self._resourceAllocs
 | 
				
			||||||
 | 
					        for at in allocTypes or allocs.keys():
 | 
				
			||||||
 | 
					            if resource in allocs[at]:
 | 
				
			||||||
 | 
					                allocs[at].remove(resource)
 | 
				
			||||||
 | 
					        resource._removeAllocations(self, allocTypes)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def allocatedUserIds(self):
 | 
					    def allocatedUserIds(self):
 | 
				
			||||||
        return ()
 | 
					        return ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getAllocType(self, resource):
 | 
					    def getAllocTypes(self, resource):
 | 
				
			||||||
        return 'standard'
 | 
					        return ('standard',)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getAllAllocTypes(self):
 | 
					    def getAllAllocTypes(self):
 | 
				
			||||||
        return ('standard',)
 | 
					        return ('standard',)
 | 
				
			||||||
| 
						 | 
					@ -103,7 +114,8 @@ class Task(OrderedContainer):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Helper methods:
 | 
					    # Helper methods:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _createTaskName(self):
 | 
					    def _createTaskName(self, container=None):
 | 
				
			||||||
        prefix = 'tsk'
 | 
					        prefix = 'tsk'
 | 
				
			||||||
        last = max([ int(n[len(prefix):]) for n in self.__parent__.keys() ] or [1])
 | 
					        container = container or zapi.getParent(self)
 | 
				
			||||||
 | 
					        last = max([ int(n[len(prefix):]) for n in container.keys() ] or [1])
 | 
				
			||||||
        return prefix + str(last+1)
 | 
					        return prefix + str(last+1)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -54,6 +54,14 @@ class Test(unittest.TestCase):
 | 
				
			||||||
        t1.allocateResource(r1)
 | 
					        t1.allocateResource(r1)
 | 
				
			||||||
        self.assertEqual((t1,), r1.getTasksAllocatedTo())
 | 
					        self.assertEqual((t1,), r1.getTasksAllocatedTo())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def testDeallocateFromTask(self):
 | 
				
			||||||
 | 
					        t1 = self.t1
 | 
				
			||||||
 | 
					        r1 = self.r1
 | 
				
			||||||
 | 
					        t1.allocateResource(r1)
 | 
				
			||||||
 | 
					        self.assertEqual((t1,), r1.getTasksAllocatedTo())
 | 
				
			||||||
 | 
					        t1.deallocateResource(r1)
 | 
				
			||||||
 | 
					        self.assertEqual((), r1.getTasksAllocatedTo())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_suite():
 | 
					def test_suite():
 | 
				
			||||||
    return unittest.TestSuite((
 | 
					    return unittest.TestSuite((
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,9 +4,11 @@ import unittest
 | 
				
			||||||
#from zope.testing.doctestunit import DocTestSuite
 | 
					#from zope.testing.doctestunit import DocTestSuite
 | 
				
			||||||
from zope.interface.verify import verifyClass
 | 
					from zope.interface.verify import verifyClass
 | 
				
			||||||
#from zope.app.tests.setup import placelessSetUp
 | 
					#from zope.app.tests.setup import placelessSetUp
 | 
				
			||||||
 | 
					from zope.app.tests.setup import placefulSetUp
 | 
				
			||||||
#from zope.app.container.tests.test_icontainer import TestSampleContainer
 | 
					#from zope.app.container.tests.test_icontainer import TestSampleContainer
 | 
				
			||||||
from zope.app.container.interfaces import IContained
 | 
					from zope.app.container.interfaces import IContained
 | 
				
			||||||
from zope.app.folder import Folder
 | 
					from zope.app.folder import Folder
 | 
				
			||||||
 | 
					from zope.app import zapi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from loops.task import Task
 | 
					from loops.task import Task
 | 
				
			||||||
from loops.interfaces import ITask
 | 
					from loops.interfaces import ITask
 | 
				
			||||||
| 
						 | 
					@ -16,7 +18,8 @@ class TestTask(unittest.TestCase):
 | 
				
			||||||
    "Test methods of the Task class."
 | 
					    "Test methods of the Task class."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
        #placelessSetUp()
 | 
					#        placelessSetUp()
 | 
				
			||||||
 | 
					        placefulSetUp()
 | 
				
			||||||
        self.f1 = Folder()
 | 
					        self.f1 = Folder()
 | 
				
			||||||
        self.f1.__name__ = u'f1'
 | 
					        self.f1.__name__ = u'f1'
 | 
				
			||||||
        self.t1 = Task()
 | 
					        self.t1 = Task()
 | 
				
			||||||
| 
						 | 
					@ -35,8 +38,8 @@ class TestTask(unittest.TestCase):
 | 
				
			||||||
        verifyClass(ITask, Task)
 | 
					        verifyClass(ITask, Task)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def testContained(self):
 | 
					    def testContained(self):
 | 
				
			||||||
        self.assertEqual(u'tsk1', self.t1.__name__)
 | 
					        self.assertEqual(u'tsk1', zapi.name(self.t1))
 | 
				
			||||||
        self.assertEqual(u'f1', self.t1.__parent__.__name__)
 | 
					        self.assertEqual(u'f1', zapi.name(zapi.getParent(self.t1)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def testTitle(self):
 | 
					    def testTitle(self):
 | 
				
			||||||
        t = Task()
 | 
					        t = Task()
 | 
				
			||||||
| 
						 | 
					@ -75,9 +78,9 @@ from loops.resource import Resource
 | 
				
			||||||
class TestTaskResource(unittest.TestCase):
 | 
					class TestTaskResource(unittest.TestCase):
 | 
				
			||||||
    "Test methods of the Task class related to Resource allocations."
 | 
					    "Test methods of the Task class related to Resource allocations."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
        #placelessSetUp()
 | 
					        #placelessSetUp()
 | 
				
			||||||
 | 
					        #placefulSetUp()
 | 
				
			||||||
        self.f1 = Folder()
 | 
					        self.f1 = Folder()
 | 
				
			||||||
        self.f1.__name__ = u'f1'
 | 
					        self.f1.__name__ = u'f1'
 | 
				
			||||||
        self.t1 = Task()
 | 
					        self.t1 = Task()
 | 
				
			||||||
| 
						 | 
					@ -97,6 +100,20 @@ class TestTaskResource(unittest.TestCase):
 | 
				
			||||||
        t1.allocateResource(r1)
 | 
					        t1.allocateResource(r1)
 | 
				
			||||||
        self.assertEqual((r1,), t1.getAllocatedResources())
 | 
					        self.assertEqual((r1,), t1.getAllocatedResources())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def testDeallocateResource(self):
 | 
				
			||||||
 | 
					        t1 = self.t1
 | 
				
			||||||
 | 
					        r1 = self.r1
 | 
				
			||||||
 | 
					        t1.allocateResource(r1)
 | 
				
			||||||
 | 
					        self.assertEqual((r1,), t1.getAllocatedResources())
 | 
				
			||||||
 | 
					        t1.deallocateResource(r1)
 | 
				
			||||||
 | 
					        self.assertEqual((), t1.getAllocatedResources())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def testCreateAndAllocateResource(self):
 | 
				
			||||||
 | 
					        t1 = self.t1
 | 
				
			||||||
 | 
					        self.assertEqual((), t1.getAllocatedResources())
 | 
				
			||||||
 | 
					        r1 = t1.createAndAllocateResource()
 | 
				
			||||||
 | 
					        self.assertEqual((r1,), t1.getAllocatedResources())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_suite():
 | 
					def test_suite():
 | 
				
			||||||
    return unittest.TestSuite((
 | 
					    return unittest.TestSuite((
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue