Example #1
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)
Example #2
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)
Example #3
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')
Example #4
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)
Example #5
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)
Example #6
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)
Example #7
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)
Example #8
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')
Example #9
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)
Example #10
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)
Example #11
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)
Example #12
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)
Example #13
0
 def test_add_content_to_empty_cell(self):
     a = Cell()
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Example #14
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')
Example #15
0
 def test_add_nothing_to_empty_cell(self):
     a = Cell()
     a.add_content(None)
     self.assertEqual(a.content, None)
Example #16
0
 def test_add_content_to_empty_cell(self):
     a = Cell()
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Example #17
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))
Example #18
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)

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)

    # as does the refinement of that estimate by adding another
    # measurement.

    fall_time = Cell('fall time')

    fall_duration(fall_time, building_height)
Example #20
0
    # 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'})

    # Indeed, our estimate for the height of the building depends on our
    # measurements of the barometer and the shadow. We can try
    # dropping the barometer off the roof, but if we do a bad job of
    # timing its fall, our estimate won’t improve.

    fall_time = Cell('fall time')
    fall_duration(fall_time, building_height)
Example #21
0

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)
    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)

    # as does the refinement of that estimate by adding another
    # measurement.

    fall_time = Cell('fall time')

    fall_duration(fall_time, building_height)
Example #23
0
 def test_add_same_content_to_filled_cell(self):
     a = Cell(content='hello')
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Example #24
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))
Example #25
0
 def test_add_same_content_to_filled_cell(self):
     a = Cell(content='hello')
     a.add_content('hello')
     self.assertEqual(a.content, 'hello')
Example #26
0
 def test_add_nothing_to_empty_cell(self):
     a = Cell()
     a.add_content(None)
     self.assertEqual(a.content, None)
Example #27
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')