mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-11 01:35:18 +00:00
Merge branch 'plugins'
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A plugin class that can be externally implemented and loaded via the
|
||||
@@ -17,11 +19,19 @@ public abstract class Plugin {
|
||||
* A hash map of functions mapped to their string names.
|
||||
*/
|
||||
private HashMap<String, Function> functions;
|
||||
/**
|
||||
* A hash map of operators mapped to their string names.
|
||||
*/
|
||||
private HashMap<String, Operator> operators;
|
||||
/**
|
||||
* The plugin manager in which to search for functions
|
||||
* not inside this package,
|
||||
*/
|
||||
private PluginManager manager;
|
||||
/**
|
||||
* Whether this plugin has been loaded.
|
||||
*/
|
||||
private boolean enabled;
|
||||
|
||||
private Plugin(){ }
|
||||
|
||||
@@ -32,15 +42,24 @@ public abstract class Plugin {
|
||||
public Plugin(PluginManager manager) {
|
||||
this.manager = manager;
|
||||
functions = new HashMap<>();
|
||||
operators = new HashMap<>();
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the current plugin provides the given function name.
|
||||
* @param functionName the name of the function provided.
|
||||
* @return true of the function exists, false if it doesn't.
|
||||
* Gets the list of functions provided by this plugin.
|
||||
* @return the list of registered functions.
|
||||
*/
|
||||
public final boolean hasFunction(String functionName) {
|
||||
return functions.containsKey(functionName);
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,37 +71,91 @@ public abstract class Plugin {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the function, loading the necessary instances
|
||||
* of functions.
|
||||
*/
|
||||
public final void enable(){
|
||||
if(enabled) return;
|
||||
onEnable();
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the plugin, clearing loaded data store by default
|
||||
* and calling its disable() method.
|
||||
*/
|
||||
public final void disable(){
|
||||
if(!enabled) return;
|
||||
onDisable();
|
||||
functions.clear();
|
||||
operators.clear();
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers a function abstract class with the
|
||||
* plugin internally, which makes it accessible to the plugin manager.
|
||||
* @param name the name to register by.
|
||||
* @param toRegister the function implementation.
|
||||
* @return true if the function was registered successfully, false if not.
|
||||
*/
|
||||
protected final boolean registerFunction(String name, Function toRegister) {
|
||||
if(functionFor(name) == null){
|
||||
functions.put(name, toRegister);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
protected final void registerFunction(String name, Function toRegister) {
|
||||
functions.put(name, toRegister);
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers an operator abstract class
|
||||
* with the plugin internally, which makes it accessible to
|
||||
* the plugin manager.
|
||||
* @param name the name of the operator.
|
||||
* @param operator the operator to register.
|
||||
*/
|
||||
protected final void registerOperator(String name, Operator operator) {
|
||||
operators.put(name, operator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given function name.
|
||||
* This can be used by the plugins internally in order to call functions
|
||||
* they do not provide.
|
||||
* @param name then name for which to search
|
||||
* @param name the name for which to search
|
||||
* @return the resulting function, or null if none was found for that name.
|
||||
*/
|
||||
protected final Function functionFor(String name) {
|
||||
return manager.functionFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given operator name.
|
||||
* This can be used by the plugins internally in order to call
|
||||
* operations they do not provide.
|
||||
* @param name the name for which to search
|
||||
* @return the resulting operator, or null if none was found for that name.
|
||||
*/
|
||||
protected final Operator operatorFor(String name) {
|
||||
return manager.operatorFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method to be overridden by plugin implementation, in which the plugins
|
||||
* are supposed to register the functions they provide and do any other
|
||||
* necessary setup.
|
||||
*/
|
||||
public abstract void load();
|
||||
public abstract void onEnable();
|
||||
|
||||
/**
|
||||
* Abstract method overridden by the plugin implementation, in which the plugins
|
||||
* are supposed to dispose of loaded functions, operators, and macros.
|
||||
*/
|
||||
public abstract void onDisable();
|
||||
|
||||
}
|
||||
|
||||
20
src/org/nwapw/abacus/plugin/PluginListener.java
Normal file
20
src/org/nwapw/abacus/plugin/PluginListener.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
/**
|
||||
* A listener that responds to changes in the PluginManager.
|
||||
*/
|
||||
public interface PluginListener {
|
||||
|
||||
/**
|
||||
* Called when the PluginManager loads plugins.
|
||||
* @param manager the manager that fired the event.
|
||||
*/
|
||||
public void onLoad(PluginManager manager);
|
||||
|
||||
/**
|
||||
* Called when the PluginManager unloads all its plugins.
|
||||
* @param manager the manager that fired the event.
|
||||
*/
|
||||
public void onUnload(PluginManager manager);
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A class that controls instances of plugins, allowing for them
|
||||
@@ -21,6 +21,23 @@ public class PluginManager {
|
||||
* that is, found in a plugin and returned.
|
||||
*/
|
||||
private HashMap<String, Function> cachedFunctions;
|
||||
/**
|
||||
* List of operators tha have been cached,
|
||||
* that is, found in a plugin and returned.
|
||||
*/
|
||||
private HashMap<String, Operator> cachedOperators;
|
||||
/**
|
||||
* List of all functions loaded by the plugins.
|
||||
*/
|
||||
private HashSet<String> allFunctions;
|
||||
/**
|
||||
* List of all operators loaded by the plugins.
|
||||
*/
|
||||
private HashSet<String> allOperators;
|
||||
/**
|
||||
* The list of plugin listeners attached to this instance.
|
||||
*/
|
||||
private HashSet<PluginListener> listeners;
|
||||
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
@@ -28,27 +45,58 @@ public class PluginManager {
|
||||
public PluginManager(){
|
||||
plugins = new ArrayList<>();
|
||||
cachedFunctions = new HashMap<>();
|
||||
cachedOperators = new HashMap<>();
|
||||
allFunctions = new HashSet<>();
|
||||
allOperators = new HashSet<>();
|
||||
listeners = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the plugin list for a certain value, retrieving the Plugin's
|
||||
* list of items of the type using the setFunction and getting the value
|
||||
* of it is available via getFunction. If the value is contained
|
||||
* 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
|
||||
* @return the retrieved element, or null if it was not found.
|
||||
*/
|
||||
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
||||
java.util.function.Function<Plugin, Set<String>> setFunction,
|
||||
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
||||
String 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.
|
||||
* @param name the name of the function
|
||||
* @return the function under the given name.
|
||||
*/
|
||||
public Function functionFor(String name){
|
||||
if(cachedFunctions.containsKey(name)) {
|
||||
return cachedFunctions.get(name);
|
||||
}
|
||||
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
||||
}
|
||||
|
||||
Function loadedFunction = null;
|
||||
for(Plugin plugin : plugins){
|
||||
if(plugin.hasFunction(name)){
|
||||
loadedFunction = plugin.getFunction(name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
cachedFunctions.put(name, loadedFunction);
|
||||
return loadedFunction;
|
||||
/**
|
||||
* Gets an operator under the given name.
|
||||
* @param name the name of the operator.
|
||||
* @return the operator under the given name.
|
||||
*/
|
||||
public Operator operatorFor(String name){
|
||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,8 +104,6 @@ public class PluginManager {
|
||||
* @param plugin the plugin to add.
|
||||
*/
|
||||
public void addInstantiated(Plugin plugin){
|
||||
plugin.load();
|
||||
cachedFunctions.clear();
|
||||
plugins.add(plugin);
|
||||
}
|
||||
|
||||
@@ -75,4 +121,66 @@ public class PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void load(){
|
||||
for(Plugin plugin : plugins) plugin.enable();
|
||||
for(Plugin plugin : plugins){
|
||||
allFunctions.addAll(plugin.providedFunctions());
|
||||
allOperators.addAll(plugin.providedOperators());
|
||||
}
|
||||
listeners.forEach(e -> e.onLoad(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unloads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void unload(){
|
||||
for(Plugin plugin : plugins) plugin.disable();
|
||||
allFunctions.clear();
|
||||
allOperators.clear();
|
||||
listeners.forEach(e -> e.onUnload(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void reload(){
|
||||
unload();
|
||||
reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the functions loaded by the Plugin Manager.
|
||||
* @return the set of all functions that were loaded.
|
||||
*/
|
||||
public HashSet<String> getAllFunctions() {
|
||||
return allFunctions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the operators loaded by the Plugin Manager.
|
||||
* @return the set of all operators that were loaded.
|
||||
*/
|
||||
public HashSet<String> getAllOperators() {
|
||||
return allOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a plugin change listener to this plugin manager.
|
||||
* @param listener the listener to add.
|
||||
*/
|
||||
public void addListener(PluginListener listener){
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the plugin change listener from this plugin manager.
|
||||
* @param listener the listener to remove.
|
||||
*/
|
||||
public void removeListener(PluginListener listener){
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public class StandardPlugin extends Plugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
public void onEnable() {
|
||||
registerFunction("+", new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
@@ -234,11 +234,16 @@ public class StandardPlugin extends Plugin {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nth term of the Taylor series (centered at 0) of e^x
|
||||
* @param n the term required (n >= 0).
|
||||
* @param x the real number at which the series is evaluated.
|
||||
* @return
|
||||
* @return the nth term of the series.
|
||||
*/
|
||||
private NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
||||
return x.intPow(n).divide(this.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||
@@ -249,7 +254,7 @@ public class StandardPlugin extends Plugin {
|
||||
* such that the error is at most maxError.
|
||||
* @param maxError Maximum error permissible (This should probably be positive.)
|
||||
* @param x where the function is evaluated.
|
||||
* @return
|
||||
* @return the number of terms needed to evaluated the exponential function.
|
||||
*/
|
||||
private int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
||||
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
||||
@@ -284,7 +289,7 @@ public class StandardPlugin extends Plugin {
|
||||
/**
|
||||
* Returns the maximum error based on the precision of the class of number.
|
||||
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
||||
* @return
|
||||
* @return the maximum error.
|
||||
*/
|
||||
private NumberInterface getMaxError(NumberInterface number){
|
||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.precision());
|
||||
|
||||
Reference in New Issue
Block a user