def getSolChoice(self, req, term): #print "req is", req req.testValidity() req = req.completeSquish() if req.__repr__() in self.subject_req_choices: solution = self.subject_req_choices[req.__repr__()] #print "key:", str(req) #print "value:", str(solution) else: #print "key not found: ", req.__repr__(), "\n", req.__repr__() in self.subject_req_choices #print "dict is: " #for x in self.subject_req_choices.items(): #print x if req.isLeaf(): if term.getReq(req.getSingleSubj()) == Requirement(): solution = req #print req else: pre_requirements = req.expand(term) pre_requirements.removeReq(req) solution = Requirement([req, self.getSolChoice(pre_requirements, term)], 2) elif req.isTotal(): solution_components = [self.getSolChoice(sub_req, term) for sub_req in req.reqs] unique_components = list(set(solution_components)) num_duplicates = len(solution_components) - len(unique_components) #print "num dup is ", num_duplicates solution = Requirement(unique_components, req.numNeeded-num_duplicates) #In this case, req is a partial requirement else: solution_components = [self.getSolChoice(sub_req, term) for sub_req in sample(req.reqs, req.numNeeded)] unique_components = list(set(solution_components)) num_duplicates = len(solution_components) - len(unique_components) solution = Requirement(unique_components, req.numNeeded-num_duplicates) solution = solution.completeSquish() solution.testValidity() #print "solution is ", solution self.subject_req_choices[req.__repr__()] = solution #print "RETURNING ", solution return solution
def testAddSubject(self): self.reqNew = Requirement([], 0, None) self.assertTrue(self.reqNew.isSatisfied([])) self.reqNew.addSubject(Subject("8.01")) self.assertFalse(self.reqNew.isSatisfied([])) self.assertTrue(self.reqNew.isSatisfied([Subject("8.01")]))
def testAddReq(self): self.reqNew = Requirement([], 0, None) self.reqNew.addReq(self.dset.mathReq) self.assertEqual(self.reqNew.getNumChoices(), 1, "must add a req to the list")
class Test(unittest.TestCase): def setUp(self): self.dset = Dataset() self.dset.dataSetup() def tearDown(self): del self.dset def testConstruct(self): pass def testCompare(self): self.assertEqual(Requirement(), Requirement(), "Assert Equal: %s == %s" % (Requirement(), Requirement())) self.assertEqual( Requirement([], 1, self.dset.subjects[0], ""), Requirement([], 1, self.dset.subjects[0], ""), "Assert Equal: %s == %s" % (Requirement([], 1, self.dset.subjects[0], ""), Requirement([], 1, self.dset.subjects[0], "")), ) self.assertEqual( self.dset.reqs63, self.dset.reqs63, "Assert Equal: %s == %s" % (self.dset.reqs63, self.dset.reqs63) ) self.assertEqual( self.dset.reqs63.expand(self.dset.terms[0]), self.dset.reqs63.expand(self.dset.terms[0]), "Assert Equal: %s == %s" % (self.dset.reqs63.expand(self.dset.terms[0]), self.dset.reqs63.expand(self.dset.terms[0])), ) def testIsSatisfied(self): self.assertTrue(Requirement().isSatisfied([])) self.assertTrue(self.dset.physReq.isSatisfied([Subject("8.02")])) self.assertTrue(self.dset.physReq.isSatisfied([Subject("8.02"), Subject("18.01")])) self.assertTrue(self.dset.mathReq.isSatisfied([Subject("6.042"), Subject("18.03")])) self.assertTrue(self.dset.mathReq.isSatisfied([Subject("6.042"), Subject("18.06")])) self.assertTrue(self.dset.mathReq.isSatisfied([Subject("6.042"), Subject("18.06"), Subject("18.03")])) self.assertFalse(self.dset.physReq.isSatisfied([Subject("6.01")])) self.assertFalse(self.dset.mathReq.isSatisfied([Subject("18.03"), Subject("18.06")])) def testIsTotal(self): self.assertTrue(self.dset.physReq.isTotal()) self.assertTrue(self.dset.mathReq.isTotal()) self.assertFalse(self.dset.AUSReq.isTotal()) def testNumChoices(self): self.assertEqual(self.dset.physReq.getNumChoices(), 1) self.assertEqual(self.dset.mathReq.getNumChoices(), 2) self.assertEqual(Requirement().getNumChoices(), 0) def testAddSubject(self): self.reqNew = Requirement([], 0, None) self.assertTrue(self.reqNew.isSatisfied([])) self.reqNew.addSubject(Subject("8.01")) self.assertFalse(self.reqNew.isSatisfied([])) self.assertTrue(self.reqNew.isSatisfied([Subject("8.01")])) def testAddReq(self): self.reqNew = Requirement([], 0, None) self.reqNew.addReq(self.dset.mathReq) self.assertEqual(self.reqNew.getNumChoices(), 1, "must add a req to the list") def testExpand(self): a = self.dset.reqs63.expand(self.dset.terms[0]).squish() self.assertTrue( set(a.expand(self.dset.terms[0]).getSubjects()) == set(a.expand(self.dset.terms[0]).expand(self.dset.terms[0]).getSubjects()), "Assert: %s == %s" % ( set(a.expand(self.dset.terms[0]).getSubjects()), set(a.expand(self.dset.terms[0]).expand(self.dset.terms[0]).getSubjects()), ), ) def testExpand2(self): for subj in self.dset.subjects[:20]: print subj req_to_check = self.dset.terms[0].getReq(subj) print "req_to_check is ", req_to_check # if the req is the same as the expansion of the req, then it had better be a class with no prerequisites if req_to_check.expand(self.dset.terms[0]) == req_to_check: print "expanded is same\n" self.assertTrue( len(req_to_check.reqs) == 0 and ( req_to_check.singleSubject is None or self.dset.terms[0].getReq(req_to_check.singleSubject) == Requirement() ), "Assert empty req: %s reqs and %s singlesubject" % (req_to_check.reqs, req_to_check.singleSubject), ) def testSquish(self): a = self.dset.reqs63.expand(self.dset.terms[0]).squish() self.assertTrue( set(a.getSubjects()) == set(a.squish().getSubjects()), "Assert: %s == %s" % (set(a.getSubjects()), set(a.squish().getSubjects())), ) self.assertTrue( set(a.getSubjects()) == set(a.squish().squish().getSubjects()), "Assert: %s == %s" % (set(a.getSubjects()), set(a.squish().squish().getSubjects())), ) self.assertTrue( Requirement().squish() == Requirement(), "Assert: %s == %s" % (Requirement().squish(), Requirement()) )