mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 16:15:19 +00:00
Compare commits
15 Commits
plugins-up
...
plugin-lis
| Author | SHA1 | Date | |
|---|---|---|---|
| 4056013d1f | |||
| be28e26607 | |||
| 2f1ed5f0d1 | |||
| 2615273d28 | |||
| 6e1d2ce629 | |||
| 44b8efd9bc | |||
| 2502c90837 | |||
| e49f28a850 | |||
| 88e4a87d81 | |||
| cda09518c3 | |||
| 56510d97de | |||
| c2ae0b4138 | |||
| 16938b4e06 | |||
| d964fbfb6f | |||
| 9713f24ed2 |
@@ -9,7 +9,7 @@ import org.nwapw.abacus.parsing.ShuntingYardParser;
|
|||||||
import org.nwapw.abacus.parsing.TreeBuilder;
|
import org.nwapw.abacus.parsing.TreeBuilder;
|
||||||
import org.nwapw.abacus.plugin.ClassFinder;
|
import org.nwapw.abacus.plugin.ClassFinder;
|
||||||
import org.nwapw.abacus.plugin.PluginManager;
|
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.NumberReducer;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ public class Abacus {
|
|||||||
* Creates a new instance of the Abacus calculator.
|
* Creates a new instance of the Abacus calculator.
|
||||||
*/
|
*/
|
||||||
public Abacus() {
|
public Abacus() {
|
||||||
pluginManager = new PluginManager();
|
pluginManager = new PluginManager(this);
|
||||||
numberReducer = new NumberReducer(this);
|
numberReducer = new NumberReducer(this);
|
||||||
configuration = new Configuration(CONFIG_FILE);
|
configuration = new Configuration(CONFIG_FILE);
|
||||||
configuration.saveTo(CONFIG_FILE);
|
configuration.saveTo(CONFIG_FILE);
|
||||||
@@ -67,22 +67,16 @@ public class Abacus {
|
|||||||
|
|
||||||
pluginManager.addListener(lexerTokenizer);
|
pluginManager.addListener(lexerTokenizer);
|
||||||
pluginManager.addListener(shuntingYardParser);
|
pluginManager.addListener(shuntingYardParser);
|
||||||
//pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||||
/*
|
|
||||||
try {
|
try {
|
||||||
ClassFinder.loadJars("plugins")
|
ClassFinder.loadJars("plugins")
|
||||||
.forEach(plugin -> pluginManager.addClass(plugin));
|
.forEach(plugin -> pluginManager.addClass(plugin));
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}//*/
|
}
|
||||||
pluginManager.load();
|
pluginManager.load();
|
||||||
}
|
}
|
||||||
public void loadClass(Class<?> newClass){
|
|
||||||
pluginManager.addClass(newClass);
|
|
||||||
}
|
|
||||||
public void unloadClass(Class<?> newClass){
|
|
||||||
pluginManager.removeClass(newClass);
|
|
||||||
}
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AbacusApplication.launch(AbacusApplication.class, args);
|
AbacusApplication.launch(AbacusApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import com.moandjiezana.toml.TomlWriter;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The configuration object that stores
|
* The configuration object that stores
|
||||||
@@ -12,26 +15,38 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class Configuration {
|
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.
|
* The TOML writer used to write this configuration to a file.
|
||||||
*/
|
*/
|
||||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
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.
|
* The implementation of the number that should be used.
|
||||||
*/
|
*/
|
||||||
private String numberImplementation = "naive";
|
private String numberImplementation = "<default>";
|
||||||
|
/**
|
||||||
|
* The list of disabled plugins in this Configuration.
|
||||||
|
*/
|
||||||
|
private Set<String> disabledPlugins = new HashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new configuration with the given values.
|
* Creates a new configuration with the given values.
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
public Configuration(String numberImplementation){
|
public Configuration(String numberImplementation, String[] disabledPlugins){
|
||||||
this.numberImplementation = numberImplementation;
|
this.numberImplementation = numberImplementation;
|
||||||
|
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,7 +55,7 @@ public class Configuration {
|
|||||||
*/
|
*/
|
||||||
public Configuration(File fromFile){
|
public Configuration(File fromFile){
|
||||||
if(!fromFile.exists()) return;
|
if(!fromFile.exists()) return;
|
||||||
copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
|
copyFrom(new Toml(DEFAULT_TOML).read(fromFile).to(Configuration.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,6 +64,7 @@ public class Configuration {
|
|||||||
*/
|
*/
|
||||||
public void copyFrom(Configuration otherConfiguration){
|
public void copyFrom(Configuration otherConfiguration){
|
||||||
this.numberImplementation = otherConfiguration.numberImplementation;
|
this.numberImplementation = otherConfiguration.numberImplementation;
|
||||||
|
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,4 +96,13 @@ public class Configuration {
|
|||||||
public void setNumberImplementation(String numberImplementation) {
|
public void setNumberImplementation(String numberImplementation) {
|
||||||
this.numberImplementation = 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.
|
* The type of an operator, describing how it should behave.
|
||||||
*/
|
*/
|
||||||
public enum OperatorType {
|
public enum OperatorType {
|
||||||
BINARY_INFIX, UNARY_POSTFIX
|
BINARY_INFIX, UNARY_POSTFIX, UNARY_PREFIX
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,21 +4,25 @@ import javafx.collections.FXCollections;
|
|||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
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.text.Text;
|
import javafx.scene.text.Text;
|
||||||
import javafx.util.Callback;
|
import javafx.util.Callback;
|
||||||
|
import javafx.util.StringConverter;
|
||||||
import org.nwapw.abacus.Abacus;
|
import org.nwapw.abacus.Abacus;
|
||||||
|
import org.nwapw.abacus.config.Configuration;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.plugin.ClassFinder;
|
import org.nwapw.abacus.plugin.PluginListener;
|
||||||
|
import org.nwapw.abacus.plugin.PluginManager;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The controller for the abacus FX UI, responsible
|
* The controller for the abacus FX UI, responsible
|
||||||
* for all the user interaction.
|
* for all the user interaction.
|
||||||
*/
|
*/
|
||||||
public class AbacusController {
|
public class AbacusController implements PluginListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant string that is displayed if the text could not be lexed or parsed.
|
* Constant string that is displayed if the text could not be lexed or parsed.
|
||||||
@@ -46,11 +50,7 @@ public class AbacusController {
|
|||||||
@FXML
|
@FXML
|
||||||
private ComboBox<String> numberImplementationBox;
|
private ComboBox<String> numberImplementationBox;
|
||||||
@FXML
|
@FXML
|
||||||
private Button loadButton;
|
private ListView<ToggleablePlugin> enabledPluginView;
|
||||||
@FXML
|
|
||||||
private Button unloadButton;
|
|
||||||
@FXML
|
|
||||||
private TextField loadField;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of history entries, created by the users.
|
* The list of history entries, created by the users.
|
||||||
@@ -63,23 +63,39 @@ public class AbacusController {
|
|||||||
*/
|
*/
|
||||||
private ObservableList<String> numberImplementationOptions;
|
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;
|
||||||
|
|
||||||
private Abacus abacus;
|
private Abacus abacus;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize(){
|
public void initialize(){
|
||||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||||
param -> new CopyableCell<>();
|
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();
|
historyData = FXCollections.observableArrayList();
|
||||||
historyTable.setItems(historyData);
|
historyTable.setItems(historyData);
|
||||||
numberImplementationOptions = FXCollections.observableArrayList();
|
numberImplementationOptions = FXCollections.observableArrayList();
|
||||||
numberImplementationBox.setItems(numberImplementationOptions);
|
numberImplementationBox.setItems(numberImplementationOptions);
|
||||||
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
|
||||||
-> {
|
|
||||||
abacus.getConfiguration().setNumberImplementation(newValue);
|
|
||||||
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
|
||||||
});
|
|
||||||
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
||||||
|
enabledPlugins = FXCollections.observableArrayList();
|
||||||
|
enabledPluginView.setItems(enabledPlugins);
|
||||||
|
enabledPluginView.setCellFactory(pluginCellFactory);
|
||||||
inputColumn.setCellFactory(cellFactory);
|
inputColumn.setCellFactory(cellFactory);
|
||||||
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
||||||
parsedColumn.setCellFactory(cellFactory);
|
parsedColumn.setCellFactory(cellFactory);
|
||||||
@@ -88,10 +104,8 @@ public class AbacusController {
|
|||||||
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
||||||
|
|
||||||
abacus = new Abacus();
|
abacus = new Abacus();
|
||||||
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
abacus.getPluginManager().addListener(this);
|
||||||
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
abacus.getPluginManager().reload();
|
||||||
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
|
||||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -115,38 +129,42 @@ public class AbacusController {
|
|||||||
inputButton.setDisable(false);
|
inputButton.setDisable(false);
|
||||||
inputField.setText("");
|
inputField.setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void loadClass(){
|
private void performReload(){
|
||||||
try {
|
Configuration configuration = abacus.getConfiguration();
|
||||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
||||||
String name = "";
|
disabledPlugins.clear();
|
||||||
//String name = plugin.getName();
|
for(ToggleablePlugin pluginEntry : enabledPlugins){
|
||||||
while(!(name.indexOf('/') ==-1)){
|
if(!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
abacus.getPluginManager().reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void unloadClass(){
|
private void performSave(){
|
||||||
try {
|
Configuration configuration = abacus.getConfiguration();
|
||||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
|
||||||
String name = plugin.getName();
|
configuration.saveTo(Abacus.CONFIG_FILE);
|
||||||
while(!(name.indexOf('/') ==-1)){
|
}
|
||||||
name=name.substring(name.indexOf('/')+1);
|
|
||||||
}
|
@Override
|
||||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
public void onLoad(PluginManager manager) {
|
||||||
//abacus.unloadClass(plugin);
|
Configuration configuration = abacus.getConfiguration();
|
||||||
}
|
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
||||||
}
|
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
String actualImplementation = configuration.getNumberImplementation();
|
||||||
e.printStackTrace();
|
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "<default>";
|
||||||
|
numberImplementationBox.getSelectionModel().select(toSelect);
|
||||||
|
for(Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()){
|
||||||
|
String fullName = pluginClass.getName();
|
||||||
|
enabledPlugins.add(new ToggleablePlugin(!disabledPlugins.contains(fullName), fullName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUnload(PluginManager manager) {
|
||||||
|
enabledPlugins.clear();
|
||||||
|
numberImplementationOptions.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/main/java/org/nwapw/abacus/fx/ToggleablePlugin.java
Normal file
29
src/main/java/org/nwapw/abacus/fx/ToggleablePlugin.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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,9 +55,12 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
||||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||||
|
TokenType previousType;
|
||||||
|
TokenType matchType = null;
|
||||||
while (!from.isEmpty()) {
|
while (!from.isEmpty()) {
|
||||||
Match<TokenType> match = from.remove(0);
|
Match<TokenType> match = from.remove(0);
|
||||||
TokenType matchType = match.getType();
|
previousType = matchType;
|
||||||
|
matchType = match.getType();
|
||||||
if (matchType == TokenType.NUM) {
|
if (matchType == TokenType.NUM) {
|
||||||
output.add(match);
|
output.add(match);
|
||||||
} else if (matchType == TokenType.FUNCTION) {
|
} else if (matchType == TokenType.FUNCTION) {
|
||||||
@@ -74,7 +77,13 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!tokenStack.empty()) {
|
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) {
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
TokenType otherMatchType = otherMatch.getType();
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||||
@@ -103,8 +112,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
}
|
}
|
||||||
while (!tokenStack.empty()) {
|
while (!tokenStack.empty()) {
|
||||||
Match<TokenType> match = tokenStack.peek();
|
Match<TokenType> match = tokenStack.peek();
|
||||||
TokenType matchType = match.getType();
|
TokenType newMatchType = match.getType();
|
||||||
if (!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION)) return null;
|
||||||
output.add(tokenStack.pop());
|
output.add(tokenStack.pop());
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
@@ -127,11 +136,11 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
TreeNode right = constructRecursive(matches);
|
TreeNode right = constructRecursive(matches);
|
||||||
TreeNode left = constructRecursive(matches);
|
TreeNode left = constructRecursive(matches);
|
||||||
if (left == null || right == null) return null;
|
if (left == null || right == null) return null;
|
||||||
else return new BinaryInfixNode(operator, left, right);
|
else return new BinaryNode(operator, left, right);
|
||||||
} else {
|
} else {
|
||||||
TreeNode applyTo = constructRecursive(matches);
|
TreeNode applyTo = constructRecursive(matches);
|
||||||
if (applyTo == null) return null;
|
if (applyTo == null) return null;
|
||||||
else return new UnaryPrefixNode(operator, applyTo);
|
else return new UnaryNode(operator, applyTo);
|
||||||
}
|
}
|
||||||
} else if (matchType == TokenType.NUM) {
|
} else if (matchType == TokenType.NUM) {
|
||||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||||
|
|||||||
@@ -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;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
@@ -52,11 +53,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 abacus instance used to access other
|
||||||
|
* components of the application.
|
||||||
|
*/
|
||||||
|
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<>();
|
||||||
@@ -140,11 +147,6 @@ public class PluginManager {
|
|||||||
plugins.add(plugin);
|
plugins.add(plugin);
|
||||||
loadedPluginClasses.add(plugin.getClass());
|
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
|
* Instantiates a class of plugin, and adds it to this
|
||||||
@@ -160,21 +162,18 @@ public class PluginManager {
|
|||||||
e.printStackTrace();
|
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.
|
* Loads all the plugins in the PluginManager.
|
||||||
*/
|
*/
|
||||||
public void load() {
|
public void load() {
|
||||||
for (Plugin plugin : plugins) plugin.enable();
|
Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins();
|
||||||
for (Plugin plugin : plugins) {
|
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());
|
allFunctions.addAll(plugin.providedFunctions());
|
||||||
allOperators.addAll(plugin.providedOperators());
|
allOperators.addAll(plugin.providedOperators());
|
||||||
allNumbers.addAll(plugin.providedNumbers());
|
allNumbers.addAll(plugin.providedNumbers());
|
||||||
@@ -186,11 +185,18 @@ public class PluginManager {
|
|||||||
* Unloads all the plugins in the PluginManager.
|
* Unloads all the plugins in the PluginManager.
|
||||||
*/
|
*/
|
||||||
public void unload() {
|
public void unload() {
|
||||||
for (Plugin plugin : plugins) plugin.disable();
|
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();
|
||||||
allFunctions.clear();
|
allFunctions.clear();
|
||||||
allOperators.clear();
|
allOperators.clear();
|
||||||
allNumbers.clear();
|
allNumbers.clear();
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -198,7 +204,7 @@ public class PluginManager {
|
|||||||
*/
|
*/
|
||||||
public void reload() {
|
public void reload() {
|
||||||
unload();
|
unload();
|
||||||
reload();
|
load();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,4 +252,12 @@ public class PluginManager {
|
|||||||
listeners.remove(listener);
|
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,6 +52,20 @@ public class StandardPlugin extends Plugin {
|
|||||||
return params[0].subtract(params[1]);
|
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, *
|
* The multiplication operator, *
|
||||||
*/
|
*/
|
||||||
@@ -76,16 +90,12 @@ public class StandardPlugin extends Plugin {
|
|||||||
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface product = params[0];
|
return params[0].divide(params[1]);
|
||||||
for (int i = 1; i < params.length; i++) {
|
|
||||||
product = product.multiply(params[i]);
|
|
||||||
}
|
|
||||||
return product;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
@@ -317,6 +327,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
registerOperator("+", OP_ADD);
|
registerOperator("+", OP_ADD);
|
||||||
registerOperator("-", OP_SUBTRACT);
|
registerOperator("-", OP_SUBTRACT);
|
||||||
|
registerOperator("`", OP_NEGATE);
|
||||||
registerOperator("*", OP_MULTIPLY);
|
registerOperator("*", OP_MULTIPLY);
|
||||||
registerOperator("/", OP_DIVIDE);
|
registerOperator("/", OP_DIVIDE);
|
||||||
registerOperator("^", OP_CARET);
|
registerOperator("^", OP_CARET);
|
||||||
@@ -335,7 +346,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||||
if(!factorialLists.containsKey(numberClass)){
|
if(!factorialLists.containsKey(numberClass)){
|
||||||
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
factorialLists.put(numberClass, new ArrayList<>());
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
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.
|
* A tree node that represents an operation being applied to two operands.
|
||||||
*/
|
*/
|
||||||
public class BinaryInfixNode extends TreeNode {
|
public class BinaryNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
@@ -18,7 +18,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private BinaryInfixNode() {
|
private BinaryNode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +27,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation) {
|
public BinaryNode(String operation) {
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
* @param left the left node of the expression.
|
* @param left the left node of the expression.
|
||||||
* @param right the right node of the expression.
|
* @param right the right node of the expression.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
|
public BinaryNode(String operation, TreeNode left, TreeNode right) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
@@ -28,15 +28,15 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
|||||||
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||||
if (node instanceof NumberNode) {
|
if (node instanceof NumberNode) {
|
||||||
return ((NumberNode) node).getNumber();
|
return ((NumberNode) node).getNumber();
|
||||||
} else if (node instanceof BinaryInfixNode) {
|
} else if (node instanceof BinaryNode) {
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
Function function = abacus.getPluginManager().operatorFor(((BinaryNode) 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 UnaryNode) {
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
NumberInterface child = (NumberInterface) children[0];
|
||||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
Function functionn = abacus.getPluginManager().operatorFor(((UnaryNode) 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) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
public class UnaryPrefixNode extends TreeNode {
|
public class UnaryNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation this node will apply.
|
* The operation this node will apply.
|
||||||
@@ -16,7 +16,7 @@ public class UnaryPrefixNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation) {
|
public UnaryNode(String operation) {
|
||||||
this(operation, null);
|
this(operation, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ public class UnaryPrefixNode extends TreeNode {
|
|||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
* @param applyTo the node to apply the function to.
|
* @param applyTo the node to apply the function to.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation, TreeNode applyTo) {
|
public UnaryNode(String operation, TreeNode applyTo) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.applyTo = applyTo;
|
this.applyTo = applyTo;
|
||||||
}
|
}
|
||||||
@@ -46,9 +46,13 @@
|
|||||||
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
||||||
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
||||||
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
||||||
<Button fx:id="loadButton" text="Load" GridPane.rowIndex="1" GridPane.columnIndex="0" maxWidth = "Infinity" onAction="#loadClass"/>
|
<ListView fx:id="enabledPluginView"
|
||||||
<Button fx:id="unloadButton" text="Unload" GridPane.rowIndex="1" GridPane.columnIndex="1" maxWidth = "Infinity" onAction="#unloadClass"/>
|
GridPane.rowIndex="1" GridPane.columnIndex="0"
|
||||||
<TextField fx:id="loadField" GridPane.rowIndex="1" GridPane.columnIndex="2"/>
|
GridPane.columnSpan="2" maxHeight="100"/>
|
||||||
|
<HBox GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" spacing="10">
|
||||||
|
<Button text="Apply" onAction="#performSave"/>
|
||||||
|
<Button text="Reload" onAction="#performReload"/>
|
||||||
|
</HBox>
|
||||||
</GridPane>
|
</GridPane>
|
||||||
</Tab>
|
</Tab>
|
||||||
</TabPane>
|
</TabPane>
|
||||||
|
|||||||
Reference in New Issue
Block a user