mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 16:45:21 +00:00
Compare commits
18 Commits
stoppable-
...
remove-cac
| Author | SHA1 | Date | |
|---|---|---|---|
| 35254d3e99 | |||
| 44f018060d | |||
|
|
6a15c266c4 | ||
| 9f61fc5dbe | |||
| bae6ee5526 | |||
| 4f94700aef | |||
| b7152da58d | |||
| d17a8a9fa7 | |||
| 71c9f0d141 | |||
| fb02984e60 | |||
| a9ac4681f0 | |||
| 62d7053441 | |||
| f3cbb600ac | |||
| abc0e2d59f | |||
| f7d1be086b | |||
| 21a925d6d2 | |||
| 0d21898f20 | |||
| 3e39087fde |
@@ -6,16 +6,12 @@ import org.nwapw.abacus.number.NumberInterface;
|
|||||||
import org.nwapw.abacus.parsing.LexerTokenizer;
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
||||||
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
||||||
import org.nwapw.abacus.parsing.TreeBuilder;
|
import org.nwapw.abacus.parsing.TreeBuilder;
|
||||||
import org.nwapw.abacus.plugin.ClassFinder;
|
|
||||||
import org.nwapw.abacus.plugin.NumberImplementation;
|
import org.nwapw.abacus.plugin.NumberImplementation;
|
||||||
import org.nwapw.abacus.plugin.PluginManager;
|
import org.nwapw.abacus.plugin.PluginManager;
|
||||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
import org.nwapw.abacus.tree.NumberReducer;
|
import org.nwapw.abacus.tree.NumberReducer;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main calculator class. This is responsible
|
* The main calculator class. This is responsible
|
||||||
* for piecing together all of the components, allowing
|
* for piecing together all of the components, allowing
|
||||||
@@ -27,10 +23,6 @@ public class Abacus {
|
|||||||
* The default number implementation to be used if no other one is available / selected.
|
* The default number implementation to be used if no other one is available / selected.
|
||||||
*/
|
*/
|
||||||
public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE;
|
public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE;
|
||||||
/**
|
|
||||||
* The file used for saving and loading configuration.
|
|
||||||
*/
|
|
||||||
public static final File CONFIG_FILE = new File("config.toml");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The plugin manager responsible for
|
* The plugin manager responsible for
|
||||||
@@ -54,26 +46,19 @@ public class Abacus {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of the Abacus calculator.
|
* Creates a new instance of the Abacus calculator.
|
||||||
|
*
|
||||||
|
* @param configuration the configuration object for this Abacus instance.
|
||||||
*/
|
*/
|
||||||
public Abacus() {
|
public Abacus(Configuration configuration) {
|
||||||
pluginManager = new PluginManager(this);
|
pluginManager = new PluginManager(this);
|
||||||
numberReducer = new NumberReducer(this);
|
numberReducer = new NumberReducer(this);
|
||||||
configuration = new Configuration(CONFIG_FILE);
|
this.configuration = new Configuration(configuration);
|
||||||
configuration.saveTo(CONFIG_FILE);
|
|
||||||
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
||||||
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
||||||
|
|
||||||
pluginManager.addListener(lexerTokenizer);
|
|
||||||
pluginManager.addListener(shuntingYardParser);
|
pluginManager.addListener(shuntingYardParser);
|
||||||
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
pluginManager.addListener(lexerTokenizer);
|
||||||
try {
|
|
||||||
ClassFinder.loadJars("plugins")
|
|
||||||
.forEach(plugin -> pluginManager.addClass(plugin));
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
pluginManager.load();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -39,6 +39,15 @@ public class Configuration {
|
|||||||
*/
|
*/
|
||||||
private Set<String> disabledPlugins = new HashSet<>();
|
private Set<String> disabledPlugins = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration form the given configuration.
|
||||||
|
*
|
||||||
|
* @param copyFrom the configuration to copy.
|
||||||
|
*/
|
||||||
|
public Configuration(Configuration copyFrom){
|
||||||
|
copyFrom(copyFrom);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new configuration with the given values.
|
* Creates a new configuration with the given values.
|
||||||
*
|
*
|
||||||
@@ -85,6 +94,15 @@ public class Configuration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of this configuration as a string.
|
||||||
|
*
|
||||||
|
* @return the string that represents this configuration.
|
||||||
|
*/
|
||||||
|
public String asTomlString(){
|
||||||
|
return TOML_WRITER.write(this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation from this configuration.
|
* Gets the number implementation from this configuration.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ public class Operator {
|
|||||||
* Creates a new operator with the given parameters.
|
* Creates a new operator with the given parameters.
|
||||||
*
|
*
|
||||||
* @param associativity the associativity of the operator.
|
* @param associativity the associativity of the operator.
|
||||||
|
* @param operatorType the type of this operator, like binary infix or unary postfix.
|
||||||
* @param precedence the precedence of the operator.
|
* @param precedence the precedence of the operator.
|
||||||
* @param function the function that the operator calls.
|
* @param function the function that the operator calls.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -13,10 +13,14 @@ import org.nwapw.abacus.Abacus;
|
|||||||
import org.nwapw.abacus.config.Configuration;
|
import org.nwapw.abacus.config.Configuration;
|
||||||
import org.nwapw.abacus.number.ComputationInterruptedException;
|
import org.nwapw.abacus.number.ComputationInterruptedException;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
import org.nwapw.abacus.plugin.ClassFinder;
|
||||||
import org.nwapw.abacus.plugin.PluginListener;
|
import org.nwapw.abacus.plugin.PluginListener;
|
||||||
import org.nwapw.abacus.plugin.PluginManager;
|
import org.nwapw.abacus.plugin.PluginManager;
|
||||||
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
@@ -26,6 +30,10 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public class AbacusController implements PluginListener {
|
public class AbacusController implements PluginListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The file used for saving and loading configuration.
|
||||||
|
*/
|
||||||
|
public static final File CONFIG_FILE = new File("config.toml");
|
||||||
/**
|
/**
|
||||||
* The title for the apply alert dialog.
|
* The title for the apply alert dialog.
|
||||||
*/
|
*/
|
||||||
@@ -205,9 +213,16 @@ public class AbacusController implements PluginListener {
|
|||||||
if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
|
if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
abacus = new Abacus();
|
abacus = new Abacus(new Configuration(CONFIG_FILE));
|
||||||
abacus.getPluginManager().addListener(this);
|
PluginManager abacusPluginManager = abacus.getPluginManager();
|
||||||
abacus.getPluginManager().reload();
|
abacusPluginManager.addListener(this);
|
||||||
|
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
||||||
|
try {
|
||||||
|
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
abacusPluginManager.reload();
|
||||||
|
|
||||||
changesMade = false;
|
changesMade = false;
|
||||||
reloadAlertShown = false;
|
reloadAlertShown = false;
|
||||||
@@ -255,7 +270,7 @@ public class AbacusController implements PluginListener {
|
|||||||
for (ToggleablePlugin pluginEntry : enabledPlugins) {
|
for (ToggleablePlugin pluginEntry : enabledPlugins) {
|
||||||
if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
||||||
}
|
}
|
||||||
configuration.saveTo(Abacus.CONFIG_FILE);
|
configuration.saveTo(CONFIG_FILE);
|
||||||
changesMade = false;
|
changesMade = false;
|
||||||
reloadAlertShown = false;
|
reloadAlertShown = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class ValueNode<T> extends PatternNode<T> {
|
|||||||
/**
|
/**
|
||||||
* Creates a new node that matches the given character.
|
* Creates a new node that matches the given character.
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value the character value of the node.
|
||||||
*/
|
*/
|
||||||
public ValueNode(char value) {
|
public ValueNode(char value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ public abstract class NumberInterface {
|
|||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* Also, checks if the thread has been interrupted, and if so, throws
|
||||||
* an exception.
|
* an exception.
|
||||||
*
|
*
|
||||||
* @return the least integer >= the number, if int can hold the value.
|
* @return the least integer bigger or equal to the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface ceiling(){
|
public final NumberInterface ceiling(){
|
||||||
checkInterrupted();
|
checkInterrupted();
|
||||||
@@ -192,7 +192,7 @@ public abstract class NumberInterface {
|
|||||||
/**
|
/**
|
||||||
* Return the greatest integer less than or equal to the number.
|
* Return the greatest integer less than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the greatest int >= the number, if int can hold the value.
|
* @return the greatest integer smaller or equal the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface floorInternal();
|
protected abstract NumberInterface floorInternal();
|
||||||
|
|
||||||
@@ -230,7 +230,7 @@ public abstract class NumberInterface {
|
|||||||
* Returns the integer representation of this number, discarding any fractional part,
|
* Returns the integer representation of this number, discarding any fractional part,
|
||||||
* if int can hold the value.
|
* if int can hold the value.
|
||||||
*
|
*
|
||||||
* @return
|
* @return the integer value of this number.
|
||||||
*/
|
*/
|
||||||
public abstract int intValue();
|
public abstract int intValue();
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public abstract class NumberImplementation {
|
|||||||
* Creates a new number implementation with the given data.
|
* Creates a new number implementation with the given data.
|
||||||
*
|
*
|
||||||
* @param implementation the implementation class.
|
* @param implementation the implementation class.
|
||||||
* @param priority the priority, higher -> more likely to be converted into.
|
* @param priority the priority, higher means more likely to be converted into.
|
||||||
*/
|
*/
|
||||||
public NumberImplementation(Class<? extends NumberInterface> implementation, int priority) {
|
public NumberImplementation(Class<? extends NumberInterface> implementation, int priority) {
|
||||||
this.implementation = implementation;
|
this.implementation = implementation;
|
||||||
|
|||||||
@@ -17,18 +17,6 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public abstract class Plugin {
|
public abstract class Plugin {
|
||||||
|
|
||||||
/**
|
|
||||||
* A hash map of functions mapped to their string names.
|
|
||||||
*/
|
|
||||||
private Map<String, Function> functions;
|
|
||||||
/**
|
|
||||||
* A hash map of operators mapped to their string names.
|
|
||||||
*/
|
|
||||||
private Map<String, Operator> operators;
|
|
||||||
/**
|
|
||||||
* The map of the number implementations this plugin provides.
|
|
||||||
*/
|
|
||||||
private Map<String, NumberImplementation> numberImplementations;
|
|
||||||
/**
|
/**
|
||||||
* The plugin manager in which to search for functions
|
* The plugin manager in which to search for functions
|
||||||
* not inside this package,
|
* not inside this package,
|
||||||
@@ -49,69 +37,9 @@ public abstract class Plugin {
|
|||||||
*/
|
*/
|
||||||
public Plugin(PluginManager manager) {
|
public Plugin(PluginManager manager) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
functions = new HashMap<>();
|
|
||||||
operators = new HashMap<>();
|
|
||||||
numberImplementations = new HashMap<>();
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of functions provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered functions.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedFunctions() {
|
|
||||||
return functions.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of functions provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered functions.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedOperators() {
|
|
||||||
return operators.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of number implementations provided by this plugin.
|
|
||||||
*
|
|
||||||
* @return the list of registered number implementations.
|
|
||||||
*/
|
|
||||||
public final Set<String> providedNumberImplementations() {
|
|
||||||
return numberImplementations.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a function under the given function name.
|
|
||||||
*
|
|
||||||
* @param functionName the name of the function to get
|
|
||||||
* @return the function, or null if this plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final Function getFunction(String functionName) {
|
|
||||||
return functions.get(functionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an operator under the given operator name.
|
|
||||||
*
|
|
||||||
* @param operatorName the name of the operator to get.
|
|
||||||
* @return the operator, or null if this plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final Operator getOperator(String operatorName) {
|
|
||||||
return operators.get(operatorName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number implementation under the given name.
|
|
||||||
*
|
|
||||||
* @param name the name of the number implementation to look up.
|
|
||||||
* @return the number implementation associated with that name, or null if the plugin doesn't provide it.
|
|
||||||
*/
|
|
||||||
public final NumberImplementation getNumberImplementation(String name) {
|
|
||||||
return numberImplementations.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the function, loading the necessary instances
|
* Enables the function, loading the necessary instances
|
||||||
* of functions.
|
* of functions.
|
||||||
@@ -129,8 +57,6 @@ public abstract class Plugin {
|
|||||||
public final void disable() {
|
public final void disable() {
|
||||||
if (!enabled) return;
|
if (!enabled) return;
|
||||||
onDisable();
|
onDisable();
|
||||||
functions.clear();
|
|
||||||
operators.clear();
|
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +68,7 @@ public abstract class Plugin {
|
|||||||
* @param toRegister the function implementation.
|
* @param toRegister the function implementation.
|
||||||
*/
|
*/
|
||||||
protected final void registerFunction(String name, Function toRegister) {
|
protected final void registerFunction(String name, Function toRegister) {
|
||||||
functions.put(name, toRegister);
|
manager.registerFunction(name, toRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -154,7 +80,7 @@ public abstract class Plugin {
|
|||||||
* @param operator the operator to register.
|
* @param operator the operator to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerOperator(String name, Operator operator) {
|
protected final void registerOperator(String name, Operator operator) {
|
||||||
operators.put(name, operator);
|
manager.registerOperator(name, operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -165,7 +91,7 @@ public abstract class Plugin {
|
|||||||
* @param implementation the actual implementation class to register.
|
* @param implementation the actual implementation class to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
|
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
|
||||||
numberImplementations.put(name, implementation);
|
manager.registerNumberImplementation(name, implementation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -213,7 +139,7 @@ public abstract class Plugin {
|
|||||||
* @param forClass the class to which to find the pi instance.
|
* @param forClass the class to which to find the pi instance.
|
||||||
* @return the pi value for the given class.
|
* @return the pi value for the given class.
|
||||||
*/
|
*/
|
||||||
protected final NumberInterface getPi(Class<? extends NumberInterface> forClass) {
|
protected final NumberInterface piFor(Class<? extends NumberInterface> forClass) {
|
||||||
return manager.piFor(forClass);
|
return manager.piFor(forClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,20 +23,17 @@ public class PluginManager {
|
|||||||
*/
|
*/
|
||||||
private Set<Plugin> plugins;
|
private Set<Plugin> plugins;
|
||||||
/**
|
/**
|
||||||
* List of functions that have been cached,
|
* The map of functions registered by the plugins.
|
||||||
* that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, Function> cachedFunctions;
|
private Map<String, Function> registeredFunctions;
|
||||||
/**
|
/**
|
||||||
* List of operators that have been cached,
|
* The map of operators registered by the plugins
|
||||||
* that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, Operator> cachedOperators;
|
private Map<String, Operator> registeredOperators;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have
|
* The map of number implementations registered by the plugins.
|
||||||
* been cached, that is, found in a plugin and returned.
|
|
||||||
*/
|
*/
|
||||||
private Map<String, NumberImplementation> cachedNumberImplementations;
|
private Map<String, NumberImplementation> registeredNumberImplementations;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have been
|
* The list of number implementations that have been
|
||||||
* found by their implementation class.
|
* found by their implementation class.
|
||||||
@@ -46,18 +43,6 @@ public class PluginManager {
|
|||||||
* The pi values for each implementation class that have already been computer.
|
* The pi values for each implementation class that have already been computer.
|
||||||
*/
|
*/
|
||||||
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
||||||
/**
|
|
||||||
* List of all functions loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allFunctions;
|
|
||||||
/**
|
|
||||||
* List of all operators loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allOperators;
|
|
||||||
/**
|
|
||||||
* List of all the number implementations loaded by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<String> allNumberImplementations;
|
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
@@ -70,84 +55,73 @@ public class PluginManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new plugin manager.
|
* Creates a new plugin manager.
|
||||||
|
*
|
||||||
|
* @param abacus the abacus instance.
|
||||||
*/
|
*/
|
||||||
public PluginManager(Abacus abacus) {
|
public PluginManager(Abacus abacus) {
|
||||||
this.abacus = abacus;
|
this.abacus = abacus;
|
||||||
loadedPluginClasses = new HashSet<>();
|
loadedPluginClasses = new HashSet<>();
|
||||||
plugins = new HashSet<>();
|
plugins = new HashSet<>();
|
||||||
cachedFunctions = new HashMap<>();
|
registeredFunctions = new HashMap<>();
|
||||||
cachedOperators = new HashMap<>();
|
registeredOperators = new HashMap<>();
|
||||||
cachedNumberImplementations = new HashMap<>();
|
registeredNumberImplementations = new HashMap<>();
|
||||||
cachedInterfaceImplementations = new HashMap<>();
|
cachedInterfaceImplementations = new HashMap<>();
|
||||||
cachedPi = new HashMap<>();
|
cachedPi = new HashMap<>();
|
||||||
allFunctions = new HashSet<>();
|
|
||||||
allOperators = new HashSet<>();
|
|
||||||
allNumberImplementations = new HashSet<>();
|
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the plugin list for a certain value, retrieving the Plugin's
|
* Registers a function under the given name.
|
||||||
* list of items of the type using the setFunction and getting the value
|
* @param name the name of the function.
|
||||||
* of it is available via getFunction. If the value is contained
|
* @param function the function to register.
|
||||||
* in the cache, it returns the cached value instead.
|
|
||||||
*
|
|
||||||
* @param plugins the plugin list to search.
|
|
||||||
* @param cache the cache to use
|
|
||||||
* @param setFunction the function to retrieve a set of available T's from the plugin
|
|
||||||
* @param getFunction the function to get the T value under the given name
|
|
||||||
* @param name the name to search for
|
|
||||||
* @param <T> the type of element being search
|
|
||||||
* @param <K> the type of key that the cache is indexed by.
|
|
||||||
* @return the retrieved element, or null if it was not found.
|
|
||||||
*/
|
*/
|
||||||
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
|
public void registerFunction(String name, Function function){
|
||||||
java.util.function.Function<Plugin, Set<K>> setFunction,
|
registeredFunctions.put(name, function);
|
||||||
java.util.function.BiFunction<Plugin, K, T> getFunction,
|
|
||||||
K name) {
|
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
|
||||||
|
|
||||||
T loadedValue = null;
|
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (setFunction.apply(plugin).contains(name)) {
|
|
||||||
loadedValue = getFunction.apply(plugin, name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cache.put(name, loadedValue);
|
|
||||||
return loadedValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a function under the given name.
|
* Registers an operator under the given name.
|
||||||
*
|
|
||||||
* @param name the name of the function
|
|
||||||
* @return the function under the given name.
|
|
||||||
*/
|
|
||||||
public Function functionFor(String name) {
|
|
||||||
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets an operator under the given name.
|
|
||||||
*
|
|
||||||
* @param name the name of the operator.
|
* @param name the name of the operator.
|
||||||
* @return the operator under the given name.
|
* @param operator the operator to register.
|
||||||
*/
|
*/
|
||||||
public Operator operatorFor(String name) {
|
public void registerOperator(String name, Operator operator){
|
||||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
registeredOperators.put(name, operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation under the given name.
|
* Registers a number implementation under the given name.
|
||||||
*
|
* @param name the name of the number implementation.
|
||||||
* @param name the name of the implementation.
|
* @param implementation the number implementation to register.
|
||||||
* @return the implementation.
|
|
||||||
*/
|
*/
|
||||||
public NumberImplementation numberImplementationFor(String name) {
|
public void registerNumberImplementation(String name, NumberImplementation implementation){
|
||||||
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
|
registeredNumberImplementations.put(name, implementation);
|
||||||
Plugin::getNumberImplementation, name);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the function registered under the given name.
|
||||||
|
* @param name the name of the function.
|
||||||
|
* @return the function, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public Function functionFor(String name){
|
||||||
|
return registeredFunctions.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operator registered under the given name.
|
||||||
|
* @param name the name of the operator.
|
||||||
|
* @return the operator, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public Operator operatorFor(String name){
|
||||||
|
return registeredOperators.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number implementation registered under the given name.
|
||||||
|
* @param name the name of the number implementation.
|
||||||
|
* @return the number implementation, or null if it was not found.
|
||||||
|
*/
|
||||||
|
public NumberImplementation numberImplementationFor(String name){
|
||||||
|
return registeredNumberImplementations.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,14 +133,11 @@ public class PluginManager {
|
|||||||
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
|
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
|
||||||
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
||||||
NumberImplementation toReturn = null;
|
NumberImplementation toReturn = null;
|
||||||
outside:
|
for(String key : registeredNumberImplementations.keySet()){
|
||||||
for (Plugin plugin : plugins) {
|
NumberImplementation implementation = registeredNumberImplementations.get(key);
|
||||||
for (String implementationName : plugin.providedNumberImplementations()) {
|
if(implementation.getImplementation() == name) {
|
||||||
NumberImplementation implementation = plugin.getNumberImplementation(implementationName);
|
toReturn = implementation;
|
||||||
if (implementation.getImplementation().equals(name)) {
|
break;
|
||||||
toReturn = implementation;
|
|
||||||
break outside;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cachedInterfaceImplementations.put(name, toReturn);
|
cachedInterfaceImplementations.put(name, toReturn);
|
||||||
@@ -225,12 +196,6 @@ public class PluginManager {
|
|||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.enable();
|
plugin.enable();
|
||||||
}
|
}
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
|
||||||
allFunctions.addAll(plugin.providedFunctions());
|
|
||||||
allOperators.addAll(plugin.providedOperators());
|
|
||||||
allNumberImplementations.addAll(plugin.providedNumberImplementations());
|
|
||||||
}
|
|
||||||
listeners.forEach(e -> e.onLoad(this));
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,14 +209,8 @@ public class PluginManager {
|
|||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.disable();
|
plugin.disable();
|
||||||
}
|
}
|
||||||
cachedFunctions.clear();
|
|
||||||
cachedOperators.clear();
|
|
||||||
cachedNumberImplementations.clear();
|
|
||||||
cachedInterfaceImplementations.clear();
|
cachedInterfaceImplementations.clear();
|
||||||
cachedPi.clear();
|
cachedPi.clear();
|
||||||
allFunctions.clear();
|
|
||||||
allOperators.clear();
|
|
||||||
allNumberImplementations.clear();
|
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,7 +228,7 @@ public class PluginManager {
|
|||||||
* @return the set of all functions that were loaded.
|
* @return the set of all functions that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllFunctions() {
|
public Set<String> getAllFunctions() {
|
||||||
return allFunctions;
|
return registeredFunctions.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -278,7 +237,7 @@ public class PluginManager {
|
|||||||
* @return the set of all operators that were loaded.
|
* @return the set of all operators that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllOperators() {
|
public Set<String> getAllOperators() {
|
||||||
return allOperators;
|
return registeredOperators.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -287,7 +246,7 @@ public class PluginManager {
|
|||||||
* @return the set of all implementations that were loaded.
|
* @return the set of all implementations that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllNumberImplementations() {
|
public Set<String> getAllNumberImplementations() {
|
||||||
return allNumberImplementations;
|
return registeredNumberImplementations.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -358,7 +358,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface pi = getPi(params[0].getClass());
|
NumberInterface pi = piFor(params[0].getClass());
|
||||||
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
||||||
NumberInterface theta = getSmallAngle(params[0], pi);
|
NumberInterface theta = getSmallAngle(params[0], pi);
|
||||||
//System.out.println(theta);
|
//System.out.println(theta);
|
||||||
@@ -382,7 +382,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return functionSin.apply(getPi(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
|
return functionSin.apply(piFor(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
|
||||||
.subtract(params[0]));
|
.subtract(params[0]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -439,7 +439,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return functionCos.apply(params[0]).divide(functionCos.apply(params[0]));
|
return functionCos.apply(params[0]).divide(functionSin.apply(params[0]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -531,22 +531,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
return theta;
|
return theta;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NumberInterface intPow(NumberInterface number, Class<? extends NumberInterface> numberClass, NumberInterface exponent) {
|
|
||||||
if (exponent.compareTo((new NaiveNumber(0)).promoteTo(numberClass)) == 0) {
|
|
||||||
return (new NaiveNumber(1)).promoteTo(numberClass);
|
|
||||||
}
|
|
||||||
boolean takeReciprocal = exponent.compareTo((new NaiveNumber(0)).promoteTo(numberClass)) < 0;
|
|
||||||
exponent = FUNCTION_ABS.apply(exponent);
|
|
||||||
NumberInterface power = number;
|
|
||||||
for (NumberInterface currentExponent = (new NaiveNumber(1)).promoteTo(numberClass); currentExponent.compareTo(exponent) < 0; currentExponent = currentExponent.add((new NaiveNumber(1)).promoteTo(numberClass))) {
|
|
||||||
power = power.multiply(number);
|
|
||||||
}
|
|
||||||
if (takeReciprocal) {
|
|
||||||
power = (new NaiveNumber(1)).promoteTo(numberClass).divide(power);
|
|
||||||
}
|
|
||||||
return power;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
||||||
|
|||||||
104
src/test/java/org/nwapw/abacus/tests/CalculationTests.java
Normal file
104
src/test/java/org/nwapw/abacus/tests/CalculationTests.java
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package org.nwapw.abacus.tests;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.nwapw.abacus.Abacus;
|
||||||
|
import org.nwapw.abacus.config.Configuration;
|
||||||
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
|
public class CalculationTests {
|
||||||
|
|
||||||
|
private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void prepareTests(){
|
||||||
|
abacus.getPluginManager().addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
||||||
|
abacus.getPluginManager().load();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testOutput(String input, String parseOutput, String output){
|
||||||
|
TreeNode parsedTree = abacus.parseString(input);
|
||||||
|
Assert.assertNotNull(parsedTree);
|
||||||
|
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
||||||
|
NumberInterface result = abacus.evaluateTree(parsedTree);
|
||||||
|
Assert.assertNotNull(result);
|
||||||
|
Assert.assertTrue(result.toString().startsWith(output));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testEvalError(String input, String parseOutput){
|
||||||
|
TreeNode parsedTree = abacus.parseString(input);
|
||||||
|
Assert.assertNotNull(parsedTree);
|
||||||
|
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
||||||
|
Assert.assertNull(abacus.evaluateTree(parsedTree));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddition(){
|
||||||
|
testOutput("9.5+10", "(9.5+10)", "19.5");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubtraction(){
|
||||||
|
testOutput("9.5-10", "(9.5-10)", "-0.5");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiplication(){
|
||||||
|
testOutput("9.5*10", "(9.5*10)", "95");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDivision(){
|
||||||
|
testOutput("9.5/2", "(9.5/2)", "4.75");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNegation(){
|
||||||
|
testOutput("-9.5", "(9.5)`", "-9.5");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFactorial(){
|
||||||
|
testOutput("7!", "(7)!", "5040");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAbs(){
|
||||||
|
testOutput("abs(-1)", "abs((1)`)", "1");
|
||||||
|
testOutput("abs(1)", "abs(1)", "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLn(){
|
||||||
|
testEvalError("ln(-1)", "ln((1)`)");
|
||||||
|
testOutput("ln2", "ln(2)", "0.6931471805599453094172321214581765680755");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSqrt(){
|
||||||
|
testOutput("sqrt0", "sqrt(0)", "0");
|
||||||
|
testOutput("sqrt4", "sqrt(4)", "2");
|
||||||
|
testOutput("sqrt2", "sqrt(2)", "1.4142135623730950488016887242096980785696");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExp(){
|
||||||
|
testOutput("exp0", "exp(0)", "1");
|
||||||
|
testOutput("exp1", "exp(1)", "2.718281828459045235360287471352662497757247");
|
||||||
|
testOutput("exp300", "exp(300)", "19424263952412559365842088360176992193662086");
|
||||||
|
testOutput("exp300", "exp(300)", "19424263952412559365842088360176992193662086");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPow(){
|
||||||
|
testOutput("0^2", "(0^2)", "0");
|
||||||
|
testOutput("2^0", "(2^0)", "1");
|
||||||
|
testOutput("2^1", "(2^1)", "2");
|
||||||
|
testOutput("2^-1", "(2^(1)`)", "0.5");
|
||||||
|
testOutput("2^50", "(2^50)", "112589990684262");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import org.junit.Assert;
|
|||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.nwapw.abacus.Abacus;
|
import org.nwapw.abacus.Abacus;
|
||||||
|
import org.nwapw.abacus.config.Configuration;
|
||||||
import org.nwapw.abacus.function.Function;
|
import org.nwapw.abacus.function.Function;
|
||||||
import org.nwapw.abacus.function.Operator;
|
import org.nwapw.abacus.function.Operator;
|
||||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
@@ -18,7 +19,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class TokenizerTests {
|
public class TokenizerTests {
|
||||||
|
|
||||||
private static Abacus abacus = new Abacus();
|
private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
|
||||||
private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||||
private static Function subtractFunction = new Function() {
|
private static Function subtractFunction = new Function() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user