mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 08:35:20 +00:00
Compare commits
1 Commits
live-reloa
...
rational-n
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9192e671f4 |
1
build.gradle
Normal file → Executable file
1
build.gradle
Normal file → Executable file
@@ -11,6 +11,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
|
||||
compile 'com.moandjiezana.toml:toml4j:0.7.1'
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8"
|
||||
testCompile 'junit:junit:4.12'
|
||||
|
||||
@@ -255,7 +255,13 @@ public class AbacusController implements PluginListener {
|
||||
abacus = new Abacus(new Configuration(CONFIG_FILE));
|
||||
PluginManager abacusPluginManager = abacus.getPluginManager();
|
||||
abacusPluginManager.addListener(this);
|
||||
performScan();
|
||||
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
||||
try {
|
||||
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
abacusPluginManager.reload();
|
||||
|
||||
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
|
||||
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||
@@ -305,19 +311,6 @@ public class AbacusController implements PluginListener {
|
||||
reloadAlertShown = false;
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void performScan(){
|
||||
PluginManager abacusPluginManager = abacus.getPluginManager();
|
||||
abacusPluginManager.removeAll();
|
||||
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
||||
try {
|
||||
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
abacusPluginManager.reload();
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void performReload() {
|
||||
alertIfApplyNeeded(true);
|
||||
|
||||
@@ -240,7 +240,6 @@ public abstract class NumberInterface {
|
||||
* @param toClass the class to promote to.
|
||||
* @return the resulting new instance.
|
||||
*/
|
||||
@Deprecated
|
||||
protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
|
||||
|
||||
/**
|
||||
@@ -251,7 +250,6 @@ public abstract class NumberInterface {
|
||||
* @param toClass the class to promote to.
|
||||
* @return the resulting new instance.
|
||||
*/
|
||||
@Deprecated
|
||||
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
checkInterrupted();
|
||||
return promoteToInternal(toClass);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.math.MathContext;
|
||||
|
||||
/**
|
||||
@@ -61,6 +62,15 @@ public class PreciseNumber extends NumberInterface {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a precise number from the given BigInteger.
|
||||
*
|
||||
* @param value a BigInteger object representing the value of the number.
|
||||
*/
|
||||
public PreciseNumber(BigInteger value) {
|
||||
this.value = new BigDecimal(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxPrecision() {
|
||||
return internalContext.getPrecision();
|
||||
|
||||
115
src/main/java/org/nwapw/abacus/number/RationalNumber.java
Executable file
115
src/main/java/org/nwapw/abacus/number/RationalNumber.java
Executable file
@@ -0,0 +1,115 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import org.apache.commons.math3.fraction.BigFraction;
|
||||
import org.apache.commons.math3.fraction.Fraction;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class RationalNumber extends NumberInterface{
|
||||
|
||||
static final RationalNumber ONE = new RationalNumber(BigFraction.ONE);
|
||||
|
||||
/**
|
||||
* The value of the number.
|
||||
*/
|
||||
private BigFraction value;
|
||||
|
||||
/**
|
||||
* Constructs a new instance with the given value.
|
||||
* @param value
|
||||
*/
|
||||
public RationalNumber(BigFraction value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxPrecision() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface multiplyInternal(NumberInterface multiplier) {
|
||||
return new RationalNumber(value.multiply(((RationalNumber)multiplier).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface divideInternal(NumberInterface divisor) {
|
||||
return new RationalNumber(value.divide(((RationalNumber)divisor).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface addInternal(NumberInterface summand) {
|
||||
return new RationalNumber(value.add(((RationalNumber)summand).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface subtractInternal(NumberInterface subtrahend) {
|
||||
return new RationalNumber(value.subtract(((RationalNumber)subtrahend).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface negateInternal() {
|
||||
return new RationalNumber(value.negate());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface intPowInternal(int exponent) {
|
||||
return new RationalNumber(value.pow(exponent));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(NumberInterface number) {
|
||||
return value.compareTo(((RationalNumber)number).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int signum() {
|
||||
return value.getNumerator().signum();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface ceilingInternal() {
|
||||
if(value.getNumeratorAsInt() != 1){
|
||||
return floorInternal().add(ONE);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface floorInternal() {
|
||||
BigInteger floor = value.bigDecimalValue().toBigInteger();
|
||||
if(value.compareTo(BigFraction.ZERO) < 0 && value.getDenominatorAsInt() != 1){
|
||||
floor = floor.subtract(BigInteger.ONE);
|
||||
}
|
||||
return new RationalNumber(new BigFraction(floor));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface fractionalPartInternal() {
|
||||
return this.subtractInternal(floorInternal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface getMaxError() {
|
||||
return toPreciseNumber().getMaxError();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return toPreciseNumber().toString();
|
||||
}
|
||||
|
||||
PreciseNumber toPreciseNumber(){
|
||||
return (PreciseNumber) new PreciseNumber(value.getNumerator()).divideInternal(new PreciseNumber(value.getDenominator()));
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public abstract class NumberImplementation {
|
||||
/**
|
||||
* The list of paths through which this implementation can be promoted.
|
||||
*/
|
||||
private Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
||||
protected Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
||||
/**
|
||||
* The implementation class for this implementation.
|
||||
*/
|
||||
|
||||
@@ -187,24 +187,6 @@ public class PluginManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the plugin with the given class from the manager.
|
||||
* @param toRemove the plugin to remove.
|
||||
*/
|
||||
public void removeClass(Class<? extends Plugin> toRemove){
|
||||
if(!loadedPluginClasses.contains(toRemove)) return;
|
||||
plugins.removeIf(plugin -> plugin.getClass() == toRemove);
|
||||
loadedPluginClasses.remove(toRemove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all plugins from this plugin manager.
|
||||
*/
|
||||
public void removeAll(){
|
||||
loadedPluginClasses.clear();
|
||||
plugins.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the plugins in the PluginManager.
|
||||
*/
|
||||
|
||||
19
src/main/java/org/nwapw/abacus/tree/Reducer.java
Normal file
19
src/main/java/org/nwapw/abacus/tree/Reducer.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
/**
|
||||
* Interface used to reduce a tree into a single value.
|
||||
*
|
||||
* @param <T> the value to reduce into.
|
||||
*/
|
||||
public interface Reducer<T> {
|
||||
|
||||
/**
|
||||
* Reduces the given tree into a single value of type T.
|
||||
*
|
||||
* @param node the node being passed in to be reduced.
|
||||
* @param children the already-reduced children of this node.
|
||||
* @return the resulting value from the reduce.
|
||||
*/
|
||||
public T reduceNode(TreeNode node, Object... children);
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@ data class FunctionNode(val function: String) : TreeNode() {
|
||||
val children: MutableList<TreeNode> = mutableListOf()
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
val children = Array<Any>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||
val children = Array<Any?>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||
return reducer.reduceNode(this, *children)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package org.nwapw.abacus.tree
|
||||
|
||||
/**
|
||||
* Reducer interface that takes a tree and returns a single value.
|
||||
*
|
||||
* The reducer walks the tree, visiting the children first, converting them into
|
||||
* a value, and then attempts to reduce the parent. Eventually, the single final value is returned.
|
||||
*/
|
||||
interface Reducer <out T> {
|
||||
|
||||
/**
|
||||
* Reduces the given tree node, given its already reduced children.
|
||||
*
|
||||
* @param treeNode the tree node to reduce.
|
||||
* @param children the list of children, of type T.
|
||||
*/
|
||||
fun reduceNode(treeNode: TreeNode, vararg children: Any) : T?
|
||||
|
||||
}
|
||||
@@ -57,7 +57,6 @@
|
||||
<Button text="Apply" onAction="#performSave"/>
|
||||
<Button text="Reload Plugins" onAction="#performReload"/>
|
||||
<Button text="Apply and Reload" onAction="#performSaveAndReload"/>
|
||||
<Button text="Scan Plugins" onAction="#performScan"/>
|
||||
</FlowPane>
|
||||
</GridPane>
|
||||
</Tab>
|
||||
|
||||
Reference in New Issue
Block a user