From 3fa243375bce335ed8e046855058f9ad5790ccda Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Fri, 23 Mar 2018 22:56:43 +0100 Subject: [PATCH] Added ExpandRule1 and replaced FractionsRule2 --- src/main/resources/rules.csv | 1 + src/main/resources/rules/ExpandRule1.java | 137 +++++++++++++++++++ src/main/resources/rules/FractionsRule2.java | 42 +++--- 3 files changed, 157 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/rules/ExpandRule1.java diff --git a/src/main/resources/rules.csv b/src/main/resources/rules.csv index 7c2cfc01..694a46ae 100644 --- a/src/main/resources/rules.csv +++ b/src/main/resources/rules.csv @@ -1,2 +1,3 @@ Rule file +ExpandRule1 FractionsRule2 diff --git a/src/main/resources/rules/ExpandRule1.java b/src/main/resources/rules/ExpandRule1.java new file mode 100644 index 00000000..1dad8003 --- /dev/null +++ b/src/main/resources/rules/ExpandRule1.java @@ -0,0 +1,137 @@ +/* +SETTINGS: (please don't move this part) + PATH=ExpandRule1 +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.MathContext; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; + +/** + * Expand rule + * -(+a+b) = -a-b + * -(+a-b) = -a+b + * + * @author Andrea Cavalli + * + */ +public class ExpandRule1 implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExpandRule1"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + Multiplication fnc = (Multiplication) f; + if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) { + Function 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 (f instanceof Subtraction || f instanceof SumSubtraction) { + FunctionOperator fnc = (FunctionOperator) f; + Function expr = fnc.getParameter2(); + if (expr instanceof Sum) { + isExecutable = true; + } else if (expr instanceof Subtraction) { + isExecutable = true; + } else if (expr instanceof SumSubtraction) { + isExecutable = true; + } + } + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + MathContext root = f.getMathContext(); + + Function expr = null; + int fromSubtraction = 0; + FunctionOperator subtraction = null; + if (f instanceof Multiplication) { + expr = ((Multiplication) f).getParameter2(); + } else if (f instanceof Subtraction || f instanceof SumSubtraction) { + expr = ((Multiplication) f).getParameter2(); + if (f instanceof Subtraction) { + fromSubtraction = 1; + } else { + fromSubtraction = 2; + } + } + + if (f instanceof SumSubtraction) { + + } + + Function fnc = expr; + if (fnc instanceof Sum) { + Function a = ((Sum)fnc).getParameter1(); + Function b = ((Sum)fnc).getParameter2(); + Function fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); + if (fromSubtraction > 0) { + subtraction = new Subtraction(root, ((FunctionOperator)f).getParameter1(), fnc2); + result.add(subtraction); + } else { + result.add(fnc2); + } + } else if (fnc instanceof Subtraction) { + Function a = ((Subtraction)fnc).getParameter1(); + Function b = ((Subtraction)fnc).getParameter2(); + Function fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); + if (fromSubtraction > 0) { + subtraction = new Subtraction(root, ((FunctionOperator)f).getParameter1(), fnc2); + result.add(subtraction); + } else { + result.add(fnc2); + } + } else if (fnc instanceof SumSubtraction) { + Function a = ((SumSubtraction)fnc).getParameter1(); + Function b = ((SumSubtraction)fnc).getParameter2(); + Function fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); + Function fnc3 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); + if (fromSubtraction > 0) { + subtraction = new SumSubtraction(root, ((FunctionOperator)f).getParameter1(), fnc2); + result.add(subtraction); + subtraction = new SumSubtraction(root, ((FunctionOperator)f).getParameter1(), fnc3); + result.add(subtraction); + result.add(subtraction); + } else { + result.add(fnc2); + result.add(fnc2); + } + } + return result; + } else { + return null; + } + + } +} diff --git a/src/main/resources/rules/FractionsRule2.java b/src/main/resources/rules/FractionsRule2.java index 971f2148..a4e3735e 100644 --- a/src/main/resources/rules/FractionsRule2.java +++ b/src/main/resources/rules/FractionsRule2.java @@ -4,62 +4,58 @@ SETTINGS: (please don't move this part) */ //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 + * a / 1 = a * * @author Andrea Cavalli * */ public class FractionsRule2 implements Rule { - - public static final long serialVersionUID = 11239034890L; - + // Rule name @Override public String getRuleName() { return "FractionsRule2"; } + // Rule type @Override public RuleType getRuleType() { return RuleType.CALCULATION; } - + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override public ObjectArrayList execute(Function f) { boolean isExecutable = false; - MathContext root = f.getMathContext(); - if (ScriptUtils.instanceOf(f, Division.class)) { + if (f instanceof Division) { 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 (fnc.getParameter2() instanceof Number) { + Number numb = (Number) fnc.getParameter2(); + if (numb.equals(new Number(f.getMathContext(), 1))) { + isExecutable = true; } } } if (isExecutable) { ObjectArrayList result = new ObjectArrayList<>(); - result.add(new Number(f.getMathContext(), 0)); + Division fnc = (Division) f; + result.add(fnc.getParameter1()); return result; } else { return null;