/*
 * BEGIN LICENSE BLOCK
 * Version: CMPL 1.1
 *
 * The contents of this file are subject to the Cisco-style Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file except
 * in compliance with the License.  You may obtain a copy of the License
 * at www.eclipse-clp.org/license.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
 * the License for the specific language governing rights and limitations
 * under the License. 
 * 
 * The Original Code is  The ECLiPSe Constraint Logic Programming System. 
 * The Initial Developer of the Original Code is  Cisco Systems, Inc. 
 * Portions created by the Initial Developer are
 * Copyright (C) 1990,2006 Cisco Systems, Inc.  All Rights Reserved.
 * 
 * Contributor(s): Mireille Ducasse, ECRC.
 * 
 * END LICENSE BLOCK
 *
 *	$Id: step_by_step_demos.op,v 1.1.1.1 2006/09/23 01:54:37 snovello Exp $
 *
 */
reset_step_demo :-
	set_default(traced_ports),
	set_default(slot_display),
	set_default(command, next/0, step_by_step, opium).

opium_demo(
	name		: step_demo_0,
	demo_goal	: (buggy(nqueens)),
	condition	: (init_demo_series, reset_step_demo, set_flag(toplevel_module, opium)),
	message		: 
"This goal loads the nqueens program in the traced session. \n\
You may want to start the goal in the traced session and type \n\
\"continue\" to see its overall behavior."
	).


opium_demo(
	name		: step_demo_1,
	demo_goal	: (set_default_parameters(step_by_step), get_parameter(traced_ports, Default), n(10)),
	condition	: (goto_np(1), set_parameter(slot_display, [on,on,on,on,off,on,on,off])),
	message		: 
 "This goal shows which lines the \"next\" command shows by default. The \n\
first slot of the trace lines is the chronological line number. Note \n\
that there are some \"holes\" due to skipped lines (\"unify\" and \"redo\")."
	).


opium_demo(
	name		: step_demo_2,
	demo_goal	: (set_parameter(traced_ports, [[call,next,exit,exit_c,fail,delay,resume,leave,unify, redo]]),
			   back(10)),
	condition	: (opium_nl(trace)),
	message		: 
 "This goal sets all the trace lines to be traced. Note that as \"unify\" \n\
and \"redo\" have been added to the list of traced ports the 10 lines \n\
shown by \"back(10)\" are not the same as the 10 lines previously shown."
	).


opium_demo(
	name		: step_demo_3,
	demo_goal	: (next, curr_port(Port), Port\==unify, Port \==next),
	condition	: (opium_nl(trace)),
	message		:
 "Trace till condition: This goal traces lines forwards according to \n\
\"traced_port\" and will not stop at lines whose port is \"unify\" or \"next\". \n\
It does not set anything permanently as opposed to setting these ports \n\
leashed.  (Type \";\" in the Opium-1 window to get further trace lines.)"
	).

	
opium_demo(
	name		: step_demo_4,
	demo_goal	: (next, curr_port(Port), (Port==unify; Port==next)),
	condition	: (opium_nl(trace)),
	message		:
 "Trace till condition: This goal will trace all lines forwards \n\
according to \"traced_port\" and will stop only at \"unify\" and \"next\" (if \n\
these ports ports are set to be traced!).(Type \";\" in the Opium-1  \n\
window to get further trace lines.)"
	).



opium_demo(
	name		: step_demo_5,
	demo_goal	: (next_np, curr_port(Port), Port\==unify, Port\==next, print_line),
	condition	: (opium_nl(trace)),
	message		:
 "Skip till condition: This goal skips all the coming \"unify\" and \"next\" \n\
trace lines. \"next_np\" retrieves the next line (according to \"traced_ports\")  \n\
but does not print it.  It does not set anything permanently as \n\
opposed to setting these two ports untraced with the \"traced_port\" \n\
parameter.  (Type \";\" in the Opium-1 window to get further trace \n\
lines.)"
	).

	
opium_demo(
	name		: step_demo_6,
	demo_goal	: (next_np, curr_port(Port), (Port==unify; Port==next), print_line),
	condition	: (opium_nl(trace)),
	message		:
 "Skip till condition: This goal will go directly to a trace line \n\
corresponding to a \"unify\" or a \"next\" (if they are traced!). Note that if \n\
you want to be sure to trace the \"unify\" and \"next\" lines even if they are \n\
set untraced you can use \"f_get(_,_,_, [unify, next], _)\"."
	).


opium_demo(
	name		: step_demo_7_1,
	demo_goal	: (spy(safe/1), leap, leap, leap),
	condition	: (opium_clear(help, pce), 
			   opium_clear(source, pce), 
			   system(clear), 
			   goto(1)),
	message		: 
 "The computation fails, we want to check how \"safe/1\" behaves. \"spy/1\" \n\
sets a break point on it, and \"leap/0\" retrieves and prints the lines  \n\
related to spied points."
	).

% show queens is loaded global in display_demos
opium_demo(
	name		: step_demo_7_2,
	demo_goal	: (leap_np, curr_port(fail), print_line),
	condition	: (opium_nl(trace)),
	message		: 
 "The previous goal showed that a breakpoint is not precise enough. \n\
Actually seeing the failing of \"safe/1\" would be enough."
	).


opium_demo(
	name		: step_demo_7_3,
	demo_goal	: (leap_np, curr_port(fail), print_line, curr_arg([X]), show_queens(X)),
	condition	: (opium_nl(trace), lib(show_queens)),
	message		: 
 "In order to determine more easily whether the failure of \"safe/1\" is  \n\
correct or not, we can use a progam which displays the chessboard. \n\
The goal is the same as the previous one except that the value of the \n\
argument of safe is retrieved ans printed with the \"show_queens/1\" \n\
predicate."
	).


opium_demo(
	name		: step_demo_7_4,
	demo_goal	: (leap_np, curr_port(fail), 
			   curr_arg([X]), ( X== [2,4,1,3] ; X == [3,1,4,2]), 
			   print_line, show_queens(X)),
	condition	: (opium_nl(trace)),
	message		: 
 "\"safe/1\" calls itself recursively and there are too many failures to \n\
check. We know that it should not fail for [2,4,1,3] and [3,1,2,4]. \n\
The goal checks whether these two cases occur. (Type \";\" in the \n\
Opium-1 window to see the second case). \"safe/1\" definitely needs  \n\
to be debugged further."
	).

opium_demo(
	name		: step_demo_7_5,
	demo_goal	: (nospy(safe/1), leap),
	condition	: (true),
	message		: 
 "Removes the breakpoint from \"safe/1\"."
	).


opium_demo(
	name		: step_demo_custom_1_1,
	demo_goal	: (man(next),
			   implementation_link(command, next/0, Default, Module )),
	condition	: (opium_clear(help, pce), 
			   opium_clear(source, pce), 
			   system(clear)),
	message		: 
 "Assume you want to customize the \"next/0\" command. You first ask \n\
for its manual entry to know the type of the command (\"man\"). Then \n\
you ask for the name of the default implementation which you want to \n\
reuse (\"implementation_link\")."
	).


opium_demo(
	name		: step_demo_custom_1_2,
	demo_goal	: (rebuild_object(command,next/0,new_next_impl,step_by_step,opium), 
			   next(10)),
	condition	: (opium_nl(trace)),
	message		: 
 "Now that we know that the \"next\" command is of type trace, we know \n\
that the new implementation should not print any trace line (see \n\
chapter of manual about \"setting the environement\"). Assume that we \n\
want the \"next\" command to always print a newline before \"call\" \n\
lines. We can define a \"new_next_impl\" predicate as defined below. And \n\
tell Opium to use it as implementation for the \"next\" command using \n\
\"rebuild_object\". \n\
	new_next_impl :-  \n\
        	next_Op,  \n\
        	(  curr_port(call) \n\
        	-> opium_nl(trace) \n\
        	;  true \n\
		)."
	).

new_next_impl :- 
        next_Op, 
        (  curr_port(call)
        -> opium_nl(trace)
        ;  true
	).



opium_demo(
	name		: step_demo_custom_2,
	demo_goal	: (set_default(command, next/0, step_by_step, opium),
			   n(5)),
	condition	: (opium_nl(trace)),
	message		: 
 "If you want to set the customized commands back to their default \n\
values use \"set_default/4\"."
	).


opium_demo(
	name		: step_demo_reset,
	demo_goal	: (reset_step_demo),
	condition	: (true),
	message		: 
 "Resets all parameters and customizations done for the steo_by_step \n\
demos."
	).
