From 601a275277f040705caf2e0630bafaa5135b15eb Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Thu, 22 Mar 2018 08:34:19 +0100 Subject: [PATCH 1/3] Removed unused imports --- .../java/org/warp/picalculator/math/functions/Variable.java | 5 ----- 1 file changed, 5 deletions(-) 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 908a9218..14cda652 100755 --- a/src/main/java/org/warp/picalculator/math/functions/Variable.java +++ b/src/main/java/org/warp/picalculator/math/functions/Variable.java @@ -1,15 +1,10 @@ package org.warp.picalculator.math.functions; -import java.util.List; - -import org.nevec.rjm.BigDecimalMath; import org.warp.picalculator.Error; import org.warp.picalculator.gui.expression.blocks.Block; import org.warp.picalculator.gui.expression.blocks.BlockChar; import org.warp.picalculator.math.Function; import org.warp.picalculator.math.MathContext; -import org.warp.picalculator.Utils; -import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.rules.Rule; import it.unimi.dsi.fastutil.objects.ObjectArrayList; From 4a53bb68b48facb762b33919ee8151a4ad6638d3 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Thu, 22 Mar 2018 10:06:31 +0100 Subject: [PATCH 2/3] New java rules --- .settings/org.eclipse.jdt.core.prefs | 3 + functions_Division.ser | Bin 0 -> 1106 bytes pom.xml | 48 ++++++- .../java/org/warp/picalculator/Utils.java | 15 ++ .../picalculator/math/rules/RulesManager.java | 85 ++++++++++-- src/main/resources/math-rules.csv | 2 + src/main/resources/rules.csv | 44 ------ src/main/resources/rules/ExpandRule1.js | 128 ------------------ src/main/resources/rules/ExpandRule2.js | 127 ----------------- src/main/resources/rules/ExpandRule5.js | 60 -------- src/main/resources/rules/ExponentRule1.js | 55 -------- src/main/resources/rules/ExponentRule15.js | 60 -------- src/main/resources/rules/ExponentRule16.js | 68 ---------- src/main/resources/rules/ExponentRule17.js | 61 --------- src/main/resources/rules/ExponentRule2.js | 53 -------- src/main/resources/rules/ExponentRule3.js | 53 -------- src/main/resources/rules/ExponentRule4.js | 65 --------- src/main/resources/rules/ExponentRule9.js | 58 -------- src/main/resources/rules/FractionsRule1.js | 65 --------- src/main/resources/rules/FractionsRule11.js | 71 ---------- src/main/resources/rules/FractionsRule12.js | 68 ---------- src/main/resources/rules/FractionsRule14.js | 104 -------------- src/main/resources/rules/FractionsRule2.java | 64 +++++++++ src/main/resources/rules/FractionsRule2.js | 57 -------- src/main/resources/rules/FractionsRule3.js | 53 -------- src/main/resources/rules/FractionsRule4.js | 61 --------- src/main/resources/rules/FractionsRule5.js | 76 ----------- src/main/resources/rules/NumberRule1.js | 64 --------- src/main/resources/rules/NumberRule2.js | 83 ------------ src/main/resources/rules/NumberRule3.js | 80 ----------- src/main/resources/rules/NumberRule4.js | 55 -------- src/main/resources/rules/NumberRule5.js | 71 ---------- src/main/resources/rules/NumberRule7.js | 54 -------- src/main/resources/rules/UndefinedRule1.js | 57 -------- src/main/resources/rules/UndefinedRule2.js | 60 -------- src/main/resources/rules/VariableRule1.js | 83 ------------ src/main/resources/rules/VariableRule2.js | 72 ---------- src/main/resources/rules/VariableRule3.js | 73 ---------- src/main/resources/rules/_EXAMPLE RULE.js | 108 --------------- .../resources/rules/functions/Division.js | 76 ----------- .../resources/rules/functions/EmptyNumber.js | 35 ----- .../resources/rules/functions/Expression.js | 46 ------- src/main/resources/rules/functions/Joke.js | 35 ----- .../rules/functions/Multiplication.js | 52 ------- .../resources/rules/functions/Negative.js | 62 --------- src/main/resources/rules/functions/Number.js | 49 ------- src/main/resources/rules/functions/Power.js | 53 -------- src/main/resources/rules/functions/Root.js | 78 ----------- .../resources/rules/functions/Subtraction.js | 52 ------- src/main/resources/rules/functions/Sum.js | 52 ------- .../rules/functions/SumSubtraction.js | 53 -------- .../resources/rules/functions/Variable.js | 55 -------- 52 files changed, 202 insertions(+), 2960 deletions(-) create mode 100644 functions_Division.ser create mode 100644 src/main/resources/math-rules.csv delete mode 100644 src/main/resources/rules.csv delete mode 100644 src/main/resources/rules/ExpandRule1.js delete mode 100644 src/main/resources/rules/ExpandRule2.js delete mode 100644 src/main/resources/rules/ExpandRule5.js delete mode 100644 src/main/resources/rules/ExponentRule1.js delete mode 100644 src/main/resources/rules/ExponentRule15.js delete mode 100644 src/main/resources/rules/ExponentRule16.js delete mode 100644 src/main/resources/rules/ExponentRule17.js delete mode 100644 src/main/resources/rules/ExponentRule2.js delete mode 100644 src/main/resources/rules/ExponentRule3.js delete mode 100644 src/main/resources/rules/ExponentRule4.js delete mode 100644 src/main/resources/rules/ExponentRule9.js delete mode 100644 src/main/resources/rules/FractionsRule1.js delete mode 100644 src/main/resources/rules/FractionsRule11.js delete mode 100644 src/main/resources/rules/FractionsRule12.js delete mode 100644 src/main/resources/rules/FractionsRule14.js create mode 100644 src/main/resources/rules/FractionsRule2.java delete mode 100644 src/main/resources/rules/FractionsRule2.js delete mode 100644 src/main/resources/rules/FractionsRule3.js delete mode 100644 src/main/resources/rules/FractionsRule4.js delete mode 100644 src/main/resources/rules/FractionsRule5.js delete mode 100644 src/main/resources/rules/NumberRule1.js delete mode 100644 src/main/resources/rules/NumberRule2.js delete mode 100644 src/main/resources/rules/NumberRule3.js delete mode 100644 src/main/resources/rules/NumberRule4.js delete mode 100644 src/main/resources/rules/NumberRule5.js delete mode 100644 src/main/resources/rules/NumberRule7.js delete mode 100644 src/main/resources/rules/UndefinedRule1.js delete mode 100644 src/main/resources/rules/UndefinedRule2.js delete mode 100644 src/main/resources/rules/VariableRule1.js delete mode 100644 src/main/resources/rules/VariableRule2.js delete mode 100644 src/main/resources/rules/VariableRule3.js delete mode 100644 src/main/resources/rules/_EXAMPLE RULE.js delete mode 100644 src/main/resources/rules/functions/Division.js delete mode 100644 src/main/resources/rules/functions/EmptyNumber.js delete mode 100644 src/main/resources/rules/functions/Expression.js delete mode 100644 src/main/resources/rules/functions/Joke.js delete mode 100644 src/main/resources/rules/functions/Multiplication.js delete mode 100644 src/main/resources/rules/functions/Negative.js delete mode 100644 src/main/resources/rules/functions/Number.js delete mode 100644 src/main/resources/rules/functions/Power.js delete mode 100644 src/main/resources/rules/functions/Root.js delete mode 100644 src/main/resources/rules/functions/Subtraction.js delete mode 100644 src/main/resources/rules/functions/Sum.js delete mode 100644 src/main/resources/rules/functions/SumSubtraction.js delete mode 100644 src/main/resources/rules/functions/Variable.js diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 25af46fa..96cf561c 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,3 +1,6 @@ eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter diff --git a/functions_Division.ser b/functions_Division.ser new file mode 100644 index 0000000000000000000000000000000000000000..5cacce7b3f3143a59a16cb63d93641a39eb31047 GIT binary patch literal 1106 zcmah|&r20i82x-cO<$R2C249AY7uo`5eD)^C@~PmC-kh15L{=j?_Is~qkF&cO$K3$ z+SR_GzreOZ`+hClM9?zY1Z@i1wMeOZXPkL25}L*QxaWN5p839WpMQt8yGp{z6}mGMk~%*F~zM%|3-y!x{GXyVuB zgnKyU&dz5Hv7sC``eU=1A*fd+8RPxViK&6o;Ol=F3pMH2jkS+&$4^hxUv9NR-hwup zhKeB^wajisqgBk9B@4P7hLnprrWCDeUT+gmyXRiDvGz&<_6<8iz9E})%no{nAq^dJZM zSttm-OF49xn6%)a!)!rKAEpIW$~bPfpktW}R;Di7{Vuo+Yrd1MM8R);Aq=D=PU0{h zVi4|zB0xA$mmFCFU5KT-OqtQ_*6{u&I6?KFtRbBDWtG&a3`oFjD(p~D^d)gA)`$!R zQ{=k2&rwfLR3aQ|{v^f`LVBL+CV*w4N>_le+N|v-)9U{Fu4* zBeYMN9i}p{w*i{B!3@}J00?Y(Inu(k)bn~`5E)aW(H}}fb?pEE literal 0 HcmV?d00001 diff --git a/pom.xml b/pom.xml index 8ee94dcf..8d7fc06d 100755 --- a/pom.xml +++ b/pom.xml @@ -18,13 +18,37 @@ + - - junit - junit - 4.12 - test - + it.unimi.dsi fastutil @@ -55,6 +79,18 @@ pngj 2.1.0 + + net.openhft + compiler + 2.3.0 + + + jdk.tools + jdk.tools + jdk1.8.0 + system + ${project.basedir}/libs/tools.jar + 1.8 diff --git a/src/main/java/org/warp/picalculator/Utils.java b/src/main/java/org/warp/picalculator/Utils.java index 943f28b7..e2bb38e8 100755 --- a/src/main/java/org/warp/picalculator/Utils.java +++ b/src/main/java/org/warp/picalculator/Utils.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.nevec.rjm.BigDecimalMath; import org.nevec.rjm.Rational; @@ -841,6 +842,14 @@ public class Utils { } } + public static InputStream getResourceStreamSafe(String string) throws IOException, URISyntaxException { + try { + return getResourceStream(string); + } catch (Exception ex) { + return null; + } + } + public static InputStream getResourceStream(String string) throws IOException, URISyntaxException { URL res = Main.instance.getClass().getResource(string); boolean isResource = res != null; @@ -865,4 +874,10 @@ public class Utils { return Files.newInputStream(Paths.get(string.substring(1))); } } + + public static String read(InputStream input) throws IOException { + try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) { + return buffer.lines().collect(Collectors.joining("\n")); + } + } } 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 4dbdf2de..1395e0c8 100644 --- a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java +++ b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java @@ -2,10 +2,14 @@ package org.warp.picalculator.math.rules; import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.net.URI; @@ -14,11 +18,15 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -38,6 +46,7 @@ import org.warp.picalculator.math.functions.Variable.V_TYPE; import org.warp.picalculator.math.solver.MathSolver; import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import net.openhft.compiler.CompilerUtils; public class RulesManager { @@ -53,9 +62,9 @@ public class RulesManager { rules[val.ordinal()] = new ObjectArrayList(); } try { - final Path rulesPath = Utils.getResource("/rules.csv"); + final Path rulesPath = Utils.getResource("/math-rules.csv"); if (!Files.exists(rulesPath)) { - throw new FileNotFoundException("rules.csv not found!"); + throw new FileNotFoundException("math-rules.csv not found!"); } ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); List ruleLines = Files.readAllLines(rulesPath); @@ -64,23 +73,81 @@ public class RulesManager { if (rulesLine.length() > 0) { String[] ruleDetails = rulesLine.split(",", 1); String ruleName = ruleDetails[0]; - Utils.out.println("Evaluating /rules/" + ruleName + ".js"); - InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js"); + String ruleNameEscaped = ruleName.replace(".", "_"); + Utils.out.println("Evaluating /rules/" + ruleNameEscaped + ".java"); + String pathWithoutExtension = "/rules/" + ruleNameEscaped; + String compiledFile = ruleNameEscaped + ".ser"; + InputStream compiledResourcePath = Utils.getResourceStreamSafe(compiledFile); + String scriptFile = pathWithoutExtension + ".java"; + InputStream resourcePath = Utils.getResourceStream(scriptFile); if (resourcePath == null) { - System.err.println(new FileNotFoundException("/rules/" + ruleName + ".js not found!")); + System.err.println(new FileNotFoundException("/rules/" + ruleName + ".java not found!")); } else { - engine.eval(new InputStreamReader(resourcePath)); + boolean loadedFromCache = false; + if (compiledResourcePath != null) { + if (Files.readAttributes(Paths.get(compiledFile), BasicFileAttributes.class).creationTime().compareTo(Files.readAttributes(Paths.get(scriptFile), BasicFileAttributes.class).creationTime()) < 0) { + //The cached file is older than the source file. Deleting it. + try { + Files.deleteIfExists(Paths.get(compiledFile)); + } catch (Exception ex2) { + ex2.printStackTrace(); + } + } else { + try { + //Trying to read the compiled script + ObjectInputStream ois = new ObjectInputStream(compiledResourcePath); + RulesManager.addRule((Rule) ois.readObject()); + loadedFromCache = true; + } catch (Exception ex) { + ex.printStackTrace(); + try { + Files.deleteIfExists(Paths.get(compiledFile)); + } catch (Exception ex2) { + ex2.printStackTrace(); + } + } + } + } + if (!loadedFromCache) { + Rule r; + try { + r = compileJavaRule(scriptFile); + RulesManager.addRule(r); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Path p = Paths.get(compiledFile.replace("/", "_")).toAbsolutePath(); + System.out.println(p); + p.toFile().createNewFile(); + OutputStream fout = Files.newOutputStream(p, StandardOpenOption.CREATE); + ObjectOutputStream oos = new ObjectOutputStream(fout); + oos.writeObject(script); + + } } } } } catch (URISyntaxException | IOException e) { e.printStackTrace(); System.exit(1); - } catch (ScriptException e) { - e.printStackTrace(); } } - + + public static Rule compileJavaRule(String scriptFile) throws IOException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException { + // dynamically you can call + InputStream resource = Utils.getResourceStream(scriptFile); + + String text = Utils.read(resource); + String[] textArray = text.split("\\n", 2); + String javaClassName = textArray[0].substring(2, textArray[0].length() - 2); + String javaCode = textArray[1]; + Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(javaClassName, javaCode); + Rule rule = (Rule) aClass.newInstance(); + return rule; + } + public static void warmUp() { ObjectArrayList uselessResult = null; boolean uselessVariable = false; diff --git a/src/main/resources/math-rules.csv b/src/main/resources/math-rules.csv new file mode 100644 index 00000000..7c2cfc01 --- /dev/null +++ b/src/main/resources/math-rules.csv @@ -0,0 +1,2 @@ +Rule file +FractionsRule2 diff --git a/src/main/resources/rules.csv b/src/main/resources/rules.csv deleted file mode 100644 index 9dd05b34..00000000 --- a/src/main/resources/rules.csv +++ /dev/null @@ -1,44 +0,0 @@ -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 -ExpandRule1 -ExpandRule2 -ExpandRule5 -ExponentRule1 -ExponentRule2 -ExponentRule3 -ExponentRule4 -ExponentRule9 -ExponentRule15 -ExponentRule16 -ExponentRule17 -FractionsRule1 -FractionsRule2 -FractionsRule3 -FractionsRule4 -FractionsRule5 -FractionsRule11 -FractionsRule12 -FractionsRule14 -NumberRule1 -NumberRule2 -NumberRule3 -NumberRule4 -NumberRule5 -NumberRule7 -UndefinedRule1 -UndefinedRule2 -VariableRule1 -VariableRule2 -VariableRule3 diff --git a/src/main/resources/rules/ExpandRule1.js b/src/main/resources/rules/ExpandRule1.js deleted file mode 100644 index 8bde6d43..00000000 --- a/src/main/resources/rules/ExpandRule1.js +++ /dev/null @@ -1,128 +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; - -/** - * Expand rule - * -(+a+b) = -a-b - * -(+a-b) = -a+b - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExpandRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var fnc = f; - if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) { - var expr = fnc.getParameter2(); - if (ScriptUtils.instanceOf(expr, Sum.class)) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(expr, Subtraction.class)) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(expr, SumSubtraction.class)) { - isExecutable = true; - } - } - } else if (ScriptUtils.instanceOf(f,Subtraction.class) || ScriptUtils.instanceOf(f,SumSubtraction.class)) { - var fnc = f; - var expr = fnc.getParameter2(); - if (ScriptUtils.instanceOf(expr,Sum.class)) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(expr,Subtraction.class)) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(expr,SumSubtraction.class)) { - isExecutable = true; - } - } - if (isExecutable) { - var result = new ObjectArrayList(); - var root = f.getMathContext(); - - var expr = null; - var fromSubtraction = 0; - var subtraction = null; - if (ScriptUtils.instanceOf(f,Multiplication.class)) { - expr = f.getParameter2(); - } else if (ScriptUtils.instanceOf(f, Subtraction.class) || f instanceof SumSubtraction) { - expr = f.getParameter2(); - if (ScriptUtils.instanceOf(f, Subtraction.class)) { - fromSubtraction = 1; - } else { - fromSubtraction = 2; - } - } - - if (ScriptUtils.instanceOf(f, SumSubtraction.class)) { - - } - - var fnc = expr; - if (ScriptUtils.instanceOf(fnc, Sum.class)) { - var a = fnc.getParameter1(); - var b = fnc.getParameter2(); - var fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); - if (fromSubtraction > 0) { - subtraction = new Subtraction(root, f.getParameter1(), fnc2); - result.add(subtraction); - } else { - result.add(fnc2); - } - } else if (ScriptUtils.instanceOf(fnc, Subtraction.class)) { - var a = fnc.getParameter1(); - var b = fnc.getParameter2(); - var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); - if (fromSubtraction > 0) { - subtraction = new Subtraction(root, f.getParameter1(), fnc2); - result.add(subtraction); - } else { - result.add(fnc2); - } - } else if (ScriptUtils.instanceOf(fnc, SumSubtraction.class)) { - 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); - if (fromSubtraction > 0) { - subtraction = new SumSubtraction(root, f.getParameter1(), fnc2); - result.add(subtraction); - subtraction = new SumSubtraction(root, f.getParameter1(), fnc3); - result.add(subtraction); - result.add(subtraction); - } else { - result.add(fnc2); - result.add(fnc2); - } - } - return result; - } else { - return null; - } - - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExpandRule2.js b/src/main/resources/rules/ExpandRule2.js deleted file mode 100644 index e6f42aee..00000000 --- a/src/main/resources/rules/ExpandRule2.js +++ /dev/null @@ -1,127 +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; - -/** - * Expand rule - * -(-a) = a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExpandRule2"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var fnc = f; - if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) { - var expr = fnc.getParameter2(); - if (expr instanceof Sum) { - isExecutable = true; - } else if (expr instanceof Subtraction) { - isExecutable = true; - } else if (expr instanceof SumSubtraction) { - isExecutable = true; - } - } - } else if (ScriptUtils.instanceOf(f,Subtraction.class) || ScriptUtils.instanceOf(f,SumSubtraction.class)) { - var fnc = f; - var expr = fnc.getParameter2(); - if (ScriptUtils.instanceOf(expr,Sum.class)) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(expr,Subtraction.class)) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(expr,SumSubtraction.class)) { - isExecutable = true; - } - } - if (isExecutable) { - var result = new ObjectArrayList(); - var root = f.getMathContext(); - - var expr = null; - var fromSubtraction = 0; - var subtraction = null; - if (ScriptUtils.instanceOf(f,Multiplication.class)) { - expr = f.getParameter2(); - } else if (f instanceof Subtraction || f instanceof SumSubtraction) { - expr = f.getParameter2(); - if (f instanceof Subtraction) { - fromSubtraction = 1; - } else { - fromSubtraction = 2; - } - } - - if (f instanceof SumSubtraction) { - - } - - var fnc = 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); - if (fromSubtraction > 0) { - subtraction = new Subtraction(root, 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); - if (fromSubtraction > 0) { - subtraction = new Subtraction(root, 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); - if (fromSubtraction > 0) { - subtraction = new SumSubtraction(root, f.getParameter1(), fnc2); - result.add(subtraction); - subtraction = new SumSubtraction(root, f.getParameter1(), fnc3); - result.add(subtraction); - result.add(subtraction); - } else { - result.add(fnc2); - result.add(fnc2); - } - } - return result; - } else { - return null; - } - - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExpandRule5.js b/src/main/resources/rules/ExpandRule5.js deleted file mode 100644 index e5925763..00000000 --- a/src/main/resources/rules/ExpandRule5.js +++ /dev/null @@ -1,60 +0,0 @@ -// Imports -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Expression = org.warp.picalculator.math.functions.Expression; -var Negative = org.warp.picalculator.math.functions.Negative; -var Subtraction = org.warp.picalculator.math.functions.Subtraction; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Expand rule - * -(-a) = a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExpandRule5"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Negative.class)) { - isExecutable = ScriptUtils.instanceOf(f.getParameter(), Negative.class); - } else if (ScriptUtils.instanceOf(f, Multiplication.class)) { - if (f.getParameter1().equals(new Number(f.getMathContext(), -1)) && ScriptUtils.instanceOf(f.getParameter2(), Multiplication.class)) { - isExecutable = f.getParameter2().getParameter1().equals(f.getParameter1()); - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - - if (ScriptUtils.instanceOf(f, Negative.class)) { - var fnc = f; - result.add(((fnc.getParameter()).getParameter()).getParameter()); - } else if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var fnc = f; - result.add(fnc.getParameter2().getParameter2()); - } - return result; - } else { - return null; - } - } - -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule1.js b/src/main/resources/rules/ExponentRule1.js deleted file mode 100644 index c6232ca7..00000000 --- a/src/main/resources/rules/ExponentRule1.js +++ /dev/null @@ -1,55 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * 1^a=1 - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - var isExecutable; - var root = f.getMathContext(); - if (ScriptUtils.instanceOf(f, Power.class)) { - if (f.getParameter1().equals(new Number(root, 1))) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - result.add(new Number(root, 1)); - return result; - } else { - return null; - } - } - -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); diff --git a/src/main/resources/rules/ExponentRule15.js b/src/main/resources/rules/ExponentRule15.js deleted file mode 100644 index f2516a0f..00000000 --- a/src/main/resources/rules/ExponentRule15.js +++ /dev/null @@ -1,60 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * a*a=a^2 - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule15"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var fnc = f; - if (fnc.getParameter1().equals(fnc.getParameter2())) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var fnc = f; - var a = fnc.getParameter1(); - var two = new Number(root, 2); - var p = new Power(root, a, two); - result.add(p); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule16.js b/src/main/resources/rules/ExponentRule16.js deleted file mode 100644 index f975f0d3..00000000 --- a/src/main/resources/rules/ExponentRule16.js +++ /dev/null @@ -1,68 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; -var Sum = org.warp.picalculator.math.functions.Sum; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * (a ^ b) * (a ^ c) = a ^ (b + c) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule16"; - }, - // Rule type - getRuleType: function() { - return RuleType.REDUCTION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) { - isExecutable = (fnc.getParameter1()).getParameter1().equals((fnc.getParameter2()).getParameter1()); - } else if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) { - isExecutable = (fnc.getParameter1()).getParameter1().equals(fnc.getParameter2()); - } else if (ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) { - isExecutable = (fnc.getParameter2()).getParameter1().equals(fnc.getParameter1()); - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) { - result.add(new Power(root, (fnc.getParameter1()).getParameter1(), new Sum(root, fnc.getParameter1().getParameter2(), fnc.getParameter2().getParameter2()))); - } else if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) { - result.add(new Power(root, fnc.getParameter2(), new Sum(root, fnc.getParameter1().getParameter2(), new Number(root, 1)))); - } else if (ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) { - result.add(new Power(root, fnc.getParameter1(), new Sum(root, new Number(root, 1), fnc.getParameter2().getParameter2()))); - } - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule17.js b/src/main/resources/rules/ExponentRule17.js deleted file mode 100644 index 0ae297a3..00000000 --- a/src/main/resources/rules/ExponentRule17.js +++ /dev/null @@ -1,61 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; -var Root = org.warp.picalculator.math.functions.Root; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * a√x=x^1/a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule17"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Root.class)) { - var fnc = f; - if (fnc.getParameter1().equals(fnc.getParameter2())) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var fnc = f; - var a = fnc.getParameter1(); - var two = new Number(root, 2); - var p = new Power(fnc.getMathContext(), a, two); - result.add(p); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule2.js b/src/main/resources/rules/ExponentRule2.js deleted file mode 100644 index a7be8ac8..00000000 --- a/src/main/resources/rules/ExponentRule2.js +++ /dev/null @@ -1,53 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * a^1=a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule2"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var fnc = f; - if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) { - isExecutable = true; - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - result.add((f).getParameter1()); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule3.js b/src/main/resources/rules/ExponentRule3.js deleted file mode 100644 index 8fd675c5..00000000 --- a/src/main/resources/rules/ExponentRule3.js +++ /dev/null @@ -1,53 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * a^0=1 - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule3"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var fnc = f; - if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) { - isExecutable = true; - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - result.add(new Number(f.getMathContext(), 1)); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule4.js b/src/main/resources/rules/ExponentRule4.js deleted file mode 100644 index 4b87bbd1..00000000 --- a/src/main/resources/rules/ExponentRule4.js +++ /dev/null @@ -1,65 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Expression = org.warp.picalculator.math.functions.Expression; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * (a*b)^n=a^n*b^n - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule4"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var fnc = f; - if (fnc.getParameter1() instanceof Multiplication && ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var 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; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/ExponentRule9.js b/src/main/resources/rules/ExponentRule9.js deleted file mode 100644 index 353073b0..00000000 --- a/src/main/resources/rules/ExponentRule9.js +++ /dev/null @@ -1,58 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Exponent rule - * (a ^ b) ^ c = a ^ (b * c) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "ExponentRule9"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var 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; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule1.js b/src/main/resources/rules/FractionsRule1.js deleted file mode 100644 index 15548e72..00000000 --- a/src/main/resources/rules/FractionsRule1.js +++ /dev/null @@ -1,65 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Division = org.warp.picalculator.math.functions.Division; -var Number = org.warp.picalculator.math.functions.Number; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * 0 / a = 0 - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - var root = f.getMathContext(); - if (ScriptUtils.instanceOf(f, Division.class)) { - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Number.class)) { - var numb1 = fnc.getParameter1(); - if (numb1.equals(new Number(root, 0))) { - if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { - var numb2 = fnc.getParameter2(); - if (numb2.equals(new Number(root, 0)) == false) { - isExecutable = true; - } - } else { - isExecutable = true; - } - } - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - result.add(new Number(f.getMathContext(), 0)); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule11.js b/src/main/resources/rules/FractionsRule11.js deleted file mode 100644 index d6c1877c..00000000 --- a/src/main/resources/rules/FractionsRule11.js +++ /dev/null @@ -1,71 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Division = org.warp.picalculator.math.functions.Division; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * a / (b / c) = (a * c) / b - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule11"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Division.class)) { - var fnc = f; - var a; - var c; - var div2; - if (ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) { - div2 = fnc.getParameter2(); - a = fnc.getParameter1(); - c = div2.getParameter2(); - isExecutable = true; - } else { - isExecutable = false; - } - } - if (isExecutable) { - var result = new ObjectArrayList(); - var fnc = f; - var a; - var b; - var c; - - var div2 = fnc.getParameter2(); - - a = fnc.getParameter1(); - b = div2.getParameter1(); - c = div2.getParameter2(); - result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), b)); - - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule12.js b/src/main/resources/rules/FractionsRule12.js deleted file mode 100644 index a8261e9f..00000000 --- a/src/main/resources/rules/FractionsRule12.js +++ /dev/null @@ -1,68 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Division = org.warp.picalculator.math.functions.Division; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * (b / c) / a = b / (a * c) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule12"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Division.class)) { - var fnc = f; - var a; - var c; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) { - var div2 = fnc.getParameter1(); - a = fnc.getParameter1(); - c = div2.getParameter2(); - isExecutable = true; - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - var fnc = f; - var a; - var b; - var c; - - var div2 = 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; - } - } -} - -//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/FractionsRule14.js b/src/main/resources/rules/FractionsRule14.js deleted file mode 100644 index e9cb0e42..00000000 --- a/src/main/resources/rules/FractionsRule14.js +++ /dev/null @@ -1,104 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Division = org.warp.picalculator.math.functions.Division; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * (a / b) * (c / d) = (a * c) / (b * d) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule14"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var fnc = f; - var a; - var b; - var c; - var d; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) { - var div1 = fnc.getParameter1(); - var div2 = fnc.getParameter2(); - a = div1.getParameter1(); - b = div1.getParameter2(); - c = div2.getParameter1(); - d = div2.getParameter2(); - isExecutable = true; - } else if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) { - var div1 = fnc.getParameter1(); - a = div1.getParameter1(); - b = div1.getParameter2(); - c = fnc.getParameter2(); - isExecutable = true; - } else if (ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) { - var div2 = fnc.getParameter2(); - a = fnc.getParameter1(); - c = div2.getParameter1(); - d = div2.getParameter2(); - isExecutable = true; - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - var fnc = f; - var a; - var b; - var c; - var d; - - if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) { - var div1 = fnc.getParameter1(); - var div2 = fnc.getParameter2(); - a = div1.getParameter1(); - b = div1.getParameter2(); - c = div2.getParameter1(); - d = div2.getParameter2(); - var div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), new Multiplication(fnc.getMathContext(), b, d)); - result.add(div); - } else if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) { - var div1 = fnc.getParameter1(); - a = div1.getParameter1(); - b = div1.getParameter2(); - c = fnc.getParameter2(); - var div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), b); - result.add(div); - } else if (ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) { - var div2 = fnc.getParameter2(); - a = fnc.getParameter1(); - c = div2.getParameter1(); - d = div2.getParameter2(); - var div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), d); - result.add(div); - } - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule2.java b/src/main/resources/rules/FractionsRule2.java new file mode 100644 index 00000000..e0521313 --- /dev/null +++ b/src/main/resources/rules/FractionsRule2.java @@ -0,0 +1,64 @@ +/*org.warp.picalculator.math.rules.FractionsRule2*/ +package org.warp.picalculator.math.rules; + +//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 org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.ScriptUtils; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * 0 / a = 0 + * + * @author Andrea Cavalli + * + */ +public class FractionsRule2 implements Rule { + + @Override + public String getRuleName() { + return "FractionsRule1"; + } + + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + MathContext root = f.getMathContext(); + if (ScriptUtils.instanceOf(f, Division.class)) { + Division fnc = (Division) f; + if (ScriptUtils.instanceOf(fnc.getParameter1(), Number.class)) { + Number numb1 = (Number) fnc.getParameter1(); + if (numb1.equals(new Number(root, 0))) { + if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { + Number numb2 = (Number) fnc.getParameter2(); + if (numb2.equals(new Number(root, 0)) == false) { + isExecutable = true; + } + } else { + isExecutable = true; + } + } + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(f.getMathContext(), 0)); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/FractionsRule2.js b/src/main/resources/rules/FractionsRule2.js deleted file mode 100644 index 1a0137f3..00000000 --- a/src/main/resources/rules/FractionsRule2.js +++ /dev/null @@ -1,57 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Division = org.warp.picalculator.math.functions.Division; -var Number = org.warp.picalculator.math.functions.Number; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * a / 1 = a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule2"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Division.class)) { - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { - var numb = fnc.getParameter2(); - if (numb.equals(new Number(f.getMathContext(), 1))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - var fnc = f; - result.add(fnc.getParameter1()); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule3.js b/src/main/resources/rules/FractionsRule3.js deleted file mode 100644 index 2deb3a56..00000000 --- a/src/main/resources/rules/FractionsRule3.js +++ /dev/null @@ -1,53 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Division = org.warp.picalculator.math.functions.Division; -var Number = org.warp.picalculator.math.functions.Number; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * a / a = 1 - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule3"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Division.class)) { - var fnc = f; - if (fnc.getParameter1().equals(fnc.getParameter2())) { - isExecutable = true; - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - result.add(new Number(f.getMathContext(), 1)); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule4.js b/src/main/resources/rules/FractionsRule4.js deleted file mode 100644 index 0d117603..00000000 --- a/src/main/resources/rules/FractionsRule4.js +++ /dev/null @@ -1,61 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var Division = org.warp.picalculator.math.functions.Division; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * (a / b) ^ -1 = b / a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule4"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { - var n2 = fnc.getParameter2(); - if (n2.equals(new Number(f.getMathContext(), -1))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - var fnc = f; - var a = (fnc.getParameter1()).getParameter1(); - var b = (fnc.getParameter1()).getParameter2(); - var res = new Division(f.getMathContext(), b, a); - result.add(res); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/FractionsRule5.js b/src/main/resources/rules/FractionsRule5.js deleted file mode 100644 index 58aaccc6..00000000 --- a/src/main/resources/rules/FractionsRule5.js +++ /dev/null @@ -1,76 +0,0 @@ -//Imports - - -var BigDecimal = Java.type("java.math.BigDecimal"); - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Division = org.warp.picalculator.math.functions.Division; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Fractions rule - * (a / b) ^ -c = (b / a) ^ c - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "FractionsRule5"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) { - if (ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class) && fnc.getParameter2().getParameter1().equals(new Number(f.getMathContext(), -1))) { - isExecutable = true; - } else if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { - var n2 = fnc.getParameter2(); - if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { - isExecutable = true; - } - } - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var fnc = f; - var a = (fnc.getParameter1()).getParameter1(); - var b = (fnc.getParameter1()).getParameter2(); - var c; - if (ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class)) { - c = fnc.getParameter2().getParameter2(); - } else { - c = fnc.getParameter2().multiply(new Number(root, "-1")); - } - var dv = new Division(root, b, a); - var pow = new Power(root, dv, c); - result.add(pow); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/NumberRule1.js b/src/main/resources/rules/NumberRule1.js deleted file mode 100644 index f15979bf..00000000 --- a/src/main/resources/rules/NumberRule1.js +++ /dev/null @@ -1,64 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Number rule - * a * 0 = 0 - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "NumberRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var root = f.getMathContext(); - var mult = f; - if (ScriptUtils.instanceOf(mult.getParameter1(), Number.class)) { - var numb = mult.getParameter1(); - if (numb.equals(new Number(root, 0))) { - isExecutable = true; - } - } - if (ScriptUtils.instanceOf(mult.getParameter2(), Number.class)) { - var numb = mult.getParameter2(); - if (numb.equals(new Number(root, 0))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var result = new ObjectArrayList(); - result.add(new Number(f.getMathContext(), "0")); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/NumberRule2.js b/src/main/resources/rules/NumberRule2.js deleted file mode 100644 index 28500067..00000000 --- a/src/main/resources/rules/NumberRule2.js +++ /dev/null @@ -1,83 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Number rule - * a * 1 = a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "NumberRule2"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var root = f.getMathContext(); - var mult = f; - if (ScriptUtils.instanceOf(mult.getParameter1(), Number.class)) { - var numb = mult.getParameter1(); - if (numb.equals(new Number(root, 1))) { - isExecutable = true; - } - } - if (ScriptUtils.instanceOf(mult.getParameter2(), Number.class)) { - var numb = mult.getParameter2(); - if (numb.equals(new Number(root, 1))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var a = null; - var aFound = false; - var mult = f; - if (aFound == false & ScriptUtils.instanceOf(mult.getParameter1(), Number.class)) { - var numb = mult.getParameter1(); - if (numb.equals(new Number(root, 1))) { - a = mult.getParameter2(); - aFound = true; - } - } - if (aFound == false && ScriptUtils.instanceOf(mult.getParameter2(), Number.class)) { - var numb = mult.getParameter2(); - if (numb.equals(new Number(root, 1))) { - a = mult.getParameter1(); - aFound = true; - } - } - - result.add(a); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/NumberRule3.js b/src/main/resources/rules/NumberRule3.js deleted file mode 100644 index 93acfaff..00000000 --- a/src/main/resources/rules/NumberRule3.js +++ /dev/null @@ -1,80 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Negative = org.warp.picalculator.math.functions.Negative; -var Number = org.warp.picalculator.math.functions.Number; -var Subtraction = org.warp.picalculator.math.functions.Subtraction; -var Sum = org.warp.picalculator.math.functions.Sum; -var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Number rule - * a - a = 0 - * -a + a = 0 - * a ± a = {0, 2a} - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "NumberRule3"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Subtraction.class)) { - var sub = f; - if (sub.getParameter1().equals(sub.getParameter2())) { - isExecutable = true; - } - } else if (ScriptUtils.instanceOf(f, Sum.class)) { - var sub = f; - if (ScriptUtils.instanceOf(sub.getParameter1(), Multiplication.class)) { - if (ScriptUtils.instanceOf(sub.getParameter1().getParameter1(), Number.class) && sub.getParameter1().getParameter1().equals(new Number(f.getMathContext(), -1))) { - var neg = sub.getParameter1().getParameter2(); - if (neg.equals(sub.getParameter2())) { - isExecutable = true; - } - } - } - } else if (ScriptUtils.instanceOf(f, SumSubtraction.class)) { - var sub = f; - if (sub.getParameter1().equals(sub.getParameter2())) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - if (ScriptUtils.instanceOf(f, SumSubtraction.class)) { - var mul = new Multiplication(root, new Number(root, 2), f.getParameter1()); - result.add(mul); - } - result.add(new Number(root, 0)); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/NumberRule4.js b/src/main/resources/rules/NumberRule4.js deleted file mode 100644 index 6db37fe8..00000000 --- a/src/main/resources/rules/NumberRule4.js +++ /dev/null @@ -1,55 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Subtraction = org.warp.picalculator.math.functions.Subtraction; -var Sum = org.warp.picalculator.math.functions.Sum; -var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Number rule - * a ± b = {a+b, a-b} - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "NumberRule4"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, SumSubtraction.class)) { - isExecutable = true; - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var ss = f; - result.add(new Sum(root, ss.getParameter1(), ss.getParameter2())); - result.add(new Subtraction(root, ss.getParameter1(), ss.getParameter2())); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/NumberRule5.js b/src/main/resources/rules/NumberRule5.js deleted file mode 100644 index 2e3d25aa..00000000 --- a/src/main/resources/rules/NumberRule5.js +++ /dev/null @@ -1,71 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var FunctionOperator = org.warp.picalculator.math.FunctionOperator; -var MathContext = org.warp.picalculator.math.MathContext; -var Number = org.warp.picalculator.math.functions.Number; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Number rule - * a + 0 = a - * 0 + a = a - * a - 0 = a - * 0 - a = -a - * a ± 0 = a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "NumberRule5"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Sum.class) || ScriptUtils.instanceOf(f, Subtraction.class) || ScriptUtils.instanceOf(f, SumSubtraction.class)) { - var root = f.getMathContext(); - var fnc = f; - if (fnc.getParameter1().equals(new Number(root, 0)) || fnc.getParameter2().equals(new Number(root, 0))) { - if (!(fnc.getParameter1().equals(new Number(root, 0)) && ScriptUtils.instanceOf(f, SumSubtraction.class))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var fnc = f; - var a = fnc.getParameter1(); - if (a.equals(new Number(root, 0))) { - if (ScriptUtils.instanceOf(f, Subtraction.class)) { - a = new Multiplication(root, new Number(root, -1), fnc.getParameter2()); - } else { - a = fnc.getParameter2(); - } - } - result.add(a); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/NumberRule7.js b/src/main/resources/rules/NumberRule7.js deleted file mode 100644 index 2d431d69..00000000 --- a/src/main/resources/rules/NumberRule7.js +++ /dev/null @@ -1,54 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Sum = org.warp.picalculator.math.functions.Sum; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Number rule - * a + a = 2a - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "NumberRule7"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXPANSION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Sum.class)) { - isExecutable = f.getParameter1().equals(f.getParameter2()); - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - var mult = new Multiplication(root, new Number(root, 2), f.getParameter1()); - result.add(mult); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/UndefinedRule1.js b/src/main/resources/rules/UndefinedRule1.js deleted file mode 100644 index c3db7625..00000000 --- a/src/main/resources/rules/UndefinedRule1.js +++ /dev/null @@ -1,57 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Number = org.warp.picalculator.math.functions.Number; -var Power = org.warp.picalculator.math.functions.Power; -var Undefined = org.warp.picalculator.math.functions.Undefined; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Undefined rule - * 0^0=undefined - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "UndefinedRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXISTENCE; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Power.class)) { - var root = f.getMathContext(); - var fnc = f; - if (fnc.getParameter1().equals(new Number(root, 0)) && fnc.getParameter2().equals(new Number(root, 0))) { - isExecutable = true; - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - result.add(new Undefined(root)); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/UndefinedRule2.js b/src/main/resources/rules/UndefinedRule2.js deleted file mode 100644 index fbecdb0c..00000000 --- a/src/main/resources/rules/UndefinedRule2.js +++ /dev/null @@ -1,60 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var MathContext = org.warp.picalculator.math.MathContext; -var Division = org.warp.picalculator.math.functions.Division; -var Number = org.warp.picalculator.math.functions.Number; -var Undefined = org.warp.picalculator.math.functions.Undefined; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Undefined rule - * a / 0 = undefined - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "UndefinedRule2"; - }, - // Rule type - getRuleType: function() { - return RuleType.EXISTENCE; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - if (ScriptUtils.instanceOf(f, Division.class)) { - var root = f.getMathContext(); - var fnc = f; - if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) { - var numb = fnc.getParameter2(); - if (numb.equals(new Number(root, 0))) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var root = f.getMathContext(); - var result = new ObjectArrayList(); - result.add(new Undefined(root)); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/VariableRule1.js b/src/main/resources/rules/VariableRule1.js deleted file mode 100644 index d05f56f8..00000000 --- a/src/main/resources/rules/VariableRule1.js +++ /dev/null @@ -1,83 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var FunctionOperator = org.warp.picalculator.math.FunctionOperator; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Subtraction = org.warp.picalculator.math.functions.Subtraction; -var Sum = org.warp.picalculator.math.functions.Sum; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Variable rule - * ax+bx=(a+b)*x (a,b NUMBER; x VARIABLE|MULTIPLICATION) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "VariableRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.REDUCTION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - var fnc = f; - if (ScriptUtils.instanceOf(f, Subtraction.class) || ScriptUtils.instanceOf(f, Sum.class)) { - if (ScriptUtils.instanceOf(fnc.getParameter1(), Multiplication.class) & ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class)) { - var m1 = fnc.getParameter1(); - var m2 = fnc.getParameter2(); - if (m1.getParameter1().equals(m2.getParameter1()) || m1.getParameter2().equals(m2.getParameter2())) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var root = fnc.getMathContext(); - var result = new ObjectArrayList(); - var m1 = fnc.getParameter1(); - var m2 = fnc.getParameter2(); - var a; - var b; - var x; - if (m1.getParameter2().equals(m2.getParameter2())) { - x = m1.getParameter2(); - a = m1.getParameter1(); - b = m2.getParameter1(); - } else { - x = m1.getParameter1(); - a = m1.getParameter2(); - b = m2.getParameter2(); - } - - var rets; - if (ScriptUtils.instanceOf(fnc, Sum.class)) { - rets = new Sum(root, a, b); - } else { - rets = new Subtraction(root, a, b); - } - var retm = new Multiplication(root, rets, x); - result.add(retm); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/VariableRule2.js b/src/main/resources/rules/VariableRule2.js deleted file mode 100644 index b96ff723..00000000 --- a/src/main/resources/rules/VariableRule2.js +++ /dev/null @@ -1,72 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var FunctionOperator = org.warp.picalculator.math.FunctionOperator; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Subtraction = org.warp.picalculator.math.functions.Subtraction; -var Sum = org.warp.picalculator.math.functions.Sum; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Variable rule - * ax+x=(a+1)*x (a,b NUMBER; x VARIABLES) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "VariableRule2"; - }, - // Rule type - getRuleType: function() { - return RuleType.REDUCTION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - var fnc = f; - if (ScriptUtils.instanceOf(f, Sum.class) || ScriptUtils.instanceOf(f, Subtraction.class)) { - if (ScriptUtils.instanceOf(fnc.getParameter1(), Multiplication.class)) { - var m1 = fnc.getParameter1(); - if (m1.getParameter2().equals(fnc.getParameter2())) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var root = fnc.getMathContext(); - var result = new ObjectArrayList(); - var m1 = fnc.getParameter1(); - var a = m1.getParameter1(); - var x = fnc.getParameter2(); - - var rets; - if (ScriptUtils.instanceOf(fnc, Sum.class)) { - 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); - result.add(retm); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/VariableRule3.js b/src/main/resources/rules/VariableRule3.js deleted file mode 100644 index 89bd7aa9..00000000 --- a/src/main/resources/rules/VariableRule3.js +++ /dev/null @@ -1,73 +0,0 @@ -//Imports - - -var Error = org.warp.picalculator.Error; -var Function = org.warp.picalculator.math.Function; -var FunctionOperator = org.warp.picalculator.math.FunctionOperator; -var MathContext = org.warp.picalculator.math.MathContext; -var Multiplication = org.warp.picalculator.math.functions.Multiplication; -var Number = org.warp.picalculator.math.functions.Number; -var Subtraction = org.warp.picalculator.math.functions.Subtraction; -var Sum = org.warp.picalculator.math.functions.Sum; - -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); - -/** - * Variable rule - * x+ax=(a+1)*x (a,b NUMBER; x VARIABLES) - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "VariableRule3"; - }, - // Rule type - getRuleType: function() { - return RuleType.REDUCTION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - - execute: function(f) { - var isExecutable; - var fnc = f; - if (ScriptUtils.instanceOf(f, Sum.class) || ScriptUtils.instanceOf(f, Subtraction.class)) { - if (ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class)) { - var m2 = fnc.getParameter2(); - if (m2.getParameter2().equals(fnc.getParameter1())) { - isExecutable = true; - } - } - } - - if (isExecutable) { - var root = fnc.getMathContext(); - var result = new ObjectArrayList(); - var m2 = fnc.getParameter2(); - var a = m2.getParameter1(); - var x = fnc.getParameter1(); - - var rets; - if (ScriptUtils.instanceOf(fnc, Sum.class)) { - 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); - result.add(retm); - return result; - } else { - return null; - } - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/_EXAMPLE RULE.js b/src/main/resources/rules/_EXAMPLE RULE.js deleted file mode 100644 index 1e3b74cc..00000000 --- a/src/main/resources/rules/_EXAMPLE RULE.js +++ /dev/null @@ -1,108 +0,0 @@ - -// Imports -var Rule = org.warp.picalculator.math.rules.Rule; -var RuleType = org.warp.picalculator.math.rules.RuleType; -var RulesManager = org.warp.picalculator.math.rules.RulesManager; - -// Rule class -var rule = { - // Rule name - getRuleName: function() { - return "ExampleRule1"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "func" - - An ObjectArrayList if it did something - */ - execute: function(func) { - return null; - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); - - - - - - - - - -CONVERSIONI: - - - - - - -inserire all'inizio: -//Imports - ------------------- -^package [^\n]+?;$ - ------------------- -^import ((?:org|com|net)[^\n]+\.([^\n]+?));$ -var $2 = $1; ------------------- -^import ([^\n]+\.([^\n]+?));$ -var $2 = Java.type("$1"); ------------------- -
||<\/b> - ------------------- -(static boolean compare\([\s\S]*?)(?:|\r\n)\treturn false;([\t\r\n}]+?public static) -$1$2 -VVVV-------------- -(static boolean compare\([\s\S]*?)return ([^;]+)([\s\S]+?public static) -$1isExecutable = $2$3 ------------------- -(?:|public)(?:|private)(?:| final) class ([^\n]+?)[ ]*\{ -var rule = {\r\n\t// Rule name\r\n\tgetRuleName: function() {\r\n\t\treturn "$1";\r\n\t},\r\n\t// Rule type\r\n\tgetRuleType: function() {\r\n\t\treturn RuleType./* TODO: RULE TYPE */;\r\n\t},\r\n\t/* Rule function\r\n\t Returns:\r\n\t - null if it's not executable on the function "f"\r\n\t - An ObjectArrayList if it did something\r\n\t*/ ------------------- -(?:|\r\n)\tpublic static boolean compare\(Function f\)[ ]*?\{ -\texecute: function(f) {\r\n\t\tvar isExecutable; ------------------- -(if \(|= |while \(|return |& |\| )([^ \n\r;]+?) instanceof ([^ \n\r;)]+) -$1ScriptUtils.instanceOf($2, $3.class) ------------------- -\t}[\r\n]*?\tpublic static ObjectArrayList execute\(Function f\) throws Error \{ -\t\tif (isExecutable) { ------------------- - -aggiungere tab - ------------------- - -inserire in fondo al codice -} else { - return null; -} - ------------------ -\([A-Z][a-z]*?\)( |) - ------------------ -([^.])(?:final ) -$1 ------------------ -[A-Z][A-z<>?]*? ([A-z]+?)(?: |)=(?: |) -var $1 = ------------------ -(new [A-Z][A-z]+?)<[^;()+\-*\/]*?> -$1 ------------------ - -inserire in fondo: - -//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/Division.js b/src/main/resources/rules/functions/Division.js deleted file mode 100644 index 486d7e6a..00000000 --- a/src/main/resources/rules/functions/Division.js +++ /dev/null @@ -1,76 +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; - -/** - * Division - * a/b = c - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Division"; - }, - // 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, Division.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)) { - if (mathContext.exactMode) { - if (variable1.isInteger() && variable2.isInteger()) { - var factors1, factors2, mcm; - try { - factors1 = variable1.getFactors(); - factors2 = variable2.getFactors(); - mcm = ScriptUtils.mcm(factors1, factors2); - } catch (error) { - 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) { - 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)); - 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/EmptyNumber.js b/src/main/resources/rules/functions/EmptyNumber.js deleted file mode 100644 index 3c1eaf1e..00000000 --- a/src/main/resources/rules/functions/EmptyNumber.js +++ /dev/null @@ -1,35 +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; - -/** - * EmptyNumber - * - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "EmptyNumber"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - 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/Expression.js b/src/main/resources/rules/functions/Expression.js deleted file mode 100644 index 62457c39..00000000 --- a/src/main/resources/rules/functions/Expression.js +++ /dev/null @@ -1,46 +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 Expression = org.warp.picalculator.math.functions.Expression; - -/** - * Expression - * (x) = x - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Expression"; - }, - // 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, Expression.class)) { - var result = new ObjectArrayList(); - result.add(f.getParameter(0)); - 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/Joke.js b/src/main/resources/rules/functions/Joke.js deleted file mode 100644 index 8f12f600..00000000 --- a/src/main/resources/rules/functions/Joke.js +++ /dev/null @@ -1,35 +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; - -/** - * Joke - * - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Joke"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - 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/Multiplication.js b/src/main/resources/rules/functions/Multiplication.js deleted file mode 100644 index 58be484b..00000000 --- a/src/main/resources/rules/functions/Multiplication.js +++ /dev/null @@ -1,52 +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; - -/** - * Multiplication - * a*b = c - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Multiplication"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - if (ScriptUtils.instanceOf(f, Multiplication.class)) { - var result = new ObjectArrayList(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var mathContext = f.getMathContext(); - if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) { - //multiply a by b (a*b = c) - result.add(variable1.multiply(variable2)); - return result; - } - } - return null; - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/Negative.js b/src/main/resources/rules/functions/Negative.js deleted file mode 100644 index f2523cd0..00000000 --- a/src/main/resources/rules/functions/Negative.js +++ /dev/null @@ -1,62 +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 Number = org.warp.picalculator.math.functions.Number; -var Negative = org.warp.picalculator.math.functions.Negative; -var Error = org.warp.picalculator.Error; -var Errors = org.warp.picalculator.Errors; -var NullPointerException = java.lang.NullPointerException; -var NumberFormatException = java.lang.NumberFormatException; -var ArithmeticException = java.lang.ArithmeticException; - -/** - * Negative - * -a = b - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Negative"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - if (ScriptUtils.instanceOf(f, Negative.class)) { - var result = new ObjectArrayList(); - var variable = f.getParameter(); - var mathContext = f.getMathContext(); - if (ScriptUtils.instanceOf(variable, Number.class)) { - //-a = a*-1 = b - try { - result.add(variable.multiply(new Number(mathContext, -1))); - } catch (ex) { - if (ScriptUtils.instanceOf(ex, NullPointerException.class)) { - throw new Error(Errors.ERROR); - } else if (ScriptUtils.instanceOf(ex, NumberFormatException.class)) { - throw new Error(Errors.SYNTAX_ERROR); - } else if (ScriptUtils.instanceOf(ex, ArithmeticException.class)) { - throw new Error(Errors.NUMBER_TOO_SMALL); - } - } - return result; - } - } - return null; - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/Number.js b/src/main/resources/rules/functions/Number.js deleted file mode 100644 index b2c2bdfe..00000000 --- a/src/main/resources/rules/functions/Number.js +++ /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/Power.js b/src/main/resources/rules/functions/Power.js deleted file mode 100644 index c7feccef..00000000 --- a/src/main/resources/rules/functions/Power.js +++ /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/Root.js b/src/main/resources/rules/functions/Root.js deleted file mode 100644 index 064d35dd..00000000 --- a/src/main/resources/rules/functions/Root.js +++ /dev/null @@ -1,78 +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 Root = org.warp.picalculator.math.functions.Root; -var RootSquare = org.warp.picalculator.math.functions.RootSquare; -var BigDecimal = java.math.BigDecimal; - -/** - * Root - * a√b = c - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Root"; - }, - // Rule type - getRuleType: function() { - return RuleType.CALCULATION; - }, - /* Rule function - Returns: - - null if it's not executable on the function "f" - - An ObjectArrayList if it did something - */ - execute: function(f) { - var isSquare = false; - if ((isSquare = ScriptUtils.instanceOf(f, RootSquare.class)) || ScriptUtils.instanceOf(f, Root.class)) { - var result = new ObjectArrayList(); - var mathContext = f.getMathContext(); - var variable1 = f.getParameter1(); - var variable2 = f.getParameter2(); - var isSolvable = false; - var canBePorted = false; - if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) { - isSolvable = isSolvable|!mathContext.exactMode; - if (!isSolvable) { - try { - var resultVar = variable2.pow(new Number(mathContext, BigDecimal.ONE).divide(variable1)); - var originalVariable = resultVar.pow(new Number(mathContext, 2)); - if (originalVariable.equals(f.getParameter2())) { - isSolvable = true; - } - } catch (ex) { - ex.printStackTrace(); - } - } - } - if (!isSquare && !isSolvable && ScriptUtils.instanceOf(variable1, Number.class) && variable1.equals(new Number(mathContext, 2))) { - canBePorted = true; - } - - if (isSolvable) { - result.add(variable2.pow(new Number(mathContext, BigInteger.ONE).divide(variable1))); - return result; - } - if (canBePorted) { - result.add(new RootSquare(mathContext, variable2)); - } - } - return null; - } -} - -//Add this rule to the list of rules -RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/Subtraction.js b/src/main/resources/rules/functions/Subtraction.js deleted file mode 100644 index d3353602..00000000 --- a/src/main/resources/rules/functions/Subtraction.js +++ /dev/null @@ -1,52 +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; - -/** - * Subtraction - * a-b = c - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Subtraction"; - }, - // 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, Subtraction.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 = a+(b*-1) = c - result.add(variable1.add(variable2.multiply(new Number(mathContext, -1)))); - 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/Sum.js b/src/main/resources/rules/functions/Sum.js deleted file mode 100644 index 45bb5fe1..00000000 --- a/src/main/resources/rules/functions/Sum.js +++ /dev/null @@ -1,52 +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; - -/** - * Sum - * a+b = c - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Sum"; - }, - // 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, Sum.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.add(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/SumSubtraction.js b/src/main/resources/rules/functions/SumSubtraction.js deleted file mode 100644 index cb04a2ed..00000000 --- a/src/main/resources/rules/functions/SumSubtraction.js +++ /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; - -/** - * SumSumbraction - * a±b = c, d - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "SumSubtraction"; - }, - // 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, SumSubtraction.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, d - result.add(variable1.add(variable2)); - result.add(variable1.add(variable2.multiply(new Number(mathContext, -1)))); - 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/Variable.js b/src/main/resources/rules/functions/Variable.js deleted file mode 100644 index 8f10c9a6..00000000 --- a/src/main/resources/rules/functions/Variable.js +++ /dev/null @@ -1,55 +0,0 @@ -// Imports -var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); -var BigDecimalMath = org.nevec.rjm.BigDecimalMath; -var Utils = org.warp.picalculator.Utils; -var MathematicalSymbols = org.warp.picalculator.math.MathematicalSymbols; -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 Variable = org.warp.picalculator.math.functions.Variable; -var Number = org.warp.picalculator.math.functions.Number; - -/** - * Variable - * a = n - * - * @author Andrea Cavalli - * - */ -var rule = { - // Rule name - getRuleName: function() { - return "Variable"; - }, - // 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, Variable.class)) { - var result = new ObjectArrayList(); - var variable = f.getChar(); - var mathContext = f.getMathContext(); - if (mathContext.exactMode == false) { - if (variable.equals(MathematicalSymbols.PI)) { - //a = n - result.add(new Number(mathContext, BigDecimalMath.pi(new java.math.MathContext(Utils.scale, Utils.scaleMode2)))); - 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 From e49bb8ff62881d402d23aba150e5aa03409f760f Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Thu, 22 Mar 2018 15:54:06 +0100 Subject: [PATCH 3/3] Updated RulesManager --- .classpath | 2 +- .../org/warp/picalculator/math/rules/Rule.java | 4 +++- .../picalculator/math/rules/RulesManager.java | 16 ++++++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index f048d21f..6c802bcb 100644 --- a/.classpath +++ b/.classpath @@ -6,7 +6,7 @@ - + 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 821bd3b3..82c8a1db 100644 --- a/src/main/java/org/warp/picalculator/math/rules/Rule.java +++ b/src/main/java/org/warp/picalculator/math/rules/Rule.java @@ -1,5 +1,7 @@ package org.warp.picalculator.math.rules; +import java.io.Serializable; + import org.warp.picalculator.math.Function; import it.unimi.dsi.fastutil.objects.ObjectArrayList; @@ -10,7 +12,7 @@ import jdk.nashorn.internal.objects.annotations.SpecializedFunction; * @author Andrea Cavalli * */ -public interface Rule { +public interface Rule extends Serializable { /** * Get rule name * @return 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 1395e0c8..d30ac325 100644 --- a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java +++ b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java @@ -109,21 +109,21 @@ public class RulesManager { } } if (!loadedFromCache) { - Rule r; + Rule r = null; try { r = compileJavaRule(scriptFile); RulesManager.addRule(r); + + Path p = Paths.get(compiledFile.replace("/", "_")).toAbsolutePath(); + System.out.println(p); + p.toFile().createNewFile(); + OutputStream fout = Files.newOutputStream(p, StandardOpenOption.CREATE); + ObjectOutputStream oos = new ObjectOutputStream(fout); + oos.writeObject(r); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } - - Path p = Paths.get(compiledFile.replace("/", "_")).toAbsolutePath(); - System.out.println(p); - p.toFile().createNewFile(); - OutputStream fout = Files.newOutputStream(p, StandardOpenOption.CREATE); - ObjectOutputStream oos = new ObjectOutputStream(fout); - oos.writeObject(script); } }