1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Rewrite Abacus to be the central class of the application.

This commit is contained in:
Danila Fedorin 2017-07-28 21:25:02 -07:00
parent a63a3b6bae
commit a16e85dfcd
4 changed files with 155 additions and 80 deletions

View File

@ -1,46 +1,148 @@
package org.nwapw.abacus; package org.nwapw.abacus;
import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.function.Operator;
//import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.window.Window;
import org.nwapw.abacus.plugin.ClassFinder; import org.nwapw.abacus.plugin.ClassFinder;
import org.nwapw.abacus.plugin.PluginListener;
import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.tree.NumberReducer;
import org.nwapw.abacus.tree.TreeBuilder;
import org.nwapw.abacus.tree.TreeNode;
import org.nwapw.abacus.window.Window;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
public class Abacus { /**
* 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 {
/**
* The main Abacus UI.
*/
private Window mainUi; private Window mainUi;
private PluginManager manager; /**
* 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;
/**
* Creates a new instance of the Abacus calculator.
*/
public Abacus(){ public Abacus(){
init(); pluginManager = new PluginManager(this);
mainUi = new Window(this);
numberReducer = new NumberReducer(this);
pluginManager.addListener(this);
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
try {
ClassFinder.loadJars("plugins")
.forEach(plugin -> pluginManager.addClass(plugin));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
pluginManager.load();
mainUi.setVisible(true);
} }
private void init() { /**
* 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;
}
/**
* 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;
}
/**
* 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);
}
@Override
public void onLoad(PluginManager manager) {
treeBuilder = new TreeBuilder();
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;
}
public static void main(String[] args){
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) { } catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
manager = new PluginManager();
manager.addInstantiated(new StandardPlugin(manager));
try {
ClassFinder.loadJars("plugins")
.forEach(plugin -> manager.addClass(plugin));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
mainUi = new Window(manager);
mainUi.setVisible(true);
manager.load();
}
public static void main(String[] args){
new Abacus(); new Abacus();
} }
} }

View File

@ -1,5 +1,6 @@
package org.nwapw.abacus.plugin; package org.nwapw.abacus.plugin;
import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.Operator;
@ -42,11 +43,17 @@ public class PluginManager {
* The list of plugin listeners attached to this instance. * The list of plugin listeners attached to this instance.
*/ */
private Set<PluginListener> listeners; private Set<PluginListener> listeners;
/**
* The instance of Abacus that is used to interact with its other
* components.
*/
private Abacus abacus;
/** /**
* Creates a new plugin manager. * Creates a new plugin manager.
*/ */
public PluginManager(){ public PluginManager(Abacus abacus){
this.abacus = abacus;
loadedPluginClasses = new HashSet<>(); loadedPluginClasses = new HashSet<>();
plugins = new HashSet<>(); plugins = new HashSet<>();
cachedFunctions = new HashMap<>(); cachedFunctions = new HashMap<>();

View File

@ -1,8 +1,8 @@
package org.nwapw.abacus.tree; package org.nwapw.abacus.tree;
import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.PluginManager;
/** /**
* A reducer implementation that turns a tree into a single number. * A reducer implementation that turns a tree into a single number.
@ -13,14 +13,14 @@ public class NumberReducer implements Reducer<NumberInterface> {
/** /**
* The plugin manager from which to draw the functions. * The plugin manager from which to draw the functions.
*/ */
private PluginManager manager; private Abacus abacus;
/** /**
* Creates a new number reducer with the given plugin manager. * Creates a new number reducer.
* @param manager the plugin manager. * @param abacus the calculator instance.
*/ */
public NumberReducer(PluginManager manager){ public NumberReducer(Abacus abacus){
this.manager = manager; this.abacus = abacus;
} }
@Override @Override
@ -30,12 +30,12 @@ public class NumberReducer implements Reducer<NumberInterface> {
} else if(node instanceof BinaryInfixNode){ } else if(node instanceof BinaryInfixNode){
NumberInterface left = (NumberInterface) children[0]; NumberInterface left = (NumberInterface) children[0];
NumberInterface right = (NumberInterface) children[1]; NumberInterface right = (NumberInterface) children[1];
Function function = manager.operatorFor(((BinaryInfixNode) node).getOperation()).getFunction(); Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
if(function == null) return null; if(function == null) return null;
return function.apply(left, right); return function.apply(left, right);
} else if(node instanceof UnaryPrefixNode) { } else if(node instanceof UnaryPrefixNode) {
NumberInterface child = (NumberInterface) children[0]; NumberInterface child = (NumberInterface) children[0];
Function functionn = manager.operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction(); Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
if(functionn == null) return null; if(functionn == null) return null;
return functionn.apply(child); return functionn.apply(child);
} else if(node instanceof FunctionNode){ } else if(node instanceof FunctionNode){
@ -43,7 +43,7 @@ public class NumberReducer implements Reducer<NumberInterface> {
for(int i = 0; i < convertedChildren.length; i++){ for(int i = 0; i < convertedChildren.length; i++){
convertedChildren[i] = (NumberInterface) children[i]; convertedChildren[i] = (NumberInterface) children[i];
} }
Function function = manager.functionFor(((FunctionNode) node).getFunction()); Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getFunction());
if(function == null) return null; if(function == null) return null;
return function.apply(convertedChildren); return function.apply(convertedChildren);
} }

View File

@ -1,11 +1,7 @@
package org.nwapw.abacus.window; package org.nwapw.abacus.window;
import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.PluginListener;
import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.tree.NumberReducer;
import org.nwapw.abacus.tree.TreeBuilder;
import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.TreeNode;
import javax.swing.*; import javax.swing.*;
@ -18,7 +14,7 @@ import java.awt.event.MouseEvent;
/** /**
* The main UI window for the calculator. * The main UI window for the calculator.
*/ */
public class Window extends JFrame implements PluginListener { public class Window extends JFrame {
private static final String CALC_STRING = "Calculate"; private static final String CALC_STRING = "Calculate";
private static final String SYNTAX_ERR_STRING = "Syntax Error"; private static final String SYNTAX_ERR_STRING = "Syntax Error";
@ -47,18 +43,10 @@ public class Window extends JFrame implements PluginListener {
}; };
/** /**
* The plugin manager used to retrieve functions. * The instance of the Abacus class, used
* for interaction with plugins and configuration.
*/ */
private PluginManager manager; private Abacus abacus;
/**
* The builder used to construct the parse trees.
*/
private TreeBuilder builder;
/**
* The reducer used to evaluate the tree.
*/
private NumberReducer reducer;
/** /**
* The last output by the calculator. * The last output by the calculator.
*/ */
@ -130,15 +118,14 @@ public class Window extends JFrame implements PluginListener {
* Action listener that causes the input to be evaluated. * Action listener that causes the input to be evaluated.
*/ */
private ActionListener evaluateListener = (event) -> { private ActionListener evaluateListener = (event) -> {
if(builder == null) return; TreeNode parsedExpression = abacus.parseString(inputField.getText());
TreeNode parsedExpression = builder.fromString(inputField.getText());
if(parsedExpression == null){ if(parsedExpression == null){
lastOutputArea.setText(SYNTAX_ERR_STRING); lastOutputArea.setText(SYNTAX_ERR_STRING);
return; return;
} }
NumberInterface numberInterface = parsedExpression.reduce(reducer); NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
if(numberInterface == null){ if(numberInterface == null) {
lastOutputArea.setText(EVAL_ERR_STRING);; lastOutputArea.setText(EVAL_ERR_STRING);
return; return;
} }
lastOutput = numberInterface.toString(); lastOutput = numberInterface.toString();
@ -159,13 +146,11 @@ public class Window extends JFrame implements PluginListener {
/** /**
* Creates a new window with the given manager. * Creates a new window with the given manager.
* @param manager the manager to use. * @param abacus the calculator instance to interact with other components.
*/ */
public Window(PluginManager manager){ public Window(Abacus abacus){
this(); this();
this.manager = manager; this.abacus = abacus;
manager.addListener(this);
reducer = new NumberReducer(manager);
} }
/** /**
@ -261,23 +246,4 @@ public class Window extends JFrame implements PluginListener {
}); });
} }
@Override
public void onLoad(PluginManager manager) {
builder = new TreeBuilder();
for(String function : manager.getAllFunctions()){
builder.registerFunction(function);
}
for(String operator : manager.getAllOperators()){
Operator operatorObject = manager.operatorFor(operator);
builder.registerOperator(operator,
operatorObject.getAssociativity(),
operatorObject.getType(),
operatorObject.getPrecedence());
}
}
@Override
public void onUnload(PluginManager manager) {
builder = null;
}
} }