Changeset 4522 for trunk/examples

Show
Ignore:
Timestamp:
10/20/08 11:40:03 (3 months ago)
Author:
pmoura
Message:

Improved the documentation of the "complements", "hello_world", "hooks", and "msglog" examples.

Updated all the examples that make use of event-driven programming ("birthdays", "bricks", "complements", "msglog", "polygons", "profiling", and "searching") to ensure correct functionality after fixing the bug in cleaning compiler options after compilation of source files.

Location:
trunk/examples
Files:
28 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/benchmarks/loader_events.lgt

    r3754 r4522  
    11 
    2 % uncomment the next line if your Prolog compiler supports modules 
     2% uncomment the next line only if your Prolog compiler supports modules 
    33%:- ensure_loaded(module). 
    44 
    55:- 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  
    8282        B::position(Xb, Yb), 
    8383        Ya2 is Yb + 1, 
    84         {A::move(Xb, Ya2)}, 
     84        {A::move(Xb, Ya2)},     % this message must be compiled with event support 
    8585        ^^add_tuple([A, B]). 
    8686 
     
    9494        !, 
    9595        Y2 is Y - 1, 
    96         (Bottom::position(X, Y2) -> 
     96        (   Bottom::position(X, Y2) -> 
    9797            true 
    98             ; 
    99             ::remove_tuple([Top, Bottom])). 
     98        ;   ::remove_tuple([Top, Bottom]) 
     99        ). 
    100100 
    101101    propagate(after, move(X, Y), Bottom, bottom, [Top, Bottom]) :- 
    102102        !, 
    103103        Y2 is Y + 1, 
    104         {Top::move(X, Y2)}. 
     104        {Top::move(X, Y2)}.     % this message must be compiled with event support 
    105105 
    106106:- end_object. 
  • trunk/examples/bricks/loader.lgt

    r3853 r4522  
    44    logtalk_load(roots(loader), [reload(skip)]),        % allow for static binding 
    55    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  
    2020 
    2121    % define a "before" event handler for the complemented object: 
    22     before(_, Message, Sender) :- 
     22    before(This, Message, Sender) :- 
     23        this(This), 
    2324        write('Received message '), writeq(Message), write(' from '), writeq(Sender), nl. 
    2425 
  • trunk/examples/complements/loader.lgt

    r3954 r4522  
    11 
    22:- initialization( 
    3     logtalk_load(complements, [events(on)])). 
     3    logtalk_load(complements)). 
  • trunk/examples/complements/SCRIPT.txt

    r4508 r4522  
    2222Object = employee. 
    2323yes  
     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 
     31yes 
    2432 
    2533 
     
    4856Property = defined_in(logging) 
    4957yes 
     58 
     59 
     60% turn event support off: 
     61 
     62| ?- set_logtalk_flag(events, off). 
     63 
     64yes 
  • trunk/examples/hello_world/hello_world.lgt

    r2626 r4522  
    22:- object(hello_world). 
    33 
     4    % the initialization/1 directive argument is automatically executed 
     5    % when the object is loaded into memory: 
    46    :- initialization((nl, write('********** Hello World! **********'), nl)). 
    57 
  • trunk/examples/hello_world/NOTES.txt

    r4507 r4522  
    1010 
    1111 
    12 No self-respected programming language could do without a "Hello  
     12No self-respecting programming language could do without a "Hello  
    1313World" example! 
    1414 
  • trunk/examples/metapredicates/closures.lgt

    r3378 r4522  
    1616 
    1717    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 the 
    19                                 % 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) 
    2020 
    2121    :- public(test_this/0).     % simple predicate for testing calls to a local meta-predicate 
  • trunk/examples/msglog/loader.lgt

    r3853 r4522  
    22:- initialization(( 
    33    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  
    11 
    22:- object(msglog, 
    3     implements(monitoring)). 
    4  
     3    implements(monitoring)).    % built-in protocol for event handler methods 
    54 
    65    :- info([ 
    7         version is 1.1, 
     6        version is 1.2, 
    87        author is 'Paulo Moura', 
    9         date is 2007/01/13, 
     8        date is 2008/10/20, 
    109        comment is 'Monitor for recording, replaying, and saving user messages.']). 
    11  
    1210 
    1311    :- public(record/0). 
     
    3634        [comment is 'Erases recorded messages.']). 
    3735 
    38  
    3936    :- private(log_/2). 
    4037    :- dynamic(log_/2). 
     
    4441         argnames is ['Object', 'Message']]). 
    4542 
    46  
    4743    record :- 
    4844        self(Self), 
    4945        abolish_events(_, _, _, _, Self), 
    50         define_events(before, _, _, user, Self). 
    51  
     46        define_events(before, _, _, user, Self), 
     47        set_logtalk_flag(events, on). 
    5248 
    5349    stop :- 
     50        set_logtalk_flag(events, off), 
    5451        self(Self), 
    5552        abolish_events(_, _, _, _, Self). 
    56  
    5753 
    5854    replay :- 
     
    6157        forall(::log_(Object, Message), {Object::Message}). 
    6258 
    63  
    6459    print :- 
    6560        forall( 
     
    6762            (writeq(Object), write('::'), writeq(Message), write('.'), nl)). 
    6863 
    69  
    7064    erase :- 
    7165        ::retractall(log_(_, _)). 
    7266 
    73  
    7467    before(Object, Message, _) :- 
    7568        self(Self), 
    76         (Self = Object -> 
     69        (   Self = Object -> 
    7770            true 
    78             ; 
    79             ::assertz(log_(Object, Message))). 
    80  
     71        ;   ::assertz(log_(Object, Message)) 
     72        ). 
    8173 
    8274:- end_object. 
  • trunk/examples/msglog/NOTES.txt

    r4507 r4522  
    1212To load this example and for sample queries, please see the SCRIPT.txt file. 
    1313 
    14 If you need more than one message recorder, just create a new prototype  
    15 as an extension of the object msglog. 
    16  
     14This example illustrates how to use Logtalk event-driven programming support 
     15for implementing a simple message logger for messages sent from the command- 
     16line (i.e. from the pseudo-object user).   If you need more than one message  
     17logger, just create a new prototype as an extension of the object msglog. 
  • trunk/examples/polygons/loader.lgt

    r3853 r4522  
    66    logtalk_load(roots(loader), [reload(skip)]),        % allow for static binding 
    77    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  
    6363 
    6464 
    65 % move triangle to a new position 
     65% move the triangle and the hexagon to new positions 
    6666 
    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). 
    7468yes 
    7569 
  • trunk/examples/profiling/loader.lgt

    r3853 r4522  
    44        [library(dates_loader), library(events_loader), library(metapredicates_loader), library(types_loader)], 
    55        [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  
    33    implements(monitoring), 
    44    imports(monitor)). 
    5  
    65 
    76    :- info([ 
     
    109        date is 2006/12/14, 
    1110        comment is 'Message counter monitor.']). 
    12  
    1311 
    1412    :- public(report/0). 
     
    2119    :- info(stop/0, 
    2220        [comment is 'Stops message counting.']). 
    23  
    2421 
    2522    :- private(calls/2). 
     
    3936    :- mode(exits(?object, ?predicate_indicator,?integer), zero_or_more). 
    4037 
    41  
    4238    report :- 
    4339        forall( 
     
    4642             write('  total of calls: '), write(Calls), nl, 
    4743             write('  total of exits: '), 
    48              (::exits(Object, Exits) -> 
    49                  write(Exits), nl, nl 
    50                  ; 
    51                  write(0), nl, nl), 
     44             (  ::exits(Object, Exits) -> 
     45                write(Exits), nl, nl 
     46             ;  write(0), nl, nl 
     47             ), 
    5248            forall( 
    5349                ::calls(Object, Functor/Arity, Calls2), 
     
    6056                        write(0), nl, nl))))). 
    6157 
    62  
    6358    stop :- 
    6459        ::retractall(calls(_, _)), 
     
    6863        ::reset_monitor. 
    6964 
    70  
    7165    before(Object, Message, _) :- 
    72         (::retract(calls(Object, Old)) -> 
     66        (   ::retract(calls(Object, Old)) -> 
    7367            New is Old + 1 
    74             ; 
    75             New = 1), 
     68        ;   New = 1 
     69        ), 
    7670        ::assertz(calls(Object, New)), 
    7771        functor(Message, Functor, Arity), 
    78         (::retract(calls(Object, Functor/Arity, Old2)) -> 
     72        (   ::retract(calls(Object, Functor/Arity, Old2)) -> 
    7973            New2 is Old2 + 1 
    80             ; 
    81             New2 = 1 ), 
     74        ;   New2 = 1 
     75        ), 
    8276        ::assertz(calls(Object, Functor/Arity, New2)). 
    8377 
    84  
    8578    after(Object, Message, _) :- 
    86         (::retract(exits(Object, Old)) -> 
     79        (   ::retract(exits(Object, Old)) -> 
    8780            New is Old + 1 
    88             ; 
    89             New = 1), 
     81        ;   New = 1 
     82        ), 
    9083        ::assertz(exits(Object, New)), 
    9184        functor(Message, Functor, Arity), 
    92         (::retract(exits(Object, Functor/Arity, Old2)) -> 
     85        (   ::retract(exits(Object, Functor/Arity, Old2)) -> 
    9386            New2 is Old2 + 1 
    94             ; 
    95             New2 = 1), 
     87        ;   New2 = 1 
     88        ), 
    9689        ::assertz(exits(Object, Functor/Arity, New2)). 
    9790 
    98  
    9991:- end_object. 
  • trunk/examples/profiling/SCRIPT.txt

    r4507 r4522  
    1414| ?- logtalk_load(profiling(loader)). 
    1515... 
     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 
     23yes 
    1624 
    1725 
     
    112120 
    113121yes 
     122 
     123 
     124% turn event support off: 
     125 
     126| ?- set_logtalk_flag(events, off). 
     127 
     128yes 
  • trunk/examples/profiling/stop_watch.lgt

    r3179 r4522  
    44    imports(monitor)). 
    55 
    6  
    76    :- info([ 
    8         version is 1.1, 
     7        version is 1.2, 
    98        author is 'Paulo Moura', 
    10         date is 2006/12/14, 
     9        date is 2008/10/20, 
    1110        comment is 'Message executing time monitor.']). 
    1211 
    13  
    14     :- uses(time). 
    15  
     12    :- uses(time, [cpu_time/1]). 
    1613 
    1714    before(Object, Message, Sender) :- 
    1815        write(Object), write(' <-- '), writeq(Message), 
    1916        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. 
    2118 
    2219    after(Object, Message, Sender) :- 
    2320        write(Object), write(' <-- '), writeq(Message), 
    2421        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. 
    2723 
    2824:- end_object. 
  • trunk/examples/profiling/timer.lgt

    r3013 r4522  
    11 
    22:- object(timer). 
    3  
    43 
    54    :- info([ 
     
    98        comment is 'Call executing time profiler.']). 
    109 
    11  
    1210    :- uses(time, [cpu_time/1]). 
    1311    :- uses(loop, [forto/3]). 
    1412 
    15  
    1613    :- public(timer/2). 
    1714    :- meta_predicate(timer(::, *)). 
    18  
    1915    :- mode(timer(+callable, -number), one). 
    20  
    2116    :- info(timer/2, 
    2217        [comment is 'Returns time to execute a call.', 
    2318         argnames is ['Call', 'Time']]). 
    2419 
    25  
    2620    :- public(timer/3). 
    2721    :- meta_predicate(timer(::, *, *)). 
    28  
    2922    :- mode(timer(+callable, +integer, -float), one). 
    30  
    3123    :- info(timer/3, 
    3224        [comment is 'Returns the average time needed to to execute a call.', 
    3325         argnames is ['Call', 'Times', 'Time']]). 
    34  
    3526 
    3627    timer(Call, Time) :- 
     
    3930        cpu_time(End), 
    4031        Time is End - Start. 
    41  
    4232 
    4333    timer(Call, Times, Time) :- 
     
    5141        Time is (End - Start - Overhead) / Times. 
    5242 
    53  
    5443:- end_object. 
  • trunk/examples/searching/blind_search1.lgt

    r2381 r4522  
    33    instantiates(class), 
    44    specializes(search_strategy)). 
    5  
    65 
    76    :- info([ 
     
    1211        parnames is ['Bound']]). 
    1312 
    14  
    1513    :- public(bound/1). 
    1614    :- mode(bound(?integer), zero_or_one). 
     
    1816        [comment is 'Search depth bound.', 
    1917         argnames is ['Bound']]). 
    20  
    2118 
    2219    :- protected(search/4). 
     
    2623         argnames is ['Space', 'State', 'Bound', 'Path']]). 
    2724 
    28  
    2925    bound(Bound) :- 
    3026        parameter(1, Bound). 
    31  
    3227 
    3328    solve(Space, State, Path) :- 
     
    3530        ::search(Space, State, Bound, Path). 
    3631 
    37  
    3832:- end_object. 
  • trunk/examples/searching/heuristic_search1.lgt

    r2381 r4522  
    33    instantiates(class), 
    44    specializes(search_strategy)). 
    5  
    65 
    76    :- info([ 
     
    1110        comment is 'Heuristic state space search strategies.', 
    1211        parnames is ['Threshold']]). 
    13  
    1412 
    1513    :- public(threshold/1). 
     
    2523         argnames is ['Space', 'State', 'Path', 'Cost']]). 
    2624 
    27  
    2825    :- protected(search/5). 
    2926    :- mode(search(+object, +nonvar, +number, -list, -number), zero_or_more). 
     
    3229         argnames is ['Space', 'State', 'Threshold', 'Path', 'Cost']]). 
    3330 
    34  
    3531    solve(Space, State, Path) :- 
    3632        ::solve(Space, State, Path, _). 
    37  
    3833 
    3934    solve(Space, State, Path, Cost) :- 
     
    4136        ::search(Space, State, Threshold, Path, Cost). 
    4237 
    43  
    4438    threshold(Threshold) :- 
    4539        parameter(1, Threshold). 
    4640 
    47  
    4841:- end_object. 
  • trunk/examples/searching/hill_climbing1.lgt

    r4307 r4522  
    22:- object(hill_climbing(Threshold), 
    33    instantiates(heuristic_search(Threshold))). 
    4  
    54 
    65    :- info([ 
     
    1110        parnames is ['Threshold']]). 
    1211 
    13  
    1412    :- uses(list, [member/2, reverse/2, sort/2]). 
    15  
    1613 
    1714    search(Space, State, Threshold, Solution, Cost) :-