Finished adding the rules of the functions
This commit is contained in:
parent
738a1f3982
commit
f952f51ba8
@ -8,6 +8,10 @@ functions/Negative
|
|||||||
functions/Number
|
functions/Number
|
||||||
functions/Power
|
functions/Power
|
||||||
functions/Root
|
functions/Root
|
||||||
|
functions/Subtraction
|
||||||
|
functions/Sum
|
||||||
|
functions/SumSubtraction
|
||||||
|
functions/Variable
|
||||||
ExpandRule1
|
ExpandRule1
|
||||||
ExpandRule2
|
ExpandRule2
|
||||||
ExpandRule5
|
ExpandRule5
|
|
52
src/main/resources/rules/functions/Subtraction.js
Normal file
52
src/main/resources/rules/functions/Subtraction.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
|
||||||
|
var Sum = org.warp.picalculator.math.functions.Sum;
|
||||||
|
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
|
||||||
|
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Division = org.warp.picalculator.math.functions.Division;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Function> 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));
|
52
src/main/resources/rules/functions/Sum.js
Normal file
52
src/main/resources/rules/functions/Sum.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
|
||||||
|
var Sum = org.warp.picalculator.math.functions.Sum;
|
||||||
|
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
|
||||||
|
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Division = org.warp.picalculator.math.functions.Division;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Function> 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));
|
53
src/main/resources/rules/functions/SumSubtraction.js
Normal file
53
src/main/resources/rules/functions/SumSubtraction.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// Imports
|
||||||
|
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
|
||||||
|
var ScriptUtils = org.warp.picalculator.ScriptUtils;
|
||||||
|
var Rule = org.warp.picalculator.math.rules.Rule;
|
||||||
|
var RuleType = org.warp.picalculator.math.rules.RuleType;
|
||||||
|
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
|
||||||
|
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
|
||||||
|
var Sum = org.warp.picalculator.math.functions.Sum;
|
||||||
|
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
|
||||||
|
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
|
||||||
|
var Number = org.warp.picalculator.math.functions.Number;
|
||||||
|
var Division = org.warp.picalculator.math.functions.Division;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<Function> 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));
|
55
src/main/resources/rules/functions/Variable.js
Normal file
55
src/main/resources/rules/functions/Variable.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
// 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<Function> 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));
|
Loading…
x
Reference in New Issue
Block a user