def test_shouldThereBeMoreThanOneApplicableQCreturnTheStricterOne(): goal = Pragmatic(Decomposition.AND, "Root") task = Task("T1") 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.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_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_shouldIncludeNonApplicableContexts(): goal = Pragmatic(False, "Root") task = Task("T1") 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.addNonapplicableContext(wrongContext) goal.interp.addQualityConstraint(qc) interp = Interpretation() interp.addQualityConstraint(qc) current.append(wrongContext) assert PragmaticPlanning().isAchievable(goal, current, interp) is None current.append(context) assert PragmaticPlanning().isAchievable(goal, current, interp) is None current.remove(wrongContext) assert PragmaticPlanning().isAchievable(goal, current, interp) assert PragmaticPlanning().isAchievable(goal, current, interp).getTasks() assert 1 == len(goal.isAchievable(current, interp).getTasks())
def test_ApplicableDeps(): goal = Pragmatic(Decomposition.AND, "Root") task = Task("T1") 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.addDependency(task) goal.addApplicableContext(context) goal.interp.addQualityConstraint(qc) interp = Interpretation() interp.addQualityConstraint(qc) current = [] current.append(wrongContext) assert PragmaticPlanning().isAchievable(goal, current, interp) is None current.append(context) assert len(goal.isAchievable(current, interp).getTasks()) == 1
def test_shouldBeUnachievable(): root = Goal(Decomposition.AND, "root") context1 = Context("c1") context2 = Context("c2") current = [] current.append(context1) task1 = Task("T1") task2 = Task("T2") task1.addApplicableContext(context2) task2.addApplicableContext(context2) root.addDependency(task1) root.addDependency(task2) deps = [] deps.append(task1) deps.append(task2) plan = PragmaticPlanning().isAchievable(root, current, None) assert plan is None
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("T1") assert task.applicableContext is None task.addApplicableContext([context1, context2]) assert 2 == len(task.applicableContext)
def test_aNonApplicableRootGoalIsNotAchievable(): goal = Goal(Decomposition.AND, "G1") 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 PragmaticPlanning().isAchievable(goal, fullContext, interp) is None
def test_aGoalOrDecomposedWithTwoTasksMayNotBeAchievable(): goal = Goal(Decomposition.OR, "Root") task1 = Task("T1") task2 = Task("T2") 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.addApplicableContext(current) interp = Interpretation() interp.addQualityConstraint(qc) plan = PragmaticPlanning().isAchievable(goal, fullContext, interp) assert goal.decomposition is Decomposition.OR assert plan is None
def test_getApplicableQC(): goal = Pragmatic(Decomposition.AND, "Root") task = Task("T1") 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.addApplicableContext(context) goal.interp.addQualityConstraint(qc) goal.interp.addQualityConstraint(stricter) fullContext.append(context) assert stricter not in goal.interp.getQualityConstraints( fullContext) plan = PragmaticPlanning().isAchievable(goal, fullContext, goal.interp) assert len(plan.getTasks()) == 1 fullContext.append(anotherContext) assert qc in goal.interp.getQualityConstraints( fullContext) assert stricter in goal.interp.getQualityConstraints( fullContext) assert PragmaticPlanning().isAchievable(goal, fullContext, goal.interp) is None fullContext.remove(context) assert qc not in goal.interp.getQualityConstraints( fullContext) assert stricter in goal.interp.getQualityConstraints( fullContext)
def test_context_gen(): c1 = Context("c1") c2 = Context("c2") generator = ContextGenerator([c1, c2]) generatorIter = iter(generator) assert [] == next(generatorIter) lastContext = None numberOfContexts = 0 for context in generatorIter: lastContext = context numberOfContexts = numberOfContexts + 1 assert lastContext == [c1, c2] assert numberOfContexts == 4
def text_shouldProvideMetricForBaseline(): task = Task("t1") current = Context("C1") fullContext = [] fullContext.append(current) task.setProvidedQuality(None, MpersMetrics.METERS, 30.0) assert 30.0 == PragmaticPlanning().myProvidedQuality( task, MpersMetrics.METERS, fullContext)
def test_shouldBeNotApplicable(): goal = Goal(Decomposition.AND, "G1") task = Task("T1") delegation = Delegation("D1") context = Context("C1") task.addApplicableContext(context) goal.addApplicableContext(context) delegation.addApplicableContext(context) wrongContext = Context("C2") fullContext = [] fullContext.append(wrongContext) assert False is PragmaticPlanning().isApplicable(goal, fullContext) assert False is PragmaticPlanning().isApplicable(task, fullContext) assert False is PragmaticPlanning().isApplicable(delegation, fullContext)
def test_OnlyBaselineDefined(): task = Task("T1") baseline = Context(None) fullContext = [] fullContext.append(baseline) task.setProvidedQuality(baseline, MpersMetrics.METERS, 50.0) assert 50.0 == PragmaticPlanning().myProvidedQuality( task, MpersMetrics.METERS, fullContext)
def test_shouldProvideCorrectValueForMetric(): task = Task("T1") currentContext = Context("C1") fullContext = [] fullContext.append(currentContext) task.setProvidedQuality(currentContext, MpersMetrics.METERS, 30) assert 30 == PragmaticPlanning().myProvidedQuality(task, MpersMetrics.METERS, fullContext)
def metricNotFound(): task = Task("T1") currentContext = Context("C1") fullContext = [] fullContext.append(currentContext) task.setProvidedQuality(currentContext, MpersMetrics.METERS, 30.0) result = PragmaticPlanning().myProvidedQuality(task, MpersMetrics.SECONDS, fullContext) assert result is None
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(Decomposition.AND, "G1") 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_shouldProvideSpecificContextMetric(): task = Task("T2") currentContext = Context("C1") baseline = None fullContext = [] fullContext.append(currentContext) fullContext.append(baseline) task.setProvidedQuality(currentContext, MpersMetrics.METERS, 50) task.setProvidedQuality(baseline, MpersMetrics.METERS, 30) assert 50 == PragmaticPlanning().myProvidedQuality(task, MpersMetrics.METERS, fullContext)
def test_shouldGetBaselineQC(): goal = Pragmatic(Decomposition.AND, "Root") 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.addApplicableContext(context) goal.interp.addQualityConstraint(qc) goal.interp.addQualityConstraint(baselineQC) assert baselineQC in goal.interp.getQualityConstraints([None])
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 PragmaticPlanning().isAchievable(task, fullContext, interp) is None
def test_aTaskShouldBeAchievable(): task = Task("T1") 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 PragmaticPlanning().isAchievableTask(task, fullContext, interp).getTasks()
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 = PragmaticPlanning().isAchievable(root, current, None) assert plan assert task2 in plan.getTasks()
def test_aGoalWithATaskMayBeAchievable(): goal = Goal(Decomposition.AND, "Root") task = Task("T1") current = Context("C1") fullContext = [] fullContext.append(current) qc = QualityConstraint(current, CommonMetrics.SECONDS, 15, Comparison.LESS_OR_EQUAL_TO) interp = Interpretation() interp.addQualityConstraint(qc) task.addApplicableContext(current) task.setProvidedQuality(current, CommonMetrics.SECONDS, 13) goal.addDependency(task) goal.addApplicableContext(current) plan = PragmaticPlanning().isAchievable(goal, fullContext, interp) assert len(plan.getTasks()) == 1
def test_shouldGetApplicableDependencies(): root = Goal(Decomposition.AND, "root") context = Context("c1") current = [] current.append(context) task = Task("t1") goal = Goal(Decomposition.AND, "g1") delegation = Delegation("D1") 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 shouldAbideByQcIfMetricIsNotAffected(): qc = QualityConstraint(Context("C1"), CommonMetrics.SECONDS, 15, Comparison.LESS_THAN) assert True is qc.abidesByQC(15, CommonMetrics.METERS)
class MpersContexts(): # Contexts c1 = Context("c1") c2 = Context("c2") c3 = Context("c3") c4 = Context("c4") c5 = Context("c5") c6 = Context("c6") c7 = Context("c7") c8 = Context("c8") c9 = Context("c9") c10 = Context("c10") c11 = Context("c11") c12 = Context("c12")
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)
from goald.quality.common.model.decomposition import Decomposition from goald.quality.common.model.task import Task from goald.quality.common.model.metric import Metric from goald.quality.common.model.quality_constraint import QualityConstraint from goald.quality.common.model.comparison import Comparison from goald.quality.common.model.interpretation import Interpretation from tests.utils.assert_util import assertPlan from tests.test_data.mpers_metric import MpersMetrics from tests.test_data.mpers_model import MpersModel from goald.quality.planning.pragmatic.pragmatic import Pragmatic from goald.quality.planning.optimized_planning import Planning import pytest # Contexts c1 = Context("c1") c2 = Context("c2") c3 = Context("c3") c4 = Context("c4") task1 = Task("task1") task2 = Task("task2") task3 = Task("task3") task4 = Task("task4") task2.addApplicableContext(c2) task1.setProvidedQuality(None, MpersMetrics.SECONDS, 80) task2.setProvidedQuality(None, MpersMetrics.SECONDS, 60) task3.setProvidedQuality(None, MpersMetrics.SECONDS, 100) task4.setProvidedQuality(None, MpersMetrics.SECONDS, 200)
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)