| 1 | |
|---|
| 2 | :- object(brick, |
|---|
| 3 | instantiates(class), |
|---|
| 4 | specializes(object)). |
|---|
| 5 | |
|---|
| 6 | :- info([ |
|---|
| 7 | version is 1.1, |
|---|
| 8 | date is 2000/10/31, |
|---|
| 9 | author is 'Paulo Moura', |
|---|
| 10 | comment is 'Two-dimensional brick (or should I say square?) class.']). |
|---|
| 11 | |
|---|
| 12 | :- public(position/2). |
|---|
| 13 | :- mode(position(?integer, ?integer), zero_or_one). |
|---|
| 14 | :- info(position/2, [ |
|---|
| 15 | comment is 'Brick current position.', |
|---|
| 16 | argnames is ['X', 'Y']]). |
|---|
| 17 | |
|---|
| 18 | :- private(position_/2). |
|---|
| 19 | :- dynamic(position_/2). |
|---|
| 20 | :- mode(position_(?integer, ?integer), zero_or_one). |
|---|
| 21 | :- info(position_/2, [ |
|---|
| 22 | comment is 'Stores brick current position.', |
|---|
| 23 | argnames is ['X', 'Y']]). |
|---|
| 24 | |
|---|
| 25 | :- public(move/2). |
|---|
| 26 | :- mode(move(+integer, +integer), one). |
|---|
| 27 | :- info(move/2, [ |
|---|
| 28 | comment is 'Moves a brick to a new position.', |
|---|
| 29 | argnames is ['X', 'Y']]). |
|---|
| 30 | |
|---|
| 31 | position(X, Y) :- |
|---|
| 32 | ::position_(X, Y). |
|---|
| 33 | |
|---|
| 34 | move(X, Y) :- |
|---|
| 35 | ::retractall(position_(_, _)), |
|---|
| 36 | ::assertz(position_(X, Y)). |
|---|
| 37 | |
|---|
| 38 | default_init_option(position-(0, 0)). |
|---|
| 39 | default_init_option(Default) :- |
|---|
| 40 | ^^default_init_option(Default). |
|---|
| 41 | |
|---|
| 42 | process_init_option(position-(X, Y)) :- |
|---|
| 43 | ::assertz(position_(X, Y)). |
|---|
| 44 | process_init_option(Option) :- |
|---|
| 45 | ^^process_init_option(Option). |
|---|
| 46 | |
|---|
| 47 | valid_init_option(position-(X, Y)) :- |
|---|
| 48 | !, |
|---|
| 49 | integer(X), |
|---|
| 50 | integer(Y). |
|---|
| 51 | valid_init_option(Option) :- |
|---|
| 52 | ^^valid_init_option(Option). |
|---|
| 53 | |
|---|
| 54 | instance_base_name(b). |
|---|
| 55 | |
|---|
| 56 | :- end_object. |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | :- object(brick_stack, |
|---|
| 60 | instantiates(constrained_relation)). |
|---|
| 61 | |
|---|
| 62 | :- info([ |
|---|
| 63 | version is 1.0, |
|---|
| 64 | date is 1998/3/23, |
|---|
| 65 | author is 'Paulo Moura', |
|---|
| 66 | comment is 'Stack of bricks as a constrained binary relation.']). |
|---|
| 67 | |
|---|
| 68 | descriptor_([top, bottom]). |
|---|
| 69 | |
|---|
| 70 | domain_(top, brick). |
|---|
| 71 | domain_(bottom, brick). |
|---|
| 72 | |
|---|
| 73 | key_([top, bottom]). |
|---|
| 74 | |
|---|
| 75 | cardinality_(top, 0, 1). |
|---|
| 76 | cardinality_(bottom, 0, 1). |
|---|
| 77 | |
|---|
| 78 | delete_option_(top, cascade). |
|---|
| 79 | delete_option_(bottom, restrict). |
|---|
| 80 | |
|---|
| 81 | add_tuple([A, B]) :- |
|---|
| 82 | B::position(Xb, Yb), |
|---|
| 83 | Ya2 is Yb + 1, |
|---|
| 84 | {A::move(Xb, Ya2)}, % this message must be compiled with event support |
|---|
| 85 | ^^add_tuple([A, B]). |
|---|
| 86 | |
|---|
| 87 | activ_points_(top, before, []). |
|---|
| 88 | activ_points_(top, after, [move(_, _)]). |
|---|
| 89 | |
|---|
| 90 | activ_points_(bottom, before, []). |
|---|
| 91 | activ_points_(bottom, after, [move(_, _)]). |
|---|
| 92 | |
|---|
| 93 | propagate(after, move(X, Y), Top, top, [Top, Bottom]) :- |
|---|
| 94 | !, |
|---|
| 95 | Y2 is Y - 1, |
|---|
| 96 | ( Bottom::position(X, Y2) -> |
|---|
| 97 | true |
|---|
| 98 | ; ::remove_tuple([Top, Bottom]) |
|---|
| 99 | ). |
|---|
| 100 | |
|---|
| 101 | propagate(after, move(X, Y), Bottom, bottom, [Top, Bottom]) :- |
|---|
| 102 | !, |
|---|
| 103 | Y2 is Y + 1, |
|---|
| 104 | {Top::move(X, Y2)}. % this message must be compiled with event support |
|---|
| 105 | |
|---|
| 106 | :- end_object. |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | :- object(stack_monitor, |
|---|
| 110 | implements(monitoring)). |
|---|
| 111 | |
|---|
| 112 | :- info([ |
|---|
| 113 | version is 1.1, |
|---|
| 114 | date is 2006/12/14, |
|---|
| 115 | author is 'Paulo Moura', |
|---|
| 116 | comment is 'Monitor for brick movements printing an ascii representation of each brick position.']). |
|---|
| 117 | |
|---|
| 118 | :- uses(loop). |
|---|
| 119 | :- uses(list). |
|---|
| 120 | |
|---|
| 121 | after(_, move(_, _), _) :- |
|---|
| 122 | findall( |
|---|
| 123 | (Brick, X, Y), |
|---|
| 124 | (instantiates_class(Brick, brick), Brick::position(X, Y)), |
|---|
| 125 | Bricks), |
|---|
| 126 | setof(X, Brick^Y^ (list::member((Brick,X,Y), Bricks)), Xs), |
|---|
| 127 | list::last(Xs, Xmax), |
|---|
| 128 | setof(Y, Brick^X^ (list::member((Brick,X,Y), Bricks)), Ys), |
|---|
| 129 | list::last(Ys, Ymax), |
|---|
| 130 | loop::fordownto(Y, Ymax, 1, |
|---|
| 131 | (write('|'), |
|---|
| 132 | loop::forto(X, 1, Xmax, |
|---|
| 133 | (list::member((Brick, X, Y), Bricks) -> |
|---|
| 134 | write(Brick) |
|---|
| 135 | ; |
|---|
| 136 | write('.'))), |
|---|
| 137 | nl)), |
|---|
| 138 | write('-'), |
|---|
| 139 | loop::forto(X, 1, Xmax, write('-')), nl. |
|---|
| 140 | |
|---|
| 141 | :- end_object. |
|---|