mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 00:25:20 +00:00
Compare commits
20 Commits
provider-r
...
plugins-up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c03e191c36 | ||
| 5de9453bec | |||
| d205651332 | |||
| 6f99f07150 | |||
| 2cf41c1029 | |||
| 76677ef494 | |||
| 0cd40b028a | |||
| 7cb04a1222 | |||
| 0a97eeb442 | |||
| 05d0755526 | |||
| 0b97a935bf | |||
| 211e963db0 | |||
| d8145acc8f | |||
| 63b8162a9b | |||
| c655c63233 | |||
| 27ff1a47b5 | |||
| 2941252f7d | |||
| 44ed0199d4 | |||
| 5b582a7dbe | |||
| a02086e791 |
@@ -1,21 +1,21 @@
|
|||||||
package org.nwapw.abacus;
|
package org.nwapw.abacus;
|
||||||
|
|
||||||
import org.nwapw.abacus.config.ConfigurationObject;
|
import org.nwapw.abacus.config.Configuration;
|
||||||
|
import org.nwapw.abacus.fx.AbacusApplication;
|
||||||
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.parsing.LexerTokenizer;
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
||||||
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
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.NumberImplementation;
|
|
||||||
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;
|
||||||
import org.nwapw.abacus.window.Window;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main calculator class. This is responsible
|
* The main calculator class. This is responsible
|
||||||
@@ -24,7 +24,10 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class Abacus {
|
public class Abacus {
|
||||||
|
|
||||||
public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE;
|
/**
|
||||||
|
* The default implementation to use for the number representation.
|
||||||
|
*/
|
||||||
|
public static final Class<? extends NumberInterface> DEFAULT_NUMBER = NaiveNumber.class;
|
||||||
/**
|
/**
|
||||||
* The file used for saving and loading configuration.
|
* The file used for saving and loading configuration.
|
||||||
*/
|
*/
|
||||||
@@ -43,7 +46,7 @@ public class Abacus {
|
|||||||
/**
|
/**
|
||||||
* The configuration loaded from a file.
|
* The configuration loaded from a file.
|
||||||
*/
|
*/
|
||||||
private ConfigurationObject configuration;
|
private Configuration configuration;
|
||||||
/**
|
/**
|
||||||
* The tree builder used to construct a tree
|
* The tree builder used to construct a tree
|
||||||
* from a string.
|
* from a string.
|
||||||
@@ -56,32 +59,32 @@ public class Abacus {
|
|||||||
public Abacus() {
|
public Abacus() {
|
||||||
pluginManager = new PluginManager();
|
pluginManager = new PluginManager();
|
||||||
numberReducer = new NumberReducer(this);
|
numberReducer = new NumberReducer(this);
|
||||||
configuration = new ConfigurationObject(CONFIG_FILE);
|
configuration = new Configuration(CONFIG_FILE);
|
||||||
configuration.save(CONFIG_FILE);
|
configuration.saveTo(CONFIG_FILE);
|
||||||
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
||||||
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
||||||
|
|
||||||
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) {
|
||||||
try {
|
AbacusApplication.launch(AbacusApplication.class, args);
|
||||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
||||||
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
new Window(new Abacus()).setVisible(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,7 +120,7 @@ public class Abacus {
|
|||||||
*
|
*
|
||||||
* @return the configuration object.
|
* @return the configuration object.
|
||||||
*/
|
*/
|
||||||
public ConfigurationObject getConfiguration() {
|
public Configuration getConfiguration() {
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,10 +153,15 @@ public class Abacus {
|
|||||||
* @return the resulting number.
|
* @return the resulting number.
|
||||||
*/
|
*/
|
||||||
public NumberInterface numberFromString(String numberString) {
|
public NumberInterface numberFromString(String numberString) {
|
||||||
NumberImplementation toInstantiate =
|
Class<? extends NumberInterface> toInstantiate =
|
||||||
pluginManager.numberImplementationFor(configuration.getNumberImplementation());
|
pluginManager.numberFor(configuration.getNumberImplementation());
|
||||||
if (toInstantiate == null) toInstantiate = DEFAULT_IMPLEMENTATION;
|
if (toInstantiate == null) toInstantiate = DEFAULT_NUMBER;
|
||||||
|
|
||||||
return toInstantiate.instanceForString(numberString);
|
try {
|
||||||
|
return toInstantiate.getConstructor(String.class).newInstance(numberString);
|
||||||
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,83 @@
|
|||||||
package org.nwapw.abacus.config;
|
package org.nwapw.abacus.config;
|
||||||
|
|
||||||
|
import com.moandjiezana.toml.Toml;
|
||||||
|
import com.moandjiezana.toml.TomlWriter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serializable class that will be used to load TOML
|
* The configuration object that stores
|
||||||
* configurations.
|
* options that the user can change.
|
||||||
*/
|
*/
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The type of number this calculator should use.
|
* The TOML writer used to write this configuration to a file.
|
||||||
*/
|
*/
|
||||||
public String numberType;
|
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 = "naive";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration with the given values.
|
||||||
|
* @param numberImplementation the number implementation, like "naive" or "precise"
|
||||||
|
*/
|
||||||
|
public Configuration(String numberImplementation){
|
||||||
|
this.numberImplementation = numberImplementation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a configuration from a given file, keeping non-specified fields default.
|
||||||
|
* @param fromFile the file to load from.
|
||||||
|
*/
|
||||||
|
public Configuration(File fromFile){
|
||||||
|
if(!fromFile.exists()) return;
|
||||||
|
copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies the values from the given configuration into this one.
|
||||||
|
* @param otherConfiguration the configuration to copy from.
|
||||||
|
*/
|
||||||
|
public void copyFrom(Configuration otherConfiguration){
|
||||||
|
this.numberImplementation = otherConfiguration.numberImplementation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves this configuration to the given file, creating
|
||||||
|
* any directories that do not exist.
|
||||||
|
* @param file the file to save to.
|
||||||
|
*/
|
||||||
|
public void saveTo(File file){
|
||||||
|
if(file.getParentFile() != null) file.getParentFile().mkdirs();
|
||||||
|
try {
|
||||||
|
TOML_WRITER.write(this, file);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number implementation from this configuration.
|
||||||
|
* @return the number implementation.
|
||||||
|
*/
|
||||||
|
public String getNumberImplementation() {
|
||||||
|
return numberImplementation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the number implementation for the configuration
|
||||||
|
* @param numberImplementation the number implementation.
|
||||||
|
*/
|
||||||
|
public void setNumberImplementation(String numberImplementation) {
|
||||||
|
this.numberImplementation = numberImplementation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,111 +0,0 @@
|
|||||||
package org.nwapw.abacus.config;
|
|
||||||
|
|
||||||
import com.moandjiezana.toml.Toml;
|
|
||||||
import com.moandjiezana.toml.TomlWriter;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A configuration object, which essentially
|
|
||||||
* manages saving, loading, and getting values
|
|
||||||
* from the configuration. While Configuration is
|
|
||||||
* the data model, this is the interface with it.
|
|
||||||
*/
|
|
||||||
public class ConfigurationObject {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The writer used to store the configuration.
|
|
||||||
*/
|
|
||||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
|
||||||
/**
|
|
||||||
* The configuration instance being modeled.
|
|
||||||
*/
|
|
||||||
private Configuration configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new configuration object with the given config.
|
|
||||||
*
|
|
||||||
* @param config the config to use.
|
|
||||||
*/
|
|
||||||
public ConfigurationObject(Configuration config) {
|
|
||||||
setup(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a configuration object by attempting to
|
|
||||||
* load a config from the given path, using the
|
|
||||||
* default configuration otherwise.
|
|
||||||
*
|
|
||||||
* @param path the path to attempt to load.
|
|
||||||
*/
|
|
||||||
public ConfigurationObject(File path) {
|
|
||||||
Configuration config;
|
|
||||||
if (!path.exists()) {
|
|
||||||
config = getDefaultConfig();
|
|
||||||
} else {
|
|
||||||
Toml parse = new Toml();
|
|
||||||
parse.read(path);
|
|
||||||
config = parse.to(Configuration.class);
|
|
||||||
}
|
|
||||||
setup(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new configuration object with the
|
|
||||||
* default configuration.
|
|
||||||
*/
|
|
||||||
public ConfigurationObject() {
|
|
||||||
setup(getDefaultConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets up the ConfigurationObject.
|
|
||||||
* different constructors do different things,
|
|
||||||
* but they all lead here.
|
|
||||||
*
|
|
||||||
* @param configuration the configuration to set up with.
|
|
||||||
*/
|
|
||||||
private void setup(Configuration configuration) {
|
|
||||||
this.configuration = configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a default configuration.
|
|
||||||
*
|
|
||||||
* @return the newly created default configuration.
|
|
||||||
*/
|
|
||||||
private Configuration getDefaultConfig() {
|
|
||||||
configuration = new Configuration();
|
|
||||||
configuration.numberType = "naive";
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the implementation the user has requested to
|
|
||||||
* represent their numbers.
|
|
||||||
*
|
|
||||||
* @return the implementation name.
|
|
||||||
*/
|
|
||||||
public String getNumberImplementation() {
|
|
||||||
return configuration.numberType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves the ConfigurationObject to the given file.
|
|
||||||
*
|
|
||||||
* @param toFile the file to save ot.
|
|
||||||
* @return true if the save succeed, false if otherwise.
|
|
||||||
*/
|
|
||||||
public boolean save(File toFile) {
|
|
||||||
if (toFile.getParentFile() != null) toFile.getParentFile().mkdirs();
|
|
||||||
try {
|
|
||||||
TOML_WRITER.write(configuration, toFile);
|
|
||||||
return true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
package org.nwapw.abacus.function;
|
package org.nwapw.abacus.function;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
import org.nwapw.abacus.number.PreciseNumber;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function that operates on one or more
|
* A function that operates on one or more
|
||||||
@@ -8,6 +12,15 @@ import org.nwapw.abacus.number.NumberInterface;
|
|||||||
*/
|
*/
|
||||||
public abstract class Function {
|
public abstract class Function {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A map to correctly promote different number implementations to each other.
|
||||||
|
*/
|
||||||
|
private static final HashMap<Class<? extends NumberInterface>, Integer> priorityMap =
|
||||||
|
new HashMap<Class<? extends NumberInterface>, Integer>() {{
|
||||||
|
put(NaiveNumber.class, 0);
|
||||||
|
put(PreciseNumber.class, 1);
|
||||||
|
}};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the given params will work for the given function.
|
* Checks whether the given params will work for the given function.
|
||||||
*
|
*
|
||||||
|
|||||||
24
src/main/java/org/nwapw/abacus/fx/AbacusApplication.java
Normal file
24
src/main/java/org/nwapw/abacus/fx/AbacusApplication.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.fxml.FXMLLoader;
|
||||||
|
import javafx.scene.Parent;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main application class for JavaFX responsible for loading
|
||||||
|
* and displaying the fxml file.
|
||||||
|
*/
|
||||||
|
public class AbacusApplication extends Application {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage primaryStage) throws Exception {
|
||||||
|
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
|
||||||
|
Scene mainScene = new Scene(parent, 320, 480);
|
||||||
|
primaryStage.setScene(mainScene);
|
||||||
|
primaryStage.setTitle("Abacus");
|
||||||
|
primaryStage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
152
src/main/java/org/nwapw/abacus/fx/AbacusController.java
Normal file
152
src/main/java/org/nwapw/abacus/fx/AbacusController.java
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.collections.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.scene.control.*;
|
||||||
|
import javafx.scene.text.Text;
|
||||||
|
import javafx.util.Callback;
|
||||||
|
import org.nwapw.abacus.Abacus;
|
||||||
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
import org.nwapw.abacus.plugin.ClassFinder;
|
||||||
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The controller for the abacus FX UI, responsible
|
||||||
|
* for all the user interaction.
|
||||||
|
*/
|
||||||
|
public class AbacusController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant string that is displayed if the text could not be lexed or parsed.
|
||||||
|
*/
|
||||||
|
private static final String ERR_SYNTAX = "Syntax Error";
|
||||||
|
/**
|
||||||
|
* Constant string that is displayed if the tree could not be reduced.
|
||||||
|
*/
|
||||||
|
private static final String ERR_EVAL = "Evaluation Error";
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private TableView<HistoryModel> historyTable;
|
||||||
|
@FXML
|
||||||
|
private TableColumn<HistoryModel, String> inputColumn;
|
||||||
|
@FXML
|
||||||
|
private TableColumn<HistoryModel, String> parsedColumn;
|
||||||
|
@FXML
|
||||||
|
private TableColumn<HistoryModel, String> outputColumn;
|
||||||
|
@FXML
|
||||||
|
private Text outputText;
|
||||||
|
@FXML
|
||||||
|
private TextField inputField;
|
||||||
|
@FXML
|
||||||
|
private Button inputButton;
|
||||||
|
@FXML
|
||||||
|
private ComboBox<String> numberImplementationBox;
|
||||||
|
@FXML
|
||||||
|
private Button loadButton;
|
||||||
|
@FXML
|
||||||
|
private Button unloadButton;
|
||||||
|
@FXML
|
||||||
|
private TextField loadField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of history entries, created by the users.
|
||||||
|
*/
|
||||||
|
private ObservableList<HistoryModel> historyData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The abacus instance used for calculations and all
|
||||||
|
* other main processing code.
|
||||||
|
*/
|
||||||
|
private ObservableList<String> numberImplementationOptions;
|
||||||
|
|
||||||
|
private Abacus abacus;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void initialize(){
|
||||||
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||||
|
param -> new CopyableCell<>();
|
||||||
|
|
||||||
|
historyData = FXCollections.observableArrayList();
|
||||||
|
historyTable.setItems(historyData);
|
||||||
|
numberImplementationOptions = FXCollections.observableArrayList();
|
||||||
|
numberImplementationBox.setItems(numberImplementationOptions);
|
||||||
|
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
||||||
|
-> {
|
||||||
|
abacus.getConfiguration().setNumberImplementation(newValue);
|
||||||
|
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
||||||
|
});
|
||||||
|
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
||||||
|
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());
|
||||||
|
|
||||||
|
abacus = new Abacus();
|
||||||
|
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
||||||
|
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
||||||
|
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
||||||
|
numberImplementationBox.getSelectionModel().select(toSelect);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void performCalculation(){
|
||||||
|
inputButton.setDisable(true);
|
||||||
|
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||||
|
if(constructedTree == null){
|
||||||
|
outputText.setText(ERR_SYNTAX);
|
||||||
|
inputButton.setDisable(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
||||||
|
if(evaluatedNumber == null){
|
||||||
|
outputText.setText(ERR_EVAL);
|
||||||
|
inputButton.setDisable(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
outputText.setText(evaluatedNumber.toString());
|
||||||
|
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
||||||
|
|
||||||
|
inputButton.setDisable(false);
|
||||||
|
inputField.setText("");
|
||||||
|
}
|
||||||
|
@FXML
|
||||||
|
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 unloadClass(){
|
||||||
|
try {
|
||||||
|
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
||||||
|
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.unloadClass(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
src/main/java/org/nwapw/abacus/fx/CopyableCell.java
Normal file
35
src/main/java/org/nwapw/abacus/fx/CopyableCell.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.scene.control.TableCell;
|
||||||
|
import javafx.scene.input.MouseEvent;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A cell that copies its value to the clipboard
|
||||||
|
* when double clicked.
|
||||||
|
* @param <S> The type of the table view generic type.
|
||||||
|
* @param <T> The type of the value contained in the cell.
|
||||||
|
*/
|
||||||
|
public class CopyableCell<S, T> extends TableCell<S, T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new copyable cell.
|
||||||
|
*/
|
||||||
|
public CopyableCell(){
|
||||||
|
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
|
||||||
|
if(event.getClickCount() == 2){
|
||||||
|
Toolkit.getDefaultToolkit().getSystemClipboard()
|
||||||
|
.setContents(new StringSelection(getText()), null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void updateItem(T item, boolean empty) {
|
||||||
|
super.updateItem(item, empty);
|
||||||
|
setText((empty || item == null) ? null : item.toString());
|
||||||
|
setGraphic(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
87
src/main/java/org/nwapw/abacus/fx/HistoryModel.java
Normal file
87
src/main/java/org/nwapw/abacus/fx/HistoryModel.java
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
|
import javafx.beans.property.StringProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data model used for storing history entries.
|
||||||
|
*/
|
||||||
|
public class HistoryModel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* for the user input.
|
||||||
|
*/
|
||||||
|
private final StringProperty input;
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* that contains the parsed input.
|
||||||
|
*/
|
||||||
|
private final StringProperty parsed;
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* that contains the program output.
|
||||||
|
*/
|
||||||
|
private final StringProperty output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new history model with the given variables.
|
||||||
|
* @param input the user input
|
||||||
|
* @param parsed the parsed input
|
||||||
|
* @param output the program output.
|
||||||
|
*/
|
||||||
|
public HistoryModel(String input, String parsed, String output){
|
||||||
|
this.input = new SimpleStringProperty();
|
||||||
|
this.parsed = new SimpleStringProperty();
|
||||||
|
this.output = new SimpleStringProperty();
|
||||||
|
this.input.setValue(input);
|
||||||
|
this.parsed.setValue(parsed);
|
||||||
|
this.output.setValue(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the input property.
|
||||||
|
* @return the input property.
|
||||||
|
*/
|
||||||
|
public StringProperty inputProperty() {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets the input.
|
||||||
|
* @return the input.
|
||||||
|
*/
|
||||||
|
public String getInput() {
|
||||||
|
return input.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the parsed input property.
|
||||||
|
* @return the parsed input property.
|
||||||
|
*/
|
||||||
|
public StringProperty parsedProperty() {
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets the parsed input.
|
||||||
|
* @return the parsed input.
|
||||||
|
*/
|
||||||
|
public String getParsed() {
|
||||||
|
return parsed.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the output property.
|
||||||
|
* @return the output property.
|
||||||
|
*/
|
||||||
|
public StringProperty outputProperty() {
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets the program output.
|
||||||
|
* @return the output.
|
||||||
|
*/
|
||||||
|
public String getOutput() {
|
||||||
|
return output.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -94,23 +94,8 @@ public class NaiveNumber implements NumberInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface ceiling() {
|
public int ceiling() {
|
||||||
return new NaiveNumber(Math.ceil(value));
|
return (int) Math.ceil(value);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface floor() {
|
|
||||||
return new NaiveNumber(Math.floor(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface fractionalPart() {
|
|
||||||
return new NaiveNumber(value - Math.floor(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int intValue() {
|
|
||||||
return (int)value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -83,26 +83,7 @@ public interface 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 >= the number, if int can hold the value.
|
* @return the least integer >= the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
NumberInterface ceiling();
|
int ceiling();
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the greatest integer less than or equal to the number.
|
|
||||||
* @return the greatest int >= the number, if int can hold the value.
|
|
||||||
*/
|
|
||||||
NumberInterface floor();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the fractional part of the number.
|
|
||||||
* @return the fractional part of the number.
|
|
||||||
*/
|
|
||||||
NumberInterface fractionalPart();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the integer representation of this number, discarding any fractional part,
|
|
||||||
* if int can hold the value.
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
int intValue();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promotes this class to another number class.
|
* Promotes this class to another number class.
|
||||||
|
|||||||
@@ -3,20 +3,24 @@ package org.nwapw.abacus.number;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A number that uses a BigDecimal to store its value,
|
||||||
|
* leading to infinite possible precision.
|
||||||
|
*/
|
||||||
public class PreciseNumber implements NumberInterface {
|
public class PreciseNumber implements NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number one.
|
* The number one.
|
||||||
*/
|
*/
|
||||||
public static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
||||||
/**
|
/**
|
||||||
* The number zero.
|
* The number zero.
|
||||||
*/
|
*/
|
||||||
public static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
||||||
/**
|
/**
|
||||||
* The number ten.
|
* The number ten.
|
||||||
*/
|
*/
|
||||||
public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value of the PreciseNumber.
|
* The value of the PreciseNumber.
|
||||||
@@ -95,38 +99,8 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface ceiling() {
|
public int ceiling() {
|
||||||
String str = value.toPlainString();
|
return (int) Math.ceil(value.doubleValue());
|
||||||
int decimalIndex = str.indexOf('.');
|
|
||||||
if(decimalIndex != -1){
|
|
||||||
return this.floor().add(ONE);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface floor() {
|
|
||||||
String str = value.toPlainString();
|
|
||||||
int decimalIndex = str.indexOf('.');
|
|
||||||
if(decimalIndex != -1){
|
|
||||||
return new PreciseNumber(str.substring(0, decimalIndex));
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface fractionalPart() {
|
|
||||||
String str = value.toPlainString();
|
|
||||||
int decimalIndex = str.indexOf('.');
|
|
||||||
if(decimalIndex != -1){
|
|
||||||
return new PreciseNumber(str.substring(decimalIndex + 1));
|
|
||||||
}
|
|
||||||
return ZERO;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int intValue() {
|
|
||||||
return value.intValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
package org.nwapw.abacus.plugin;
|
|
||||||
|
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class that holds data about a number implementation.
|
|
||||||
*/
|
|
||||||
public abstract class NumberImplementation {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The list of paths through which this implementation can be promoted.
|
|
||||||
*/
|
|
||||||
protected Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
|
||||||
/**
|
|
||||||
* The implementation class for this implementation.
|
|
||||||
*/
|
|
||||||
private Class<? extends NumberInterface> implementation;
|
|
||||||
/**
|
|
||||||
* The priority of converting into this number implementation.
|
|
||||||
*/
|
|
||||||
private int priority;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new number implementation with the given data.
|
|
||||||
* @param implementation the implementation class.
|
|
||||||
* @param priority the priority, higher -> more likely to be converted into.
|
|
||||||
*/
|
|
||||||
public NumberImplementation(Class<? extends NumberInterface> implementation, int priority){
|
|
||||||
this.implementation = implementation;
|
|
||||||
this.priority = priority;
|
|
||||||
promotionPaths = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the list of all promotion paths this implementation can take.
|
|
||||||
* @return the map of documentation paths.
|
|
||||||
*/
|
|
||||||
public final Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> getPromotionPaths(){
|
|
||||||
return promotionPaths;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the implementation class used by this implementation.
|
|
||||||
* @return the implementation class.
|
|
||||||
*/
|
|
||||||
public final Class<? extends NumberInterface> getImplementation(){
|
|
||||||
return implementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the priority of this number implementation.
|
|
||||||
* @return the priority.
|
|
||||||
*/
|
|
||||||
public final int getPriority(){
|
|
||||||
return priority;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract function to create a new instance from a string.
|
|
||||||
* @param string the string to create a number from.
|
|
||||||
* @return the resulting number.
|
|
||||||
*/
|
|
||||||
public abstract NumberInterface instanceForString(String string);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the instance of pi with the given implementation.
|
|
||||||
* @return pi
|
|
||||||
*/
|
|
||||||
public abstract NumberInterface instanceForPi();
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -26,9 +26,9 @@ public abstract class Plugin {
|
|||||||
*/
|
*/
|
||||||
private Map<String, Operator> operators;
|
private Map<String, Operator> operators;
|
||||||
/**
|
/**
|
||||||
* The map of the number implementations this plugin provides.
|
* A hash map of operators mapped to their string names.
|
||||||
*/
|
*/
|
||||||
private Map<String, NumberImplementation> numberImplementations;
|
private Map<String, Class<? extends NumberInterface>> numbers;
|
||||||
/**
|
/**
|
||||||
* 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,
|
||||||
@@ -51,7 +51,7 @@ public abstract class Plugin {
|
|||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
functions = new HashMap<>();
|
functions = new HashMap<>();
|
||||||
operators = new HashMap<>();
|
operators = new HashMap<>();
|
||||||
numberImplementations = new HashMap<>();
|
numbers = new HashMap<>();
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,12 +74,12 @@ public abstract class Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of number implementations provided by this plugin.
|
* Gets the list of all numbers provided by this plugin.
|
||||||
*
|
*
|
||||||
* @return the list of registered number implementations.
|
* @return the list of registered numbers.
|
||||||
*/
|
*/
|
||||||
public final Set<String> providedNumberImplementations(){
|
public final Set<String> providedNumbers() {
|
||||||
return numberImplementations.keySet();
|
return numbers.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,13 +103,13 @@ public abstract class Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation under the given name.
|
* Gets the class under the given name.
|
||||||
*
|
*
|
||||||
* @param name the name of the number implementation to look up.
|
* @param numberName the name of the class.
|
||||||
* @return the number implementation associated with that name, or null if the plugin doesn't provide it.
|
* @return the class, or null if the plugin doesn't provide it.
|
||||||
*/
|
*/
|
||||||
public final NumberImplementation getNumberImplementation(String name){
|
public final Class<? extends NumberInterface> getNumber(String numberName) {
|
||||||
return numberImplementations.get(name);
|
return numbers.get(numberName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,13 +158,16 @@ public abstract class Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To be used in load(). Registers a new number implementation with the plugin.
|
* To be used in load(). Registers a number class
|
||||||
* This makes it accessible to the plugin manager.
|
* with the plugin internally, which makes it possible
|
||||||
* @param name the name of the implementation.
|
* for the user to select it as an "implementation" for the
|
||||||
* @param implementation the actual implementation class to register.
|
* number that they would like to use.
|
||||||
|
*
|
||||||
|
* @param name the name to register it under.
|
||||||
|
* @param toRegister the class to register.
|
||||||
*/
|
*/
|
||||||
protected final void registerNumberImplementation(String name, NumberImplementation implementation){
|
protected final void registerNumber(String name, Class<? extends NumberInterface> toRegister) {
|
||||||
numberImplementations.put(name, implementation);
|
numbers.put(name, toRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,30 +194,6 @@ public abstract class Plugin {
|
|||||||
return manager.operatorFor(name);
|
return manager.operatorFor(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches the PluginManager for the given number implementation
|
|
||||||
* name. This can be used by the plugins internally in order to find
|
|
||||||
* implementations that they do not provide.
|
|
||||||
*
|
|
||||||
* @param name the name for which to search.
|
|
||||||
* @return the resulting number implementation, or null if none was found.
|
|
||||||
*/
|
|
||||||
protected final NumberImplementation numberImplementationFor(String name){
|
|
||||||
return manager.numberImplementationFor(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches the plugin manager for a Pi value for the given number implementation.
|
|
||||||
* This is done so that number implementations with various degrees of precision
|
|
||||||
* can provide their own pi values, without losing said precision by
|
|
||||||
* promoting NaiveNumbers.
|
|
||||||
* @param forClass the class to which to find the pi instance.
|
|
||||||
* @return the pi value for the given class.
|
|
||||||
*/
|
|
||||||
protected final NumberInterface getPi(Class<? extends NumberInterface> forClass){
|
|
||||||
return manager.piFor(forClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract method to be overridden by plugin implementation, in which the plugins
|
* Abstract method to be overridden by plugin implementation, in which the plugins
|
||||||
* are supposed to register the functions they provide and do any other
|
* are supposed to register the functions they provide and do any other
|
||||||
|
|||||||
@@ -32,19 +32,10 @@ public class PluginManager {
|
|||||||
*/
|
*/
|
||||||
private Map<String, Operator> cachedOperators;
|
private Map<String, Operator> cachedOperators;
|
||||||
/**
|
/**
|
||||||
* The list of number implementations that have
|
* List of registered number implementations that have
|
||||||
* been cached, that is, found in a plugin and returned.
|
* been cached, that is, found in a plugin and returned.
|
||||||
*/
|
*/
|
||||||
private Map<String, NumberImplementation> cachedNumberImplementations;
|
private Map<String, Class<? extends NumberInterface>> cachedNumbers;
|
||||||
/**
|
|
||||||
* The list of number implementations that have been
|
|
||||||
* found by their implementation class.
|
|
||||||
*/
|
|
||||||
private Map<Class<? extends NumberInterface>, NumberImplementation> cachedInterfaceImplementations;
|
|
||||||
/**
|
|
||||||
* The pi values for each implementation class that have already been computer.
|
|
||||||
*/
|
|
||||||
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
|
|
||||||
/**
|
/**
|
||||||
* List of all functions loaded by the plugins.
|
* List of all functions loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
@@ -54,9 +45,9 @@ public class PluginManager {
|
|||||||
*/
|
*/
|
||||||
private Set<String> allOperators;
|
private Set<String> allOperators;
|
||||||
/**
|
/**
|
||||||
* List of all the number implementations loaded by the plugins.
|
* List of all numbers loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private Set<String> allNumberImplementations;
|
private Set<String> allNumbers;
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
@@ -70,12 +61,10 @@ public class PluginManager {
|
|||||||
plugins = new HashSet<>();
|
plugins = new HashSet<>();
|
||||||
cachedFunctions = new HashMap<>();
|
cachedFunctions = new HashMap<>();
|
||||||
cachedOperators = new HashMap<>();
|
cachedOperators = new HashMap<>();
|
||||||
cachedNumberImplementations = new HashMap<>();
|
cachedNumbers = new HashMap<>();
|
||||||
cachedInterfaceImplementations = new HashMap<>();
|
|
||||||
cachedPi = new HashMap<>();
|
|
||||||
allFunctions = new HashSet<>();
|
allFunctions = new HashSet<>();
|
||||||
allOperators = new HashSet<>();
|
allOperators = new HashSet<>();
|
||||||
allNumberImplementations = new HashSet<>();
|
allNumbers = new HashSet<>();
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,13 +80,12 @@ public class PluginManager {
|
|||||||
* @param getFunction the function to get the T value under the given name
|
* @param getFunction the function to get the T value under the given name
|
||||||
* @param name the name to search for
|
* @param name the name to search for
|
||||||
* @param <T> the type of element being search
|
* @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.
|
* @return the retrieved element, or null if it was not found.
|
||||||
*/
|
*/
|
||||||
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
|
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
||||||
java.util.function.Function<Plugin, Set<K>> setFunction,
|
java.util.function.Function<Plugin, Set<String>> setFunction,
|
||||||
java.util.function.BiFunction<Plugin, K, T> getFunction,
|
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
||||||
K name) {
|
String name) {
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
if (cache.containsKey(name)) return cache.get(name);
|
||||||
|
|
||||||
T loadedValue = null;
|
T loadedValue = null;
|
||||||
@@ -133,51 +121,13 @@ public class PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation under the given name.
|
* Gets a numer implementation under the given name.
|
||||||
|
*
|
||||||
* @param name the name of the implementation.
|
* @param name the name of the implementation.
|
||||||
* @return the implementation.
|
* @return the implementation class
|
||||||
*/
|
*/
|
||||||
public NumberImplementation numberImplementationFor(String name){
|
public Class<? extends NumberInterface> numberFor(String name) {
|
||||||
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
|
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
||||||
Plugin::getNumberImplementation, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number implementation for the given implementation class.
|
|
||||||
* @param name the class for which to find the implementation.
|
|
||||||
* @return the implementation.
|
|
||||||
*/
|
|
||||||
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name){
|
|
||||||
if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
|
|
||||||
NumberImplementation toReturn = null;
|
|
||||||
outside:
|
|
||||||
for(Plugin plugin : plugins){
|
|
||||||
for(String implementationName : plugin.providedNumberImplementations()){
|
|
||||||
NumberImplementation implementation = plugin.getNumberImplementation(implementationName);
|
|
||||||
if(implementation.getImplementation().equals(name)) {
|
|
||||||
toReturn = implementation;
|
|
||||||
break outside;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cachedInterfaceImplementations.put(name, toReturn);
|
|
||||||
return toReturn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the mathematical constant pi for the given implementation class.
|
|
||||||
* @param forClass the class for which to find pi.
|
|
||||||
* @return pi
|
|
||||||
*/
|
|
||||||
public NumberInterface piFor(Class<? extends NumberInterface> forClass){
|
|
||||||
if(cachedPi.containsKey(forClass)) return cachedPi.get(forClass);
|
|
||||||
NumberImplementation implementation = interfaceImplementationFor(forClass);
|
|
||||||
NumberInterface generatedPi = null;
|
|
||||||
if(implementation != null){
|
|
||||||
generatedPi = implementation.instanceForPi();
|
|
||||||
}
|
|
||||||
cachedPi.put(forClass, generatedPi);
|
|
||||||
return generatedPi;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -190,6 +140,11 @@ 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
|
||||||
@@ -205,6 +160,14 @@ 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.
|
||||||
@@ -214,7 +177,7 @@ public class PluginManager {
|
|||||||
for (Plugin plugin : plugins) {
|
for (Plugin plugin : plugins) {
|
||||||
allFunctions.addAll(plugin.providedFunctions());
|
allFunctions.addAll(plugin.providedFunctions());
|
||||||
allOperators.addAll(plugin.providedOperators());
|
allOperators.addAll(plugin.providedOperators());
|
||||||
allNumberImplementations.addAll(plugin.providedNumberImplementations());
|
allNumbers.addAll(plugin.providedNumbers());
|
||||||
}
|
}
|
||||||
listeners.forEach(e -> e.onLoad(this));
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
}
|
}
|
||||||
@@ -226,7 +189,7 @@ public class PluginManager {
|
|||||||
for (Plugin plugin : plugins) plugin.disable();
|
for (Plugin plugin : plugins) plugin.disable();
|
||||||
allFunctions.clear();
|
allFunctions.clear();
|
||||||
allOperators.clear();
|
allOperators.clear();
|
||||||
allNumberImplementations.clear();
|
allNumbers.clear();
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,12 +220,12 @@ public class PluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all the number implementations loaded by the Plugin Manager.
|
* Gets all the number implementations loaded by the Plugin Manager
|
||||||
*
|
*
|
||||||
* @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> getAllNumbers() {
|
||||||
return allNumberImplementations;
|
return allNumbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
//right and left refer to lhs and rhs in the above inequality.
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||||
NumberInterface nextNumerator = params[0];
|
NumberInterface nextNumerator = params[0];
|
||||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
|
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
|
||||||
do{
|
do{
|
||||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
||||||
n++;
|
n++;
|
||||||
@@ -280,84 +280,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* The sine function (the argument is interpreted in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionSin = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
NumberInterface pi = getPi(params[0].getClass());
|
|
||||||
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
|
||||||
NumberInterface theta = getSmallAngle(params[0], pi);
|
|
||||||
//System.out.println(theta);
|
|
||||||
if(theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0){
|
|
||||||
theta = theta.subtract(twoPi);
|
|
||||||
}
|
|
||||||
else if(theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0){
|
|
||||||
theta = pi.subtract(theta);
|
|
||||||
}
|
|
||||||
//System.out.println(theta);
|
|
||||||
return sinTaylor(theta);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The implementation for double-based naive numbers.
|
|
||||||
*/
|
|
||||||
public static final NumberImplementation IMPLEMENTATION_NAIVE = new NumberImplementation(NaiveNumber.class, 0) {
|
|
||||||
@Override
|
|
||||||
public NumberInterface instanceForString(String string) {
|
|
||||||
return new NaiveNumber(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface instanceForPi() {
|
|
||||||
return new NaiveNumber(Math.PI);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The implementation for the infinite-precision BigDecimal.
|
|
||||||
*/
|
|
||||||
public static final NumberImplementation IMPLEMENTATION_PRECISE = new NumberImplementation(PreciseNumber.class, 0) {
|
|
||||||
@Override
|
|
||||||
public NumberInterface instanceForString(String string) {
|
|
||||||
return new PreciseNumber(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface instanceForPi() {
|
|
||||||
NumberInterface C = FUNCTION_SQRT.apply(new PreciseNumber("10005")).multiply(new PreciseNumber("426880"));
|
|
||||||
NumberInterface M = PreciseNumber.ONE;
|
|
||||||
NumberInterface L = new PreciseNumber("13591409");
|
|
||||||
NumberInterface X = M;
|
|
||||||
NumberInterface sum = L;
|
|
||||||
int termsNeeded = C.getMaxPrecision()/13 + 1;
|
|
||||||
|
|
||||||
NumberInterface lSummand = new PreciseNumber("545140134");
|
|
||||||
NumberInterface xMultiplier = new PreciseNumber("262537412")
|
|
||||||
.multiply(new PreciseNumber("1000000000"))
|
|
||||||
.add(new PreciseNumber("640768000"))
|
|
||||||
.negate();
|
|
||||||
for(int i = 0; i < termsNeeded; i++){
|
|
||||||
M = M
|
|
||||||
.multiply(new NaiveNumber(12*i+2).promoteTo(PreciseNumber.class))
|
|
||||||
.multiply(new NaiveNumber(12*i+6).promoteTo(PreciseNumber.class))
|
|
||||||
.multiply(new NaiveNumber(12*i+10).promoteTo(PreciseNumber.class))
|
|
||||||
.divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(PreciseNumber.class));
|
|
||||||
L = L.add(lSummand);
|
|
||||||
X = X.multiply(xMultiplier);
|
|
||||||
sum = sum.add(M.multiply(L).divide(X));
|
|
||||||
}
|
|
||||||
return C.divide(sum);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public StandardPlugin(PluginManager manager) {
|
public StandardPlugin(PluginManager manager) {
|
||||||
super(manager);
|
super(manager);
|
||||||
}
|
}
|
||||||
@@ -390,8 +312,8 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
registerNumber("naive", NaiveNumber.class);
|
||||||
registerNumberImplementation("precise", IMPLEMENTATION_PRECISE);
|
registerNumber("precise", PreciseNumber.class);
|
||||||
|
|
||||||
registerOperator("+", OP_ADD);
|
registerOperator("+", OP_ADD);
|
||||||
registerOperator("-", OP_SUBTRACT);
|
registerOperator("-", OP_SUBTRACT);
|
||||||
@@ -404,7 +326,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
registerFunction("exp", FUNCTION_EXP);
|
registerFunction("exp", FUNCTION_EXP);
|
||||||
registerFunction("ln", FUNCTION_LN);
|
registerFunction("ln", FUNCTION_LN);
|
||||||
registerFunction("sqrt", FUNCTION_SQRT);
|
registerFunction("sqrt", FUNCTION_SQRT);
|
||||||
registerFunction("sin", functionSin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -412,13 +333,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A factorial function that uses memoization for each number class; it efficiently
|
|
||||||
* computes factorials of non-negative integers.
|
|
||||||
* @param numberClass type of number to return.
|
|
||||||
* @param n non-negative integer.
|
|
||||||
* @return a number of numClass with value n factorial.
|
|
||||||
*/
|
|
||||||
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<NumberInterface>());
|
||||||
@@ -434,36 +348,4 @@ public class StandardPlugin extends Plugin {
|
|||||||
return list.get(n);
|
return list.get(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the value of the Taylor series for sin (centered at 0) at x.
|
|
||||||
* @param x where the series is evaluated.
|
|
||||||
* @return the value of the series
|
|
||||||
*/
|
|
||||||
private static NumberInterface sinTaylor(NumberInterface x){
|
|
||||||
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
|
||||||
NumberInterface maxError = getMaxError(x);
|
|
||||||
int n = 1;
|
|
||||||
do{
|
|
||||||
n += 2;
|
|
||||||
power = power.multiply(multiplier);
|
|
||||||
currentTerm = power.divide(factorial(x.getClass(), n));
|
|
||||||
sum = sum.add(currentTerm);
|
|
||||||
} while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0);
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an equivalent angle in the interval [0, 2pi)
|
|
||||||
* @param phi an angle (in radians).
|
|
||||||
* @return theta in [0, 2pi) that differs from phi by a multiple of 2pi.
|
|
||||||
*/
|
|
||||||
private static NumberInterface getSmallAngle(NumberInterface phi, NumberInterface pi){
|
|
||||||
NumberInterface twoPi = pi.multiply(new NaiveNumber("2").promoteTo(phi.getClass()));
|
|
||||||
NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi
|
|
||||||
.multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi).
|
|
||||||
if(phi.signum() < 0){
|
|
||||||
theta = twoPi.subtract(theta);
|
|
||||||
}
|
|
||||||
return theta;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
57
src/main/resources/abacus.fxml
Normal file
57
src/main/resources/abacus.fxml
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.control.*?>
|
||||||
|
<?import javafx.scene.layout.BorderPane?>
|
||||||
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
<?import javafx.scene.text.Text?>
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
<?import javafx.scene.layout.GridPane?>
|
||||||
|
<BorderPane xmlns="http://javafx.com/javafx"
|
||||||
|
xmlns:fx="http://javafx.com/fxml"
|
||||||
|
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||||
|
<center>
|
||||||
|
<TabPane>
|
||||||
|
<Tab text="Calculator" closable="false">
|
||||||
|
<BorderPane>
|
||||||
|
<center>
|
||||||
|
<TableView fx:id="historyTable">
|
||||||
|
<columnResizePolicy>
|
||||||
|
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
|
||||||
|
</columnResizePolicy>
|
||||||
|
<columns>
|
||||||
|
<TableColumn fx:id="inputColumn" text="Input" sortable="false"/>
|
||||||
|
<TableColumn fx:id="parsedColumn" text="Parsed" sortable="false"/>
|
||||||
|
<TableColumn fx:id="outputColumn" text="Output" sortable="false"/>
|
||||||
|
</columns>
|
||||||
|
</TableView>
|
||||||
|
</center>
|
||||||
|
<bottom>
|
||||||
|
<VBox>
|
||||||
|
<ScrollPane prefHeight="50" vbarPolicy="NEVER">
|
||||||
|
<padding>
|
||||||
|
<Insets top="10" bottom="10" left="10" right="10"/>
|
||||||
|
</padding>
|
||||||
|
<Text fx:id="outputText"/>
|
||||||
|
</ScrollPane>
|
||||||
|
<TextField fx:id="inputField" onAction="#performCalculation"/>
|
||||||
|
<Button fx:id="inputButton" text="Calculate" maxWidth="Infinity"
|
||||||
|
onAction="#performCalculation"/>
|
||||||
|
</VBox>
|
||||||
|
</bottom>
|
||||||
|
</BorderPane>
|
||||||
|
</Tab>
|
||||||
|
<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"/>
|
||||||
|
<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>
|
||||||
|
</center>
|
||||||
|
|
||||||
|
</BorderPane>
|
||||||
Reference in New Issue
Block a user