Esempio n. 1
0
 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))
Esempio n. 2
0
 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)))
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
    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)
Esempio n. 6
0
    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)
Esempio n. 7
0
    def test_false_to_true(self):
        a = Cell(content=False)
        b = Cell()

        inverter(a, b)

        scheduler.run()

        self.assertEqual(b.content, True)
Esempio n. 8
0
    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)
Esempio n. 9
0
    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')
Esempio n. 10
0
    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])
Esempio n. 11
0
    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)
Esempio n. 12
0
    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)
Esempio n. 13
0
    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)
Esempio n. 14
0
    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)
Esempio n. 15
0
    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)
Esempio n. 16
0
    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)
Esempio n. 17
0
    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')
Esempio n. 18
0
    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)
Esempio n. 19
0
    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)
Esempio n. 20
0
    def test_integer(self):
        a = Cell()

        constant(5)(a)

        scheduler.run()

        self.assertEqual(a.content, 5)
Esempio n. 21
0
    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)
Esempio n. 22
0
    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)
Esempio n. 23
0
    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)
Esempio n. 24
0
    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)
Esempio n. 25
0
    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)
Esempio n. 26
0
    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')
Esempio n. 27
0
    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)
Esempio n. 28
0
    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)
Esempio n. 29
0
 def test_add_content_to_empty_cell(self):
     a = Cell()
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Esempio n. 30
0
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)
Esempio n. 31
0
 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')
Esempio n. 32
0
 def similar_triangles_helper():
     ratio = Cell('ratio')
     product(s_ba, ratio, h_ba)
     product(s, ratio, h)
Esempio n. 33
0
        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'})
Esempio n. 34
0
 def test_add_same_content_to_filled_cell(self):
     a = Cell(content='hello')
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Esempio n. 35
0
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)
Esempio n. 37
0
 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))
Esempio n. 38
0
 def sqrt_network_helper():
     one = Cell('one')
     (constant(1))(one)
     sqrt_iter(x, one, answer)
Esempio n. 39
0
 def test_add_same_content_to_filled_cell(self):
     a = Cell(content='hello')
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Esempio n. 40
0
    def test_new_cell_with_neighbors(self):
        f = lambda x: x

        a = Cell(content='hello')
        a.new_neighbor(f)
        self.assertEqual(a.neighbors, [f])
Esempio n. 41
0
        # `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):
Esempio n. 42
0
 def test_add_nothing_to_empty_cell(self):
     a = Cell()
     a.add_content(None)
     self.assertEqual(a.content, None)
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)
Esempio n. 44
0
    def test_new_cell_with_neighbors(self):
        f = lambda x: x

        a = Cell(content='hello')
        a.new_neighbor(f)
        self.assertEqual(a.neighbors, [f])
Esempio n. 45
0
 def test_new_cell_has_no_neighbors(self):
     a = Cell(content='hello')
     self.assertEqual(a.neighbors, [])