Skip to content

pranjal710/Abacus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Abacus

A RESTful service that evaluates mathematical expressions, persists requests and results, and allows querying expressions by result.


Features

  • Evaluates infix mathematical expressions (e.g. 3 + 4 * (2 - 1))
  • Supports operator precedence and parentheses
  • Persists each expression and its result, and provides a unique id
  • Provides search endpoint to retrieve expressions by result, and id
  • Input validation driven by parsing (not regex-based), and early fail
  • Throws validation error (only recognizes [0-9], brackets[(, )] and operators[/, *, +, -]

Run

  1. Checkout the project from github: https://github.com/pranjal710/Abacus
  2. This project has been tested on Intellij 2023.3.3 (Community Edition), Gradle 8.5(bundled), OpenJDK 21.0.2, and 10.4.32-MariaDB (and MySQL))
  3. Update the file src/main/resources/evaluation-configuration.yaml
    1. database > jdbcurl > Swap ABACUS to the right DB name, and change host and port
    2. DB should be present on the server
  4. If the required table are not present in DB, then Liquibase needs to run. To run:
    1. Gradle task: application > liquibase -> gradle liquibase
  5. To run application:
    1. Gradle task application > run -> gradle run
  6. Default port the server comes on are 9090 for application and 9091 for admin
    1. This can be changed in the yaml file
  7. API Usage
    1. API to evaluate and save: POST: http://localhost:9090/rest/api/expressions/evaluate , example payload: { "expression" : "3+4*6-12" }
    2. API to search by ID: GET: http://localhost:9090/rest/api/expressions/query/id?identifier={id}
    3. API to search by result: GET: http://localhost:9090/rest/api/expressions/query/result?identifier={result}
    4. Host is assumed to be localhost, which can be changed to your host.
    5. Service assumes DB server to be up an running, with required tables

Design Overview

The service uses a parser-driven validation approach:

  1. ExpressionValidator.java - validates provided expression
  2. NumbersOperatorsTokenizer.java – converts the expressions into tokens
  3. InfixEvaluator.java – evaluates tokenized expression, by converting them to postfix
  4. PosfixEvaluator.java – evaluates a postfix tokenized expression
  5. ArithmeticOperators.java - available arithmetic operators
  6. TokenType.java - types of tokens supported
  7. ExpressionsDao.java - abstracts db queries

Invalid expressions fail during validation and are reported with meaningful error messages.


Tech Stack

  • Java
  • Dropwizard
  • MySQL
  • Gradle
  • JUnit 5
  • Mockito
  • Liquibase

Assumptions & Trade-offs

  • Expressions are purely numeric (no variables or functions)
  • Validation is achieved via parsing
  • ORM is intentionally avoided for simplicity
  • Instead of building an AST, stack is used to evaluate given limited use case

Possible Enhancements

  1. Health Check
  2. AST based evaluation, to support variables
  3. ANTLR, for richer grammar
  4. ORM integration
  5. Function support
  6. Cache of evaluated expressions
  7. Pagination for queries
  8. Code Analyser
  9. Test Coverage eg. Jacoco

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages