def test_multiplier_exact(self): c1 = Cell(content=Supported(3)) c2 = Cell(content=Supported(4)) c3 = Cell() multiplier(c1, c2, c3) scheduler.run() self.assertEqual(c3.content, Supported(12))
def test_multiplier_interval(self): c1 = Cell(content=Supported(Interval(3, 4))) c2 = Cell(content=Supported(Interval(5, 6))) c3 = Cell() multiplier(c1, c2, c3) scheduler.run() self.assertEqual(c3.content, Supported(Interval(15, 24)))
def test_add_existing_neighbor(self): f = lambda x: x a = Cell(content='hello') a.new_neighbor(f) a.new_neighbor(f) self.assertEqual(len(a.neighbors), 1)
def helper(): x_over_g = Cell('x/g') g_plus_x_over_g = Cell('g+x/g') two = Cell('two') divider(x, g, x_over_g) adder(g, x_over_g, g_plus_x_over_g) (constant(2))(two) divider(g_plus_x_over_g, two, h)
def to_do(): g_to_2 = Cell('g^2') x_minus_g_to_2 = Cell('x-g^2') ax_minus_g_to_2 = Cell('abs(x-g^2)') multiplier(g, g, g_to_2) subtractor(x, g_to_2, x_minus_g_to_2) absolute_value(x_minus_g_to_2, ax_minus_g_to_2) less_than(ax_minus_g_to_2, eps, done)
def test_false_to_true(self): a = Cell(content=False) b = Cell() inverter(a, b) scheduler.run() self.assertEqual(b.content, True)
def test_false(self): a = Cell(content=False) b = Cell(content='yes') c = Cell() switch(a, b, c) scheduler.run() self.assertEqual(c.content, None)
def test_true(self): a = Cell(content=True) b = Cell(content='yes') c = Cell() switch(a, b, c) scheduler.run() self.assertEqual(c.content, 'yes')
def new_propagator_appends_function_to_neighbors(self): a = Cell() b = Cell() c = Cell() f = lambda x: x Propagator([a, b, c], f) for cell in [a, b, c]: self.assertEqual(cell.neighbors, [f])
def test_integer_false(self): a = Cell(content=13) b = Cell(content=15) c = Cell() greater_than(a, b, c) scheduler.run() self.assertEqual(c.content, False)
def test_integer_true(self): a = Cell(content=17) b = Cell(content=15) c = Cell() greater_than(a, b, c) scheduler.run() self.assertEqual(c.content, True)
def fall_duration_helper(): g = Cell('g') one_half = Cell('one half') t_to_2 = Cell('t^2') g_times_t_to_2 = Cell('gt^2') (constant(Interval(9.789, 9.832)))(g) (constant(Interval(0.5, 0.5)))(one_half) quadratic(t, t_to_2) product(g, t_to_2, g_times_t_to_2) product(one_half, g_times_t_to_2, h)
def fall_duration_helper(): g = Cell('g') one_half = Cell('one half') t_to_2 = Cell('t^2') g_times_t_to_2 = Cell('gt^2') (constant(Interval(9.789, 9.832)))(g) (constant(Interval(1 / 2, 1 / 2)))(one_half) squarer(t, t_to_2) multiplier(g, t_to_2, g_times_t_to_2) multiplier(one_half, g_times_t_to_2, h)
def test_integer(self): a = Cell() b = Cell() c = Cell() multiplier(a, b, c) a.add_content(5) b.add_content(3) scheduler.run() self.assertEqual(c.content, 15)
def test_float(self): a = Cell() b = Cell() c = Cell() adder(a, b, c) a.add_content(1.5) b.add_content(1.3) scheduler.run() self.assertEqual(c.content, 2.8)
def test_str(self): a = Cell() b = Cell() c = Cell() adder(a, b, c) a.add_content('15') b.add_content('13') scheduler.run() self.assertEqual(c.content, '1513')
def test_integer(self): a = Cell() b = Cell() c = Cell() subtractor(a, b, c) a.add_content(15) b.add_content(13) scheduler.run() self.assertEqual(c.content, 2)
def test_integer(self): a = Cell() b = Cell() c = Cell() divider(a, b, c) a.add_content(9) b.add_content(3) scheduler.run() self.assertEqual(c.content, 3)
def test_integer(self): a = Cell() constant(5)(a) scheduler.run() self.assertEqual(a.content, 5)
def test_integer(self): a = Cell() b = Cell() a_ = Cell() b_ = Cell() absolute_value(a, b) absolute_value(a_, b_) a.add_content(-9) a_.add_content(9) scheduler.run() self.assertEqual(b.content, 9) self.assertEqual(b_.content, 9)
def sqrt_iter_helper(): debug("sqrt_iter_helper: {x}, {g}, {answer}".format(**vars())) done = Cell('done') not_done = Cell('not(done)') x_if_not_done = Cell('x if not(done)') g_if_not_done = Cell('g if not(done)') new_g = Cell('new g') good_enuf(g, x, done) switch(done, g, answer) inverter(done, not_done) switch(not_done, x, x_if_not_done) switch(not_done, g, g_if_not_done) heron_step(x_if_not_done, g_if_not_done, new_g) # Clever recursion: this will call this helper again only if # `x_if_not_done` is not None. # # `x_if_not_done` will not have content if `answer` has content, # so it will stop recursing when an answer is found. sqrt_iter(x_if_not_done, new_g, answer)
def test_add_content_to_empty_cell(self): a = Cell() a.add_content('hello') self.assertEqual(a.content, 'hello')
defined in this module. """ def good_enuf(g, x, done): @compound(neighbors=[g, x]) def to_do(): g_to_2 = Cell('g^2') x_minus_g_to_2 = Cell('x-g^2') ax_minus_g_to_2 = Cell('abs(x-g^2)') multiplier(g, g, g_to_2) subtractor(x, g_to_2, x_minus_g_to_2) absolute_value(x_minus_g_to_2, ax_minus_g_to_2) less_than(ax_minus_g_to_2, eps, done) return to_do if __name__ == '__main__': scheduler.initialize() x = Cell('x') answer = Cell('answer') sqrt_network(x, answer) x.add_content(2) scheduler.run() print(answer.content)
def test_new_cell_with_content(self): a = Cell(content='hello') b = Cell() b.add_content('hello') self.assertEqual(a.content, 'hello') self.assertEqual(b.content, 'hello')
def similar_triangles_helper(): ratio = Cell('ratio') product(s_ba, ratio, h_ba) product(s, ratio, h)
product(one_half, g_times_t_to_2, h) return fall_duration_helper if __name__ == '__main__': scheduler.initialize() # We now build a sequence of sample dependency tracking systems of # increasing complexity. We start with a relatively simple system # that only tracks and reports the provenance of its data. # # How do we want our provenance system to work? We can make cells # and define networks as usual, but if we add supported values as inputs, # we get supported values as outputs: barometer_height = Cell('barometer height') barometer_shadow = Cell('barometer shadow') building_height = Cell('building height') building_shadow = Cell('building shadow') similar_triangles(barometer_shadow, barometer_height, building_shadow, building_height) building_shadow.add_content(Supported(Interval(54.9, 55.1), ['shadows'])) barometer_height.add_content(Supported(Interval(0.3, 0.32), ['shadows'])) barometer_shadow.add_content(Supported(Interval(0.36, 0.37), ['shadows'])) scheduler.run() print(building_height.content) # Supported(Interval(44.51351351351351, 48.977777777777774), {'shadows'})
def test_add_same_content_to_filled_cell(self): a = Cell(content='hello') a.add_content('hello') self.assertEqual(a.content, 'hello')
def switch(predicate, if_true, output): return conditional(predicate, if_true, Cell('_'), output)
def similar_triangles(s_ba, h_ba, s, h): @compound(neighbors=[s_ba, h_ba, s]) def similar_triangles_helper(): ratio = Cell('ratio') product(s_ba, ratio, h_ba) product(s, ratio, h) return similar_triangles_helper if __name__ == '__main__': scheduler.initialize() # Now the estimation of the building’s height works just fine, barometer_height = Cell('barometer height') barometer_shadow = Cell('barometer shadow') building_height = Cell('building height') building_shadow = Cell('building shadow') similar_triangles(barometer_shadow, barometer_height, building_shadow, building_height) building_shadow.add_content(Interval(54.9, 55.1)) barometer_height.add_content(Interval(0.3, 0.32)) barometer_shadow.add_content(Interval(0.36, 0.37)) scheduler.run() print(building_height.content) # Interval(44.51351351351351, 48.977777777777774)
def test_add_new_content_to_filled_cell_returns_contradiction(self): a = Cell(content='hello') a.add_content('world') self.assertTrue(is_contradictory(a.content))
def sqrt_network_helper(): one = Cell('one') (constant(1))(one) sqrt_iter(x, one, answer)
def test_new_cell_with_neighbors(self): f = lambda x: x a = Cell(content='hello') a.new_neighbor(f) self.assertEqual(a.neighbors, [f])
# `x_if_not_done` is not None. # # `x_if_not_done` will not have content if `answer` has content, # so it will stop recursing when an answer is found. sqrt_iter(x_if_not_done, new_g, answer) return sqrt_iter_helper """ A `Cell` object containing a very small number. Used in `good_enuf`. """ eps = Cell('eps', content=0.0000000001) """ Creates a propagator network that stores `True` in `done` if `g` is good enough as a guess of the square root of `x`, and stores `False` otherwise. To be good enough, `g` and `x` contents have to obey this equation: abs(x - pow(g, 2)) <= eps Where `eps` is a `Cell` object containing a very small number, and is defined in this module. """ def good_enuf(g, x, done):
def test_add_nothing_to_empty_cell(self): a = Cell() a.add_content(None) self.assertEqual(a.content, None)
def test_new_cell_has_no_neighbors(self): a = Cell(content='hello') self.assertEqual(a.neighbors, [])