Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g595.topilskiy.task04 #319

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
42 changes: 42 additions & 0 deletions homework-g595-topilskiy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,57 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>homework-g595-topilskiy</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>

<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>net.sourceforge.jeval</groupId>
<artifactId>jeval</artifactId>
<version>0.9.4</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>

<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-tests</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest;

import ru.mipt.java2016.homework.base.task1.ParsingException;
import ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest.function.CalculatorFunctionObject;

import java.util.List;

/**
* Interface for a Calculator which can hold user-defined Functions and Variables
*
* @author Artem K. Topilskiy
* @since 16.12.16.
*/
public interface IFunctionalCalculator {
/**
* Methods of interacting with calculator VARIABLES
*/
/**
* @return the double value under the alias of variableAlias
*/
Double getVariable(String variableAlias);

/**
* Make the alias of variableAlias reflect to the double value
*/
boolean putVariable(String variableAlias, Double value);

/**
* Delete the alias of variableAlias and its held value
*/
boolean deleteVariable(String variableAlias);

/**
* @return the list of aliases of variables in the calculator
*/
List<String> getVariableList();


/**
* Methods of interacting with calculator FUNCTIONS
*/
/**
* @return a CalculatorFunction object under the alias of functionAlias
* NOTE: predefined functions cannot be dealiased
*/
CalculatorFunctionObject getFunction(String functionAlias);

/**
* Make the alias of functionAlias reflect to CalculatorFunction(expression, arguments)
*/
boolean putFunction(String functionAlias, String expression, List<String> arguments);

/**
* Delete the alias of functionAlias and its held function
*/
boolean deleteFunction(String functionAlias);

/**
* @return the list of aliases of functions in the calculator
*/
List<String> getFunctionList();


/**
* Methods of CALCULATION of the value of expression
* (using the kept function and variable sets)
*/
Double calculate(String expression) throws ParsingException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest;

import ru.mipt.java2016.homework.base.task1.ParsingException;
import ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest.function.CalculateableFunction;
import ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest.function.CalculatorFunctionObject;
import ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest.function.IEvaluateableFunction;
import ru.mipt.java2016.homework.g595.topilskiy.task4.calculator.rest.function.PredefinedFunction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* REST-ready Calculator, which can work with user-defined functions/arguments
*
* @author Artem K. Topilskiy
* @since 16.12.16.
*/
public class RESTCalculator implements IFunctionalCalculator {
/**
* Stored Function and Variable Data
*/
private Map<String, IEvaluateableFunction> functions = new ConcurrentHashMap<>();
private Map<String, Double> variables = new ConcurrentHashMap<>();

/**
* Initialization functions
*/
private void initializePredefinedFunctions() {
functions.put("sin", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.SIN));
functions.put("cos", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.COS));
functions.put("tg", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.TG));
functions.put("sqrt", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.SQRT));
functions.put("pow", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.POW));
functions.put("abs", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.ABS));
functions.put("sign", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.SIGN));
functions.put("log", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.LOG));
functions.put("log2", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.LOG2));
functions.put("rnd", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.RND));
functions.put("max", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.MAX));
functions.put("min", new PredefinedFunction(PredefinedFunction.PredefinedFunctionType.MIN));
}

/**
* CONSTRUCTOR
*/
public RESTCalculator() {
initializePredefinedFunctions();
}

/**
* OVERRIDE
*/

/**
* Methods of interacting with calculator VARIABLES
*/
/**
* @return the double value under the alias of variableAlias
*/
@Override
public Double getVariable(String variableAlias) {
return variables.get(variableAlias);
}

/**
* Make the alias of variableAlias reflect to the double value
*/
@Override
public boolean putVariable(String variableAlias, Double value) {
if (functions.containsKey(variableAlias)) {
return false;
} else {
variables.put(variableAlias, value);
return true;
}
}

/**
* Delete the alias of variableAlias and its held value
*/
@Override
public boolean deleteVariable(String variableAlias) {
if (variables.containsKey(variableAlias)) {
variables.remove(variableAlias);
return true;
}

return false;
}

/**
* @return the list of aliases of variables in the calculator
*/
@Override
public List<String> getVariableList() {
return variables.keySet().stream().collect(Collectors.toList());
}


/**
* Methods of interacting with calculator FUNCTIONS
*/
/**
* @return a CalculatorFunction object under the alias of functionAlias
* NOTE: predefined functions cannot be dealiased
*/
@Override
public CalculatorFunctionObject getFunction(String functionAlias) {
if (!functions.containsKey(functionAlias) || functions.get(functionAlias).isPredefined()) {
return null;
}

CalculateableFunction function = (CalculateableFunction) functions.get(functionAlias);
return new CalculatorFunctionObject(function.getFunctionExpression(), function.getParameterList());
}

/**
* Make the alias of functionAlias reflect to CalculatorFunction(expression, arguments)
*/
@Override
public boolean putFunction(String functionAlias, String expression, List<String> arguments) {
if (variables.containsKey(functionAlias) ||
(functions.containsKey(functionAlias) && functions.get(functionAlias).isPredefined())) {
return false;
}

try {
functions.put(functionAlias, new CalculateableFunction(expression, arguments, functions, variables));
} catch (ParsingException e) {
return false;
}

return true;
}

/**
* Delete the alias of functionAlias and its held function
*/
@Override
public boolean deleteFunction(String functionAlias) {
if (functions.containsKey(functionAlias)) {
IEvaluateableFunction function = functions.get(functionAlias);
if (function == null || function.isPredefined()) {
return false;
} else {
functions.remove(functionAlias);
return true;
}
}

return false;
}

/**
* @return the list of aliases of functions in the calculator
*/
@Override
public List<String> getFunctionList() {
return functions.keySet().stream().collect(Collectors.toList());
}


/**
* Methods of CALCULATION of the value of expression
* (using the kept function and variable sets)
*/
@Override
public Double calculate(String expression) throws ParsingException {
return new CalculateableFunction(expression, new ArrayList<>(), functions, variables).evaluate();
}
}
Loading