1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-27 00:55:19 +00:00

Compare commits

..

3 Commits

Author SHA1 Message Date
Arthur Drobot
8a9df051cf Optimize pow for integer bases. 2017-08-07 13:43:12 -07:00
Arthur Drobot
4eda15b3fb Modify some functions in PreciseNumber for consistency. 2017-08-07 13:41:45 -07:00
Arthur Drobot
d0ccb8b625 Improve some comments of NumberInterface functions. 2017-08-07 10:54:27 -07:00
11 changed files with 210 additions and 201 deletions

View File

@@ -30,10 +30,6 @@ public class Configuration {
*/ */
private static final TomlWriter TOML_WRITER = new TomlWriter(); private static final TomlWriter TOML_WRITER = new TomlWriter();
/**
* The computation delay for which the thread can run without interruption.
*/
private double computationDelay = 0;
/** /**
* The implementation of the number that should be used. * The implementation of the number that should be used.
*/ */
@@ -55,12 +51,10 @@ public class Configuration {
/** /**
* Creates a new configuration with the given values. * Creates a new configuration with the given values.
* *
* @param computationDelay the delay before the computation gets killed.
* @param numberImplementation the number implementation, like "naive" or "precise" * @param numberImplementation the number implementation, like "naive" or "precise"
* @param disabledPlugins the list of disabled plugins. * @param disabledPlugins the list of disabled plugins.
*/ */
public Configuration(double computationDelay, String numberImplementation, String[] disabledPlugins) { public Configuration(String numberImplementation, String[] disabledPlugins) {
this.computationDelay = computationDelay;
this.numberImplementation = numberImplementation; this.numberImplementation = numberImplementation;
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins)); this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
} }
@@ -81,7 +75,6 @@ public class Configuration {
* @param otherConfiguration the configuration to copy from. * @param otherConfiguration the configuration to copy from.
*/ */
public void copyFrom(Configuration otherConfiguration) { public void copyFrom(Configuration otherConfiguration) {
this.computationDelay = otherConfiguration.computationDelay;
this.numberImplementation = otherConfiguration.numberImplementation; this.numberImplementation = otherConfiguration.numberImplementation;
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins); this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
} }
@@ -137,23 +130,4 @@ public class Configuration {
return disabledPlugins; return disabledPlugins;
} }
/**
* Gets the computation delay specified in the configuration.
*
* @return the computaton delay.
*/
public double getComputationDelay() {
return computationDelay;
}
/**
* Sets the computation delay.
*
* @param computationDelay the new computation delay.
*/
public void setComputationDelay(double computationDelay) {
this.computationDelay = computationDelay;
}
} }

View File

@@ -12,25 +12,13 @@ import javafx.stage.Stage;
*/ */
public class AbacusApplication extends Application { public class AbacusApplication extends Application {
/**
* The controller currently managing the application.
*/
private AbacusController controller;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/abacus.fxml")); Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
Parent parent = loader.load();
controller = loader.getController();
Scene mainScene = new Scene(parent, 320, 480); Scene mainScene = new Scene(parent, 320, 480);
primaryStage.setScene(mainScene); primaryStage.setScene(mainScene);
primaryStage.setTitle("Abacus"); primaryStage.setTitle("Abacus");
primaryStage.show(); primaryStage.show();
} }
@Override
public void stop() throws Exception {
super.stop();
controller.performStop();
}
} }

View File

@@ -1,11 +1,8 @@
package org.nwapw.abacus.fx; package org.nwapw.abacus.fx;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxListCell; import javafx.scene.control.cell.CheckBoxListCell;
@@ -73,8 +70,6 @@ public class AbacusController implements PluginListener {
@FXML @FXML
private Tab settingsTab; private Tab settingsTab;
@FXML @FXML
private Tab functionListTab;
@FXML
private TableView<HistoryModel> historyTable; private TableView<HistoryModel> historyTable;
@FXML @FXML
private TableColumn<HistoryModel, String> inputColumn; private TableColumn<HistoryModel, String> inputColumn;
@@ -94,12 +89,6 @@ public class AbacusController implements PluginListener {
private ComboBox<String> numberImplementationBox; private ComboBox<String> numberImplementationBox;
@FXML @FXML
private ListView<ToggleablePlugin> enabledPluginView; private ListView<ToggleablePlugin> enabledPluginView;
@FXML
private TextField computationLimitField;
@FXML
private ListView<String> functionListView;
@FXML
private TextField functionListSearchField;
/** /**
* The list of history entries, created by the users. * The list of history entries, created by the users.
@@ -117,14 +106,6 @@ public class AbacusController implements PluginListener {
* and, when reloaded, get added to the plugin manager's black list. * and, when reloaded, get added to the plugin manager's black list.
*/ */
private ObservableList<ToggleablePlugin> enabledPlugins; private ObservableList<ToggleablePlugin> enabledPlugins;
/**
* The list of functions that are registered in the calculator.
*/
private ObservableList<String> functionList;
/**
* The filtered list displayed to the user.
*/
private FilteredList<String> functionFilter;
/** /**
* The abacus instance used for changing the plugin configuration. * The abacus instance used for changing the plugin configuration.
@@ -143,17 +124,6 @@ public class AbacusController implements PluginListener {
* The alert shown when a press to "apply" is needed. * The alert shown when a press to "apply" is needed.
*/ */
private Alert reloadAlert; private Alert reloadAlert;
/**
* The runnable that takes care of killing computations that take too long.
*/
private final Runnable TIMER_RUNNABLE = () -> {
try {
Configuration abacusConfig = abacus.getConfiguration();
if(abacusConfig.getComputationDelay() == 0) return;
Thread.sleep((long) (abacusConfig.getComputationDelay() * 1000));
performStop();
} catch (InterruptedException e) { }
};
/** /**
* The runnable used to perform the calculation. * The runnable used to perform the calculation.
*/ */
@@ -191,10 +161,6 @@ public class AbacusController implements PluginListener {
}); });
} }
}; };
/**
* The thread that is waiting to pause the calculation.
*/
private Thread computationLimitThread;
/** /**
* The thread in which the computation runs. * The thread in which the computation runs.
*/ */
@@ -228,11 +194,6 @@ public class AbacusController implements PluginListener {
} }
}); });
functionList = FXCollections.observableArrayList();
functionFilter = new FilteredList<>(functionList, (s) -> true);
functionListView.setItems(functionFilter);
functionListSearchField.textProperty().addListener((observable, oldValue, newValue) ->
functionFilter.setPredicate((newValue.length() == 0) ? ((s) -> true) : ((s) -> s.contains(newValue))));
historyData = FXCollections.observableArrayList(); historyData = FXCollections.observableArrayList();
historyTable.setItems(historyData); historyTable.setItems(historyData);
numberImplementationOptions = FXCollections.observableArrayList(); numberImplementationOptions = FXCollections.observableArrayList();
@@ -263,15 +224,6 @@ public class AbacusController implements PluginListener {
} }
abacusPluginManager.reload(); abacusPluginManager.reload();
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
if(!newValue.matches("(\\d+(\\.\\d*)?)?")) {
computationLimitField.setText(oldValue);
} else {
changesMade = true;
}
});
changesMade = false; changesMade = false;
reloadAlertShown = false; reloadAlertShown = false;
@@ -282,29 +234,21 @@ public class AbacusController implements PluginListener {
} }
@FXML @FXML
public void performCalculation() { private void performCalculation() {
inputButton.setDisable(true); inputButton.setDisable(true);
stopButton.setDisable(false); stopButton.setDisable(false);
calculationThread = new Thread(CALCULATION_RUNNABLE); calculationThread = new Thread(CALCULATION_RUNNABLE);
calculationThread.start(); calculationThread.start();
computationLimitThread = new Thread(TIMER_RUNNABLE);
computationLimitThread.start();
} }
@FXML @FXML
public void performStop(){ private void performStop(){
if(calculationThread != null) { if(calculationThread != null)
calculationThread.interrupt(); calculationThread.interrupt();
calculationThread = null;
}
if(computationLimitThread != null){
computationLimitThread.interrupt();
computationLimitThread = null;
}
} }
@FXML @FXML
public void performSaveAndReload() { private void performSaveAndReload() {
performSave(); performSave();
performReload(); performReload();
changesMade = false; changesMade = false;
@@ -312,13 +256,13 @@ public class AbacusController implements PluginListener {
} }
@FXML @FXML
public void performReload() { private void performReload() {
alertIfApplyNeeded(true); alertIfApplyNeeded(true);
abacus.getPluginManager().reload(); abacus.getPluginManager().reload();
} }
@FXML @FXML
public void performSave() { private void performSave() {
Configuration configuration = abacus.getConfiguration(); Configuration configuration = abacus.getConfiguration();
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem()); configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
Set<String> disabledPlugins = configuration.getDisabledPlugins(); Set<String> disabledPlugins = configuration.getDisabledPlugins();
@@ -326,8 +270,6 @@ 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());
} }
if(computationLimitField.getText().matches("\\d*(\\.\\d+)?") && computationLimitField.getText().length() != 0)
configuration.setComputationDelay(Double.parseDouble(computationLimitField.getText()));
configuration.saveTo(CONFIG_FILE); configuration.saveTo(CONFIG_FILE);
changesMade = false; changesMade = false;
reloadAlertShown = false; reloadAlertShown = false;
@@ -347,15 +289,11 @@ public class AbacusController implements PluginListener {
plugin.enabledProperty().addListener(e -> changesMade = true); plugin.enabledProperty().addListener(e -> changesMade = true);
enabledPlugins.add(plugin); enabledPlugins.add(plugin);
} }
functionList.addAll(manager.getAllFunctions());
functionList.sort(String::compareTo);
} }
@Override @Override
public void onUnload(PluginManager manager) { public void onUnload(PluginManager manager) {
functionList.clear();
enabledPlugins.clear(); enabledPlugins.clear();
numberImplementationOptions.clear(); numberImplementationOptions.clear();
} }
} }

View File

@@ -173,7 +173,7 @@ public abstract class NumberInterface {
/** /**
* Returns the least integer greater than or equal to the number. * Returns the least integer greater than or equal to the number.
* *
* @return the least integer greater or equal to the number, if int can hold the value. * @return the least integer >= the number, if int can hold the value.
*/ */
protected abstract NumberInterface ceilingInternal(); protected abstract NumberInterface ceilingInternal();
@@ -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 bigger or equal to the number, if int can hold the value. * @return the least integer bigger or equal to the number.
*/ */
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 integer smaller or equal the number, if int can hold the value. * @return the greatest integer smaller or equal the number.
*/ */
protected abstract NumberInterface floorInternal(); protected abstract NumberInterface floorInternal();
@@ -201,7 +201,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 greatest int smaller or equal to the number, if int can hold the value. * @return the greatest int greater than or equal to the number.
*/ */
public final NumberInterface floor(){ public final NumberInterface floor(){
checkInterrupted(); checkInterrupted();
@@ -216,7 +216,7 @@ public abstract class NumberInterface {
protected abstract NumberInterface fractionalPartInternal(); protected abstract NumberInterface fractionalPartInternal();
/** /**
* Returns the fractional part of the number. * Returns the fractional part of the number, specifically x - floor(x).
* Also, checks if the thread has been interrupted, * Also, checks if the thread has been interrupted,
* and if so, throws an exception. * and if so, throws an exception.
* @return the fractional part of the number. * @return the fractional part of the number.

View File

@@ -113,19 +113,18 @@ public class PreciseNumber extends NumberInterface {
String str = value.toPlainString(); String str = value.toPlainString();
int decimalIndex = str.indexOf('.'); int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) { if (decimalIndex != -1) {
return new PreciseNumber(str.substring(0, decimalIndex)); NumberInterface floor = new PreciseNumber(str.substring(0, decimalIndex));
if(signum() == -1){
floor = floor.subtract(ONE);
}
return floor;
} }
return this; return this;
} }
@Override @Override
public NumberInterface fractionalPartInternal() { public NumberInterface fractionalPartInternal() {
String str = value.toPlainString(); return this.subtractInternal(floorInternal());
int decimalIndex = str.indexOf('.');
if (decimalIndex != -1) {
return new PreciseNumber(str.substring(decimalIndex + 1));
}
return ZERO;
} }
@Override @Override

View File

@@ -17,6 +17,18 @@ 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,
@@ -37,9 +49,69 @@ 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.
@@ -57,6 +129,8 @@ 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;
} }
@@ -68,7 +142,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) {
manager.registerFunction(name, toRegister); functions.put(name, toRegister);
} }
/** /**
@@ -80,7 +154,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) {
manager.registerOperator(name, operator); operators.put(name, operator);
} }
/** /**
@@ -91,7 +165,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) {
manager.registerNumberImplementation(name, implementation); numberImplementations.put(name, implementation);
} }
/** /**
@@ -139,7 +213,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 piFor(Class<? extends NumberInterface> forClass) { protected final NumberInterface getPi(Class<? extends NumberInterface> forClass) {
return manager.piFor(forClass); return manager.piFor(forClass);
} }

View File

@@ -23,17 +23,20 @@ public class PluginManager {
*/ */
private Set<Plugin> plugins; private Set<Plugin> plugins;
/** /**
* The map of functions registered by the plugins. * List of functions that have been cached,
* that is, found in a plugin and returned.
*/ */
private Map<String, Function> registeredFunctions; private Map<String, Function> cachedFunctions;
/** /**
* The map of operators registered by the plugins * List of operators that have been cached,
* that is, found in a plugin and returned.
*/ */
private Map<String, Operator> registeredOperators; private Map<String, Operator> cachedOperators;
/** /**
* The map of number implementations registered by the plugins. * The list of number implementations that have
* been cached, that is, found in a plugin and returned.
*/ */
private Map<String, NumberImplementation> registeredNumberImplementations; private Map<String, NumberImplementation> cachedNumberImplementations;
/** /**
* 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.
@@ -43,6 +46,18 @@ 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.
*/ */
@@ -62,66 +77,79 @@ public class PluginManager {
this.abacus = abacus; this.abacus = abacus;
loadedPluginClasses = new HashSet<>(); loadedPluginClasses = new HashSet<>();
plugins = new HashSet<>(); plugins = new HashSet<>();
registeredFunctions = new HashMap<>(); cachedFunctions = new HashMap<>();
registeredOperators = new HashMap<>(); cachedOperators = new HashMap<>();
registeredNumberImplementations = new HashMap<>(); cachedNumberImplementations = 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<>();
} }
/** /**
* Registers a function under the given name. * Searches the plugin list for a certain value, retrieving the Plugin's
* @param name the name of the function. * list of items of the type using the setFunction and getting the value
* @param function the function to register. * 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
* @param <K> the type of key that the cache is indexed by.
* @return the retrieved element, or null if it was not found.
*/ */
public void registerFunction(String name, Function function){ private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
registeredFunctions.put(name, function); java.util.function.Function<Plugin, Set<K>> setFunction,
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;
} }
/** /**
* Registers an operator under the given name. * 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) {
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.
* @param operator the operator to register. * @return the operator under the given name.
*/ */
public void registerOperator(String name, Operator operator){ public Operator operatorFor(String name) {
registeredOperators.put(name, operator); return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
} }
/** /**
* Registers a number implementation under the given name. * Gets the number implementation under the given name.
* @param name the name of the number implementation. *
* @param implementation the number implementation to register. * @param name the name of the implementation.
* @return the implementation.
*/ */
public void registerNumberImplementation(String name, NumberImplementation implementation){ public NumberImplementation numberImplementationFor(String name) {
registeredNumberImplementations.put(name, implementation); return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
} 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);
} }
/** /**
@@ -133,11 +161,14 @@ 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;
for(String key : registeredNumberImplementations.keySet()){ outside:
NumberImplementation implementation = registeredNumberImplementations.get(key); for (Plugin plugin : plugins) {
if(implementation.getImplementation() == name) { for (String implementationName : plugin.providedNumberImplementations()) {
toReturn = implementation; NumberImplementation implementation = plugin.getNumberImplementation(implementationName);
break; if (implementation.getImplementation().equals(name)) {
toReturn = implementation;
break outside;
}
} }
} }
cachedInterfaceImplementations.put(name, toReturn); cachedInterfaceImplementations.put(name, toReturn);
@@ -196,6 +227,12 @@ 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));
} }
@@ -209,11 +246,14 @@ public class PluginManager {
if (disabledPlugins.contains(plugin.getClass().getName())) continue; if (disabledPlugins.contains(plugin.getClass().getName())) continue;
plugin.disable(); plugin.disable();
} }
registeredFunctions.clear(); cachedFunctions.clear();
registeredOperators.clear(); cachedOperators.clear();
registeredNumberImplementations.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));
} }
@@ -231,7 +271,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 registeredFunctions.keySet(); return allFunctions;
} }
/** /**
@@ -240,7 +280,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 registeredOperators.keySet(); return allOperators;
} }
/** /**
@@ -249,7 +289,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 registeredNumberImplementations.keySet(); return allNumberImplementations;
} }
/** /**

View File

@@ -349,6 +349,13 @@ public class StandardPlugin extends Plugin {
return NaiveNumber.ZERO.promoteTo(params[0].getClass()); return NaiveNumber.ZERO.promoteTo(params[0].getClass());
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0) else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
return NaiveNumber.ONE.promoteTo(params[1].getClass()); return NaiveNumber.ONE.promoteTo(params[1].getClass());
//Detect integer bases:
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
&& FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0){
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
}
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1])); return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
} }
}); });
@@ -363,7 +370,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface pi = piFor(params[0].getClass()); NumberInterface pi = getPi(params[0].getClass());
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2)); NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2));
NumberInterface theta = getSmallAngle(params[0], pi); NumberInterface theta = getSmallAngle(params[0], pi);
//System.out.println(theta); //System.out.println(theta);
@@ -387,7 +394,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return functionSin.apply(piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) return functionSin.apply(getPi(params[0].getClass()).divide(fromInt(params[0].getClass(), 2))
.subtract(params[0])); .subtract(params[0]));
} }
}; };

View File

@@ -50,9 +50,7 @@
<ListView fx:id="enabledPluginView" <ListView fx:id="enabledPluginView"
GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnIndex="0"
GridPane.columnSpan="2" maxHeight="100"/> GridPane.columnSpan="2" maxHeight="100"/>
<Text GridPane.columnIndex="0" GridPane.rowIndex="2" text="Computation Limit"/> <FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10"
<TextField fx:id="computationLimitField" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="3" hgap="10"
vgap="10"> vgap="10">
<Button text="Apply" onAction="#performSave"/> <Button text="Apply" onAction="#performSave"/>
<Button text="Reload Plugins" onAction="#performReload"/> <Button text="Reload Plugins" onAction="#performReload"/>
@@ -60,15 +58,6 @@
</FlowPane> </FlowPane>
</GridPane> </GridPane>
</Tab> </Tab>
<Tab fx:id="functionListTab" text="Functions" closable="false">
<VBox spacing="10">
<padding>
<Insets left="10" right="10" top="10" bottom="10"/>
</padding>
<TextField fx:id="functionListSearchField" maxWidth="Infinity"/>
<ListView maxWidth="Infinity" fx:id="functionListView"/>
</VBox>
</Tab>
</TabPane> </TabPane>
</center> </center>

View File

@@ -11,7 +11,7 @@ import org.nwapw.abacus.tree.TreeNode;
public class CalculationTests { public class CalculationTests {
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{})); private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
@BeforeClass @BeforeClass
public static void prepareTests(){ public static void prepareTests(){

View File

@@ -19,7 +19,7 @@ import java.util.List;
public class TokenizerTests { public class TokenizerTests {
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{})); 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