def test_shouldBeUnachievable(): root = Goal(Decomposition.AND) context1 = Context("c1") context2 = Context("c2") current = [] current.append(context1) task1 = Task() task2 = Task() task1.addApplicableContext(context2) task2.addApplicableContext(context2) root.addDependency(task1) root.addDependency(task2) deps = [] deps.append(task1) deps.append(task2) plan = root.isAchievable(current, None) assert plan is None
def test_shouldIncludeNonApplicableContexts(): goal = Pragmatic(False) task = Task() context = Context("C1") wrongContext = Context("C2") current = [] qc = QualityConstraint(context, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) task.addApplicableContext(context) task.setProvidedQuality(context, CommonMetrics.SECONDS, 13) goal.addDependency(task) goal.setIdentifier("Root") goal.addNonapplicableContext(wrongContext) goal.interp.addQualityConstraint(qc) interp = Interpretation() interp.addQualityConstraint(qc) current.append(wrongContext) assert goal.isAchievable(current, interp) is None current.append(context) assert goal.isAchievable(current, interp) is None current.remove(wrongContext) assert goal.isAchievable(current, interp) assert goal.isAchievable(current, interp).getTasks() assert 1 == len(goal.isAchievable(current, interp).getTasks())
def test_ApplicableDeps(): goal = Pragmatic(Decomposition.AND) task = Task() context = Context("C1") wrongContext = Context("C2") qc = QualityConstraint(context, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) task.addApplicableContext(context) task.setProvidedQuality(context, CommonMetrics.SECONDS, 13) goal.setIdentifier("Root") goal.addDependency(task) goal.addApplicableContext(context) goal.interp.addQualityConstraint(qc) interp = Interpretation() interp.addQualityConstraint(qc) current = [] current.append(wrongContext) assert goal.isAchievable(current, interp) is None current.append(context) assert len(goal.isAchievable(current, interp).getTasks()) == 1
def test_shouldComplainAboutDifferentMetrics(): lessStrictQC = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) moreStrictQC = QualityConstraint(Context("C2"), CommonMetrics.METERS, 10, Comparison.LESS_THAN) lessStrictQC.stricterQC(moreStrictQC)
def test_shouldThereBeMoreThanOneApplicableQCreturnTheStricterOne(): goal = Pragmatic(Decomposition.AND) task = Task() context = Context("C1") anotherContext = Context("C2") fullContext = [] qc = QualityConstraint(context, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) stricter = QualityConstraint(anotherContext, CommonMetrics.SECONDS, 10, Comparison.LESS_OR_EQUAL_TO) goal.addDependency(task) goal.setIdentifier("Root") goal.addApplicableContext(context) goal.interp.addQualityConstraint(qc) goal.interp.addQualityConstraint(stricter) assert stricter == qc.stricterQC(stricter) fullContext.append(context) assert qc in goal.interp.getQualityConstraints(fullContext) fullContext.append(anotherContext) assert stricter in \ goal.interp.getQualityConstraints(fullContext)
def test_shouldSelectStricterConstraint(): lessStrictQC = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) moreStrictQC = QualityConstraint(Context("C2"), CommonMetrics.SECONDS, 10, Comparison.LESS_THAN) assert moreStrictQC is lessStrictQC.stricterQC(moreStrictQC) assert moreStrictQC is moreStrictQC.stricterQC(lessStrictQC)
def test_shouldAddSeveralContextsAtOnce(): context1 = Context("C1") context2 = Context("C2") task = Task() assert task.applicableContext is None task.addApplicableContext([context1, context2]) assert 2 == len(task.applicableContext)
def test_aNonApplicableRootGoalIsNotAchievable(): goal = Goal(Decomposition.AND) current = Context("C1") fullContext = [] qc = QualityConstraint(current, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) goal.addApplicableContext(Context("C2")) interp = Interpretation() interp.addQualityConstraint(qc) assert goal.isAchievable(fullContext, interp) is None
def test_aGoalOrDecomposedWithTwoTasksMayNotBeAchievable(): goal = Goal(Decomposition.OR) task1 = Task() task2 = Task() current = Context("C1") fullContext = [] fullContext.append(current) qc = QualityConstraint(current, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) task1.addApplicableContext(current) task1.setProvidedQuality(current, CommonMetrics.SECONDS, 16) task2.addApplicableContext(current) task2.setProvidedQuality(current, CommonMetrics.SECONDS, 17) goal.addDependency(task1) goal.addDependency(task2) goal.setIdentifier("Root") goal.addApplicableContext(current) interp = Interpretation() interp.addQualityConstraint(qc) plan = goal.isAchievable(fullContext, interp) assert goal.decomposition is Decomposition.OR assert plan is None
def test_aGoalAndDecomposedWithTwoTasksMayBeAchievable(): goal = Goal(Decomposition.AND) task1 = Task() task2 = Task() current = Context("C1") fullContext = [] fullContext.append(current) qc = QualityConstraint(current, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) interp = Interpretation() interp.addQualityConstraint(qc) task1.addApplicableContext(current) task1.setProvidedQuality(current, CommonMetrics.SECONDS, 13) task2.addApplicableContext(current) task2.setProvidedQuality(current, CommonMetrics.SECONDS, 11) goal.addDependency(task1) goal.addDependency(task2) goal.setIdentifier("Root") goal.addApplicableContext(current) plan = goal.isAchievable(fullContext, interp) assert 2 == len(plan.getTasks())
def test_OnlyBaselineDefined(): task = Task() baseline = Context(None) fullContext = set() fullContext.add(baseline) task.setProvidedQuality(baseline, MpersMetrics.METERS, 50.0) assert 50.0 == task.myProvidedQuality(MpersMetrics.METERS, fullContext)
def text_shouldProvideMetricForBaseline(): task = Task("t1") current = Context("C1") fullContext = [] fullContext.append(current) task.setProvidedQuality(None, MpersMetrics.METERS, 30.0) assert 30.0 == task.myProvidedQuality(MpersMetrics.METERS, fullContext)
def test_shouldProvideCorrectValueForMetric(): task = Task("T1") currentContext = Context("C1") fullContext = set() fullContext.add(currentContext) task.setProvidedQuality(currentContext, MpersMetrics.METERS, 30) assert 30 == task.myProvidedQuality(MpersMetrics.METERS, fullContext)
def test_getApplicableQC(): goal = Pragmatic(Decomposition.AND) task = Task() context = Context("C1") anotherContext = Context("C2") fullContext = [] qc = QualityConstraint(context, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) stricter = QualityConstraint(anotherContext, CommonMetrics.SECONDS, 10, Comparison.LESS_OR_EQUAL_TO) task.setProvidedQuality(context, CommonMetrics.SECONDS, 13) goal.addDependency(task) goal.setIdentifier("Root") goal.addApplicableContext(context) goal.interp.addQualityConstraint(qc) goal.interp.addQualityConstraint(stricter) interp = Interpretation() interp.addQualityConstraint(qc) fullContext.append(context) assert stricter not in goal.interp.getQualityConstraints(fullContext) plan = goal.isAchievable(fullContext, interp) assert len(plan.getTasks()) == 1 fullContext.append(anotherContext) assert qc in goal.interp.getQualityConstraints(fullContext) assert stricter in goal.interp.getQualityConstraints(fullContext) assert goal.isAchievable(fullContext, interp) is None fullContext.remove(context) assert qc not in goal.interp.getQualityConstraints(fullContext) assert stricter in goal.interp.getQualityConstraints(fullContext)
def metricNotFound(): task = Task() currentContext = Context("C1") fullContext = set() fullContext.add(currentContext) task.setProvidedQuality(currentContext, MpersMetrics.METERS, 30.0) result = task.myProvidedQuality(MpersMetrics.SECONDS, fullContext) assert result is None
def test_shouldBeNotApplicable(): goal = Goal(Decomposition.AND) task = Task() delegation = Delegation() context = Context("C1") task.addApplicableContext(context) goal.addApplicableContext(context) delegation.addApplicableContext(context) wrongContext = Context("C2") fullContext = [] fullContext.append(wrongContext) assert False is goal.isApplicable(fullContext) assert False is task.isApplicable(fullContext) assert False is delegation.isApplicable(fullContext)
def test_shouldProvideSpecificContextMetric(): task = Task() currentContext = Context("C1") baseline = None fullContext = set() fullContext.add(currentContext) fullContext.add(baseline) task.setProvidedQuality(currentContext, MpersMetrics.METERS, 50) task.setProvidedQuality(baseline, MpersMetrics.METERS, 30) assert 50 == task.myProvidedQuality(MpersMetrics.METERS, fullContext)
def test_interpretation(): interp = Interpretation() context = Context("C1") commonMetrics = CommonMetrics() qc = QualityConstraint(context, commonMetrics.SECONDS, 15, Comparison.LESS_THAN) interp.addQualityConstraint(qc) map = interp.getContextDependentInterpretation() assert 1 == len(map) assert 1 == len(map[context]) assert 1 == len(map)
def test_shouldGetDifferentQualityConstraintsForDifferentContexts(): aContext = Context("c1") anotherContext = Context("c2") aQC = QualityConstraint(aContext, CommonMetrics.METERS, 30, Comparison.LESS_OR_EQUAL_TO) anotherQC = QualityConstraint( anotherContext, CommonMetrics.METERS, 60, Comparison.LESS_OR_EQUAL_TO) goal = Pragmatic(False) goal.interp.addQualityConstraint(aQC) goal.interp.addQualityConstraint(anotherQC) fullContext = [] fullContext.append(aContext) assert aQC in goal.interp.getQualityConstraints(fullContext) anotherFullContext = [] anotherFullContext.append(anotherContext) assert anotherQC in goal.interp.getQualityConstraints(anotherFullContext)
def test_shouldGetBaselineQC(): goal = Pragmatic(Decomposition.AND) context = Context("C1") qc = QualityConstraint(context, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) baselineQC = QualityConstraint(None, CommonMetrics.SECONDS, 10, Comparison.LESS_OR_EQUAL_TO) goal.setIdentifier("Root") goal.addApplicableContext(context) goal.interp.addQualityConstraint(qc) goal.interp.addQualityConstraint(baselineQC) assert baselineQC in goal.interp.getQualityConstraints([None])
def test_getQC(): interp = Interpretation() context = Context("C1") commonMetrics = CommonMetrics() qc1 = QualityConstraint(context, commonMetrics.SECONDS, 15, Comparison.LESS_THAN) qc2 = QualityConstraint(context, commonMetrics.METERS, 100, Comparison.LESS_THAN) interp.addQualityConstraint(qc1) interp.addQualityConstraint(qc2) listContext = [context] assert qc1 in interp.getQualityConstraints(listContext) assert qc2 in interp.getQualityConstraints(listContext)
def test_aTaskShouldBeAchievable(): task = Task() currentContext = Context("C1") fullContext = [] fullContext.append(currentContext) qc = QualityConstraint(currentContext, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) task.addApplicableContext(currentContext) task.setProvidedQuality(currentContext, CommonMetrics.SECONDS, 12) interp = Interpretation() interp.addQualityConstraint(qc) assert task in task.isAchievable(fullContext, interp).getTasks()
def test_shouldBeApplicable(): goal = Goal(Decomposition.AND) task = Task() delegation = Delegation() contextCurrent = Context("C1") fullContext = [] fullContext.append(contextCurrent) goal.addApplicableContext(contextCurrent) task.addApplicableContext(contextCurrent) delegation.addApplicableContext(contextCurrent) assert True is goal.isApplicable(fullContext) assert True is task.isApplicable(fullContext) assert True is delegation.isApplicable(fullContext)
def aTaskMayNotBeAchievable(): task = Task("T1") current = Context("C1") fullContext = [] fullContext.append(current) qc = QualityConstraint(current, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) task.addApplicableContext(current) task.setProvidedQuality(current, CommonMetrics.SECONDS, 16) interp = Interpretation() interp.addQualityConstraint(qc) assert task.isAchievable(fullContext, interp) is None
def test_shouldBeAchievable(): root = Goal(Decomposition.AND, "root") context = Context("c1") current = [] current.append(context) task1 = Task("t1") task2 = Task("t2") task1.addApplicableContext(context) root.addDependency(task1) root.addDependency(task2) plan = root.isAchievable(current, None) assert plan assert task2 in plan.getTasks()
def test_shouldGetApplicableDependencies(): root = Goal(Decomposition.AND) context = Context("c1") current = [] current.append(context) task = Task() goal = Goal(Decomposition.AND) delegation = Delegation() task.addApplicableContext(context) root.addDependency(task) root.addDependency(goal) root.addDependency(delegation) deps = [] deps.append(task) assert 1 == len(deps) assert task in deps
def test_shouldGetCorrectContexts(): qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) Context("C1") is qc.getApplicableContext()
def shouldCorrectlyCompareMetrics(): qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) assert True is qc.abidesByQC(14, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) assert True is qc.abidesByQC(14, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) assert True is qc.abidesByQC(15, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.EQUAL_TO) assert True is qc.abidesByQC(15, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.GREATER_OR_EQUAL_TO) assert True is qc.abidesByQC(15, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.GREATER_OR_EQUAL_TO) assert True is qc.abidesByQC(16, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.GREATER_THAN) assert True is qc.abidesByQC(16, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) assert False is qc.abidesByQC(16, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) assert False is qc.abidesByQC(16, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.EQUAL_TO) assert False is qc.abidesByQC(16, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.GREATER_OR_EQUAL_TO) assert False is qc.abidesByQC(14, CommonMetrics.SECONDS) qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.GREATER_THAN) assert False is qc.abidesByQC(14, CommonMetrics.SECONDS)
def shouldAbideByQcIfMetricIsNotAffected(): qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) assert True is qc.abidesByQC(15, CommonMetrics.METERS)
def test_shouldBeBetterThan(): qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) assert True is qc.abidesByQC(13, CommonMetrics.SECONDS) assert False is qc.abidesByQC(16, CommonMetrics.SECONDS)