mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
1 Commits
apply-sett
...
plugins-up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c03e191c36 |
@@ -5,10 +5,10 @@ Summer project for NWAPW.
|
||||
Created by Arthur Drobot, Danila Fedorin and Riley Jones.
|
||||
|
||||
## Project Description
|
||||
Abacus is a calculator built with extensibility and usability in mind. It provides a plugin interface, via Java, as Lua proves too difficult to link up to the Java core. The description of the internals of the project can be found on the wiki page.
|
||||
Abacus is a calculator built with extensibility and usability in mind. It provides a plugin interface, via Java, as Lua provides too difficult to link up to the Java core. The description of the internals of the project can be found on the wiki page.
|
||||
|
||||
## Current State
|
||||
Abacus is being built for the Northwest Advanced Programming Workshop, a 3 week program in which students work in teams to complete a single project, following principles of agile development. Because of its short timeframe, Abacus is not even close to completed state. Below is a list of the current features and problems.
|
||||
Abacus is being built for the Northwest Advanced Programming Workshop, a 3 week program in which students work in treams to complete a single project, following principles of agile development. Because of its short timeframe, Abacus is not even close to completed state. Below is a list of the current features and problems.
|
||||
- [x] Basic number class
|
||||
- [x] Implementation of basic functions
|
||||
- [x] Implementation of `exp`, `ln`, `sqrt` using the basic functions and Taylor Series
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.nwapw.abacus.parsing.ShuntingYardParser;
|
||||
import org.nwapw.abacus.parsing.TreeBuilder;
|
||||
import org.nwapw.abacus.plugin.ClassFinder;
|
||||
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.TreeNode;
|
||||
|
||||
@@ -57,7 +57,7 @@ public class Abacus {
|
||||
* Creates a new instance of the Abacus calculator.
|
||||
*/
|
||||
public Abacus() {
|
||||
pluginManager = new PluginManager(this);
|
||||
pluginManager = new PluginManager();
|
||||
numberReducer = new NumberReducer(this);
|
||||
configuration = new Configuration(CONFIG_FILE);
|
||||
configuration.saveTo(CONFIG_FILE);
|
||||
@@ -67,16 +67,22 @@ public class Abacus {
|
||||
|
||||
pluginManager.addListener(lexerTokenizer);
|
||||
pluginManager.addListener(shuntingYardParser);
|
||||
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||
//pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||
/*
|
||||
try {
|
||||
ClassFinder.loadJars("plugins")
|
||||
.forEach(plugin -> pluginManager.addClass(plugin));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}//*/
|
||||
pluginManager.load();
|
||||
}
|
||||
|
||||
public void loadClass(Class<?> newClass){
|
||||
pluginManager.addClass(newClass);
|
||||
}
|
||||
public void unloadClass(Class<?> newClass){
|
||||
pluginManager.removeClass(newClass);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
AbacusApplication.launch(AbacusApplication.class, args);
|
||||
}
|
||||
|
||||
@@ -5,9 +5,6 @@ import com.moandjiezana.toml.TomlWriter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The configuration object that stores
|
||||
@@ -15,38 +12,26 @@ import java.util.Set;
|
||||
*/
|
||||
public class Configuration {
|
||||
|
||||
/**
|
||||
* The defaults TOML string.
|
||||
*/
|
||||
private static final String DEFAULT_CONFIG =
|
||||
"numberImplementation = \"naive\"\n" +
|
||||
"disabledPlugins = []";
|
||||
/**
|
||||
* The defaults TOML object, parsed from the string.
|
||||
*/
|
||||
private static final Toml DEFAULT_TOML = new Toml().read(DEFAULT_CONFIG);
|
||||
/**
|
||||
* The TOML writer used to write this configuration to a file.
|
||||
*/
|
||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
||||
/**
|
||||
* The TOML reader used to load this config from a file.
|
||||
*/
|
||||
private static final Toml TOML_READER = new Toml();
|
||||
|
||||
/**
|
||||
* The implementation of the number that should be used.
|
||||
*/
|
||||
private String numberImplementation = "<default>";
|
||||
/**
|
||||
* The list of disabled plugins in this Configuration.
|
||||
*/
|
||||
private Set<String> disabledPlugins = new HashSet<>();
|
||||
private String numberImplementation = "naive";
|
||||
|
||||
/**
|
||||
* Creates a new configuration with the given values.
|
||||
* @param numberImplementation the number implementation, like "naive" or "precise"
|
||||
* @param disabledPlugins the list of disabled plugins.
|
||||
*/
|
||||
public Configuration(String numberImplementation, String[] disabledPlugins){
|
||||
public Configuration(String numberImplementation){
|
||||
this.numberImplementation = numberImplementation;
|
||||
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +40,7 @@ public class Configuration {
|
||||
*/
|
||||
public Configuration(File fromFile){
|
||||
if(!fromFile.exists()) return;
|
||||
copyFrom(new Toml(DEFAULT_TOML).read(fromFile).to(Configuration.class));
|
||||
copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +49,6 @@ public class Configuration {
|
||||
*/
|
||||
public void copyFrom(Configuration otherConfiguration){
|
||||
this.numberImplementation = otherConfiguration.numberImplementation;
|
||||
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,13 +80,4 @@ public class Configuration {
|
||||
public void setNumberImplementation(String numberImplementation) {
|
||||
this.numberImplementation = numberImplementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of disabled plugins.
|
||||
* @return the list of disabled plugins.
|
||||
*/
|
||||
public Set<String> getDisabledPlugins() {
|
||||
return disabledPlugins;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ package org.nwapw.abacus.function;
|
||||
* The type of an operator, describing how it should behave.
|
||||
*/
|
||||
public enum OperatorType {
|
||||
BINARY_INFIX, UNARY_POSTFIX, UNARY_PREFIX
|
||||
BINARY_INFIX, UNARY_POSTFIX
|
||||
}
|
||||
|
||||
@@ -4,39 +4,22 @@ import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.control.cell.CheckBoxListCell;
|
||||
import javafx.scene.text.Text;
|
||||
import javafx.util.Callback;
|
||||
import javafx.util.StringConverter;
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.config.Configuration;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.PluginListener;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
import org.nwapw.abacus.plugin.ClassFinder;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
import java.util.Set;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* The controller for the abacus FX UI, responsible
|
||||
* for all the user interaction.
|
||||
*/
|
||||
public class AbacusController implements PluginListener {
|
||||
public class AbacusController {
|
||||
|
||||
/**
|
||||
* The title for the apply alert dialog.
|
||||
*/
|
||||
private static final String APPLY_MSG_TITLE = "\"Apply\" Needed";
|
||||
/**
|
||||
* The text for the header of the apply alert dialog.
|
||||
*/
|
||||
private static final String APPLY_MSG_HEADER = "The settings have not been applied.";
|
||||
/**
|
||||
* The text for the dialog that is shown if settings haven't been applied.
|
||||
*/
|
||||
private static final String APPLY_MSG_TEXT = "You have made changes to the configuration, however, you haven't pressed \"Apply\". " +
|
||||
"The changes to the configuration will not be present in the calculator until \"Apply\" is pressed.";
|
||||
/**
|
||||
* Constant string that is displayed if the text could not be lexed or parsed.
|
||||
*/
|
||||
@@ -46,12 +29,6 @@ public class AbacusController implements PluginListener {
|
||||
*/
|
||||
private static final String ERR_EVAL = "Evaluation Error";
|
||||
|
||||
@FXML
|
||||
private TabPane coreTabPane;
|
||||
@FXML
|
||||
private Tab calculateTab;
|
||||
@FXML
|
||||
private Tab settingsTab;
|
||||
@FXML
|
||||
private TableView<HistoryModel> historyTable;
|
||||
@FXML
|
||||
@@ -69,7 +46,11 @@ public class AbacusController implements PluginListener {
|
||||
@FXML
|
||||
private ComboBox<String> numberImplementationBox;
|
||||
@FXML
|
||||
private ListView<ToggleablePlugin> enabledPluginView;
|
||||
private Button loadButton;
|
||||
@FXML
|
||||
private Button unloadButton;
|
||||
@FXML
|
||||
private TextField loadField;
|
||||
|
||||
/**
|
||||
* The list of history entries, created by the users.
|
||||
@@ -82,88 +63,35 @@ public class AbacusController implements PluginListener {
|
||||
*/
|
||||
private ObservableList<String> numberImplementationOptions;
|
||||
|
||||
/**
|
||||
* The list of plugin objects that can be toggled on and off,
|
||||
* and, when reloaded, get added to the plugin manager's black list.
|
||||
*/
|
||||
private ObservableList<ToggleablePlugin> enabledPlugins;
|
||||
|
||||
/**
|
||||
* The abacus instance used for changing the plugin configuration.
|
||||
*/
|
||||
private Abacus abacus;
|
||||
|
||||
/**
|
||||
* Boolean which represents whether changes were made to the configuration.
|
||||
*/
|
||||
private boolean changesMade;
|
||||
/**
|
||||
* Whether an alert about changes to the configuration was already shown.
|
||||
*/
|
||||
private boolean reloadAlertShown;
|
||||
/**
|
||||
* The alert shown when a press to "apply" is needed.
|
||||
*/
|
||||
private Alert reloadAlert;
|
||||
|
||||
/**
|
||||
* Alerts the user if the changes they made
|
||||
* have not yet been applied.
|
||||
*/
|
||||
private void alertIfApplyNeeded(boolean ignorePrevious){
|
||||
if(changesMade && (!reloadAlertShown || ignorePrevious)) {
|
||||
reloadAlertShown = true;
|
||||
reloadAlert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize(){
|
||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||
param -> new CopyableCell<>();
|
||||
Callback<ListView<ToggleablePlugin>, ListCell<ToggleablePlugin>> pluginCellFactory =
|
||||
param -> new CheckBoxListCell<>(ToggleablePlugin::enabledProperty, new StringConverter<ToggleablePlugin>() {
|
||||
@Override
|
||||
public String toString(ToggleablePlugin object) {
|
||||
return object.getClassName().substring(object.getClassName().lastIndexOf('.') + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ToggleablePlugin fromString(String string) {
|
||||
return new ToggleablePlugin(true, string);
|
||||
}
|
||||
});
|
||||
|
||||
historyData = FXCollections.observableArrayList();
|
||||
historyTable.setItems(historyData);
|
||||
numberImplementationOptions = FXCollections.observableArrayList();
|
||||
numberImplementationBox.setItems(numberImplementationOptions);
|
||||
numberImplementationBox.getSelectionModel().selectedIndexProperty().addListener(e -> changesMade = true);
|
||||
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
||||
-> {
|
||||
abacus.getConfiguration().setNumberImplementation(newValue);
|
||||
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
||||
});
|
||||
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
||||
enabledPlugins = FXCollections.observableArrayList();
|
||||
enabledPluginView.setItems(enabledPlugins);
|
||||
enabledPluginView.setCellFactory(pluginCellFactory);
|
||||
inputColumn.setCellFactory(cellFactory);
|
||||
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
||||
parsedColumn.setCellFactory(cellFactory);
|
||||
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
||||
outputColumn.setCellFactory(cellFactory);
|
||||
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
||||
coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if(oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
|
||||
});
|
||||
|
||||
abacus = new Abacus();
|
||||
abacus.getPluginManager().addListener(this);
|
||||
abacus.getPluginManager().reload();
|
||||
|
||||
changesMade = false;
|
||||
reloadAlertShown = false;
|
||||
|
||||
reloadAlert = new Alert(Alert.AlertType.WARNING);
|
||||
reloadAlert.setTitle(APPLY_MSG_TITLE);
|
||||
reloadAlert.setHeaderText(APPLY_MSG_HEADER);
|
||||
reloadAlert.setContentText(APPLY_MSG_TEXT);
|
||||
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
||||
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
||||
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
||||
}
|
||||
|
||||
@FXML
|
||||
@@ -187,54 +115,38 @@ public class AbacusController implements PluginListener {
|
||||
inputButton.setDisable(false);
|
||||
inputField.setText("");
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void performSaveAndReload(){
|
||||
performSave();
|
||||
performReload();
|
||||
changesMade = false;
|
||||
reloadAlertShown = false;
|
||||
private void loadClass(){
|
||||
try {
|
||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
||||
String name = "";
|
||||
//String name = plugin.getName();
|
||||
while(!(name.indexOf('/') ==-1)){
|
||||
name=name.substring(name.indexOf('/')+1);
|
||||
}
|
||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
||||
//abacus.loadClass(plugin);
|
||||
}
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void performReload(){
|
||||
alertIfApplyNeeded(true);
|
||||
abacus.getPluginManager().reload();
|
||||
private void unloadClass(){
|
||||
try {
|
||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
||||
String name = plugin.getName();
|
||||
while(!(name.indexOf('/') ==-1)){
|
||||
name=name.substring(name.indexOf('/')+1);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void performSave(){
|
||||
Configuration configuration = abacus.getConfiguration();
|
||||
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
|
||||
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
||||
disabledPlugins.clear();
|
||||
for(ToggleablePlugin pluginEntry : enabledPlugins){
|
||||
if(!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
||||
//abacus.unloadClass(plugin);
|
||||
}
|
||||
configuration.saveTo(Abacus.CONFIG_FILE);
|
||||
changesMade = false;
|
||||
reloadAlertShown = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad(PluginManager manager) {
|
||||
Configuration configuration = abacus.getConfiguration();
|
||||
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
||||
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
||||
String actualImplementation = configuration.getNumberImplementation();
|
||||
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "<default>";
|
||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
||||
for(Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()){
|
||||
String fullName = pluginClass.getName();
|
||||
ToggleablePlugin plugin = new ToggleablePlugin(!disabledPlugins.contains(fullName), fullName);
|
||||
plugin.enabledProperty().addListener(e -> changesMade = true);
|
||||
enabledPlugins.add(plugin);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnload(PluginManager manager) {
|
||||
enabledPlugins.clear();
|
||||
numberImplementationOptions.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package org.nwapw.abacus.fx;
|
||||
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
|
||||
public class ToggleablePlugin {
|
||||
|
||||
private final BooleanProperty enabled;
|
||||
private final String className;
|
||||
|
||||
public ToggleablePlugin(boolean enabled, String className){
|
||||
this.enabled = new SimpleBooleanProperty();
|
||||
this.enabled.setValue(enabled);
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
public BooleanProperty enabledProperty() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled.get();
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,12 +55,9 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||
TokenType previousType;
|
||||
TokenType matchType = null;
|
||||
while (!from.isEmpty()) {
|
||||
Match<TokenType> match = from.remove(0);
|
||||
previousType = matchType;
|
||||
matchType = match.getType();
|
||||
TokenType matchType = match.getType();
|
||||
if (matchType == TokenType.NUM) {
|
||||
output.add(match);
|
||||
} else if (matchType == TokenType.FUNCTION) {
|
||||
@@ -77,19 +74,13 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
continue;
|
||||
}
|
||||
|
||||
if(tokenString.equals("-") && (previousType == null || previousType == TokenType.OP ||
|
||||
previousType == TokenType.OPEN_PARENTH)){
|
||||
from.add(0, new Match<>("`", TokenType.OP));
|
||||
continue;
|
||||
}
|
||||
|
||||
while (!tokenStack.empty() && type == OperatorType.BINARY_INFIX) {
|
||||
while (!tokenStack.empty()) {
|
||||
Match<TokenType> otherMatch = tokenStack.peek();
|
||||
TokenType otherMatchType = otherMatch.getType();
|
||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||
|
||||
if (otherMatchType == TokenType.OP) {
|
||||
int otherPrecedence = precedenceMap.get(otherMatch.getContent());
|
||||
int otherPrecedence = precedenceMap.get(match.getContent());
|
||||
if (otherPrecedence < precedence ||
|
||||
(associativity == OperatorAssociativity.RIGHT && otherPrecedence == precedence)) {
|
||||
break;
|
||||
@@ -112,8 +103,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
}
|
||||
while (!tokenStack.empty()) {
|
||||
Match<TokenType> match = tokenStack.peek();
|
||||
TokenType newMatchType = match.getType();
|
||||
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION)) return null;
|
||||
TokenType matchType = match.getType();
|
||||
if (!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
||||
output.add(tokenStack.pop());
|
||||
}
|
||||
return output;
|
||||
@@ -136,11 +127,11 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
TreeNode right = constructRecursive(matches);
|
||||
TreeNode left = constructRecursive(matches);
|
||||
if (left == null || right == null) return null;
|
||||
else return new BinaryNode(operator, left, right);
|
||||
else return new BinaryInfixNode(operator, left, right);
|
||||
} else {
|
||||
TreeNode applyTo = constructRecursive(matches);
|
||||
if (applyTo == null) return null;
|
||||
else return new UnaryNode(operator, applyTo);
|
||||
else return new UnaryPrefixNode(operator, applyTo);
|
||||
}
|
||||
} else if (matchType == TokenType.NUM) {
|
||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
@@ -53,17 +52,11 @@ public class PluginManager {
|
||||
* The list of plugin listeners attached to this instance.
|
||||
*/
|
||||
private Set<PluginListener> listeners;
|
||||
/**
|
||||
* The abacus instance used to access other
|
||||
* components of the application.
|
||||
*/
|
||||
private Abacus abacus;
|
||||
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
*/
|
||||
public PluginManager(Abacus abacus) {
|
||||
this.abacus = abacus;
|
||||
public PluginManager() {
|
||||
loadedPluginClasses = new HashSet<>();
|
||||
plugins = new HashSet<>();
|
||||
cachedFunctions = new HashMap<>();
|
||||
@@ -147,6 +140,11 @@ public class PluginManager {
|
||||
plugins.add(plugin);
|
||||
loadedPluginClasses.add(plugin.getClass());
|
||||
}
|
||||
public void removeInstantiated(Plugin plugin){
|
||||
if (loadedPluginClasses.contains(plugin.getClass())) return;
|
||||
plugins.remove(plugin);
|
||||
loadedPluginClasses.remove(plugin.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a class of plugin, and adds it to this
|
||||
@@ -162,18 +160,21 @@ public class PluginManager {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void removeClass(Class<?> newClass){
|
||||
if (!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return;
|
||||
try {
|
||||
removeInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void load() {
|
||||
Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins();
|
||||
for (Plugin plugin : plugins) plugin.enable();
|
||||
for (Plugin plugin : plugins) {
|
||||
if(disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||
plugin.enable();
|
||||
}
|
||||
for (Plugin plugin : plugins) {
|
||||
if(disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||
allFunctions.addAll(plugin.providedFunctions());
|
||||
allOperators.addAll(plugin.providedOperators());
|
||||
allNumbers.addAll(plugin.providedNumbers());
|
||||
@@ -185,18 +186,11 @@ public class PluginManager {
|
||||
* Unloads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void unload() {
|
||||
listeners.forEach(e -> e.onUnload(this));
|
||||
Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins();
|
||||
for (Plugin plugin : plugins) {
|
||||
if(disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||
plugin.disable();
|
||||
}
|
||||
cachedFunctions.clear();
|
||||
cachedOperators.clear();
|
||||
cachedNumbers.clear();
|
||||
for (Plugin plugin : plugins) plugin.disable();
|
||||
allFunctions.clear();
|
||||
allOperators.clear();
|
||||
allNumbers.clear();
|
||||
listeners.forEach(e -> e.onUnload(this));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +198,7 @@ public class PluginManager {
|
||||
*/
|
||||
public void reload() {
|
||||
unload();
|
||||
load();
|
||||
reload();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,12 +246,4 @@ public class PluginManager {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of all the plugin class files that have been
|
||||
* added to the plugin manager.
|
||||
* @return the list of all the added plugin classes.
|
||||
*/
|
||||
public Set<Class<?>> getLoadedPluginClasses() {
|
||||
return loadedPluginClasses;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,20 +52,6 @@ public class StandardPlugin extends Plugin {
|
||||
return params[0].subtract(params[1]);
|
||||
}
|
||||
});
|
||||
/**
|
||||
* The negation operator, -
|
||||
*/
|
||||
public static final Operator OP_NEGATE = new Operator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return params[0].negate();
|
||||
}
|
||||
});
|
||||
/**
|
||||
* The multiplication operator, *
|
||||
*/
|
||||
@@ -90,12 +76,16 @@ public class StandardPlugin extends Plugin {
|
||||
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
return params.length >= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return params[0].divide(params[1]);
|
||||
NumberInterface product = params[0];
|
||||
for (int i = 1; i < params.length; i++) {
|
||||
product = product.multiply(params[i]);
|
||||
}
|
||||
return product;
|
||||
}
|
||||
});
|
||||
/**
|
||||
@@ -222,7 +212,7 @@ public class StandardPlugin extends Plugin {
|
||||
} else {
|
||||
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||
powersOf2--;
|
||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
|
||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||
break;
|
||||
//No infinite loop for you.
|
||||
}
|
||||
@@ -327,7 +317,6 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
registerOperator("+", OP_ADD);
|
||||
registerOperator("-", OP_SUBTRACT);
|
||||
registerOperator("`", OP_NEGATE);
|
||||
registerOperator("*", OP_MULTIPLY);
|
||||
registerOperator("/", OP_DIVIDE);
|
||||
registerOperator("^", OP_CARET);
|
||||
@@ -346,7 +335,7 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||
if(!factorialLists.containsKey(numberClass)){
|
||||
factorialLists.put(numberClass, new ArrayList<>());
|
||||
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.nwapw.abacus.tree;
|
||||
/**
|
||||
* A tree node that represents an operation being applied to two operands.
|
||||
*/
|
||||
public class BinaryNode extends TreeNode {
|
||||
public class BinaryInfixNode extends TreeNode {
|
||||
|
||||
/**
|
||||
* The operation being applied.
|
||||
@@ -18,7 +18,7 @@ public class BinaryNode extends TreeNode {
|
||||
*/
|
||||
private TreeNode right;
|
||||
|
||||
private BinaryNode() {
|
||||
private BinaryInfixNode() {
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +27,7 @@ public class BinaryNode extends TreeNode {
|
||||
*
|
||||
* @param operation the operation.
|
||||
*/
|
||||
public BinaryNode(String operation) {
|
||||
public BinaryInfixNode(String operation) {
|
||||
this(operation, null, null);
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class BinaryNode extends TreeNode {
|
||||
* @param left the left node of the expression.
|
||||
* @param right the right node of the expression.
|
||||
*/
|
||||
public BinaryNode(String operation, TreeNode left, TreeNode right) {
|
||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
|
||||
this.operation = operation;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
@@ -28,15 +28,15 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||
if (node instanceof NumberNode) {
|
||||
return ((NumberNode) node).getNumber();
|
||||
} else if (node instanceof BinaryNode) {
|
||||
} else if (node instanceof BinaryInfixNode) {
|
||||
NumberInterface left = (NumberInterface) children[0];
|
||||
NumberInterface right = (NumberInterface) children[1];
|
||||
Function function = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()).getFunction();
|
||||
Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
||||
if (function == null) return null;
|
||||
return function.apply(left, right);
|
||||
} else if (node instanceof UnaryNode) {
|
||||
} else if (node instanceof UnaryPrefixNode) {
|
||||
NumberInterface child = (NumberInterface) children[0];
|
||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()).getFunction();
|
||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
||||
if (functionn == null) return null;
|
||||
return functionn.apply(child);
|
||||
} else if (node instanceof FunctionNode) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
public class UnaryNode extends TreeNode {
|
||||
public class UnaryPrefixNode extends TreeNode {
|
||||
|
||||
/**
|
||||
* The operation this node will apply.
|
||||
@@ -16,7 +16,7 @@ public class UnaryNode extends TreeNode {
|
||||
*
|
||||
* @param operation the operation for this node.
|
||||
*/
|
||||
public UnaryNode(String operation) {
|
||||
public UnaryPrefixNode(String operation) {
|
||||
this(operation, null);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class UnaryNode extends TreeNode {
|
||||
* @param operation the operation for this node.
|
||||
* @param applyTo the node to apply the function to.
|
||||
*/
|
||||
public UnaryNode(String operation, TreeNode applyTo) {
|
||||
public UnaryPrefixNode(String operation, TreeNode applyTo) {
|
||||
this.operation = operation;
|
||||
this.applyTo = applyTo;
|
||||
}
|
||||
@@ -7,13 +7,12 @@
|
||||
<?import javafx.scene.text.Text?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
<?import javafx.scene.layout.FlowPane?>
|
||||
<BorderPane xmlns="http://javafx.com/javafx"
|
||||
xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||
<center>
|
||||
<TabPane fx:id="coreTabPane">
|
||||
<Tab fx:id="calculateTab" text="Calculator" closable="false">
|
||||
<TabPane>
|
||||
<Tab text="Calculator" closable="false">
|
||||
<BorderPane>
|
||||
<center>
|
||||
<TableView fx:id="historyTable">
|
||||
@@ -42,19 +41,14 @@
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
</Tab>
|
||||
<Tab fx:id="settingsTab" text="Settings" closable="false">
|
||||
<Tab text="Settings" closable="false">
|
||||
<GridPane hgap="10" vgap="10">
|
||||
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
||||
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
||||
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
||||
<ListView fx:id="enabledPluginView"
|
||||
GridPane.rowIndex="1" GridPane.columnIndex="0"
|
||||
GridPane.columnSpan="2" maxHeight="100"/>
|
||||
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10" vgap="10">
|
||||
<Button text="Apply" onAction="#performSave"/>
|
||||
<Button text="Reload Plugins" onAction="#performReload"/>
|
||||
<Button text="Apply and Reload" onAction="#performSaveAndReload"/>
|
||||
</FlowPane>
|
||||
<Button fx:id="loadButton" text="Load" GridPane.rowIndex="1" GridPane.columnIndex="0" maxWidth = "Infinity" onAction="#loadClass"/>
|
||||
<Button fx:id="unloadButton" text="Unload" GridPane.rowIndex="1" GridPane.columnIndex="1" maxWidth = "Infinity" onAction="#unloadClass"/>
|
||||
<TextField fx:id="loadField" GridPane.rowIndex="1" GridPane.columnIndex="2"/>
|
||||
</GridPane>
|
||||
</Tab>
|
||||
</TabPane>
|
||||
|
||||
Reference in New Issue
Block a user