Changeset 4522 for trunk/examples
- Timestamp:
- 10/20/08 11:40:03 (3 months ago)
- Location:
- trunk/examples
- Files:
-
- 28 modified
-
benchmarks/loader_events.lgt (modified) (1 diff)
-
bricks/bricks.lgt (modified) (2 diffs)
-
bricks/loader.lgt (modified) (1 diff)
-
complements/complements.lgt (modified) (1 diff)
-
complements/loader.lgt (modified) (1 diff)
-
complements/SCRIPT.txt (modified) (2 diffs)
-
hello_world/hello_world.lgt (modified) (1 diff)
-
hello_world/NOTES.txt (modified) (1 diff)
-
metapredicates/closures.lgt (modified) (1 diff)
-
msglog/loader.lgt (modified) (1 diff)
-
msglog/msglog.lgt (modified) (5 diffs)
-
msglog/NOTES.txt (modified) (1 diff)
-
polygons/loader.lgt (modified) (1 diff)
-
polygons/SCRIPT.txt (modified) (1 diff)
-
profiling/loader.lgt (modified) (1 diff)
-
profiling/message_counter.lgt (modified) (7 diffs)
-
profiling/SCRIPT.txt (modified) (2 diffs)
-
profiling/stop_watch.lgt (modified) (1 diff)
-
profiling/timer.lgt (modified) (4 diffs)
-
searching/blind_search1.lgt (modified) (5 diffs)
-
searching/heuristic_search1.lgt (modified) (5 diffs)
-
searching/hill_climbing1.lgt (modified) (4 diffs)
-
searching/loader.lgt (modified) (1 diff)
-
searching/performance.lgt (modified) (9 diffs)
-
searching/search_strategy.lgt (modified) (3 diffs)
-
searching/state_space.lgt (modified) (4 diffs)
-
threads/birthdays/loader.lgt (modified) (1 diff)
-
threads/birthdays/SCRIPT.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/benchmarks/loader_events.lgt
r3754 r4522 1 1 2 % uncomment the next line if your Prolog compiler supports modules2 % uncomment the next line only if your Prolog compiler supports modules 3 3 %:- ensure_loaded(module). 4 4 5 5 :- initialization( 6 logtalk_load([category, objects, database, plain, benchmarks], [events(on)])). 6 logtalk_load( 7 [category, objects, database, plain, benchmarks], 8 [events(on)])). % compile messages with event support in order 9 % to measure the implicit overhead -
trunk/examples/bricks/bricks.lgt
r3179 r4522 82 82 B::position(Xb, Yb), 83 83 Ya2 is Yb + 1, 84 {A::move(Xb, Ya2)}, 84 {A::move(Xb, Ya2)}, % this message must be compiled with event support 85 85 ^^add_tuple([A, B]). 86 86 … … 94 94 !, 95 95 Y2 is Y - 1, 96 ( Bottom::position(X, Y2) ->96 ( Bottom::position(X, Y2) -> 97 97 true 98 ;99 ::remove_tuple([Top, Bottom])).98 ; ::remove_tuple([Top, Bottom]) 99 ). 100 100 101 101 propagate(after, move(X, Y), Bottom, bottom, [Top, Bottom]) :- 102 102 !, 103 103 Y2 is Y + 1, 104 {Top::move(X, Y2)}. 104 {Top::move(X, Y2)}. % this message must be compiled with event support 105 105 106 106 :- end_object. -
trunk/examples/bricks/loader.lgt
r3853 r4522 4 4 logtalk_load(roots(loader), [reload(skip)]), % allow for static binding 5 5 logtalk_load(relations(loader), [reload(skip)]), % allow for static binding 6 logtalk_load(bricks, [events(on)]))). 6 % compile messages with event support and turn event support on in order to 7 % both use the "stack_monitor" monitor for visualizing stack changes and to 8 % allow the constrained relation "brick_stack" to perform its magic: 9 logtalk_load(bricks, [events(on)]), 10 set_logtalk_flag(events, on))). -
trunk/examples/complements/complements.lgt
r4508 r4522 20 20 21 21 % define a "before" event handler for the complemented object: 22 before(_, Message, Sender) :- 22 before(This, Message, Sender) :- 23 this(This), 23 24 write('Received message '), writeq(Message), write(' from '), writeq(Sender), nl. 24 25 -
trunk/examples/complements/loader.lgt
r3954 r4522 1 1 2 2 :- initialization( 3 logtalk_load(complements , [events(on)])).3 logtalk_load(complements)). -
trunk/examples/complements/SCRIPT.txt
r4508 r4522 22 22 Object = employee. 23 23 yes 24 25 26 % turn event support on for messages sent at the command-line 27 % (i.e. for messages sent from the pseudo-object "user"): 28 29 | ?- set_logtalk_flag(events, on). 30 31 yes 24 32 25 33 … … 48 56 Property = defined_in(logging) 49 57 yes 58 59 60 % turn event support off: 61 62 | ?- set_logtalk_flag(events, off). 63 64 yes -
trunk/examples/hello_world/hello_world.lgt
r2626 r4522 2 2 :- object(hello_world). 3 3 4 % the initialization/1 directive argument is automatically executed 5 % when the object is loaded into memory: 4 6 :- initialization((nl, write('********** Hello World! **********'), nl)). 5 7 -
trunk/examples/hello_world/NOTES.txt
r4507 r4522 10 10 11 11 12 No self-respect edprogramming language could do without a "Hello12 No self-respecting programming language could do without a "Hello 13 13 World" example! 14 14 -
trunk/examples/metapredicates/closures.lgt
r3378 r4522 16 16 17 17 apply(Closure, Arg) :- % the Logtalk compiler verifies that any closure which is a 18 call(Closure, Arg). % meta-argument is used within a call/N method that honors the19 % meta-predicate directive (in this case, apply(1, *) => call/2)18 call(Closure, Arg). % meta-argument is used within a call/N method that complies with 19 % the meta-predicate directive (in this case, apply(1, *) => call/2) 20 20 21 21 :- public(test_this/0). % simple predicate for testing calls to a local meta-predicate -
trunk/examples/msglog/loader.lgt
r3853 r4522 2 2 :- initialization(( 3 3 logtalk_load(library(types_loader), [reload(skip)]), % allow for static binding 4 logtalk_load(msglog , [events(on)]))).4 logtalk_load(msglog))). -
trunk/examples/msglog/msglog.lgt
r3304 r4522 1 1 2 2 :- object(msglog, 3 implements(monitoring)). 4 3 implements(monitoring)). % built-in protocol for event handler methods 5 4 6 5 :- info([ 7 version is 1. 1,6 version is 1.2, 8 7 author is 'Paulo Moura', 9 date is 200 7/01/13,8 date is 2008/10/20, 10 9 comment is 'Monitor for recording, replaying, and saving user messages.']). 11 12 10 13 11 :- public(record/0). … … 36 34 [comment is 'Erases recorded messages.']). 37 35 38 39 36 :- private(log_/2). 40 37 :- dynamic(log_/2). … … 44 41 argnames is ['Object', 'Message']]). 45 42 46 47 43 record :- 48 44 self(Self), 49 45 abolish_events(_, _, _, _, Self), 50 define_events(before, _, _, user, Self) .51 46 define_events(before, _, _, user, Self), 47 set_logtalk_flag(events, on). 52 48 53 49 stop :- 50 set_logtalk_flag(events, off), 54 51 self(Self), 55 52 abolish_events(_, _, _, _, Self). 56 57 53 58 54 replay :- … … 61 57 forall(::log_(Object, Message), {Object::Message}). 62 58 63 64 59 print :- 65 60 forall( … … 67 62 (writeq(Object), write('::'), writeq(Message), write('.'), nl)). 68 63 69 70 64 erase :- 71 65 ::retractall(log_(_, _)). 72 66 73 74 67 before(Object, Message, _) :- 75 68 self(Self), 76 ( Self = Object ->69 ( Self = Object -> 77 70 true 78 ; 79 ::assertz(log_(Object, Message))). 80 71 ; ::assertz(log_(Object, Message)) 72 ). 81 73 82 74 :- end_object. -
trunk/examples/msglog/NOTES.txt
r4507 r4522 12 12 To load this example and for sample queries, please see the SCRIPT.txt file. 13 13 14 If you need more than one message recorder, just create a new prototype 15 as an extension of the object msglog. 16 14 This example illustrates how to use Logtalk event-driven programming support 15 for implementing a simple message logger for messages sent from the command- 16 line (i.e. from the pseudo-object user). If you need more than one message 17 logger, just create a new prototype as an extension of the object msglog. -
trunk/examples/polygons/loader.lgt
r3853 r4522 6 6 logtalk_load(roots(loader), [reload(skip)]), % allow for static binding 7 7 logtalk_load(relations(loader), [reload(skip)]), % allow for static binding 8 logtalk_load(polygons, [events(on)]))). 8 % compile messages with event support and turn event support on in order to 9 % allow the constrained relation "concentric" to perform its magic: 10 logtalk_load(polygons, [events(on)]), 11 set_logtalk_flag(events, on))). -
trunk/examples/polygons/SCRIPT.txt
r4507 r4522 63 63 64 64 65 % move t riangle to a new position65 % move the triangle and the hexagon to new positions 66 66 67 | ?- t::move(3, 3). 68 yes 69 70 71 % move the hexagon to a new position 72 73 | ?- h::move(8, 4). 67 | ?- t::move(3, 3), h::move(8, 4). 74 68 yes 75 69 -
trunk/examples/profiling/loader.lgt
r3853 r4522 4 4 [library(dates_loader), library(events_loader), library(metapredicates_loader), library(types_loader)], 5 5 [reload(skip)]), % allow for static binding 6 logtalk_load([timer, message_counter, stop_watch] , [events(on)]))).6 logtalk_load([timer, message_counter, stop_watch]))). -
trunk/examples/profiling/message_counter.lgt
r3179 r4522 3 3 implements(monitoring), 4 4 imports(monitor)). 5 6 5 7 6 :- info([ … … 10 9 date is 2006/12/14, 11 10 comment is 'Message counter monitor.']). 12 13 11 14 12 :- public(report/0). … … 21 19 :- info(stop/0, 22 20 [comment is 'Stops message counting.']). 23 24 21 25 22 :- private(calls/2). … … 39 36 :- mode(exits(?object, ?predicate_indicator,?integer), zero_or_more). 40 37 41 42 38 report :- 43 39 forall( … … 46 42 write(' total of calls: '), write(Calls), nl, 47 43 write(' total of exits: '), 48 ( ::exits(Object, Exits) ->49 write(Exits), nl, nl50 ; 51 write(0), nl, nl),44 ( ::exits(Object, Exits) -> 45 write(Exits), nl, nl 46 ; write(0), nl, nl 47 ), 52 48 forall( 53 49 ::calls(Object, Functor/Arity, Calls2), … … 60 56 write(0), nl, nl))))). 61 57 62 63 58 stop :- 64 59 ::retractall(calls(_, _)), … … 68 63 ::reset_monitor. 69 64 70 71 65 before(Object, Message, _) :- 72 ( ::retract(calls(Object, Old)) ->66 ( ::retract(calls(Object, Old)) -> 73 67 New is Old + 1 74 ;75 New = 1),68 ; New = 1 69 ), 76 70 ::assertz(calls(Object, New)), 77 71 functor(Message, Functor, Arity), 78 ( ::retract(calls(Object, Functor/Arity, Old2)) ->72 ( ::retract(calls(Object, Functor/Arity, Old2)) -> 79 73 New2 is Old2 + 1 80 ;81 New2 = 1),74 ; New2 = 1 75 ), 82 76 ::assertz(calls(Object, Functor/Arity, New2)). 83 77 84 85 78 after(Object, Message, _) :- 86 ( ::retract(exits(Object, Old)) ->79 ( ::retract(exits(Object, Old)) -> 87 80 New is Old + 1 88 ;89 New = 1),81 ; New = 1 82 ), 90 83 ::assertz(exits(Object, New)), 91 84 functor(Message, Functor, Arity), 92 ( ::retract(exits(Object, Functor/Arity, Old2)) ->85 ( ::retract(exits(Object, Functor/Arity, Old2)) -> 93 86 New2 is Old2 + 1 94 ;95 New2 = 1),87 ; New2 = 1 88 ), 96 89 ::assertz(exits(Object, Functor/Arity, New2)). 97 90 98 99 91 :- end_object. -
trunk/examples/profiling/SCRIPT.txt
r4507 r4522 14 14 | ?- logtalk_load(profiling(loader)). 15 15 ... 16 17 18 % turn event support on for messages sent at the command-line 19 % (i.e. for messages sent from the pseudo-object "user"): 20 21 | ?- set_logtalk_flag(events, on). 22 23 yes 16 24 17 25 … … 112 120 113 121 yes 122 123 124 % turn event support off: 125 126 | ?- set_logtalk_flag(events, off). 127 128 yes -
trunk/examples/profiling/stop_watch.lgt
r3179 r4522 4 4 imports(monitor)). 5 5 6 7 6 :- info([ 8 version is 1. 1,7 version is 1.2, 9 8 author is 'Paulo Moura', 10 date is 200 6/12/14,9 date is 2008/10/20, 11 10 comment is 'Message executing time monitor.']). 12 11 13 14 :- uses(time). 15 12 :- uses(time, [cpu_time/1]). 16 13 17 14 before(Object, Message, Sender) :- 18 15 write(Object), write(' <-- '), writeq(Message), 19 16 write(' from '), write(Sender), nl, write('STARTING at '), 20 time::cpu_time(Seconds), write(Seconds), write(' seconds'), nl.17 cpu_time(Seconds), write(Seconds), write(' seconds'), nl. 21 18 22 19 after(Object, Message, Sender) :- 23 20 write(Object), write(' <-- '), writeq(Message), 24 21 write(' from '), write(Sender), nl, write('ENDING at '), 25 time::cpu_time(Seconds), write(Seconds), write(' seconds'), nl. 26 22 cpu_time(Seconds), write(Seconds), write(' seconds'), nl. 27 23 28 24 :- end_object. -
trunk/examples/profiling/timer.lgt
r3013 r4522 1 1 2 2 :- object(timer). 3 4 3 5 4 :- info([ … … 9 8 comment is 'Call executing time profiler.']). 10 9 11 12 10 :- uses(time, [cpu_time/1]). 13 11 :- uses(loop, [forto/3]). 14 12 15 16 13 :- public(timer/2). 17 14 :- meta_predicate(timer(::, *)). 18 19 15 :- mode(timer(+callable, -number), one). 20 21 16 :- info(timer/2, 22 17 [comment is 'Returns time to execute a call.', 23 18 argnames is ['Call', 'Time']]). 24 19 25 26 20 :- public(timer/3). 27 21 :- meta_predicate(timer(::, *, *)). 28 29 22 :- mode(timer(+callable, +integer, -float), one). 30 31 23 :- info(timer/3, 32 24 [comment is 'Returns the average time needed to to execute a call.', 33 25 argnames is ['Call', 'Times', 'Time']]). 34 35 26 36 27 timer(Call, Time) :- … … 39 30 cpu_time(End), 40 31 Time is End - Start. 41 42 32 43 33 timer(Call, Times, Time) :- … … 51 41 Time is (End - Start - Overhead) / Times. 52 42 53 54 43 :- end_object. -
trunk/examples/searching/blind_search1.lgt
r2381 r4522 3 3 instantiates(class), 4 4 specializes(search_strategy)). 5 6 5 7 6 :- info([ … … 12 11 parnames is ['Bound']]). 13 12 14 15 13 :- public(bound/1). 16 14 :- mode(bound(?integer), zero_or_one). … … 18 16 [comment is 'Search depth bound.', 19 17 argnames is ['Bound']]). 20 21 18 22 19 :- protected(search/4). … … 26 23 argnames is ['Space', 'State', 'Bound', 'Path']]). 27 24 28 29 25 bound(Bound) :- 30 26 parameter(1, Bound). 31 32 27 33 28 solve(Space, State, Path) :- … … 35 30 ::search(Space, State, Bound, Path). 36 31 37 38 32 :- end_object. -
trunk/examples/searching/heuristic_search1.lgt
r2381 r4522 3 3 instantiates(class), 4 4 specializes(search_strategy)). 5 6 5 7 6 :- info([ … … 11 10 comment is 'Heuristic state space search strategies.', 12 11 parnames is ['Threshold']]). 13 14 12 15 13 :- public(threshold/1). … … 25 23 argnames is ['Space', 'State', 'Path', 'Cost']]). 26 24 27 28 25 :- protected(search/5). 29 26 :- mode(search(+object, +nonvar, +number, -list, -number), zero_or_more). … … 32 29 argnames is ['Space', 'State', 'Threshold', 'Path', 'Cost']]). 33 30 34 35 31 solve(Space, State, Path) :- 36 32 ::solve(Space, State, Path, _). 37 38 33 39 34 solve(Space, State, Path, Cost) :- … … 41 36 ::search(Space, State, Threshold, Path, Cost). 42 37 43 44 38 threshold(Threshold) :- 45 39 parameter(1, Threshold). 46 40 47 48 41 :- end_object. -
trunk/examples/searching/hill_climbing1.lgt
r4307 r4522 2 2 :- object(hill_climbing(Threshold), 3 3 instantiates(heuristic_search(Threshold))). 4 5 4 6 5 :- info([ … … 11 10 parnames is ['Threshold']]). 12 11 13 14 12 :- uses(list, [member/2, reverse/2, sort/2]). 15 16 13 17 14 search(Space, State, Threshold, Solution, Cost) :- …
