Updated rules
This commit is contained in:
parent
d5d34394db
commit
d181ecaf25
@ -59,15 +59,18 @@ public class RulesManager {
|
|||||||
}
|
}
|
||||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
|
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||||
List<String> ruleLines = Files.readAllLines(rulesPath);
|
List<String> ruleLines = Files.readAllLines(rulesPath);
|
||||||
|
ruleLines.remove(0); //Remove the headers
|
||||||
for (String rulesLine : ruleLines) {
|
for (String rulesLine : ruleLines) {
|
||||||
String[] ruleDetails = rulesLine.split(",", 1);
|
if (rulesLine.length() > 0) {
|
||||||
String ruleName = ruleDetails[0];
|
String[] ruleDetails = rulesLine.split(",", 1);
|
||||||
Utils.out.println("Evaluating /rules/" + ruleName + ".js");
|
String ruleName = ruleDetails[0];
|
||||||
InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js");
|
Utils.out.println("Evaluating /rules/" + ruleName + ".js");
|
||||||
if (resourcePath == null) {
|
InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js");
|
||||||
System.err.println(new FileNotFoundException("/rules/" + ruleName + ".js not found!"));
|
if (resourcePath == null) {
|
||||||
} else {
|
System.err.println(new FileNotFoundException("/rules/" + ruleName + ".js not found!"));
|
||||||
engine.eval(new InputStreamReader(resourcePath));
|
} else {
|
||||||
|
engine.eval(new InputStreamReader(resourcePath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (URISyntaxException | IOException e) {
|
} catch (URISyntaxException | IOException e) {
|
||||||
@ -96,10 +99,10 @@ public class RulesManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Expression generateUselessExpression() {
|
private static Function generateUselessExpression() {
|
||||||
MathContext mc = new MathContext();
|
MathContext mc = new MathContext();
|
||||||
Expression expr = new Expression(mc);
|
Function expr = new Expression(mc);
|
||||||
expr = (Expression) expr.setParameter(new Variable(mc, 'x', V_TYPE.VARIABLE));
|
expr = expr.setParameter(0, new Variable(mc, 'x', V_TYPE.VARIABLE));
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
functions/Expression
|
Rule file
|
||||||
functions/Division
|
functions/Division
|
||||||
ExpandRule1
|
functions/EmptyNumber
|
||||||
ExpandRule2
|
functions/Expression
|
||||||
|
functions/Joke
|
||||||
|
functions/Multiplication
|
||||||
|
functions/Negative
|
||||||
|
functions/Number
|
||||||
|
functions/Power
|
||||||
|
functions/Root
|
||||||
|
ExpandRule1
|
||||||
|
ExpandRule2
|
||||||
ExpandRule5
|
ExpandRule5
|
|
35
src/main/resources/rules/functions/EmptyNumber.js
Normal file
35
src/main/resources/rules/functions/EmptyNumber.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EmptyNumber
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "EmptyNumber";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
35
src/main/resources/rules/functions/Joke.js
Normal file
35
src/main/resources/rules/functions/Joke.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joke
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "Joke";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
52
src/main/resources/rules/functions/Multiplication.js
Normal file
52
src/main/resources/rules/functions/Multiplication.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
|
||||||
|
var Sum = org.warp.picalculator.math.functions.Sum;
|
||||||
|
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
|
||||||
|
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Division = org.warp.picalculator.math.functions.Division;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiplication
|
||||||
|
* a*b = c
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "Multiplication";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
if (ScriptUtils.instanceOf(f, Multiplication.class)) {
|
||||||
|
var result = new ObjectArrayList();
|
||||||
|
var variable1 = f.getParameter1();
|
||||||
|
var variable2 = f.getParameter2();
|
||||||
|
var mathContext = f.getMathContext();
|
||||||
|
if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) {
|
||||||
|
//multiply a by b (a*b = c)
|
||||||
|
result.add(variable1.multiply(variable2));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
62
src/main/resources/rules/functions/Negative.js
Normal file
62
src/main/resources/rules/functions/Negative.js
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Negative = org.warp.picalculator.math.functions.Negative;
|
||||||
|
var Error = org.warp.picalculator.Error;
|
||||||
|
var Errors = org.warp.picalculator.Errors;
|
||||||
|
var NullPointerException = java.lang.NullPointerException;
|
||||||
|
var NumberFormatException = java.lang.NumberFormatException;
|
||||||
|
var ArithmeticException = java.lang.ArithmeticException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negative
|
||||||
|
* -a = b
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "Negative";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
if (ScriptUtils.instanceOf(f, Negative.class)) {
|
||||||
|
var result = new ObjectArrayList();
|
||||||
|
var variable = f.getParameter();
|
||||||
|
var mathContext = f.getMathContext();
|
||||||
|
if (ScriptUtils.instanceOf(variable, Number.class)) {
|
||||||
|
//-a = a*-1 = b
|
||||||
|
try {
|
||||||
|
result.add(variable.multiply(new Number(mathContext, -1)));
|
||||||
|
} catch (ex) {
|
||||||
|
if (ScriptUtils.instanceOf(ex, NullPointerException.class)) {
|
||||||
|
throw new Error(Errors.ERROR);
|
||||||
|
} else if (ScriptUtils.instanceOf(ex, NumberFormatException.class)) {
|
||||||
|
throw new Error(Errors.SYNTAX_ERROR);
|
||||||
|
} else if (ScriptUtils.instanceOf(ex, ArithmeticException.class)) {
|
||||||
|
throw new Error(Errors.NUMBER_TOO_SMALL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
49
src/main/resources/rules/functions/Number.js
Normal file
49
src/main/resources/rules/functions/Number.js
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var BigInteger = java.math.BigInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "Number";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
if (ScriptUtils.instanceOf(f, Number.class)) {
|
||||||
|
var result = new ObjectArrayList();
|
||||||
|
var mathContext = f.getMathContext();
|
||||||
|
if (mathContext.exactMode) {
|
||||||
|
if (f.isInteger() == false) {
|
||||||
|
var divisor = new Number(mathContext, BigInteger.TEN.pow(f.getNumberOfDecimalPlaces()));
|
||||||
|
var number = new Number(mathContext, f.getTerm().multiply(divisor.term));
|
||||||
|
var div = new Division(mathContext, number, divisor);
|
||||||
|
result.add(div);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
53
src/main/resources/rules/functions/Power.js
Normal file
53
src/main/resources/rules/functions/Power.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
|
||||||
|
var Sum = org.warp.picalculator.math.functions.Sum;
|
||||||
|
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
|
||||||
|
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Division = org.warp.picalculator.math.functions.Division;
|
||||||
|
var Power = org.warp.picalculator.math.functions.Power;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Power
|
||||||
|
* a^b = c
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "Power";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
if (ScriptUtils.instanceOf(f, Power.class)) {
|
||||||
|
var result = new ObjectArrayList();
|
||||||
|
var variable1 = f.getParameter1();
|
||||||
|
var variable2 = f.getParameter2();
|
||||||
|
var mathContext = f.getMathContext();
|
||||||
|
if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) {
|
||||||
|
//a^b = c
|
||||||
|
result.add(variable1.pow(variable2));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
78
src/main/resources/rules/functions/Root.js
Normal file
78
src/main/resources/rules/functions/Root.js
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
|
||||||
|
var Sum = org.warp.picalculator.math.functions.Sum;
|
||||||
|
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
|
||||||
|
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Division = org.warp.picalculator.math.functions.Division;
|
||||||
|
var Root = org.warp.picalculator.math.functions.Root;
|
||||||
|
var RootSquare = org.warp.picalculator.math.functions.RootSquare;
|
||||||
|
var BigDecimal = java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Root
|
||||||
|
* a√b = c
|
||||||
|
*
|
||||||
|
* @author Andrea Cavalli
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var rule = {
|
||||||
|
// Rule name
|
||||||
|
getRuleName: function() {
|
||||||
|
return "Power";
|
||||||
|
},
|
||||||
|
// Rule type
|
||||||
|
getRuleType: function() {
|
||||||
|
return RuleType.CALCULATION;
|
||||||
|
},
|
||||||
|
/* Rule function
|
||||||
|
Returns:
|
||||||
|
- null if it's not executable on the function "f"
|
||||||
|
- An ObjectArrayList<Function> if it did something
|
||||||
|
*/
|
||||||
|
execute: function(f) {
|
||||||
|
var isSquare = false;
|
||||||
|
if (ScriptUtils.instanceOf(f, Root.class) || (isSquare = ScriptUtils.instanceOf(f, RootSquare.class))) {
|
||||||
|
var result = new ObjectArrayList();
|
||||||
|
var mathContext = f.getMathContext();
|
||||||
|
var variable1 = isSquare?new Number(mathContext, 2):f.getParameter1();
|
||||||
|
var variable2 = isSquare?f.getParameter():f.getParameter2();
|
||||||
|
var isSolvable = false;
|
||||||
|
var canBePorted = false;
|
||||||
|
if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) {
|
||||||
|
isSolvable = isSolvable|!mathContext.exactMode;
|
||||||
|
if (!isSolvable) {
|
||||||
|
try {
|
||||||
|
var resultVar = variable2.pow(new Number(mathContext, BigDecimal.ONE).divide(variable1));
|
||||||
|
var originalVariable = resultVar.pow(new Number(mathContext, 2));
|
||||||
|
if (originalVariable.equals(parameter2)) {
|
||||||
|
isSolvable = true;
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isSquare && !isSolvable && ScriptUtils.instanceOf(variable1, Number.class) && variable1.equals(new Number(mathContext, 2))) {
|
||||||
|
canBePorted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isSolvable) {
|
||||||
|
result.add(variable2.pow(new Number(mathContext, BigInteger.ONE).divide(variable1)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (canBePorted) {
|
||||||
|
result.add(new RootSquare(mathContext, variable2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this rule to the list of rules
|
||||||
|
RulesManager.addRule(engine.getInterface(rule, Rule.class));
|
Loading…
x
Reference in New Issue
Block a user