From ef1430a9e4beecbdd8ba381066bda78ed8611ae9 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 6 Feb 2018 22:46:28 +0100 Subject: [PATCH] Converted more rules --- .../math/rules/FractionsRule1.java | 44 ----------- .../math/rules/FractionsRule11.java | 52 ------------- .../math/rules/FractionsRule2.java | 37 --------- .../math/rules/FractionsRule3.java | 33 -------- .../math/rules/FractionsRule4.java | 41 ---------- .../math/rules/FractionsRule5.java | 47 ------------ src/main/resources/rules.csv | 8 +- src/main/resources/rules/FractionsRule1.js | 65 ++++++++++++++++ src/main/resources/rules/FractionsRule11.js | 71 +++++++++++++++++ src/main/resources/rules/FractionsRule2.js | 57 ++++++++++++++ src/main/resources/rules/FractionsRule3.js | 53 +++++++++++++ src/main/resources/rules/FractionsRule4.js | 61 +++++++++++++++ src/main/resources/rules/FractionsRule5.js | 76 +++++++++++++++++++ 13 files changed, 390 insertions(+), 255 deletions(-) delete mode 100755 src/main/java/org/warp/picalculator/math/rules/FractionsRule1.java delete mode 100755 src/main/java/org/warp/picalculator/math/rules/FractionsRule11.java delete mode 100755 src/main/java/org/warp/picalculator/math/rules/FractionsRule2.java delete mode 100755 src/main/java/org/warp/picalculator/math/rules/FractionsRule3.java delete mode 100755 src/main/java/org/warp/picalculator/math/rules/FractionsRule4.java delete mode 100755 src/main/java/org/warp/picalculator/math/rules/FractionsRule5.java create mode 100644 src/main/resources/rules/FractionsRule1.js create mode 100644 src/main/resources/rules/FractionsRule11.js create mode 100644 src/main/resources/rules/FractionsRule2.js create mode 100644 src/main/resources/rules/FractionsRule3.js create mode 100644 src/main/resources/rules/FractionsRule4.js create mode 100644 src/main/resources/rules/FractionsRule5.js diff --git a/src/main/java/org/warp/picalculator/math/rules/FractionsRule1.java b/src/main/java/org/warp/picalculator/math/rules/FractionsRule1.java deleted file mode 100755 index a9bf6a18..00000000 --- a/src/main/java/org/warp/picalculator/math/rules/FractionsRule1.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.warp.picalculator.math.rules; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; -import org.warp.picalculator.math.MathContext; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Number; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule
- * 0 / a = 0 - * - * @author Andrea Cavalli - * - */ -public class FractionsRule1 { - - public static boolean compare(Function f) { - final MathContext root = f.getMathContext(); - final Division fnc = (Division) f; - if (fnc.getParameter1() instanceof Number) { - final Number numb1 = (Number) fnc.getParameter1(); - if (numb1.equals(new Number(root, 0))) { - if (fnc.getParameter2() instanceof Number) { - final Number numb2 = (Number) fnc.getParameter2(); - if (numb2.equals(new Number(root, 0))) { - return false; - } - } - return true; - } - } - return false; - } - - public static ObjectArrayList execute(Function f) throws Error { - final ObjectArrayList result = new ObjectArrayList<>(); - result.add(new Number(f.getMathContext(), 0)); - return result; - } - -} diff --git a/src/main/java/org/warp/picalculator/math/rules/FractionsRule11.java b/src/main/java/org/warp/picalculator/math/rules/FractionsRule11.java deleted file mode 100755 index ebe63b76..00000000 --- a/src/main/java/org/warp/picalculator/math/rules/FractionsRule11.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.warp.picalculator.math.rules; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Expression; -import org.warp.picalculator.math.functions.Multiplication; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule
- * a / (b / c) = (a * c) / b - * - * @author Andrea Cavalli - * - */ -public class FractionsRule11 { - - public static boolean compare(Function f) throws InterruptedException { - final Division fnc = (Division) f; - Function a; - Function c; - Division div2; - if (fnc.getParameter2() instanceof Division) { - div2 = (Division) fnc.getParameter2(); - } else { - return false; - } - a = fnc.getParameter1(); - c = div2.getParameter2(); - return new Multiplication(fnc.getMathContext(), a, c).simplify(this) != null; - } - - public static ObjectArrayList execute(Function f) throws Error { - final ObjectArrayList result = new ObjectArrayList<>(); - final Division fnc = (Division) f; - Function a; - Function b; - Function c; - - final Division div2 = (Division) fnc.getParameter2(); - - a = fnc.getParameter1(); - b = div2.getParameter1(); - c = div2.getParameter2(); - result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), b)); - - return result; - } - -} diff --git a/src/main/java/org/warp/picalculator/math/rules/FractionsRule2.java b/src/main/java/org/warp/picalculator/math/rules/FractionsRule2.java deleted file mode 100755 index 8c32b510..00000000 --- a/src/main/java/org/warp/picalculator/math/rules/FractionsRule2.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.warp.picalculator.math.rules; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Number; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule
- * a / 1 = a - * - * @author Andrea Cavalli - * - */ -public class FractionsRule2 { - - public static boolean compare(Function f) { - final Division fnc = (Division) f; - if (fnc.getParameter2() instanceof Number) { - final Number numb = (Number) fnc.getParameter2(); - if (numb.equals(new Number(f.getMathContext(), 1))) { - return true; - } - } - return false; - } - - public static ObjectArrayList execute(Function f) throws Error { - final ObjectArrayList result = new ObjectArrayList<>(); - final Division fnc = (Division) f; - result.add(fnc.getParameter1()); - return result; - } - -} diff --git a/src/main/java/org/warp/picalculator/math/rules/FractionsRule3.java b/src/main/java/org/warp/picalculator/math/rules/FractionsRule3.java deleted file mode 100755 index b5782332..00000000 --- a/src/main/java/org/warp/picalculator/math/rules/FractionsRule3.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.warp.picalculator.math.rules; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Number; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule
- * a / a = 1 - * - * @author Andrea Cavalli - * - */ -public class FractionsRule3 { - - public static boolean compare(Function f) { - final Division fnc = (Division) f; - if (fnc.getParameter1().equals(fnc.getParameter2())) { - return true; - } - return false; - } - - public static ObjectArrayList execute(Function f) throws Error { - final ObjectArrayList result = new ObjectArrayList<>(); - result.add(new Number(f.getMathContext(), 1)); - return result; - } - -} diff --git a/src/main/java/org/warp/picalculator/math/rules/FractionsRule4.java b/src/main/java/org/warp/picalculator/math/rules/FractionsRule4.java deleted file mode 100755 index cd0fc30d..00000000 --- a/src/main/java/org/warp/picalculator/math/rules/FractionsRule4.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.warp.picalculator.math.rules; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Number; -import org.warp.picalculator.math.functions.Power; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule
- * (a / b) ^ -1 = b / a - * - * @author Andrea Cavalli - * - */ -public class FractionsRule4 { - - public static boolean compare(Function f) { - final Power fnc = (Power) f; - if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) { - final Number n2 = (Number) fnc.getParameter2(); - if (n2.equals(new Number(f.getMathContext(), -1))) { - return true; - } - } - return false; - } - - public static ObjectArrayList execute(Function f) throws Error { - final ObjectArrayList result = new ObjectArrayList<>(); - final Power fnc = (Power) f; - final Function a = ((Division) fnc.getParameter1()).getParameter1(); - final Function b = ((Division) fnc.getParameter1()).getParameter2(); - final Division res = new Division(f.getMathContext(), b, a); - result.add(res); - return result; - } - -} diff --git a/src/main/java/org/warp/picalculator/math/rules/FractionsRule5.java b/src/main/java/org/warp/picalculator/math/rules/FractionsRule5.java deleted file mode 100755 index 0b707d7a..00000000 --- a/src/main/java/org/warp/picalculator/math/rules/FractionsRule5.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.warp.picalculator.math.rules; - -import java.math.BigDecimal; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; -import org.warp.picalculator.math.MathContext; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Number; -import org.warp.picalculator.math.functions.Power; - -import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule
- * (a / b) ^ -c = (b / a) ^ c - * - * @author Andrea Cavalli - * - */ -public class FractionsRule5 { - - public static boolean compare(Function f) { - final Power fnc = (Power) f; - if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) { - final Number n2 = (Number) fnc.getParameter2(); - if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { - return true; - } - } - return false; - } - - public static ObjectArrayList execute(Function f) throws Error { - final MathContext root = f.getMathContext(); - final ObjectArrayList result = new ObjectArrayList<>(); - final Power fnc = (Power) f; - final Function a = ((Division) fnc.getParameter1()).getParameter1(); - final Function b = ((Division) fnc.getParameter1()).getParameter2(); - final Function c = ((Number) fnc.getParameter2()).multiply(new Number(root, "-1")); - final Division dv = new Division(root, b, a); - final Power pow = new Power(root, dv, c); - result.add(pow); - return result; - } - -} diff --git a/src/main/resources/rules.csv b/src/main/resources/rules.csv index e025e313..ae79e057 100644 --- a/src/main/resources/rules.csv +++ b/src/main/resources/rules.csv @@ -22,4 +22,10 @@ ExponentRule4 ExponentRule9 ExponentRule15 ExponentRule16 -ExponentRule17 \ No newline at end of file +ExponentRule17 +FractionsRule1 +FractionsRule2 +FractionsRule3 +FractionsRule4 +FractionsRule5 +FractionsRule11 diff --git a/src/main/resources/rules/FractionsRule1.js b/src/main/resources/rules/FractionsRule1.js new file mode 100644 index 00000000..15548e72 --- /dev/null +++ b/src/main/resources/rules/FractionsRule1.js @@ -0,0 +1,65 @@ +//Imports + + +var Error = org.warp.picalculator.Error; +var Function = org.warp.picalculator.math.Function; +var MathContext = org.warp.picalculator.math.MathContext; +var Division = org.warp.picalculator.math.functions.Division; +var Number = org.warp.picalculator.math.functions.Number; + +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); + +/** + * Fractions rule + * 0 / a = 0 + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "FractionsRule1"; + }, + // Rule type + getRuleType: function() { + return RuleType.CALCULATION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + execute: function(f) { + var isExecutable; + var root = f.getMathContext(); + if (ScriptUtils.instanceOf(f, Division.class)) { + var fnc = f; + if (ScriptUtils.instanceOf(fnc.getParameter1(), Number.class)) { + var numb1 = fnc.getParameter1(); + if (numb1.equals(new Number(root, 0))) { + if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { + var numb2 = fnc.getParameter2(); + if (numb2.equals(new Number(root, 0)) == false) { + isExecutable = true; + } + } else { + isExecutable = true; + } + } + } + } + + if (isExecutable) { + var result = new ObjectArrayList(); + result.add(new Number(f.getMathContext(), 0)); + return result; + } else { + return null; + } + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule11.js b/src/main/resources/rules/FractionsRule11.js new file mode 100644 index 00000000..d6c1877c --- /dev/null +++ b/src/main/resources/rules/FractionsRule11.js @@ -0,0 +1,71 @@ +//Imports + + +var Error = org.warp.picalculator.Error; +var Function = org.warp.picalculator.math.Function; +var Division = org.warp.picalculator.math.functions.Division; +var Multiplication = org.warp.picalculator.math.functions.Multiplication; + +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); + +/** + * Fractions rule + * a / (b / c) = (a * c) / b + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "FractionsRule11"; + }, + // Rule type + getRuleType: function() { + return RuleType.EXPANSION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + execute: function(f) { + var isExecutable; + if (ScriptUtils.instanceOf(f, Division.class)) { + var fnc = f; + var a; + var c; + var div2; + if (ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) { + div2 = fnc.getParameter2(); + a = fnc.getParameter1(); + c = div2.getParameter2(); + isExecutable = true; + } else { + isExecutable = false; + } + } + if (isExecutable) { + var result = new ObjectArrayList(); + var fnc = f; + var a; + var b; + var c; + + var div2 = fnc.getParameter2(); + + a = fnc.getParameter1(); + b = div2.getParameter1(); + c = div2.getParameter2(); + result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), b)); + + return result; + } else { + return null; + } + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule2.js b/src/main/resources/rules/FractionsRule2.js new file mode 100644 index 00000000..1a0137f3 --- /dev/null +++ b/src/main/resources/rules/FractionsRule2.js @@ -0,0 +1,57 @@ +//Imports + + +var Error = org.warp.picalculator.Error; +var Function = org.warp.picalculator.math.Function; +var Division = org.warp.picalculator.math.functions.Division; +var Number = org.warp.picalculator.math.functions.Number; + +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); + +/** + * Fractions rule + * a / 1 = a + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "FractionsRule2"; + }, + // Rule type + getRuleType: function() { + return RuleType.CALCULATION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + execute: function(f) { + var isExecutable; + if (ScriptUtils.instanceOf(f, Division.class)) { + var fnc = f; + if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { + var numb = fnc.getParameter2(); + if (numb.equals(new Number(f.getMathContext(), 1))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var result = new ObjectArrayList(); + var fnc = f; + result.add(fnc.getParameter1()); + return result; + } else { + return null; + } + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule3.js b/src/main/resources/rules/FractionsRule3.js new file mode 100644 index 00000000..2deb3a56 --- /dev/null +++ b/src/main/resources/rules/FractionsRule3.js @@ -0,0 +1,53 @@ +//Imports + + +var Error = org.warp.picalculator.Error; +var Function = org.warp.picalculator.math.Function; +var Division = org.warp.picalculator.math.functions.Division; +var Number = org.warp.picalculator.math.functions.Number; + +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); + +/** + * Fractions rule + * a / a = 1 + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "FractionsRule3"; + }, + // Rule type + getRuleType: function() { + return RuleType.EXPANSION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + execute: function(f) { + var isExecutable; + if (ScriptUtils.instanceOf(f, Division.class)) { + var fnc = f; + if (fnc.getParameter1().equals(fnc.getParameter2())) { + isExecutable = true; + } + } + + if (isExecutable) { + var result = new ObjectArrayList(); + result.add(new Number(f.getMathContext(), 1)); + return result; + } else { + return null; + } + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule4.js b/src/main/resources/rules/FractionsRule4.js new file mode 100644 index 00000000..0d117603 --- /dev/null +++ b/src/main/resources/rules/FractionsRule4.js @@ -0,0 +1,61 @@ +//Imports + + +var Error = org.warp.picalculator.Error; +var Function = org.warp.picalculator.math.Function; +var Division = org.warp.picalculator.math.functions.Division; +var Number = org.warp.picalculator.math.functions.Number; +var Power = org.warp.picalculator.math.functions.Power; + +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); + +/** + * Fractions rule + * (a / b) ^ -1 = b / a + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "FractionsRule4"; + }, + // Rule type + getRuleType: function() { + return RuleType.EXPANSION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + execute: function(f) { + var isExecutable; + if (ScriptUtils.instanceOf(f, Power.class)) { + var fnc = f; + if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { + var n2 = fnc.getParameter2(); + if (n2.equals(new Number(f.getMathContext(), -1))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var result = new ObjectArrayList(); + var fnc = f; + var a = (fnc.getParameter1()).getParameter1(); + var b = (fnc.getParameter1()).getParameter2(); + var res = new Division(f.getMathContext(), b, a); + result.add(res); + return result; + } else { + return null; + } + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule5.js b/src/main/resources/rules/FractionsRule5.js new file mode 100644 index 00000000..58aaccc6 --- /dev/null +++ b/src/main/resources/rules/FractionsRule5.js @@ -0,0 +1,76 @@ +//Imports + + +var BigDecimal = Java.type("java.math.BigDecimal"); + +var Error = org.warp.picalculator.Error; +var Function = org.warp.picalculator.math.Function; +var MathContext = org.warp.picalculator.math.MathContext; +var Division = org.warp.picalculator.math.functions.Division; +var Number = org.warp.picalculator.math.functions.Number; +var Power = org.warp.picalculator.math.functions.Power; + +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); + +/** + * Fractions rule + * (a / b) ^ -c = (b / a) ^ c + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "FractionsRule5"; + }, + // Rule type + getRuleType: function() { + return RuleType.EXPANSION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + execute: function(f) { + var isExecutable; + if (ScriptUtils.instanceOf(f, Power.class)) { + var fnc = f; + if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) { + if (ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class) && fnc.getParameter2().getParameter1().equals(new Number(f.getMathContext(), -1))) { + isExecutable = true; + } else if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { + var n2 = fnc.getParameter2(); + if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { + isExecutable = true; + } + } + } + } + + if (isExecutable) { + var root = f.getMathContext(); + var result = new ObjectArrayList(); + var fnc = f; + var a = (fnc.getParameter1()).getParameter1(); + var b = (fnc.getParameter1()).getParameter2(); + var c; + if (ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class)) { + c = fnc.getParameter2().getParameter2(); + } else { + c = fnc.getParameter2().multiply(new Number(root, "-1")); + } + var dv = new Division(root, b, a); + var pow = new Power(root, dv, c); + result.add(pow); + return result; + } else { + return null; + } + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file