2017-07-24 10:04:00 -07:00
|
|
|
package org.nwapw.abacus;
|
|
|
|
|
2017-07-28 21:37:47 -07:00
|
|
|
import org.nwapw.abacus.config.ConfigurationObject;
|
2017-07-28 21:25:02 -07:00
|
|
|
import org.nwapw.abacus.function.Operator;
|
2017-07-28 22:17:22 -07:00
|
|
|
import org.nwapw.abacus.number.NaiveNumber;
|
2017-07-28 21:25:02 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
|
|
|
import org.nwapw.abacus.plugin.ClassFinder;
|
|
|
|
import org.nwapw.abacus.plugin.PluginListener;
|
2017-07-25 21:52:23 -07:00
|
|
|
import org.nwapw.abacus.plugin.PluginManager;
|
2017-07-28 15:18:23 -07:00
|
|
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
2017-07-28 21:25:02 -07:00
|
|
|
import org.nwapw.abacus.tree.NumberReducer;
|
|
|
|
import org.nwapw.abacus.tree.TreeBuilder;
|
|
|
|
import org.nwapw.abacus.tree.TreeNode;
|
2017-07-25 21:11:36 -07:00
|
|
|
import org.nwapw.abacus.window.Window;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
2017-07-28 21:37:47 -07:00
|
|
|
import java.io.File;
|
2017-07-28 11:17:54 -07:00
|
|
|
import java.io.IOException;
|
2017-07-28 22:17:22 -07:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* The main calculator class. This is responsible
|
|
|
|
* for piecing together all of the components, allowing
|
|
|
|
* their interaction with each other.
|
|
|
|
*/
|
|
|
|
public class Abacus implements PluginListener {
|
2017-07-24 10:04:00 -07:00
|
|
|
|
2017-07-28 22:17:22 -07:00
|
|
|
/**
|
|
|
|
* The default implementation to use for the number representation.
|
|
|
|
*/
|
|
|
|
public static final Class<? extends NumberInterface> DEFAULT_NUMBER = NaiveNumber.class;
|
2017-07-28 21:37:47 -07:00
|
|
|
/**
|
|
|
|
* The file used for saving and loading configuration.
|
|
|
|
*/
|
2017-07-28 22:17:22 -07:00
|
|
|
public static final File CONFIG_FILE = new File("config.toml");
|
2017-07-28 21:37:47 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* The main Abacus UI.
|
|
|
|
*/
|
2017-07-25 21:11:36 -07:00
|
|
|
private Window mainUi;
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* The plugin manager responsible for
|
|
|
|
* loading and unloading plugins,
|
|
|
|
* and getting functions from them.
|
|
|
|
*/
|
|
|
|
private PluginManager pluginManager;
|
|
|
|
/**
|
|
|
|
* Tree builder built from plugin manager,
|
|
|
|
* used to construct parse trees.
|
|
|
|
*/
|
|
|
|
private TreeBuilder treeBuilder;
|
|
|
|
/**
|
|
|
|
* The reducer used to evaluate the tree.
|
|
|
|
*/
|
|
|
|
private NumberReducer numberReducer;
|
2017-07-28 21:37:47 -07:00
|
|
|
/**
|
|
|
|
* The configuration loaded from a file.
|
|
|
|
*/
|
|
|
|
private ConfigurationObject configuration;
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* Creates a new instance of the Abacus calculator.
|
|
|
|
*/
|
2017-07-25 21:11:36 -07:00
|
|
|
public Abacus(){
|
2017-07-28 21:25:02 -07:00
|
|
|
pluginManager = new PluginManager(this);
|
|
|
|
mainUi = new Window(this);
|
|
|
|
numberReducer = new NumberReducer(this);
|
2017-07-28 21:37:47 -07:00
|
|
|
configuration = new ConfigurationObject(CONFIG_FILE);
|
|
|
|
configuration.save(CONFIG_FILE);
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
pluginManager.addListener(this);
|
|
|
|
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
2017-07-28 11:17:54 -07:00
|
|
|
try {
|
2017-07-28 14:21:43 -07:00
|
|
|
ClassFinder.loadJars("plugins")
|
2017-07-28 21:25:02 -07:00
|
|
|
.forEach(plugin -> pluginManager.addClass(plugin));
|
2017-07-28 11:35:23 -07:00
|
|
|
} catch (IOException | ClassNotFoundException e) {
|
2017-07-28 11:17:54 -07:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-07-28 21:25:02 -07:00
|
|
|
pluginManager.load();
|
|
|
|
|
2017-07-25 21:11:36 -07:00
|
|
|
mainUi.setVisible(true);
|
2017-07-28 21:25:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current tree builder.
|
|
|
|
* @return the main tree builder in this abacus instance.
|
|
|
|
*/
|
|
|
|
public TreeBuilder getTreeBuilder() {
|
|
|
|
return treeBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current plugin manager,
|
|
|
|
* @return the plugin manager in this abacus instance.
|
|
|
|
*/
|
|
|
|
public PluginManager getPluginManager() {
|
|
|
|
return pluginManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current UI.
|
|
|
|
* @return the UI window in this abacus instance.
|
|
|
|
*/
|
|
|
|
public Window getMainUi() {
|
|
|
|
return mainUi;
|
|
|
|
}
|
2017-07-28 11:17:54 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* Get the reducer that is responsible for transforming
|
|
|
|
* an expression into a number.
|
|
|
|
* @return the number reducer in this abacus instance.
|
|
|
|
*/
|
|
|
|
public NumberReducer getNumberReducer() {
|
|
|
|
return numberReducer;
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:37:47 -07:00
|
|
|
/**
|
|
|
|
* Gets the configuration object associated with this instance.
|
|
|
|
* @return the configuration object.
|
|
|
|
*/
|
|
|
|
public ConfigurationObject getConfiguration() {
|
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* Parses a string into a tree structure using the main
|
|
|
|
* tree builder.
|
|
|
|
* @param input the input string to parse
|
|
|
|
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
|
|
|
*/
|
|
|
|
public TreeNode parseString(String input){
|
|
|
|
if(treeBuilder == null) return null;
|
|
|
|
return treeBuilder.fromString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the given tree using the main
|
|
|
|
* number reducer.
|
|
|
|
* @param tree the tree to reduce, must not be null.
|
|
|
|
* @return the resulting number, or null of the reduction failed.
|
|
|
|
*/
|
|
|
|
public NumberInterface evaluateTree(TreeNode tree){
|
|
|
|
return tree.reduce(numberReducer);
|
|
|
|
}
|
|
|
|
|
2017-07-28 22:17:22 -07:00
|
|
|
public NumberInterface numberFromString(String numberString){
|
|
|
|
Class<? extends NumberInterface> toInstantiate =
|
|
|
|
pluginManager.numberFor(configuration.getNumberImplementation());
|
|
|
|
if(toInstantiate == null) toInstantiate = DEFAULT_NUMBER;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return toInstantiate.getConstructor(String.class).newInstance(numberString);
|
|
|
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
@Override
|
|
|
|
public void onLoad(PluginManager manager) {
|
2017-07-28 22:17:22 -07:00
|
|
|
treeBuilder = new TreeBuilder(this);
|
2017-07-28 21:25:02 -07:00
|
|
|
for(String function : manager.getAllFunctions()){
|
|
|
|
treeBuilder.registerFunction(function);
|
|
|
|
}
|
|
|
|
for(String operator : manager.getAllOperators()){
|
|
|
|
Operator operatorObject = manager.operatorFor(operator);
|
|
|
|
treeBuilder.registerOperator(operator,
|
|
|
|
operatorObject.getAssociativity(),
|
|
|
|
operatorObject.getType(),
|
|
|
|
operatorObject.getPrecedence());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUnload(PluginManager manager) {
|
|
|
|
treeBuilder = null;
|
2017-07-25 21:11:36 -07:00
|
|
|
}
|
|
|
|
|
2017-07-24 10:04:00 -07:00
|
|
|
public static void main(String[] args){
|
2017-07-28 21:25:02 -07:00
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2017-07-25 21:11:36 -07:00
|
|
|
new Abacus();
|
2017-07-24 10:04:00 -07:00
|
|
|
}
|
|
|
|
}
|