Page tree

Michael O'Brien

Skip to end of metadata
Go to start of metadata

Quickstart

github | jenkins | dockerhub

# pull the code
git pull https://github.com/obrienlabs/calculator.git
# build
cd calculator
biometric:calculator michaelobrien$ mvn clean install -U
# optionally build docker image
biometric:calculator michaelobrien$ cd calculator-cli/src/docker/
biometric:docker michaelobrien$ ./build.sh 
biometric:docker michaelobrien$ cd ../../
biometric:calculator-cli michaelobrien$ java -cp target/tools.obrien.calculator-cli.jar tools.obrien.calculator.Calculator "add(1, add(2,3))"

# options (add, div, mult, sub)
java -cp target/tools.obrien.calculator-cli.jar tools.obrien.calculator.Calculator "add(1, add(2,3))"
java -cp target/tools.obrien.calculator-cli.jar tools.obrien.calculator.Calculator "add(1, div(2,3))"
java -cp target/tools.obrien.calculator-cli.jar tools.obrien.calculator.Calculator "add(1, mult(2,3))"
java -cp target/tools.obrien.calculator-cli.jar tools.obrien.calculator.Calculator "add(1, sub(2,3))"


Requirements

Lexical inputs are command - not token based.  No "2 * 5" - use "mult(2, 5)"

Keywords: add, mult, div sub

expressions: 2 parameters max including recursive keyword expressions - "add(1, mult(2,5))

Analysis

There are several methods for parting mathematical expressions.  The most common that comes to mind is RPN (reverse polish notation) - where we had to enter the calculations in RPN or post order binary tree traversal.

Operations


input: add (1,2)

push "add" to the operation stack

on open bracket

push 1,2 to parameter stack

on close bracket - execute last operation stack item


Multiple passes

recursively iterate the string and evaluate ( ) expressions at the deepest level outwards

add(1, add(2,3)) 

add(1, 5)

6

Test Driven Design Use Cases

Base case operation


Recursive case operation

Design

Execution Phases

Token parsing


Operation parsing

Expression parsing

Calculation

Testing

Logging


Implementation

Usage

Running the calculator via native JDK

# clone

# build
$mvn clean install -U
[INFO] calculator ......................................... SUCCESS [  0.246 s]
[INFO] calculator-cli ..................................... SUCCESS [  3.563 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.891 s
[INFO] Finished at: 2020-02-09T16:05:23-05:00
[INFO] ------------------------------------------------------------------------
biometric:calculator michaelobrien$ java -cp calculator-cli/target/tools.obrien.calculator-cli.jar tools.obrien.calculator.Calculator "add(1, add(2,3))"
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator parse
INFO: expression: add(1, add(2,3))
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator tokenize
INFO: token: add
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator tokenize
INFO: token: 1
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator tokenize
INFO: token: add
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator tokenize
INFO: token: 2
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator tokenize
INFO: token: 3
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator parse
INFO: result: 6.0
Feb. 09, 2020 4:05:28 P.M. tools.obrien.calculator.Calculator main
INFO: Result: 6.0

Running the calculator via docker image

cd calculator/calculator-cli/src/docker
biometric:docker michaelobrien$ ./build.sh 
mkdir: ../../builds: File exists
mkdir: ../../builds/10001: File exists
Sending build context to Docker daemon  10.75kB
Step 1/6 : FROM openjdk:11
 ---> 243e95d792e3
Step 2/6 : ARG USERVICE_HOME=/opt/app/
 ---> Running in fd38a4fa02e5
Removing intermediate container fd38a4fa02e5
 ---> d3653a1c68f3
Step 3/6 : RUN mkdir -p $USERVICE_HOME
 ---> Running in 520b741b1f79
Removing intermediate container 520b741b1f79
 ---> efba67b46f12
Step 4/6 : ADD tools.obrien.calculator-cli.jar $USERVICE_HOME/lib/tools.obrien.calculator-cli.jar
 ---> e30f348bda35
Step 5/6 : ADD startService.sh $USERVICE_HOME/bin/
 ---> 02768f79a7a6
Step 6/6 : CMD ["/opt/app/bin/startService.sh"]
 ---> Running in 74950f567b96
Removing intermediate container 74950f567b96
 ---> e7ffc7cf65fb
[Warning] One or more build-args [build-id] were not consumed
Successfully built e7ffc7cf65fb
Successfully tagged obrienlabs/calculator-cli:latest
The push refers to repository [docker.io/obrienlabs/calculator-cli]
daf5d66e7e8b: Pushed 
b108c3ea5097: Pushed 
0a9f15a148f0: Pushed 
25efa461ccff: Layer already exists 
4272c5799ff4: Layer already exists 
9a11244a7e74: Layer already exists 
5f3a5adb8e97: Layer already exists 
73bfa217d66f: Layer already exists 
91ecdd7165d3: Layer already exists 
e4b20fcc48f4: Layer already exists 
0.0.1: digest: sha256:d5a8b1d8a07b4af49c9421f8df6aca407ce8e9f2552308e511fdd76bc3266dc1 size: 2417
calculator-cli
calculator-cli
starting: calculator-cli
2cb387cb2f175918d128126e975aeb2e1b4cd6c843a64bb7f8658ff30fc7dbf0
biometric:docker michaelobrien$ docker logs calculator-cli
Feb 10, 2020 3:40:56 AM tools.obrien.calculator.Calculator parse
INFO: expression: add(2,3)
Feb 10, 2020 3:40:56 AM tools.obrien.calculator.Calculator tokenize
INFO: token: add
Feb 10, 2020 3:40:56 AM tools.obrien.calculator.Calculator tokenize
INFO: token: 2
Feb 10, 2020 3:40:56 AM tools.obrien.calculator.Calculator tokenize
INFO: token: 3
Feb 10, 2020 3:40:56 AM tools.obrien.calculator.Calculator parse
INFO: result: 5.0
Feb 10, 2020 3:40:56 AM tools.obrien.calculator.Calculator main
INFO: Result: 5.0

biometric:docker michaelobrien$ docker ps -a | grep calculator
df6d8ff37e3e        obrienlabs/calculator-cli:0.0.1       "jshell"                 About a minute ago   Exited (0) About a minute ago                            calculator-cli


Running the calculator via Helm Kubernetes chart

  • No labels