def test_cyclic(self): '''Build a tree with cyclic nodes''' a = Value('a') b = Value('b') c = Value('c') one = Value('one', 1) two = Value('two', 2) three = Value('three', 3) four = Value('four', 4) a.dependants.append(c) a.dependants.append(one) b.dependants.append(c) one.dependants.append(a) two.dependants.append(a) three.dependants.append(b) four.dependants.append(b) builder = GraphBuilder() vbuilder = VBuilder() builder.add_with_dependants(one, vbuilder) builder.add_with_dependants(two, vbuilder) builder.add_with_dependants(three, vbuilder) builder.add_with_dependants(four, vbuilder) graph = builder.build() graph.evaluate() self.assertTrue(a.cyclic) self.assertTrue(c.cyclic) self.assertTrue(one.cyclic) self.assertFalse(b.cyclic) self.assertFalse(two.cyclic) self.assertFalse(three.cyclic) self.assertFalse(four.cyclic)
def test_self_build(self): '''Build a tree from nodes that know how they are connected''' a = Value('a') b = Value('b') c = Value('c') one = Value('one', 1) two = Value('two', 2) three = Value('three', 3) four = Value('four', 4) a.dependants.append(c) b.dependants.append(c) one.dependants.append(a) two.dependants.append(a) three.dependants.append(b) four.dependants.append(b) builder = GraphBuilder() vbuilder = VBuilder() builder.add_with_dependants(one, vbuilder) builder.add_with_dependants(two, vbuilder) builder.add_with_dependants(three, vbuilder) builder.add_with_dependants(four, vbuilder) graph = builder.build() graph.evaluate() self.assertEqual(a.value, 3) self.assertEqual(b.value, 7) self.assertEqual(c.value, 10)
def test_disparate(self): '''Test evaluation of two disconnected graphs in a single object''' builder = GraphBuilder() a = Value('a') b = Value('b') ops = Sum() builder.add(b).with_ops(ops) builder.add(a).with_ops(ops) builder.add(1).with_dependant(a) builder.add(2).with_dependant(a) builder.add(3).with_dependant(b) builder.add(4).with_dependant(b) graph = builder.build() graph.evaluate() self.assertEqual(a.value, 3) self.assertEqual(b.value, 7)
def test_proxy(self): '''Test replacement of operations after tree is built''' builder = GraphBuilder() a = Value('a') ops = OpsProxy() builder.add(a).with_ops(ops) builder.add(1).with_dependant(a) builder.add(2).with_dependant(a) graph = builder.build() graph.evaluate() self.assertEqual(a.value, 0) ops.ops = Sum() graph.evaluate() self.assertEqual(a.value, 3)
def __init__(self, config): self.config = config self.builder = GraphBuilder()
class Calculator: def __init__(self, config): self.config = config self.builder = GraphBuilder() @classmethod def structural(cls): config = GraphConfig() manifest = StructureManifest() config.with_manifest(manifest) return cls(config) @classmethod def scoring(cls, submission): config = GraphConfig() manifest = ScoreManifest(submission) config.with_manifest(manifest) return cls(config) def mark_program_dirty(self, program, force_dependants=False): self.builder.add_with_dependants(program, self.config.program_builder, force_dependants) def mark_survey_dirty(self, survey, force_dependants=False): self.builder.add_with_dependants(survey, self.config.survey_builder, force_dependants) def mark_entire_survey_dirty(self, survey): self.builder.add_with_dependencies(survey, self.config.survey_builder) def mark_qnode_dirty(self, qnode, force_dependants=False): self.builder.add_with_dependants(qnode, self.config.qnode_builder, force_dependants) def mark_measure_dirty(self, qnode_measure, force_dependants=False): self.builder.add_with_dependants(qnode_measure, self.config.measure_builder, force_dependants) def execute(self): graph = self.builder.build() graph.evaluate()
def test_simple(self): '''Test evaluation of a simple arithmetic tree''' builder = GraphBuilder() a = Value('a') b = Value('b') c = Value('c') ops = Sum() builder.add(c).with_ops(ops) builder.add(b).with_ops(ops).with_dependant(c) builder.add(a).with_ops(ops).with_dependant(c) builder.add(1).with_dependant(a) builder.add(2).with_dependant(a) builder.add(3).with_dependant(b) builder.add(4).with_dependant(b) graph = builder.build() graph.evaluate() self.assertEqual(a.value, 3) self.assertEqual(b.value, 7) self.assertEqual(c.value, 10)