diff --git a/.classpath b/.classpath index 8c546033..d3a6e496 100644 --- a/.classpath +++ b/.classpath @@ -11,7 +11,7 @@ - + diff --git a/src/main/java/org/warp/picalculator/KeyboardTest.java b/src/main/java/org/warp/picalculator/KeyboardTest.java index 3180ad11..17bfa0a6 100755 --- a/src/main/java/org/warp/picalculator/KeyboardTest.java +++ b/src/main/java/org/warp/picalculator/KeyboardTest.java @@ -4,7 +4,7 @@ import org.warp.picalculator.gui.screens.KeyboardDebugScreen; public class KeyboardTest { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws InterruptedException, Error { new Main(new KeyboardDebugScreen(), args); } } diff --git a/src/main/java/org/warp/picalculator/Main.java b/src/main/java/org/warp/picalculator/Main.java index dfe3aff5..efcc8edd 100755 --- a/src/main/java/org/warp/picalculator/Main.java +++ b/src/main/java/org/warp/picalculator/Main.java @@ -14,11 +14,11 @@ import com.pi4j.wiringpi.Gpio; public class Main { public static Main instance; public static String[] args; - public Main(String[] args) throws InterruptedException { + public Main(String[] args) throws InterruptedException, Error { this(new LoadingScreen(), args); } - public Main(Screen screen, String[] args) { + public Main(Screen screen, String[] args) throws InterruptedException, Error { System.out.println("WarpPI Calculator"); instance = this; Main.args = args; @@ -94,7 +94,7 @@ public class Main { Keyboard.startKeyboard(); } - public void afterStart() { + public void afterStart() throws InterruptedException, Error { DisplayManager.INSTANCE.setBrightness(0.2f); RulesManager.initialize(); RulesManager.warmUp(); @@ -104,7 +104,7 @@ public class Main { Keyboard.stopKeyboard(); } - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws InterruptedException, Error { /* * TEST: Comparing BigIntegerMath.divisors() vs programmingpraxis' Number.getFactors() function * diff --git a/src/main/java/org/warp/picalculator/math/functions/EmptyNumber.java b/src/main/java/org/warp/picalculator/math/functions/EmptyNumber.java index 884107c2..f7775d34 100755 --- a/src/main/java/org/warp/picalculator/math/functions/EmptyNumber.java +++ b/src/main/java/org/warp/picalculator/math/functions/EmptyNumber.java @@ -17,7 +17,7 @@ public class EmptyNumber implements Function { private final MathContext root; @Override - public ObjectArrayList simplify(Rule rule) throws Error { + public ObjectArrayList simplify(Rule rule) throws Error, InterruptedException { return rule.execute(this); } diff --git a/src/main/java/org/warp/picalculator/math/functions/Joke.java b/src/main/java/org/warp/picalculator/math/functions/Joke.java index e8e192f0..99f870f4 100755 --- a/src/main/java/org/warp/picalculator/math/functions/Joke.java +++ b/src/main/java/org/warp/picalculator/math/functions/Joke.java @@ -27,7 +27,7 @@ public class Joke implements Function { } @Override - public ObjectArrayList simplify(Rule rule) throws Error { + public ObjectArrayList simplify(Rule rule) throws Error, InterruptedException { return rule.execute(this); } diff --git a/src/main/java/org/warp/picalculator/math/functions/Number.java b/src/main/java/org/warp/picalculator/math/functions/Number.java index 2eb71ed4..591586bb 100755 --- a/src/main/java/org/warp/picalculator/math/functions/Number.java +++ b/src/main/java/org/warp/picalculator/math/functions/Number.java @@ -117,7 +117,7 @@ public class Number implements Function { } @Override - public ObjectArrayList simplify(Rule rule) throws Error { + public ObjectArrayList simplify(Rule rule) throws Error, InterruptedException { return rule.execute(this); } diff --git a/src/main/java/org/warp/picalculator/math/functions/Undefined.java b/src/main/java/org/warp/picalculator/math/functions/Undefined.java index b7849532..6f3510ad 100755 --- a/src/main/java/org/warp/picalculator/math/functions/Undefined.java +++ b/src/main/java/org/warp/picalculator/math/functions/Undefined.java @@ -23,7 +23,7 @@ public class Undefined implements Function { } @Override - public ObjectArrayList simplify(Rule rule) throws Error { + public ObjectArrayList simplify(Rule rule) throws Error, InterruptedException { return rule.execute(this); } diff --git a/src/main/java/org/warp/picalculator/math/functions/Variable.java b/src/main/java/org/warp/picalculator/math/functions/Variable.java index 14cda652..efc0f040 100755 --- a/src/main/java/org/warp/picalculator/math/functions/Variable.java +++ b/src/main/java/org/warp/picalculator/math/functions/Variable.java @@ -57,7 +57,7 @@ public class Variable implements Function { } @Override - public ObjectArrayList simplify(Rule rule) throws Error { + public ObjectArrayList simplify(Rule rule) throws Error, InterruptedException { return rule.execute(this); } diff --git a/src/main/java/org/warp/picalculator/math/rules/Rule.java b/src/main/java/org/warp/picalculator/math/rules/Rule.java index 82c8a1db..a7c17ee5 100644 --- a/src/main/java/org/warp/picalculator/math/rules/Rule.java +++ b/src/main/java/org/warp/picalculator/math/rules/Rule.java @@ -2,6 +2,7 @@ package org.warp.picalculator.math.rules; import java.io.Serializable; +import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -12,7 +13,7 @@ import jdk.nashorn.internal.objects.annotations.SpecializedFunction; * @author Andrea Cavalli * */ -public interface Rule extends Serializable { +public interface Rule { /** * Get rule name * @return @@ -32,6 +33,7 @@ public interface Rule extends Serializable { * * @param func * @return
  • null if it's not executable on the function func
  • An ObjectArrayList<Function> if it did something
+ * @throws Error */ - public ObjectArrayList execute(Function func); + public ObjectArrayList execute(Function func) throws Error, InterruptedException; } \ No newline at end of file diff --git a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java index dcb5a58e..21c6f7e7 100644 --- a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java +++ b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java @@ -85,7 +85,7 @@ public class RulesManager { tDir.toFile().delete(); } Utils.unzip(cacheFilePath.toString(), tDir.getParent().toString(), ""); - useCache = !StaticVars.debugOn; + useCache = true;//!StaticVars.debugOn; cacheFilePath.toFile().delete(); } catch (Exception ex) { ex.printStackTrace(); @@ -144,15 +144,16 @@ public class RulesManager { InputStream resource = Utils.getResourceStream(scriptFile); String text = Utils.read(resource); String[] textArray = text.split("\\n", 5); - System.err.println(text); - String javaClassName = textArray[2].substring(6); - String javaClassNameAndPath = new StringBuilder("org.warp.picalculator.math.rules.").append(javaClassName).toString(); - final int extIndex = javaClassNameAndPath.lastIndexOf('.'); + String javaClassDeclaration = textArray[2].substring(6); + int extIndex = javaClassDeclaration.lastIndexOf('.'); + String javaClassNameOnly = javaClassDeclaration.substring(extIndex + 1, javaClassDeclaration.length()); + String javaClassNameAndPath = new StringBuilder("org.warp.picalculator.math.rules.").append(javaClassDeclaration).toString(); + extIndex = javaClassNameAndPath.lastIndexOf('.'); String javaCode = new StringBuilder("package ").append(javaClassNameAndPath.substring(0, extIndex >= 0 ? extIndex : javaClassNameAndPath.length())).append(";\n") .append(textArray[4]).toString(); Path tDirPath = tDir.resolve(javaClassNameAndPath.replace('.', File.separatorChar)).getParent(); - Path tFileJava = tDirPath.resolve(javaClassName + ".java"); - Path tFileClass = tDirPath.resolve(javaClassName + ".class"); + Path tFileJava = tDirPath.resolve(javaClassNameOnly + ".java"); + Path tFileClass = tDirPath.resolve(javaClassNameOnly + ".class"); if (!tDirPath.toFile().exists()) { Files.createDirectories(tDirPath); } @@ -192,7 +193,7 @@ public class RulesManager { return (Rule) aClass.newInstance(); } - public static void warmUp() { + public static void warmUp() throws Error, InterruptedException { ObjectArrayList uselessResult = null; boolean uselessVariable = false; for (RuleType val : RuleType.values()) { diff --git a/src/main/resources/rules.csv b/src/main/resources/rules.csv index b87c3d4f..7ab759a4 100644 --- a/src/main/resources/rules.csv +++ b/src/main/resources/rules.csv @@ -1,17 +1,17 @@ Rule file -functions/Division -functions/EmptyNumber -functions/Expression -functions/Joke -functions/Multiplication -functions/Negative -functions/Number -functions/Power -functions/Root -functions/Subtraction -functions/Sum -functions/SumSubtraction -functions/Variable +functions/DivisionRule +functions/EmptyNumberRule +functions/ExpressionRule +functions/JokeRule +functions/MultiplicationRule +functions/NegativeRule +functions/NumberRule +functions/PowerRule +functions/RootRule +functions/SubtractionRule +functions/SumRule +functions/SumSubtractionRule +functions/VariableRule ExpandRule1 ExpandRule2 ExpandRule5 diff --git a/src/main/resources/rules/ExpandRule2.java b/src/main/resources/rules/ExpandRule2.java index f7cece30..762bfd33 100644 --- a/src/main/resources/rules/ExpandRule2.java +++ b/src/main/resources/rules/ExpandRule2.java @@ -1,19 +1,15 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExpandRule2 */ import org.warp.picalculator.math.Function; import org.warp.picalculator.math.FunctionOperator; -import org.warp.picalculator.math.FunctionDynamic; -import org.warp.picalculator.math.FunctionSingle; import org.warp.picalculator.math.MathContext; import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import org.warp.picalculator.ScriptUtils; import org.warp.picalculator.math.rules.Rule; import org.warp.picalculator.math.rules.RuleType; -import org.warp.picalculator.math.rules.RulesManager; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Sum; import org.warp.picalculator.math.functions.Subtraction; @@ -27,7 +23,7 @@ import org.warp.picalculator.math.functions.Number; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExpandRule2 implements Rule { // Rule name @Override public String getRuleName() { @@ -49,9 +45,9 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Multiplication) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) { - var expr = fnc.getParameter2(); + Function expr = fnc.getParameter2(); if (expr instanceof Sum) { isExecutable = true; } else if (expr instanceof Subtraction) { @@ -61,8 +57,8 @@ public class __INSERT_CLASS_NAME__ implements Rule { } } } else if (f instanceof Subtraction || f instanceof SumSubtraction) { - Function fnc = f; - var expr = fnc.getParameter2(); + FunctionOperator fnc = (FunctionOperator) f; + Function expr = fnc.getParameter2(); if (expr instanceof Sum) { isExecutable = true; } else if (expr instanceof Subtraction) { @@ -73,15 +69,15 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { ObjectArrayList result = new ObjectArrayList<>(); - var root = f.getMathContext(); + MathContext root = f.getMathContext(); - var expr = null; - var fromSubtraction = 0; - var subtraction = null; + Function expr = null; + int fromSubtraction = 0; + Function subtraction = null; if (f instanceof Multiplication) { - expr = f.getParameter2(); + expr = ((FunctionOperator) f).getParameter2(); } else if (f instanceof Subtraction || f instanceof SumSubtraction) { - expr = f.getParameter2(); + expr = ((FunctionOperator) f).getParameter2(); if (f instanceof Subtraction) { fromSubtraction = 1; } else { @@ -93,36 +89,36 @@ public class __INSERT_CLASS_NAME__ implements Rule { } - Function fnc = expr; + FunctionOperator fnc = (FunctionOperator) expr; if (fnc instanceof Sum) { - var a = fnc.getParameter1(); - var b = fnc.getParameter2(); - var fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); + Function a = fnc.getParameter1(); + Function b = fnc.getParameter2(); + Function fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); if (fromSubtraction > 0) { - subtraction = new Subtraction(root, f.getParameter1(), fnc2); + subtraction = new Subtraction(root, ((FunctionOperator)f).getParameter1(), fnc2); result.add(subtraction); } else { result.add(fnc2); } } else if (fnc instanceof Subtraction) { - var a = fnc.getParameter1(); - var b = fnc.getParameter2(); - var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); + Function a = fnc.getParameter1(); + Function b = fnc.getParameter2(); + Function fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); if (fromSubtraction > 0) { - subtraction = new Subtraction(root, f.getParameter1(), fnc2); + subtraction = new Subtraction(root, ((FunctionOperator)f).getParameter1(), fnc2); result.add(subtraction); } else { result.add(fnc2); } } else if (fnc instanceof SumSubtraction) { - var a = fnc.getParameter1(); - var b = fnc.getParameter2(); - var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); - var fnc3 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); + Function a = fnc.getParameter1(); + Function b = fnc.getParameter2(); + Function fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); + Function fnc3 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); if (fromSubtraction > 0) { - subtraction = new SumSubtraction(root, f.getParameter1(), fnc2); + subtraction = new SumSubtraction(root, ((FunctionOperator)f).getParameter1(), fnc2); result.add(subtraction); - subtraction = new SumSubtraction(root, f.getParameter1(), fnc3); + subtraction = new SumSubtraction(root, ((FunctionOperator)f).getParameter1(), fnc3); result.add(subtraction); result.add(subtraction); } else { diff --git a/src/main/resources/rules/ExpandRule5.java b/src/main/resources/rules/ExpandRule5.java index fde3f8fd..d77c966b 100644 --- a/src/main/resources/rules/ExpandRule5.java +++ b/src/main/resources/rules/ExpandRule5.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExpandRule5 */ import org.warp.picalculator.math.Function; @@ -10,10 +10,13 @@ import org.warp.picalculator.math.FunctionSingle; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.Error; -import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Expression; +import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Negative; import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -24,7 +27,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExpandRule5 implements Rule { // Rule name @Override public String getRuleName() { @@ -46,10 +49,10 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Negative) { - isExecutable = f.getParameter() instanceof Negative; + isExecutable = ((FunctionSingle)f).getParameter() instanceof Negative; } else if (f instanceof Multiplication) { - if (f.getParameter1().equals(new Number(f.getMathContext(), -1)) && f.getParameter2() instanceof Multiplication) { - isExecutable = f.getParameter2().getParameter1().equals(f.getParameter1()); + if (((FunctionOperator)f).getParameter1().equals(new Number(f.getMathContext(), -1)) && ((FunctionOperator)f).getParameter2() instanceof Multiplication) { + isExecutable = ((FunctionOperator)((FunctionOperator)f).getParameter2()).getParameter1().equals(((FunctionOperator)f).getParameter1()); } } @@ -57,11 +60,11 @@ public class __INSERT_CLASS_NAME__ implements Rule { ObjectArrayList result = new ObjectArrayList<>(); if (f instanceof Negative) { - Function fnc = f; - result.add(((fnc.getParameter()).getParameter()).getParameter()); + Negative fnc = (Negative) f; + result.add(((FunctionSingle)((FunctionSingle)fnc.getParameter()).getParameter()).getParameter()); } else if (f instanceof Multiplication) { - Function fnc = f; - result.add(fnc.getParameter2().getParameter2()); + FunctionOperator fnc = (FunctionOperator) f; + result.add(((FunctionOperator)fnc.getParameter2()).getParameter2()); } return result; } else { diff --git a/src/main/resources/rules/ExponentRule1.java b/src/main/resources/rules/ExponentRule1.java index a04a434d..2c39f5eb 100644 --- a/src/main/resources/rules/ExponentRule1.java +++ b/src/main/resources/rules/ExponentRule1.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule1 */ import org.warp.picalculator.math.Function; @@ -16,6 +16,8 @@ import org.warp.picalculator.math.Function; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -26,7 +28,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule1 implements Rule { // Rule name @Override public String getRuleName() { @@ -47,15 +49,14 @@ public class __INSERT_CLASS_NAME__ implements Rule { @Override public ObjectArrayList execute(Function f) { boolean isExecutable = false; - var root = f.getMathContext(); + MathContext root = f.getMathContext(); if (f instanceof Power) { - if (f.getParameter1().equals(new Number(root, 1))) { + if (((Power)f).getParameter1().equals(new Number(root, 1))) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); result.add(new Number(root, 1)); return result; diff --git a/src/main/resources/rules/ExponentRule15.java b/src/main/resources/rules/ExponentRule15.java index 358be0d6..62565e79 100644 --- a/src/main/resources/rules/ExponentRule15.java +++ b/src/main/resources/rules/ExponentRule15.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule15 */ import org.warp.picalculator.math.Function; @@ -17,6 +17,8 @@ import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -27,7 +29,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule15 implements Rule { // Rule name @Override public String getRuleName() { @@ -50,19 +52,19 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Multiplication) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1().equals(fnc.getParameter2())) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; - var a = fnc.getParameter1(); - var two = new Number(root, 2); - var p = new Power(root, a, two); + FunctionOperator fnc = (FunctionOperator) f; + Function a = fnc.getParameter1(); + Function two = new Number(root, 2); + Function p = new Power(root, a, two); result.add(p); return result; } else { diff --git a/src/main/resources/rules/ExponentRule16.java b/src/main/resources/rules/ExponentRule16.java index af12fc6d..cd33e526 100644 --- a/src/main/resources/rules/ExponentRule16.java +++ b/src/main/resources/rules/ExponentRule16.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule16 */ import org.warp.picalculator.math.Function; @@ -18,6 +18,8 @@ import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -28,7 +30,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule16 implements Rule { // Rule name @Override public String getRuleName() { @@ -51,26 +53,26 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Multiplication) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) { - isExecutable = (fnc.getParameter1()).getParameter1().equals((fnc.getParameter2()).getParameter1()); + isExecutable = ((FunctionOperator)fnc.getParameter1()).getParameter1().equals(((FunctionOperator)fnc.getParameter2()).getParameter1()); } else if (fnc.getParameter1() instanceof Power) { - isExecutable = (fnc.getParameter1()).getParameter1().equals(fnc.getParameter2()); + isExecutable = ((FunctionOperator)fnc.getParameter1()).getParameter1().equals(fnc.getParameter2()); } else if (fnc.getParameter2() instanceof Power) { - isExecutable = (fnc.getParameter2()).getParameter1().equals(fnc.getParameter1()); + isExecutable = ((FunctionOperator)fnc.getParameter2()).getParameter1().equals(fnc.getParameter1()); } } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) { - result.add(new Power(root, (fnc.getParameter1()).getParameter1(), new Sum(root, fnc.getParameter1().getParameter2(), fnc.getParameter2().getParameter2()))); + result.add(new Power(root, ((FunctionOperator)fnc.getParameter1()).getParameter1(), new Sum(root, ((FunctionOperator)fnc.getParameter1()).getParameter2(), ((FunctionOperator)fnc.getParameter2()).getParameter2()))); } else if (fnc.getParameter1() instanceof Power) { - result.add(new Power(root, fnc.getParameter2(), new Sum(root, fnc.getParameter1().getParameter2(), new Number(root, 1)))); + result.add(new Power(root, fnc.getParameter2(), new Sum(root, ((FunctionOperator)fnc.getParameter1()).getParameter2(), new Number(root, 1)))); } else if (fnc.getParameter2() instanceof Power) { - result.add(new Power(root, fnc.getParameter1(), new Sum(root, new Number(root, 1), fnc.getParameter2().getParameter2()))); + result.add(new Power(root, fnc.getParameter1(), new Sum(root, new Number(root, 1), ((FunctionOperator)fnc.getParameter2()).getParameter2()))); } return result; } else { diff --git a/src/main/resources/rules/ExponentRule17.java b/src/main/resources/rules/ExponentRule17.java index 0f74387a..3f14f070 100644 --- a/src/main/resources/rules/ExponentRule17.java +++ b/src/main/resources/rules/ExponentRule17.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule17 */ import org.warp.picalculator.math.Function; @@ -18,6 +18,8 @@ import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; import org.warp.picalculator.math.functions.Root; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -28,7 +30,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule17 implements Rule { // Rule name @Override public String getRuleName() { @@ -51,19 +53,19 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Root) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1().equals(fnc.getParameter2())) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; - var a = fnc.getParameter1(); - var two = new Number(root, 2); - var p = new Power(fnc.getMathContext(), a, two); + FunctionOperator fnc = (FunctionOperator) f; + Function a = fnc.getParameter1(); + Function two = new Number(root, 2); + Function p = new Power(root, a, two); result.add(p); return result; } else { diff --git a/src/main/resources/rules/ExponentRule2.java b/src/main/resources/rules/ExponentRule2.java index 319ba4e2..72361771 100644 --- a/src/main/resources/rules/ExponentRule2.java +++ b/src/main/resources/rules/ExponentRule2.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule2 */ import org.warp.picalculator.math.Function; @@ -15,6 +15,8 @@ import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -25,7 +27,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule2 implements Rule { // Rule name @Override public String getRuleName() { @@ -48,16 +50,16 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Power) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) { isExecutable = true; } } if (isExecutable) { - ObjectArrayList result = new ObjectArrayList<>(); - result.add((f).getParameter1()); - return result; + ObjectArrayList result = new ObjectArrayList<>(); + result.add(((FunctionOperator)f).getParameter1()); + return result; } else { return null; } diff --git a/src/main/resources/rules/ExponentRule3.java b/src/main/resources/rules/ExponentRule3.java index 32182019..ed2b1ec9 100644 --- a/src/main/resources/rules/ExponentRule3.java +++ b/src/main/resources/rules/ExponentRule3.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule3 */ import org.warp.picalculator.math.Function; @@ -15,6 +15,8 @@ import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -25,7 +27,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule3 implements Rule { // Rule name @Override public String getRuleName() { @@ -48,16 +50,16 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Power) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) { isExecutable = true; } } if (isExecutable) { - ObjectArrayList result = new ObjectArrayList<>(); - result.add(new Number(f.getMathContext(), 1)); - return result; + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(f.getMathContext(), 1)); + return result; } else { return null; } diff --git a/src/main/resources/rules/ExponentRule4.java b/src/main/resources/rules/ExponentRule4.java index 528756fd..39616d19 100644 --- a/src/main/resources/rules/ExponentRule4.java +++ b/src/main/resources/rules/ExponentRule4.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule4 */ import org.warp.picalculator.math.Function; @@ -18,6 +18,8 @@ import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -28,7 +30,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule4 implements Rule { // Rule name @Override public String getRuleName() { @@ -51,25 +53,25 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Power) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Multiplication && fnc.getParameter2() instanceof Number) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); - ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; - var mult = fnc.getParameter1(); - var a = mult.getParameter1(); - var b = mult.getParameter2(); - var n = fnc.getParameter2(); - var p1 = new Power(root, a, n); - var p2 = new Power(root, b, n); - var retMult = new Multiplication(root, p1, p2); - result.add(retMult); - return result; + MathContext root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + FunctionOperator fnc = (FunctionOperator) f; + FunctionOperator mult = (FunctionOperator) fnc.getParameter1(); + Function a = mult.getParameter1(); + Function b = mult.getParameter2(); + Function n = fnc.getParameter2(); + Function p1 = new Power(root, a, n); + Function p2 = new Power(root, b, n); + Function retMult = new Multiplication(root, p1, p2); + result.add(retMult); + return result; } else { return null; } diff --git a/src/main/resources/rules/ExponentRule9.java b/src/main/resources/rules/ExponentRule9.java index a70167b5..525aa7e3 100644 --- a/src/main/resources/rules/ExponentRule9.java +++ b/src/main/resources/rules/ExponentRule9.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=ExponentRule9 */ import org.warp.picalculator.math.Function; @@ -16,6 +16,8 @@ import org.warp.picalculator.math.Function; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -26,7 +28,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExponentRule9 implements Rule { // Rule name @Override public String getRuleName() { @@ -49,20 +51,20 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Power) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Power) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); - ObjectArrayList result = new ObjectArrayList<>(); - var powC = f; - var powB = powC.getParameter1(); - var p = new Power(root, powB.getParameter1(), new Multiplication(root, powB.getParameter2(), powC.getParameter2())); - result.add(p); - return result; + MathContext root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + FunctionOperator powC = (FunctionOperator) f; + FunctionOperator powB = (FunctionOperator) powC.getParameter1(); + Function p = new Power(root, powB.getParameter1(), new Multiplication(root, powB.getParameter2(), powC.getParameter2())); + result.add(p); + return result; } else { return null; } diff --git a/src/main/resources/rules/FractionsRule1.java b/src/main/resources/rules/FractionsRule1.java index 2a647787..dad56511 100644 --- a/src/main/resources/rules/FractionsRule1.java +++ b/src/main/resources/rules/FractionsRule1.java @@ -1,40 +1,17 @@ /* SETTINGS: (please don't move this part) - PATH=FractionsRule2 + PATH=FractionsRule1 */ //Imports - - -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.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; - -/** - * Fractions rule - * a / 1 = a/* -SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ -*/ - -import org.warp.picalculator.math.Function; import org.warp.picalculator.math.FunctionOperator; -import org.warp.picalculator.math.FunctionDynamic; -import org.warp.picalculator.math.FunctionSingle; import org.warp.picalculator.math.MathContext; -//Imports - - -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 @@ -43,7 +20,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class FractionsRule1 implements Rule { // Rule name @Override public String getRuleName() { @@ -65,14 +42,14 @@ public class __INSERT_CLASS_NAME__ implements Rule { @Override public ObjectArrayList execute(Function f) { boolean isExecutable = false; - var root = f.getMathContext(); + MathContext root = f.getMathContext(); if (f instanceof Division) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Number) { - var numb1 = fnc.getParameter1(); + Function numb1 = fnc.getParameter1(); if (numb1.equals(new Number(root, 0))) { if (fnc.getParameter2() instanceof Number) { - var numb2 = fnc.getParameter2(); + Function numb2 = fnc.getParameter2(); if (numb2.equals(new Number(root, 0)) == false) { isExecutable = true; } @@ -91,51 +68,4 @@ public class __INSERT_CLASS_NAME__ implements Rule { return null; } } -} - - * - * @author Andrea Cavalli - * - */ -public class FractionsRule2 implements Rule { - // Rule name - @Override - public String getRuleName() { - return "FractionsRule2"; - } - - // Rule type - @Override - public RuleType getRuleType() { - return RuleType.CALCULATION; - } - - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - @Override - public ObjectArrayList execute(Function f) { - boolean isExecutable = false; - if (f instanceof Division) { - Division fnc = (Division) f; - if (fnc.getParameter2() instanceof Number) { - Number numb = (Number) fnc.getParameter2(); - if (numb.equals(new Number(f.getMathContext(), 1))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - ObjectArrayList result = new ObjectArrayList<>(); - Division fnc = (Division) f; - result.add(fnc.getParameter1()); - return result; - } else { - return null; - } - } -} +} \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule11.java b/src/main/resources/rules/FractionsRule11.java new file mode 100644 index 00000000..16570f34 --- /dev/null +++ b/src/main/resources/rules/FractionsRule11.java @@ -0,0 +1,84 @@ +/* +SETTINGS: (please don't move this part) + PATH=FractionsRule11 +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * a / (b / c) = (a * c) / b + * + * @author Andrea Cavalli + * + */ +public class FractionsRule11 implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule11"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Division) { + FunctionOperator fnc = (FunctionOperator) f; + Function a; + Function c; + FunctionOperator div2; + if (fnc.getParameter2() instanceof Division) { + div2 = (FunctionOperator) fnc.getParameter2(); + a = fnc.getParameter1(); + c = div2.getParameter2(); + isExecutable = true; + } else { + isExecutable = false; + } + } + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + FunctionOperator fnc = (FunctionOperator) f; + Function a; + Function b; + Function c; + + FunctionOperator div2 = (FunctionOperator) 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; + } + } +} diff --git a/src/main/resources/rules/FractionsRule12.java b/src/main/resources/rules/FractionsRule12.java new file mode 100644 index 00000000..96e70c5e --- /dev/null +++ b/src/main/resources/rules/FractionsRule12.java @@ -0,0 +1,82 @@ +/* +SETTINGS: (please don't move this part) + PATH=FractionsRule12 +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * (b / c) / a = b / (a * c) + * + * @author Andrea Cavalli + * + */ +public class FractionsRule12 implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule12"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Division) { + FunctionOperator fnc = (FunctionOperator) f; + Function a; + Function c; + if (fnc.getParameter1() instanceof Division) { + FunctionOperator div2 = (FunctionOperator) fnc.getParameter1(); + a = fnc.getParameter1(); + c = div2.getParameter2(); + isExecutable = true; + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + FunctionOperator fnc = (FunctionOperator) f; + Function a; + Function b; + Function c; + + FunctionOperator div2 = (FunctionOperator) fnc.getParameter1(); + a = fnc.getParameter2(); + b = div2.getParameter1(); + c = div2.getParameter2(); + result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), b)); + + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/FractionsRule14.java b/src/main/resources/rules/FractionsRule14.java new file mode 100644 index 00000000..15ca607a --- /dev/null +++ b/src/main/resources/rules/FractionsRule14.java @@ -0,0 +1,118 @@ +/* +SETTINGS: (please don't move this part) + PATH=FractionsRule14 +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * (a / b) * (c / d) = (a * c) / (b * d) + * + * @author Andrea Cavalli + * + */ +public class FractionsRule14 implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule14"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + FunctionOperator fnc = (FunctionOperator) f; + Function a; + Function b; + Function c; + Function d; + if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Division) { + FunctionOperator div1 = (FunctionOperator) fnc.getParameter1(); + FunctionOperator div2 = (FunctionOperator) fnc.getParameter2(); + a = div1.getParameter1(); + b = div1.getParameter2(); + c = div2.getParameter1(); + d = div2.getParameter2(); + isExecutable = true; + } else if (fnc.getParameter1() instanceof Division) { + FunctionOperator div1 = (FunctionOperator) fnc.getParameter1(); + a = div1.getParameter1(); + b = div1.getParameter2(); + c = fnc.getParameter2(); + isExecutable = true; + } else if (fnc.getParameter2() instanceof Division) { + FunctionOperator div2 = (FunctionOperator) fnc.getParameter2(); + a = fnc.getParameter1(); + c = div2.getParameter1(); + d = div2.getParameter2(); + isExecutable = true; + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + FunctionOperator fnc = (FunctionOperator) f; + Function a; + Function b; + Function c; + Function d; + + if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Division) { + FunctionOperator div1 = (FunctionOperator) fnc.getParameter1(); + FunctionOperator div2 = (FunctionOperator) fnc.getParameter2(); + a = div1.getParameter1(); + b = div1.getParameter2(); + c = div2.getParameter1(); + d = div2.getParameter2(); + Function div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), new Multiplication(fnc.getMathContext(), b, d)); + result.add(div); + } else if (fnc.getParameter1() instanceof Division) { + FunctionOperator div1 = (FunctionOperator) fnc.getParameter1(); + a = div1.getParameter1(); + b = div1.getParameter2(); + c = fnc.getParameter2(); + Function div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), b); + result.add(div); + } else if (fnc.getParameter2() instanceof Division) { + FunctionOperator div2 = (FunctionOperator) fnc.getParameter2(); + a = fnc.getParameter1(); + c = div2.getParameter1(); + d = div2.getParameter2(); + Function div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), d); + result.add(div); + } + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/FractionsRule2.java b/src/main/resources/rules/FractionsRule2.java index a4e3735e..ac2494dc 100644 --- a/src/main/resources/rules/FractionsRule2.java +++ b/src/main/resources/rules/FractionsRule2.java @@ -10,6 +10,8 @@ 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.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; diff --git a/src/main/resources/rules/FractionsRule3.java b/src/main/resources/rules/FractionsRule3.java index 320231e8..cdb0d870 100644 --- a/src/main/resources/rules/FractionsRule3.java +++ b/src/main/resources/rules/FractionsRule3.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=FractionsRule3 */ import org.warp.picalculator.math.Function; @@ -15,6 +15,8 @@ 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.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -25,7 +27,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class FractionsRule3 implements Rule { // Rule name @Override public String getRuleName() { @@ -48,7 +50,7 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Division) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1().equals(fnc.getParameter2())) { isExecutable = true; } diff --git a/src/main/resources/rules/FractionsRule4.java b/src/main/resources/rules/FractionsRule4.java index 7a567a35..f0bee9a5 100644 --- a/src/main/resources/rules/FractionsRule4.java +++ b/src/main/resources/rules/FractionsRule4.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=FractionsRule4 */ import org.warp.picalculator.math.Function; @@ -16,6 +16,8 @@ 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 org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -26,7 +28,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class FractionsRule4 implements Rule { // Rule name @Override public String getRuleName() { @@ -49,9 +51,9 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Power) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) { - var n2 = fnc.getParameter2(); + Function n2 = fnc.getParameter2(); if (n2.equals(new Number(f.getMathContext(), -1))) { isExecutable = true; } @@ -60,10 +62,10 @@ public class __INSERT_CLASS_NAME__ implements Rule { if (isExecutable) { ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; - var a = (fnc.getParameter1()).getParameter1(); - var b = (fnc.getParameter1()).getParameter2(); - var res = new Division(f.getMathContext(), b, a); + FunctionOperator fnc = (FunctionOperator) f; + Function a = ((FunctionOperator) fnc.getParameter1()).getParameter1(); + Function b = ((FunctionOperator) fnc.getParameter1()).getParameter2(); + Function res = new Division(f.getMathContext(), b, a); result.add(res); return result; } else { diff --git a/src/main/resources/rules/FractionsRule5.java b/src/main/resources/rules/FractionsRule5.java index a10e648f..8c3980e8 100644 --- a/src/main/resources/rules/FractionsRule5.java +++ b/src/main/resources/rules/FractionsRule5.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=FractionsRule5 */ import org.warp.picalculator.math.Function; @@ -17,8 +17,11 @@ 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.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -29,7 +32,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class FractionsRule5 implements Rule { // Rule name @Override public String getRuleName() { @@ -49,15 +52,15 @@ public class __INSERT_CLASS_NAME__ implements Rule { */ @Override - public ObjectArrayList execute(Function f) { + public ObjectArrayList execute(Function f) throws Error { boolean isExecutable = false; if (f instanceof Power) { - Function fnc = f; + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Division) { - if (fnc.getParameter2() instanceof Multiplication && fnc.getParameter2().getParameter1().equals(new Number(f.getMathContext(), -1))) { + if (fnc.getParameter2() instanceof Multiplication && ((FunctionOperator) fnc.getParameter2()).getParameter1().equals(new Number(f.getMathContext(), -1))) { isExecutable = true; } else if (fnc.getParameter2() instanceof Number) { - var n2 = fnc.getParameter2(); + Number n2 = (Number) fnc.getParameter2(); if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { isExecutable = true; } @@ -66,19 +69,19 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; - var a = (fnc.getParameter1()).getParameter1(); - var b = (fnc.getParameter1()).getParameter2(); - var c; + FunctionOperator fnc = (FunctionOperator) f; + Function a = ((FunctionOperator) fnc.getParameter1()).getParameter1(); + Function b = ((FunctionOperator) fnc.getParameter1()).getParameter2(); + Function c; if (fnc.getParameter2() instanceof Multiplication) { - c = fnc.getParameter2().getParameter2(); + c = ((FunctionOperator) fnc.getParameter2()).getParameter2(); } else { - c = fnc.getParameter2().multiply(new Number(root, "-1")); + c = ((Number) fnc.getParameter2()).multiply(new Number(root, "-1")); } - var dv = new Division(root, b, a); - var pow = new Power(root, dv, c); + Function dv = new Division(root, b, a); + Function pow = new Power(root, dv, c); result.add(pow); return result; } else { diff --git a/src/main/resources/rules/NumberRule1.java b/src/main/resources/rules/NumberRule1.java index 7401fc89..1e8bb7b8 100644 --- a/src/main/resources/rules/NumberRule1.java +++ b/src/main/resources/rules/NumberRule1.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=NumberRule1 */ import org.warp.picalculator.math.Function; @@ -16,6 +16,8 @@ import org.warp.picalculator.math.Function; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -26,7 +28,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NumberRule1 implements Rule { // Rule name @Override public String getRuleName() { @@ -49,16 +51,16 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Multiplication) { - var root = f.getMathContext(); - var mult = f; + MathContext root = f.getMathContext(); + FunctionOperator mult = (FunctionOperator) f; if (mult.getParameter1() instanceof Number) { - var numb = mult.getParameter1(); + Function numb = mult.getParameter1(); if (numb.equals(new Number(root, 0))) { isExecutable = true; } } if (mult.getParameter2() instanceof Number) { - var numb = mult.getParameter2(); + Function numb = mult.getParameter2(); if (numb.equals(new Number(root, 0))) { isExecutable = true; } @@ -67,7 +69,7 @@ public class __INSERT_CLASS_NAME__ implements Rule { if (isExecutable) { ObjectArrayList result = new ObjectArrayList<>(); - result.add(new Number(f.getMathContext(), "0")); + result.add(new Number(f.getMathContext(), 0)); return result; } else { return null; diff --git a/src/main/resources/rules/NumberRule2.java b/src/main/resources/rules/NumberRule2.java index e41ca65a..6020915f 100644 --- a/src/main/resources/rules/NumberRule2.java +++ b/src/main/resources/rules/NumberRule2.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=NumberRule2 */ import org.warp.picalculator.math.Function; @@ -16,6 +16,8 @@ import org.warp.picalculator.math.Function; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -26,7 +28,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NumberRule2 implements Rule { // Rule name @Override public String getRuleName() { @@ -49,16 +51,16 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Multiplication) { - var root = f.getMathContext(); - var mult = f; + MathContext root = f.getMathContext(); + Multiplication mult = (Multiplication) f; if (mult.getParameter1() instanceof Number) { - var numb = mult.getParameter1(); + Function numb = mult.getParameter1(); if (numb.equals(new Number(root, 1))) { isExecutable = true; } } if (mult.getParameter2() instanceof Number) { - var numb = mult.getParameter2(); + Function numb = mult.getParameter2(); if (numb.equals(new Number(root, 1))) { isExecutable = true; } @@ -66,20 +68,20 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - var a = null; - var aFound = false; - var mult = f; + Function a = null; + boolean aFound = false; + Multiplication mult = (Multiplication) f; if (aFound == false & mult.getParameter1() instanceof Number) { - var numb = mult.getParameter1(); + Function numb = mult.getParameter1(); if (numb.equals(new Number(root, 1))) { a = mult.getParameter2(); aFound = true; } } if (aFound == false && mult.getParameter2() instanceof Number) { - var numb = mult.getParameter2(); + Function numb = mult.getParameter2(); if (numb.equals(new Number(root, 1))) { a = mult.getParameter1(); aFound = true; diff --git a/src/main/resources/rules/NumberRule3.java b/src/main/resources/rules/NumberRule3.java index 0bf05e78..7800db09 100644 --- a/src/main/resources/rules/NumberRule3.java +++ b/src/main/resources/rules/NumberRule3.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=NumberRule3 */ import org.warp.picalculator.math.Function; @@ -20,6 +20,8 @@ import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -32,7 +34,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NumberRule3 implements Rule { // Rule name @Override public String getRuleName() { @@ -55,32 +57,32 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Subtraction) { - var sub = f; + FunctionOperator sub = (FunctionOperator) f; if (sub.getParameter1().equals(sub.getParameter2())) { isExecutable = true; } } else if (f instanceof Sum) { - var sub = f; + FunctionOperator sub = (FunctionOperator) f; if (sub.getParameter1() instanceof Multiplication) { - if (sub.getParameter1().getParameter1() instanceof Number && sub.getParameter1().getParameter1().equals(new Number(f.getMathContext(), -1))) { - var neg = sub.getParameter1().getParameter2(); + if (((FunctionOperator) sub.getParameter1()).getParameter1() instanceof Number && ((FunctionOperator) sub.getParameter1()).getParameter1().equals(new Number(f.getMathContext(), -1))) { + Function neg = ((FunctionOperator) sub.getParameter1()).getParameter2(); if (neg.equals(sub.getParameter2())) { isExecutable = true; } } } } else if (f instanceof SumSubtraction) { - var sub = f; + FunctionOperator sub = (FunctionOperator) f; if (sub.getParameter1().equals(sub.getParameter2())) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); if (f instanceof SumSubtraction) { - var mul = new Multiplication(root, new Number(root, 2), f.getParameter1()); + Function mul = new Multiplication(root, new Number(root, 2), ((FunctionOperator) f).getParameter1()); result.add(mul); } result.add(new Number(root, 0)); diff --git a/src/main/resources/rules/NumberRule4.java b/src/main/resources/rules/NumberRule4.java index cad25021..ad722b28 100644 --- a/src/main/resources/rules/NumberRule4.java +++ b/src/main/resources/rules/NumberRule4.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=NumberRule4 */ import org.warp.picalculator.math.Function; @@ -17,6 +17,8 @@ import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -27,7 +29,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NumberRule4 implements Rule { // Rule name @Override public String getRuleName() { @@ -54,9 +56,9 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - var ss = f; + FunctionOperator ss = (FunctionOperator) f; result.add(new Sum(root, ss.getParameter1(), ss.getParameter2())); result.add(new Subtraction(root, ss.getParameter1(), ss.getParameter2())); return result; diff --git a/src/main/resources/rules/NumberRule5.java b/src/main/resources/rules/NumberRule5.java index eec39bd0..66aaab42 100644 --- a/src/main/resources/rules/NumberRule5.java +++ b/src/main/resources/rules/NumberRule5.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=NumberRule5 */ import org.warp.picalculator.math.Function; @@ -15,7 +15,13 @@ import org.warp.picalculator.Error; import org.warp.picalculator.math.Function; import org.warp.picalculator.math.FunctionOperator; import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -30,7 +36,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NumberRule5 implements Rule { // Rule name @Override public String getRuleName() { @@ -53,8 +59,8 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Sum || f instanceof Subtraction || f instanceof SumSubtraction) { - var root = f.getMathContext(); - Function fnc = f; + MathContext root = f.getMathContext(); + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1().equals(new Number(root, 0)) || fnc.getParameter2().equals(new Number(root, 0))) { if (!(fnc.getParameter1().equals(new Number(root, 0)) && f instanceof SumSubtraction)) { isExecutable = true; @@ -63,10 +69,10 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - Function fnc = f; - var a = fnc.getParameter1(); + FunctionOperator fnc = (FunctionOperator) f; + Function a = fnc.getParameter1(); if (a.equals(new Number(root, 0))) { if (f instanceof Subtraction) { a = new Multiplication(root, new Number(root, -1), fnc.getParameter2()); diff --git a/src/main/resources/rules/NumberRule7.java b/src/main/resources/rules/NumberRule7.java index 8f4acecb..74b2be67 100644 --- a/src/main/resources/rules/NumberRule7.java +++ b/src/main/resources/rules/NumberRule7.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=NumberRule7 */ import org.warp.picalculator.math.Function; @@ -17,6 +17,8 @@ import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -27,7 +29,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NumberRule7 implements Rule { // Rule name @Override public String getRuleName() { @@ -50,13 +52,13 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Sum) { - isExecutable = f.getParameter1().equals(f.getParameter2()); + isExecutable = ((FunctionOperator) f).getParameter1().equals(((FunctionOperator) f).getParameter2()); } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - var mult = new Multiplication(root, new Number(root, 2), f.getParameter1()); + Function mult = new Multiplication(root, new Number(root, 2), ((FunctionOperator) f).getParameter1()); result.add(mult); return result; } else { diff --git a/src/main/resources/rules/UndefinedRule1.java b/src/main/resources/rules/UndefinedRule1.java index 9144788a..29f27e52 100644 --- a/src/main/resources/rules/UndefinedRule1.java +++ b/src/main/resources/rules/UndefinedRule1.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=UndefinedRule1 */ import org.warp.picalculator.math.Function; @@ -17,6 +17,8 @@ import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; import org.warp.picalculator.math.functions.Undefined; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -27,7 +29,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class UndefinedRule1 implements Rule { // Rule name @Override public String getRuleName() { @@ -50,15 +52,15 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Power) { - var root = f.getMathContext(); - Function fnc = f; + MathContext root = f.getMathContext(); + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1().equals(new Number(root, 0)) && fnc.getParameter2().equals(new Number(root, 0))) { isExecutable = true; } } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); result.add(new Undefined(root)); return result; diff --git a/src/main/resources/rules/UndefinedRule2.java b/src/main/resources/rules/UndefinedRule2.java index 57e3ee57..5e61c2e9 100644 --- a/src/main/resources/rules/UndefinedRule2.java +++ b/src/main/resources/rules/UndefinedRule2.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=UndefinedRule2 */ import org.warp.picalculator.math.Function; @@ -17,6 +17,8 @@ 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.Undefined; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -27,7 +29,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class UndefinedRule2 implements Rule { // Rule name @Override public String getRuleName() { @@ -50,10 +52,10 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { boolean isExecutable = false; if (f instanceof Division) { - var root = f.getMathContext(); - Function fnc = f; + MathContext root = f.getMathContext(); + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter2() instanceof Number) { - var numb = fnc.getParameter2(); + Number numb = (Number) fnc.getParameter2(); if (numb.equals(new Number(root, 0))) { isExecutable = true; } @@ -61,7 +63,7 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = f.getMathContext(); + MathContext root = f.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); result.add(new Undefined(root)); return result; diff --git a/src/main/resources/rules/VariableRule1.java b/src/main/resources/rules/VariableRule1.java index 065473f8..2729978e 100644 --- a/src/main/resources/rules/VariableRule1.java +++ b/src/main/resources/rules/VariableRule1.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=VariableRule1 */ import org.warp.picalculator.math.Function; @@ -18,6 +18,8 @@ import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -28,7 +30,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class VariableRule1 implements Rule { // Rule name @Override public String getRuleName() { @@ -50,11 +52,11 @@ public class __INSERT_CLASS_NAME__ implements Rule { @Override public ObjectArrayList execute(Function f) { boolean isExecutable = false; - Function fnc = f; if (f instanceof Subtraction || f instanceof Sum) { + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Multiplication & fnc.getParameter2() instanceof Multiplication) { - var m1 = fnc.getParameter1(); - var m2 = fnc.getParameter2(); + FunctionOperator m1 = (FunctionOperator) fnc.getParameter1(); + FunctionOperator m2 = (FunctionOperator) fnc.getParameter2(); if (m1.getParameter1().equals(m2.getParameter1()) || m1.getParameter2().equals(m2.getParameter2())) { isExecutable = true; } @@ -62,13 +64,14 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = fnc.getMathContext(); + FunctionOperator fnc = (FunctionOperator) f; + MathContext root = fnc.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - var m1 = fnc.getParameter1(); - var m2 = fnc.getParameter2(); - var a; - var b; - var x; + FunctionOperator m1 = (FunctionOperator) fnc.getParameter1(); + FunctionOperator m2 = (FunctionOperator) fnc.getParameter2(); + Function a; + Function b; + Function x; if (m1.getParameter2().equals(m2.getParameter2())) { x = m1.getParameter2(); a = m1.getParameter1(); @@ -79,13 +82,13 @@ public class __INSERT_CLASS_NAME__ implements Rule { b = m2.getParameter2(); } - var rets; + Function rets; if (fnc instanceof Sum) { rets = new Sum(root, a, b); } else { rets = new Subtraction(root, a, b); } - var retm = new Multiplication(root, rets, x); + Function retm = new Multiplication(root, rets, x); result.add(retm); return result; } else { diff --git a/src/main/resources/rules/VariableRule2.java b/src/main/resources/rules/VariableRule2.java index 106d6f04..838b0b3f 100644 --- a/src/main/resources/rules/VariableRule2.java +++ b/src/main/resources/rules/VariableRule2.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=VariableRule2 */ import org.warp.picalculator.math.Function; @@ -19,6 +19,8 @@ import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -29,7 +31,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class VariableRule2 implements Rule { // Rule name @Override public String getRuleName() { @@ -51,10 +53,10 @@ public class __INSERT_CLASS_NAME__ implements Rule { @Override public ObjectArrayList execute(Function f) { boolean isExecutable = false; - Function fnc = f; if (f instanceof Sum || f instanceof Subtraction) { + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter1() instanceof Multiplication) { - var m1 = fnc.getParameter1(); + FunctionOperator m1 = (FunctionOperator) fnc.getParameter1(); if (m1.getParameter2().equals(fnc.getParameter2())) { isExecutable = true; } @@ -62,19 +64,20 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = fnc.getMathContext(); + FunctionOperator fnc = (FunctionOperator) f; + MathContext root = fnc.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - var m1 = fnc.getParameter1(); - var a = m1.getParameter1(); - var x = fnc.getParameter2(); + FunctionOperator m1 = (FunctionOperator) fnc.getParameter1(); + Function a = m1.getParameter1(); + Function x = fnc.getParameter2(); - var rets; + Function rets; if (fnc instanceof Sum) { rets = new Sum(root, a, new Number(root, 1)); } else { rets = new Subtraction(root, a, new Number(root, 1)); } - var retm = new Multiplication(root, rets, x); + Function retm = new Multiplication(root, rets, x); result.add(retm); return result; } else { diff --git a/src/main/resources/rules/VariableRule3.java b/src/main/resources/rules/VariableRule3.java index 8ca88e1c..60557558 100644 --- a/src/main/resources/rules/VariableRule3.java +++ b/src/main/resources/rules/VariableRule3.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=VariableRule3 */ import org.warp.picalculator.math.Function; @@ -19,6 +19,8 @@ import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -29,7 +31,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class VariableRule3 implements Rule { // Rule name @Override public String getRuleName() { @@ -51,10 +53,10 @@ public class __INSERT_CLASS_NAME__ implements Rule { @Override public ObjectArrayList execute(Function f) { boolean isExecutable = false; - Function fnc = f; if (f instanceof Sum || f instanceof Subtraction) { + FunctionOperator fnc = (FunctionOperator) f; if (fnc.getParameter2() instanceof Multiplication) { - var m2 = fnc.getParameter2(); + FunctionOperator m2 = (FunctionOperator) fnc.getParameter2(); if (m2.getParameter2().equals(fnc.getParameter1())) { isExecutable = true; } @@ -62,20 +64,21 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isExecutable) { - var root = fnc.getMathContext(); + FunctionOperator fnc = (FunctionOperator) f; + MathContext root = fnc.getMathContext(); ObjectArrayList result = new ObjectArrayList<>(); - var m2 = fnc.getParameter2(); - var a = m2.getParameter1(); - var x = fnc.getParameter1(); + FunctionOperator m2 = (FunctionOperator) fnc.getParameter2(); + Function a = m2.getParameter1(); + Function x = fnc.getParameter1(); - var rets; + Function rets; if (fnc instanceof Sum) { rets = new Sum(root, new Number(root, 1), a); } else { rets = new Subtraction(root, new Number(root, 1), a); } - var retm = new Multiplication(root, rets, x); + Function retm = new Multiplication(root, rets, x); result.add(retm); return result; } else { diff --git a/src/main/resources/rules/functions/Division.java b/src/main/resources/rules/functions/DivisionRule.java similarity index 64% rename from src/main/resources/rules/functions/Division.java rename to src/main/resources/rules/functions/DivisionRule.java index 820a6363..97c87056 100644 --- a/src/main/resources/rules/functions/Division.java +++ b/src/main/resources/rules/functions/DivisionRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.DivisionRule */ import org.warp.picalculator.math.Function; @@ -10,16 +10,21 @@ import org.warp.picalculator.math.FunctionSingle; import org.warp.picalculator.math.MathContext; import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +import java.math.BigInteger; +import java.util.LinkedList; + import org.warp.picalculator.ScriptUtils; import org.warp.picalculator.math.rules.Rule; import org.warp.picalculator.math.rules.RuleType; import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Sum; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.SumSubtraction; -import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.Error; /** * Division @@ -28,7 +33,7 @@ import org.warp.picalculator.math.functions.Division; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class DivisionRule implements Rule { // Rule name @Override public String getRuleName() { @@ -47,38 +52,38 @@ public class __INSERT_CLASS_NAME__ implements Rule { - An ObjectArrayList if it did something */ @Override - public ObjectArrayList execute(Function f) { - if (f instanceof Division) { + public ObjectArrayList execute(Function f) throws Error { + if (f instanceof DivisionRule) { ObjectArrayList result = new ObjectArrayList<>(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var mathContext = f.getMathContext(); + Function variable1 = ((FunctionOperator) f).getParameter1(); + Function variable2 = ((FunctionOperator) f).getParameter2(); + MathContext mathContext = f.getMathContext(); if (variable1 instanceof Number && variable2 instanceof Number) { if (mathContext.exactMode) { - if (variable1.isInteger() && variable2.isInteger()) { - var factors1, factors2, mcm; + if (((Number)variable1).isInteger() && ((Number)variable2).isInteger()) { + LinkedList factors1, factors2, mcm; try { - factors1 = variable1.getFactors(); - factors2 = variable2.getFactors(); + factors1 = ((Number)variable1).getFactors(); + factors2 = ((Number)variable2).getFactors(); mcm = ScriptUtils.mcm(factors1, factors2); - } catch (error) { + } catch (Exception ex) { return null; } if (mcm.size() > 0) { //true if there is at least one common factor //divide by the common factor (ab/cb = a/c) - var nmb1 = variable1.term.toBigIntegerExact(); - var nmb2 = variable2.term.toBigIntegerExact(); - mcm.forEach(function(integerNumber) { + BigInteger nmb1 = ((Number)variable1).getTerm().toBigIntegerExact(); + BigInteger nmb2 = ((Number)variable2).getTerm().toBigIntegerExact(); + for (BigInteger integerNumber : mcm) { nmb1 = nmb1.divide(integerNumber); nmb2 = nmb2.divide(integerNumber); - }); + } result.add(new Division(mathContext, new Number(mathContext, nmb1), new Number(mathContext, nmb2))); return result; } } } else { //divide a by b (a/b = c) - result.add(variable1.divide(variable2)); + result.add(((Number)variable1).divide((Number)variable2)); return result; } } diff --git a/src/main/resources/rules/functions/EmptyNumber.java b/src/main/resources/rules/functions/EmptyNumberRule.java similarity index 87% rename from src/main/resources/rules/functions/EmptyNumber.java rename to src/main/resources/rules/functions/EmptyNumberRule.java index 78660d4f..cc16c080 100644 --- a/src/main/resources/rules/functions/EmptyNumber.java +++ b/src/main/resources/rules/functions/EmptyNumberRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.EmptyNumberRule */ import org.warp.picalculator.math.Function; @@ -22,7 +22,7 @@ import org.warp.picalculator.math.rules.RulesManager; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class EmptyNumberRule implements Rule { // Rule name @Override public String getRuleName() { diff --git a/src/main/resources/rules/functions/Expression.java b/src/main/resources/rules/functions/ExpressionRule.java similarity index 90% rename from src/main/resources/rules/functions/Expression.java rename to src/main/resources/rules/functions/ExpressionRule.java index f2959bfb..77e4e23a 100644 --- a/src/main/resources/rules/functions/Expression.java +++ b/src/main/resources/rules/functions/ExpressionRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.ExpressionRule */ import org.warp.picalculator.math.Function; @@ -28,7 +28,7 @@ import org.warp.picalculator.math.functions.Expression; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class ExpressionRule implements Rule { // Rule name @Override public String getRuleName() { diff --git a/src/main/resources/rules/functions/Joke.java b/src/main/resources/rules/functions/JokeRule.java similarity index 87% rename from src/main/resources/rules/functions/Joke.java rename to src/main/resources/rules/functions/JokeRule.java index d3523fef..8fd724a1 100644 --- a/src/main/resources/rules/functions/Joke.java +++ b/src/main/resources/rules/functions/JokeRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.JokeRule */ import org.warp.picalculator.math.Function; @@ -22,7 +22,7 @@ import org.warp.picalculator.math.rules.RulesManager; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class JokeRule implements Rule { // Rule name @Override public String getRuleName() { diff --git a/src/main/resources/rules/functions/Multiplication.java b/src/main/resources/rules/functions/MultiplicationRule.java similarity index 80% rename from src/main/resources/rules/functions/Multiplication.java rename to src/main/resources/rules/functions/MultiplicationRule.java index 72520608..5c584a88 100644 --- a/src/main/resources/rules/functions/Multiplication.java +++ b/src/main/resources/rules/functions/MultiplicationRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.MultiplicationRule */ import org.warp.picalculator.math.Function; @@ -28,7 +28,7 @@ import org.warp.picalculator.math.functions.Division; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class MultiplicationRule implements Rule { // Rule name @Override public String getRuleName() { @@ -50,12 +50,12 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { if (f instanceof Multiplication) { ObjectArrayList result = new ObjectArrayList<>(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var mathContext = f.getMathContext(); + Function variable1 = ((Multiplication) f).getParameter1(); + Function variable2 = ((Multiplication) f).getParameter2(); + MathContext mathContext = f.getMathContext(); if (variable1 instanceof Number && variable2 instanceof Number) { //multiply a by b (a*b = c) - result.add(variable1.multiply(variable2)); + result.add(((Number)variable1).multiply((Number)variable2)); return result; } } diff --git a/src/main/resources/rules/functions/Negative.java b/src/main/resources/rules/functions/NegativeRule.java similarity index 80% rename from src/main/resources/rules/functions/Negative.java rename to src/main/resources/rules/functions/NegativeRule.java index b923a718..a0276af2 100644 --- a/src/main/resources/rules/functions/Negative.java +++ b/src/main/resources/rules/functions/NegativeRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.NegativeRule */ import org.warp.picalculator.math.Function; @@ -29,7 +29,7 @@ import java.lang.ArithmeticException; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class NegativeRule implements Rule { // Rule name @Override public String getRuleName() { @@ -48,16 +48,16 @@ public class __INSERT_CLASS_NAME__ implements Rule { - An ObjectArrayList if it did something */ @Override - public ObjectArrayList execute(Function f) { + public ObjectArrayList execute(Function f) throws Error { if (f instanceof Negative) { ObjectArrayList result = new ObjectArrayList<>(); - var variable = f.getParameter(); - var mathContext = f.getMathContext(); + Function variable = ((Negative)f).getParameter(); + MathContext mathContext = f.getMathContext(); if (variable instanceof Number) { //-a = a*-1 = b try { - result.add(variable.multiply(new Number(mathContext, -1))); - } catch (ex) { + result.add(((Number)variable).multiply(new Number(mathContext, -1))); + } catch (Exception ex) { if (ex instanceof NullPointerException) { throw new Error(Errors.ERROR); } else if (ex instanceof NumberFormatException) { diff --git a/src/main/resources/rules/functions/Number.java b/src/main/resources/rules/functions/Number.java deleted file mode 100644 index b2c2bdfe..00000000 --- a/src/main/resources/rules/functions/Number.java +++ /dev/null @@ -1,49 +0,0 @@ -// 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 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)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/NumberRule.java b/src/main/resources/rules/functions/NumberRule.java new file mode 100644 index 00000000..2f262ca7 --- /dev/null +++ b/src/main/resources/rules/functions/NumberRule.java @@ -0,0 +1,63 @@ +/* +SETTINGS: (please don't move this part) + PATH=functions.NumberRule +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; +import java.math.BigInteger; + +/** + * Number + * + * + * @author Andrea Cavalli + * + */ +public class NumberRule implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Number"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Number) { + ObjectArrayList result = new ObjectArrayList<>(); + MathContext mathContext = f.getMathContext(); + if (mathContext.exactMode) { + if (((Number)f).isInteger() == false) { + Number divisor = new Number(mathContext, BigInteger.TEN.pow(((Number)f).getNumberOfDecimalPlaces())); + Function number = new Number(mathContext, ((Number)f).getTerm().multiply(divisor.getTerm())); + Function div = new Division(mathContext, number, divisor); + result.add(div); + return result; + } + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Power.java b/src/main/resources/rules/functions/Power.java deleted file mode 100644 index c7feccef..00000000 --- a/src/main/resources/rules/functions/Power.java +++ /dev/null @@ -1,53 +0,0 @@ -// 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 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)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/PowerRule.java b/src/main/resources/rules/functions/PowerRule.java new file mode 100644 index 00000000..11c3bf7f --- /dev/null +++ b/src/main/resources/rules/functions/PowerRule.java @@ -0,0 +1,65 @@ +/* +SETTINGS: (please don't move this part) + PATH=functions.PowerRule +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Power; + +/** + * Power + * a^b = c + * + * @author Andrea Cavalli + * + */ +public class PowerRule implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Power"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) throws org.warp.picalculator.Error, InterruptedException { + if (f instanceof Power) { + ObjectArrayList result = new ObjectArrayList<>(); + Function variable1 = ((FunctionOperator)f).getParameter1(); + Function variable2 = ((FunctionOperator)f).getParameter2(); + MathContext mathContext = f.getMathContext(); + if (variable1 instanceof Number && variable2 instanceof Number) { + //a^b = c + result.add(((Number)variable1).pow((Number)variable2)); + return result; + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Root.java b/src/main/resources/rules/functions/RootRule.java similarity index 61% rename from src/main/resources/rules/functions/Root.java rename to src/main/resources/rules/functions/RootRule.java index 3455b931..243c817a 100644 --- a/src/main/resources/rules/functions/Root.java +++ b/src/main/resources/rules/functions/RootRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.RootRule */ import org.warp.picalculator.math.Function; @@ -10,6 +10,9 @@ import org.warp.picalculator.math.FunctionSingle; import org.warp.picalculator.math.MathContext; import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +import org.warp.picalculator.Error; +import org.warp.picalculator.Errors; import org.warp.picalculator.ScriptUtils; import org.warp.picalculator.math.rules.Rule; import org.warp.picalculator.math.rules.RuleType; @@ -23,6 +26,7 @@ import org.warp.picalculator.math.functions.Division; import org.warp.picalculator.math.functions.Root; import org.warp.picalculator.math.functions.RootSquare; import java.math.BigDecimal; +import java.math.BigInteger; /** * Root @@ -31,7 +35,7 @@ import java.math.BigDecimal; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class RootRule implements Rule { // Rule name @Override public String getRuleName() { @@ -50,26 +54,29 @@ public class __INSERT_CLASS_NAME__ implements Rule { - An ObjectArrayList if it did something */ @Override - public ObjectArrayList execute(Function f) { - var isSquare = false; + public ObjectArrayList execute(Function f) throws Error, InterruptedException { + boolean isSquare = false; if ((isSquare = f instanceof RootSquare) || f instanceof Root) { ObjectArrayList result = new ObjectArrayList<>(); - var mathContext = f.getMathContext(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var isSolvable = false; - var canBePorted = false; + MathContext mathContext = f.getMathContext(); + Function variable1 = ((FunctionOperator)f).getParameter1(); + Function variable2 = ((FunctionOperator)f).getParameter2(); + boolean isSolvable = false, canBePorted = false; if (variable1 instanceof Number && variable2 instanceof Number) { + if (mathContext.exactMode) { + result.add(((Number)variable1).pow(new Number(mathContext, BigDecimal.ONE).divide(((Number)variable1)))); + return result; + } 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(f.getParameter2())) { + Function resultVar = ((Number)variable2).pow(new Number(mathContext, BigDecimal.ONE).divide(((Number)variable1))); + Function originalVariable = ((Number)resultVar).pow(new Number(mathContext, 2)); + if ((originalVariable).equals(((FunctionOperator)f).getParameter2())) { isSolvable = true; } - } catch (ex) { - ex.printStackTrace(); + } catch (Exception ex) { + throw (Error) new Error(Errors.ERROR, ex.getMessage()).initCause(ex); } } } @@ -78,7 +85,7 @@ public class __INSERT_CLASS_NAME__ implements Rule { } if (isSolvable) { - result.add(variable2.pow(new Number(mathContext, BigInteger.ONE).divide(variable1))); + result.add(((Number)variable2).pow(new Number(mathContext, BigInteger.ONE).divide((Number)variable1))); return result; } if (canBePorted) { diff --git a/src/main/resources/rules/functions/Subtraction.java b/src/main/resources/rules/functions/SubtractionRule.java similarity index 78% rename from src/main/resources/rules/functions/Subtraction.java rename to src/main/resources/rules/functions/SubtractionRule.java index e72462e0..483f332a 100644 --- a/src/main/resources/rules/functions/Subtraction.java +++ b/src/main/resources/rules/functions/SubtractionRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.SubtractionRule */ import org.warp.picalculator.math.Function; @@ -28,7 +28,7 @@ import org.warp.picalculator.math.functions.Division; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class SubtractionRule implements Rule { // Rule name @Override public String getRuleName() { @@ -50,12 +50,12 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { if (f instanceof Subtraction) { ObjectArrayList result = new ObjectArrayList<>(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var mathContext = f.getMathContext(); + Function variable1 = ((FunctionOperator)f).getParameter1(); + Function variable2 = ((FunctionOperator)f).getParameter2(); + MathContext mathContext = f.getMathContext(); if (variable1 instanceof Number && variable2 instanceof Number) { //a-b = a+(b*-1) = c - result.add(variable1.add(variable2.multiply(new Number(mathContext, -1)))); + result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(mathContext, -1)))); return result; } } diff --git a/src/main/resources/rules/functions/Sum.java b/src/main/resources/rules/functions/SumRule.java similarity index 80% rename from src/main/resources/rules/functions/Sum.java rename to src/main/resources/rules/functions/SumRule.java index af68a09d..6a97c737 100644 --- a/src/main/resources/rules/functions/Sum.java +++ b/src/main/resources/rules/functions/SumRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.SumRule */ import org.warp.picalculator.math.Function; @@ -28,7 +28,7 @@ import org.warp.picalculator.math.functions.Division; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class SumRule implements Rule { // Rule name @Override public String getRuleName() { @@ -50,12 +50,12 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { if (f instanceof Sum) { ObjectArrayList result = new ObjectArrayList<>(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var mathContext = f.getMathContext(); + Function variable1 = ((FunctionOperator)f).getParameter1(); + Function variable2 = ((FunctionOperator)f).getParameter2(); + MathContext mathContext = f.getMathContext(); if (variable1 instanceof Number && variable2 instanceof Number) { //a+b = c - result.add(variable1.add(variable2)); + result.add(((Number)variable1).add(((Number)variable2))); return result; } } diff --git a/src/main/resources/rules/functions/SumSubtraction.java b/src/main/resources/rules/functions/SumSubtractionRule.java similarity index 76% rename from src/main/resources/rules/functions/SumSubtraction.java rename to src/main/resources/rules/functions/SumSubtractionRule.java index 4deaed00..7c78a526 100644 --- a/src/main/resources/rules/functions/SumSubtraction.java +++ b/src/main/resources/rules/functions/SumSubtractionRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.SumSubtractionRule */ import org.warp.picalculator.math.Function; @@ -28,7 +28,7 @@ import org.warp.picalculator.math.functions.Division; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class SumSubtractionRule implements Rule { // Rule name @Override public String getRuleName() { @@ -50,13 +50,13 @@ public class __INSERT_CLASS_NAME__ implements Rule { public ObjectArrayList execute(Function f) { if (f instanceof SumSubtraction) { ObjectArrayList result = new ObjectArrayList<>(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var mathContext = f.getMathContext(); + Function variable1 = ((FunctionOperator)f).getParameter1(); + Function variable2 = ((FunctionOperator)f).getParameter2(); + MathContext mathContext = f.getMathContext(); if (variable1 instanceof Number && variable2 instanceof Number) { //a±b = c, d - result.add(variable1.add(variable2)); - result.add(variable1.add(variable2.multiply(new Number(mathContext, -1)))); + result.add(((Number)variable1).add((Number)variable2)); + result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(mathContext, -1)))); return result; } } diff --git a/src/main/resources/rules/functions/Variable.java b/src/main/resources/rules/functions/VariableRule.java similarity index 83% rename from src/main/resources/rules/functions/Variable.java rename to src/main/resources/rules/functions/VariableRule.java index 202b8a12..8172b46c 100644 --- a/src/main/resources/rules/functions/Variable.java +++ b/src/main/resources/rules/functions/VariableRule.java @@ -1,6 +1,6 @@ /* SETTINGS: (please don't move this part) - PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ + PATH=functions.VariableRule */ import org.warp.picalculator.math.Function; @@ -13,6 +13,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; import org.nevec.rjm.BigDecimalMath; import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.Error; import org.warp.picalculator.ScriptUtils; import org.warp.picalculator.math.rules.Rule; import org.warp.picalculator.math.rules.RuleType; @@ -30,7 +31,7 @@ import org.warp.picalculator.math.functions.Number; * @author Andrea Cavalli * */ -public class __INSERT_CLASS_NAME__ implements Rule { +public class VariableRule implements Rule { // Rule name @Override public String getRuleName() { @@ -49,11 +50,11 @@ public class __INSERT_CLASS_NAME__ implements Rule { - An ObjectArrayList if it did something */ @Override - public ObjectArrayList execute(Function f) { + public ObjectArrayList execute(Function f) throws Error { if (f instanceof Variable) { ObjectArrayList result = new ObjectArrayList<>(); - var variable = f.getChar(); - var mathContext = f.getMathContext(); + Character variable = ((Variable)f).getChar(); + MathContext mathContext = f.getMathContext(); if (mathContext.exactMode == false) { if (variable.equals(MathematicalSymbols.PI)) { //a = n