A debugger is a tool that can be used to execute and inspect a program by controlling the parts of the program to be executed. In this task, we shall write a debugger for a declarative pipeline.
Your task is to first write a computation context for a Program, e.g.
jshell> String foo() {
...> return Stream.of("cs", "20", "30")
...> .peek(x -> System.out.println(x))
...> .reduce("", (x, y) -> x + y);
...> }
jshell> Program<String> prog = Program.<String>of(() -> foo())
prog ==> Program
and then write a Debugger to debug the program.
jshell> Debug<String> debug = Debug.<String>of(() -> foo()).inspect("debugging: ")
debug ==> Debug 1
jshell> debug.run()
cs
20
30
debugging: cs2030
cs2030
This task comprises a number of levels. You are required to complete ALL levels.
The following are the constraints imposed on this task. In general, you should keep to the constructs and programming discipline instilled throughout the module.
In the following level specifications, you will be guided on the classes and non-private methods to define.
Create a generic class Program<T> to represent a program that results in a value of type T.
The following is a sample run:
$ javac Program.java
$ jshell Program.java
jshell> Program<String> prog = Program.<String>of("cs2030")
prog ==> Program
jshell> prog.get()
$.. ==> Optional[cs2030]
jshell> prog.get() instanceof Optional
$.. ==> true
jshell> Program<String> noop = Program.<String>empty()
noop ==> Program
jshell> noop.get()
$.. ==> Optional.empty
Consider the following program:
jshell> String foo() {
...> return Stream.of("cs", "20", "30")
...> .peek(x -> System.out.println(x))
...> .reduce("", (x, y) -> x + y);
...> }
| created method foo()
We would like to "wrap" this program around a Program context with the following behaviour:
jshell> Program<String> prog = Program.<String>of(() -> foo()) prog ==> Program jshell> prog.get() cs 20 30 $.. ==> Optional[cs2030] jshell> prog.get() $.. ==> Optional[cs2030]
Notice that foo is not evaluated until get is invoked. Moreover, subsequent invocations of get need not evaluated the program further as our programs are assumed "pure" with the same result.
Modify the generic class Program to support lazy evaluation with the provided Lazy class. In particular, write another overloaded factory method of that takes in a Supplier<T> and creates a program from the given supplier. Also include a method run that takes in a Consumer<T> and "runs" the program, provided that it is not an empty program.
Note that you DO NOT need to consider using any bounded wildcards in this task.
$ javac Lazy.java Program.java $ jshell Lazy.java Program.java jshell> Program<String> prog = Program.<String>of(() -> foo()) prog ==> Program jshell> prog.run(x -> System.out.println(x)) cs 20 30 cs2030 jshell> prog.run(x -> System.out.println(x)) cs2030 jshell> Program<String> prog0 = Program.<String>empty() prog0 ==> Program jshell> prog0.run(x -> System.out.println(x)) jshell>
Thus far, we have only created programs at the start of execution. In general, a program can also take in input from other programs and provide output to other programs. We can do this in two ways: using map and flatMap.
Include the following map method:
<R> Program<R> map(Function<T, R> mapper)
that takes in a function that maps Program<T> to Program<R>. The following is an example:
$ javac Lazy.java Program.java
$ jshell Lazy.java Program.java
jshell> String foo() {
...> return Stream.of("cs", "20", "30")
...> .peek(x -> System.out.println(x))
...> .reduce("", (x, y) -> x + y);
...> }
| created method foo()
jshell> int len(String s) {
...> System.out.println("Finding length");
...> return s.length();
...> }
| created method len(String)
jshell> Program<String> prog = Program.<String>of(() -> foo())
prog ==> Program
jshell> Program<Integer> progi = prog.map(x -> len(x))
progi ==> Program
jshell> progi.get()
cs
20
30
Finding length
$.. ==> Optional[6]
jshell> progi.run(x -> System.out.println(x))
6
Notice from the above that len is passed directly to map. However, len could also be wrapped within Program. As such we need a flatMap:
<R> Program<R> flatMap(Function<T, Program<R>> mapper)
Here is a sample run:
$ javac Lazy.java Program.java $ jshell Lazy.java Program.java jshell> Program<String> prog = Program.<String>of(() -> foo()) prog ==> Program jshell> Program<Integer> progi = prog.flatMap(x -> Program.of(() -> len(x))) progi ==> Program jshell> progi.get() cs 20 30 Finding length $.. ==> Optional[6] jshell> progi.run(x -> System.out.println(x)) 6
Here are further examples with empty programs.
jshell> Program<String> noop = Program.<String>empty() noop ==> Program jshell> Optional<Integer> opti = noop.map(x -> len(x)).get() opti ==> Optional.empty
In the above, since the program starts with an empty program noop, further mapping does not result in a valid value. However, note that the final outcome from get is still typed Optional<Integer> at the end of the pipeline.
jshell> Program<String> prog = Program.<String>of(() -> foo()) prog ==> Program jshell> Optional<Integer> opti = prog.flatMap(x -> Program.<Integer>empty()).get() cs 20 30 opti ==> Optional.empty
In the above, the pipeline starts with a non-empty program, but flat-mapped with an empty program. The get method will still evaluate the start (notice the three lines of output), but no value can be evaluated at the end of the pipeline, resulting in an empty optional that is typed Optional<Integer>.
Having created the Program context, we now focus on creating the Debug debugger.
jshell> String foo() {
...> return Stream.of("cs", "20", "30")
...> .peek(x -> System.out.println(x))
...> .reduce("", (x, y) -> x + y);
...> }
| modified method foo()
jshell> Debug<String> debug = Debug.<String>of(() -> foo())
debug ==> Debug 1
A Debug context is created by passing a Supplier to the of factory method. To support lazy evaluation, Debug should encapsulate a Program; it should have no knowledge of Lazy or Optional.
Include the toString method that returns the string Debug followed by a integer value that represents the "stage" of the debugger, starting with 1.
Also include a run() method that "runs" the program within the debugger. Note that lazy evaluation is maintained when running the debugger.
jshell> debug.run() cs 20 30 cs2030 jshell> debug.run() cs2030
A Debug pipeline can also be extended using extend.
jshell> int len(String s) {
...> System.out.println("Finding length");
...> return s.length();
...> }
| created method len(String)
jshell> Debug<Integer> debug = Debug.<String>of(() -> foo()).extend(x -> len(x))
debug ==> Debug 2
jshell> debug.run()
cs
20
30
Finding length
6
jshell> debug.run()
6
In addition, we can "inspect" a specific stage of the pipeline. The inspect method takes in a String prompt.
jshell> Debug<Integer> debug = Debug.<String>of(() -> foo()).inspect("debugging: ").extend(x -> len(x))
debug ==> Debug 2
jshell> debug.run()
cs
20
30
debugging: cs2030
Finding length
6
jshell> debug.run()
6
Include the method extend and inspect in the Debug context. You may assume that each stage of the pipeline can have at most one inspect.
The following is a sample run for a two-stage debugging pipeline.
jshell> Debug<Integer> debug = Debug.<String>of(() -> foo()).inspect("debugging: ").extend(x -> len(x))
debug ==> Debug 2
jshell> debug.run(0)
jshell> debug.run(1)
cs
20
30
debugging: cs2030
jshell> debug.run(1)
jshell> debug.run(2)
Finding length
6
jshell> debug.run(2)
6
Take note of the following:
Here is another example.
jshell> Debug<Integer> debug = Debug.<String>of(() -> foo()).inspect("debugging: ").extend(x -> len(x))
debug ==> Debug 2
jshell> debug.run(2) // runs the two stages of the pipeline
cs
20
30
debugging: cs2030
Finding length
6
jshell> debug.run(1)
debug.run(1) above does not evaluate the first stage of the pipeline, since it was run previously. Also note that running one stage does not complete the pipeline, hence there is no output. Contrast this to the following:
jshell> debug.run(2) 6 jshell> debug.run(3) 6
Although the two stages were evaluated previously, both run(2) and run(3) will complete running the pipeline with the value of 6.
Once again, you are reminded that Debug should only make use of Program; it should have no knowledge of Lazy or Optional which is encapsulated within Program.