Task: handle multiple resource constraints
git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@123 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
f0928adbd8
commit
6d91ae183d
2 changed files with 28 additions and 11 deletions
17
task.py
17
task.py
|
@ -122,24 +122,27 @@ class Task(OrderedContainer):
|
||||||
|
|
||||||
# resource constraint stuff:
|
# resource constraint stuff:
|
||||||
|
|
||||||
def isResourceAllowed(self, resource):
|
def isResourceAllowed(self, resource, rcDontCheck=None):
|
||||||
rc = self.resourceConstraints
|
rc = self.resourceConstraints
|
||||||
if not rc:
|
if not rc:
|
||||||
return True
|
return True
|
||||||
for c in rc:
|
for c in rc:
|
||||||
|
if rcDontCheck and c == rcDontCheck: # don't check constraint already checked
|
||||||
|
continue
|
||||||
# that's too simple, we must check all constraints for constraintType:
|
# that's too simple, we must check all constraints for constraintType:
|
||||||
if c.isResourceAllowed(resource):
|
if not c.isResourceAllowed(resource):
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def getCandidateResources(self):
|
def getCandidateResources(self):
|
||||||
rc = self.resourceConstraints
|
rc = self.resourceConstraints
|
||||||
if not rc:
|
if not rc:
|
||||||
return ()
|
return ()
|
||||||
result = []
|
|
||||||
for c in rc:
|
for c in rc:
|
||||||
result.extend(c.getAllowedResources())
|
candidates = c.getAllowedResources()
|
||||||
return tuple(result)
|
if candidates is not None:
|
||||||
|
return tuple([ r for r in candidates if self.isResourceAllowed(r, c) ])
|
||||||
|
return ()
|
||||||
|
|
||||||
def getAllowedResources(self, candidates=None):
|
def getAllowedResources(self, candidates=None):
|
||||||
rc = self.resourceConstraints
|
rc = self.resourceConstraints
|
||||||
|
@ -149,7 +152,7 @@ class Task(OrderedContainer):
|
||||||
result = self.getCandidateResources()
|
result = self.getCandidateResources()
|
||||||
# Empty result means: can't tell
|
# Empty result means: can't tell
|
||||||
return result and result or None
|
return result and result or None
|
||||||
return tuple([ c for c in candidates if self.isResourceAllowed(c) ])
|
return tuple([ r for r in candidates if self.isResourceAllowed(r) ])
|
||||||
|
|
||||||
def isValid(self, checkSubtasks=True):
|
def isValid(self, checkSubtasks=True):
|
||||||
if self.resourceConstraints is not None:
|
if self.resourceConstraints is not None:
|
||||||
|
|
|
@ -150,7 +150,7 @@ class TestTaskResourceConstraints(unittest.TestCase):
|
||||||
|
|
||||||
# the tests...
|
# the tests...
|
||||||
|
|
||||||
def testSelectExplicit(self):
|
def testRequireExplicit(self):
|
||||||
t1 = self.t1
|
t1 = self.t1
|
||||||
r1 = self.r1
|
r1 = self.r1
|
||||||
rc1 = self.rc1
|
rc1 = self.rc1
|
||||||
|
@ -165,9 +165,23 @@ class TestTaskResourceConstraints(unittest.TestCase):
|
||||||
self.assertEqual((), t1.getAllowedResources([r1]))
|
self.assertEqual((), t1.getAllowedResources([r1]))
|
||||||
|
|
||||||
rc1.referenceValues = ([r1])
|
rc1.referenceValues = ([r1])
|
||||||
self.assertEqual(True, rc1.isResourceAllowed(r1))
|
self.assertEqual(True, t1.isResourceAllowed(r1))
|
||||||
self.assertEqual((r1,), t1.getCandidateResources())
|
self.assertEqual((r1,), t1.getCandidateResources())
|
||||||
self.assertEqual((r1,), rc1.getAllowedResources([r1]))
|
self.assertEqual((r1,), t1.getAllowedResources([r1]))
|
||||||
|
|
||||||
|
def testRCCombination(self):
|
||||||
|
t1 = self.t1
|
||||||
|
r1 = self.r1
|
||||||
|
r2 = Resource()
|
||||||
|
self.f1['rsc2'] = r2
|
||||||
|
rc1 = self.rc1
|
||||||
|
rc1.referenceValues = ([r1, r2]) # allow/select both resources
|
||||||
|
rc2 = ResourceConstraint()
|
||||||
|
rc2.referenceType = 'checkmethod'
|
||||||
|
rc2.referenceKey = 'isAllowedForTesting'
|
||||||
|
Resource.isAllowedForTesting = lambda self: zapi.name(self) == 'rsc1'
|
||||||
|
t1.resourceConstraints = [rc1, rc2]
|
||||||
|
self.assertEqual((r1,), t1.getAllowedResources())
|
||||||
|
|
||||||
def testIsValid(self):
|
def testIsValid(self):
|
||||||
t1 = self.t1
|
t1 = self.t1
|
||||||
|
|
Loading…
Add table
Reference in a new issue