Async math computing

This commit is contained in:
Andrea Cavalli 2017-11-19 12:55:48 +01:00
parent f24511ffd7
commit 35294797a3
37 changed files with 237 additions and 131 deletions

View File

@ -8,6 +8,7 @@
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

Binary file not shown.

13
pom.xml
View File

@ -7,6 +7,17 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>WarpPI Calculator</name> <name>WarpPI Calculator</name>
<url>http://warp.ovh</url> <url>http://warp.ovh</url>
<repositories>
<repository>
<id>oss-snapshots-repo</id>
<name>Sonatype OSS Maven Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -32,7 +43,7 @@
<dependency> <dependency>
<groupId>com.pi4j</groupId> <groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId> <artifactId>pi4j-core</artifactId>
<version>1.1</version> <version>1.2-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.fusesource.jansi</groupId> <groupId>org.fusesource.jansi</groupId>

View File

@ -142,11 +142,11 @@ public class TestGPU {
this.d = d; this.d = d;
r = d.getRenderer(); r = d.getRenderer();
exampleFont = d.loadFont("ex"); exampleFont = d.loadFont("norm");
exampleSkin = d.loadSkin("skin.png"); exampleSkin = d.loadSkin("skin.png");
BlockContainer.initializeFonts(d.loadFont("ex"), d.loadFont("big")); BlockContainer.initializeFonts(d.loadFont("norm"), d.loadFont("smal"));
//New expression framework test //New expression framework test
c = new NormalInputContainer(new InputContext(), false, 0, 200); c = new NormalInputContainer(new InputContext(), false, 0, 200);

View File

@ -535,7 +535,7 @@ public class Utils {
return realbytes; return realbytes;
} }
public static boolean allSolved(List<Function> expressions) throws Error { public static boolean allSolved(List<Function> expressions) throws Error, InterruptedException {
for (final Function itm : expressions) { for (final Function itm : expressions) {
if (itm.isSimplified() == false) { if (itm.isSimplified() == false) {
return false; return false;

View File

@ -237,10 +237,10 @@ public final class DisplayManager implements RenderingLoop {
private void load_fonts() throws IOException { private void load_fonts() throws IOException {
fonts = new BinaryFont[7]; fonts = new BinaryFont[7];
fonts[0] = engine.loadFont("big"); fonts[0] = engine.loadFont("smal");
fonts[1] = engine.loadFont("small"); fonts[1] = engine.loadFont("smallest");
fonts[2] = engine.loadFont("ex"); fonts[2] = engine.loadFont("norm");
fonts[3] = engine.loadFont("big"); fonts[3] = engine.loadFont("smal");
//4 //4
//fonts[5] = engine.loadFont("square"); //fonts[5] = engine.loadFont("square");
} }

View File

@ -46,8 +46,13 @@ public abstract class BlockParenthesisAbstract extends Block {
} }
r.glDrawCharLeft(x + prw, y, '╭'); r.glDrawCharLeft(x + prw, y, '╭');
r.glDrawCharLeft(x + prw, y + height - chh, '╰'); r.glDrawCharLeft(x + prw, y + height - chh, '╰');
r.glFillColor(x + prw + 3, y + 6, 2, height - 6 * 2); if (small) {
r.glFillColor(x + width - 5, y + 6, 2, height - 6 * 2); r.glFillColor(x + prw + 1, y + 5, 1, height - 4 * 2);
r.glFillColor(x + width - 3, y + 5, 1, height - 4 * 2);
} else {
r.glFillColor(x + prw + 3, y + 6, 2, height - 6 * 2);
r.glFillColor(x + width - 5, y + 6, 2, height - 6 * 2);
}
r.glDrawCharLeft(x + width - chw, y, '╮'); r.glDrawCharLeft(x + width - chw, y, '╮');
r.glDrawCharLeft(x + width - chw, y + height - chh, '╯'); r.glDrawCharLeft(x + width - chw, y + height - chh, '╯');
containerNumber.draw(ge, r, x + prw + chw, y, caret); containerNumber.draw(ge, r, x + prw + chw, y, caret);

View File

@ -38,11 +38,19 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class MathInputScreen extends Screen { public class MathInputScreen extends Screen {
private static final BinaryFont fontBig = Utils.getFont(false);
public MathContext calc; public MathContext calc;
public InputContext ic; public InputContext ic;
public InputContainer userInput; public InputContainer userInput;
public OutputContainer result; public OutputContainer result;
public int errorLevel = 0; // 0 = nessuno, 1 = risultato, 2 = tutto public int errorLevel = 0; // 0 = nessuno, 1 = risultato, 2 = tutto
private boolean computingResult = false;
private Thread computingThread;
private int computingAnimationIndex = 0;
private double computingAnimationElapsedTime = 0;
private double computingElapsedTime = 0;
private boolean computingBreakTipVisible = false;
boolean mustRefresh = true; boolean mustRefresh = true;
public MathInputScreen() { public MathInputScreen() {
@ -56,7 +64,7 @@ public class MathInputScreen extends Screen {
calc = new MathContext(); calc = new MathContext();
try { try {
BlockContainer.initializeFonts(DisplayManager.INSTANCE.engine.loadFont("ex"), DisplayManager.INSTANCE.engine.loadFont("big")); BlockContainer.initializeFonts(DisplayManager.INSTANCE.engine.loadFont("norm"), DisplayManager.INSTANCE.engine.loadFont("smal"));
} catch (final IOException e) { } catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
@ -75,7 +83,6 @@ public class MathInputScreen extends Screen {
@Override @Override
public void beforeRender(float dt) { public void beforeRender(float dt) {
if (DisplayManager.INSTANCE.error == null) { if (DisplayManager.INSTANCE.error == null) {
DisplayManager.INSTANCE.renderer.glClearColor(0xFFc5c2af); DisplayManager.INSTANCE.renderer.glClearColor(0xFFc5c2af);
} else { } else {
@ -84,9 +91,53 @@ public class MathInputScreen extends Screen {
if (userInput.beforeRender(dt)) { if (userInput.beforeRender(dt)) {
mustRefresh = true; mustRefresh = true;
} }
if (computingResult) {
computingElapsedTime += dt;
computingAnimationElapsedTime+=dt;
if (computingAnimationElapsedTime > 0.1) {
computingAnimationElapsedTime -= 0.1;
computingAnimationIndex = (computingAnimationIndex + 1) % 16;
mustRefresh = true;
}
if (computingElapsedTime > 5) {
computingBreakTipVisible = true;
}
} else {
computingElapsedTime = 0;
computingAnimationElapsedTime = 0;
computingAnimationIndex = 0;
computingBreakTipVisible = false;
}
} }
@Override
public void render() {
final Renderer renderer = DisplayManager.INSTANCE.renderer;
fontBig.use(DisplayManager.INSTANCE.engine);
final int textColor = 0xFF000000;
final int padding = 4;
renderer.glColor(textColor);
private static final BinaryFont fontBig = Utils.getFont(false); userInput.draw(DisplayManager.INSTANCE.engine, renderer, padding, padding + 20);
if (computingResult) {
renderer.glColor3f(1, 1, 1);
final int leftX = 208;
final int leftY = 16;
final int size = 32;
final int posY = computingAnimationIndex % 2;
final int posX = (computingAnimationIndex - posY) / 2;
renderer.glFillRect(DisplayManager.INSTANCE.engine.getWidth() - size - 4, DisplayManager.INSTANCE.engine.getHeight() - size - 4, size, size, leftX + size * posX, leftY + size * posY, size, size);
if (computingBreakTipVisible) {
Utils.getFont(false).use(DisplayManager.INSTANCE.engine);
renderer.glColor3f(0.75f, 0, 0);
renderer.glDrawStringRight(DisplayManager.INSTANCE.engine.getWidth() - 4 - size - 4, DisplayManager.INSTANCE.engine.getHeight() - size/2 - renderer.getCurrentFont().getCharacterHeight()/2 - 4, "Press (=) to stop");
}
} else {
if (!result.isContentEmpty()) {
result.draw(DisplayManager.INSTANCE.engine, renderer, DisplayManager.INSTANCE.engine.getWidth() - result.getWidth() - 2, DisplayManager.INSTANCE.engine.getHeight() - result.getHeight() - 2);
}
}
}
@Override @Override
public void renderStatusbar() { public void renderStatusbar() {
@ -103,19 +154,6 @@ public class MathInputScreen extends Screen {
renderer.glFillRect(2 + 18 * pos + 2 * spacersNumb, 2, 16, 16, 16 * skinN, 16 * 0, 16, 16); renderer.glFillRect(2 + 18 * pos + 2 * spacersNumb, 2, 16, 16, 16 * skinN, 16 * 0, 16, 16);
} }
@Override
public void render() {
fontBig.use(DisplayManager.INSTANCE.engine);
final int textColor = 0xFF000000;
final int padding = 4;
DisplayManager.INSTANCE.renderer.glColor(textColor);
userInput.draw(DisplayManager.INSTANCE.engine, DisplayManager.INSTANCE.renderer, padding, padding + 20);
if (!result.isContentEmpty()) {
result.draw(DisplayManager.INSTANCE.engine, DisplayManager.INSTANCE.renderer, DisplayManager.INSTANCE.engine.getWidth() - result.getWidth() - 2, DisplayManager.INSTANCE.engine.getHeight() - result.getHeight() - 2);
}
}
@Override @Override
public boolean mustBeRefreshed() { public boolean mustBeRefreshed() {
@ -188,76 +226,97 @@ public class MathInputScreen extends Screen {
calc.resultsCount = 0; calc.resultsCount = 0;
return true; return true;
} else { } else {
try { if (!computingResult) {
try { computingResult = true;
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) { computingThread = new Thread(()-> {
Expression expr = MathParser.parseInput(calc, userInput); try {
if (calc.f == null | calc.f2 == null) { try {
calc.f = new ObjectArrayList<>(); if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
calc.f2 = new ObjectArrayList<>(); Expression expr = MathParser.parseInput(calc, userInput);
} else { if (calc.f == null | calc.f2 == null) {
calc.f.clear(); calc.f = new ObjectArrayList<>();
calc.f2.clear(); calc.f2 = new ObjectArrayList<>();
} } else {
calc.f.add(expr); calc.f.clear();
int stop = 0; calc.f2.clear();
boolean done = false; }
ObjectArrayList<Function> resultExpressions = new ObjectArrayList<>(); calc.f.add(expr);
resultExpressions.add(expr.getParameter()); int stop = 0;
while (!done && stop < 3000) { boolean done = false;
ObjectArrayList<Function> newResultExpressions = new ObjectArrayList<>(); ObjectArrayList<Function> resultExpressions = new ObjectArrayList<>();
done = true; resultExpressions.add(expr.getParameter());
for (Function f : resultExpressions) { while (!done && stop < 3000) {
Function newResult = null; if (Thread.interrupted()) throw new InterruptedException();
if (f.isSimplified() == false) { ObjectArrayList<Function> newResultExpressions = new ObjectArrayList<>();
done = false; done = true;
if (f instanceof Expression) { for (Function f : resultExpressions) {
ObjectArrayList<Function> fncResult = ((Expression) f).solve(); if (Thread.interrupted()) throw new InterruptedException();
for (Function resultItem : fncResult) { Function newResult = null;
newResultExpressions.add(resultItem); if (f.isSimplified() == false) {
done = false;
if (f instanceof Expression) {
ObjectArrayList<Function> fncResult = ((Expression) f).solve();
for (Function resultItem : fncResult) {
newResultExpressions.add(resultItem);
}
} else {
List<Function> fncResult = f.simplify();
for (Function resultItem : fncResult) {
newResultExpressions.add(resultItem);
}
}
} else {
newResult = f;
} }
} else { if (newResult != null) {
List<Function> fncResult = f.simplify(); newResultExpressions.add(newResult);
for (Function resultItem : fncResult) {
newResultExpressions.add(resultItem);
} }
} }
} else { resultExpressions = newResultExpressions;
newResult = f; stop++;
} }
if (newResult != null) { Utils.out.println(2, "INPUT: " + expr);
newResultExpressions.add(newResult); for (Function rr : resultExpressions) {
Utils.out.println(1, "RESULT: " + rr.toString());
} }
ObjectArrayList<ObjectArrayList<Block>> resultBlocks = MathParser.parseOutput(calc, resultExpressions);
result.setContentAsMultipleGroups(resultBlocks);
// showVariablesDialog(() -> {
// currentExpression = newExpression;
// simplify();
// });
} }
resultExpressions = newResultExpressions; } catch (final InterruptedException ex) {
stop++; Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "Computing thread stopped.");
} catch (final Exception ex) {
if (StaticVars.debugOn) {
ex.printStackTrace();
}
throw new Error(Errors.SYNTAX_ERROR);
} }
Utils.out.println(2, "INPUT: " + expr); } catch (final Error e) {
for (Function rr : resultExpressions) { final StringWriter sw = new StringWriter();
Utils.out.println(1, "RESULT: " + rr.toString()); final PrintWriter pw = new PrintWriter(sw);
} e.printStackTrace(pw);
ObjectArrayList<ObjectArrayList<Block>> resultBlocks = MathParser.parseOutput(calc, resultExpressions); d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
result.setContentAsMultipleGroups(resultBlocks); DisplayManager.INSTANCE.error = e.id.toString();
// showVariablesDialog(() -> { System.err.println(e.id);
// currentExpression = newExpression;
// simplify();
// });
} }
} catch (final Exception ex) { computingResult = false;
if (StaticVars.debugOn) { });
ex.printStackTrace(); computingThread.setName("Computing Thread");
} computingThread.setDaemon(true);
throw new Error(Errors.SYNTAX_ERROR); computingThread.setPriority(Thread.NORM_PRIORITY + 3);
computingThread.start();
return true;
} else {
if (computingThread != null) {
computingThread.interrupt();
computingResult = false;
return true;
} }
} catch (final Error e) { return false;
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
DisplayManager.INSTANCE.error = e.id.toString();
System.err.println(e.id);
} }
return true;
} }
case NUM0: case NUM0:
typeChar('0'); typeChar('0');

View File

@ -56,15 +56,17 @@ public interface Function {
/** /**
* Simplify the current function or it's children * Simplify the current function or it's children
* @throws InterruptedException
*/ */
public List<Function> simplify() throws Error; public List<Function> simplify() throws Error, InterruptedException;
/** /**
* The current simplification status of this function and it's childrens * The current simplification status of this function and it's childrens
* *
* @return boolean * @return boolean
* @throws InterruptedException
*/ */
public boolean isSimplified(); public boolean isSimplified() throws InterruptedException;
/** /**
* *

View File

@ -87,8 +87,9 @@ public abstract class FunctionDynamic implements Function {
} }
@Override @Override
public boolean isSimplified() { public boolean isSimplified() throws InterruptedException {
for (final Function variable : functions) { for (final Function variable : functions) {
if (Thread.interrupted()) throw new InterruptedException();
if (variable.isSimplified() == false) { if (variable.isSimplified() == false) {
return false; return false;
} }
@ -106,15 +107,17 @@ public abstract class FunctionDynamic implements Function {
protected abstract boolean isSolvable(); protected abstract boolean isSolvable();
@Override @Override
public final ObjectArrayList<Function> simplify() throws Error { public final ObjectArrayList<Function> simplify() throws Error, InterruptedException {
boolean solved = true; boolean solved = true;
final Function[] fncs = getParameters(); final Function[] fncs = getParameters();
for (final Function f : fncs) { for (final Function f : fncs) {
if (Thread.interrupted()) throw new InterruptedException();
if (f.isSimplified() == false) { if (f.isSimplified() == false) {
solved = false; solved = false;
break; break;
} }
} }
if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> result = solved ? solve() : null; ObjectArrayList<Function> result = solved ? solve() : null;
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
@ -123,6 +126,7 @@ public abstract class FunctionDynamic implements Function {
final ObjectArrayList<ObjectArrayList<Function>> ln = new ObjectArrayList<>(); final ObjectArrayList<ObjectArrayList<Function>> ln = new ObjectArrayList<>();
for (final Function fnc : fncs) { for (final Function fnc : fncs) {
final ObjectArrayList<Function> l = new ObjectArrayList<>(); final ObjectArrayList<Function> l = new ObjectArrayList<>();
if (Thread.interrupted()) throw new InterruptedException();
if (fnc.isSimplified()) { if (fnc.isSimplified()) {
l.add(fnc); l.add(fnc);
} else { } else {
@ -148,8 +152,9 @@ public abstract class FunctionDynamic implements Function {
* @return The solved function. * @return The solved function.
* @throws Error * @throws Error
* Errors during computation, like a/0 or similar. * Errors during computation, like a/0 or similar.
* @throws InterruptedException
*/ */
protected abstract ObjectArrayList<Function> solve() throws Error; protected abstract ObjectArrayList<Function> solve() throws Error, InterruptedException;
@Override @Override
public MathContext getMathContext() { public MathContext getMathContext() {

View File

@ -113,7 +113,8 @@ public abstract class FunctionOperator implements Function {
} }
@Override @Override
public boolean isSimplified() { public boolean isSimplified() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
return (parameter1.isSimplified() & parameter2.isSimplified()) ? !isSolvable() : false; return (parameter1.isSimplified() & parameter2.isSimplified()) ? !isSolvable() : false;
} }
@ -123,12 +124,15 @@ public abstract class FunctionOperator implements Function {
* *
* @return <strong>true</strong> if this function can be solved, otherwise * @return <strong>true</strong> if this function can be solved, otherwise
* <strong>false</strong>. * <strong>false</strong>.
* @throws InterruptedException
*/ */
protected abstract boolean isSolvable(); protected abstract boolean isSolvable() throws InterruptedException;
@Override @Override
public final ObjectArrayList<Function> simplify() throws Error { public final ObjectArrayList<Function> simplify() throws Error, InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
final boolean solved = parameter1.isSimplified() & parameter2.isSimplified(); final boolean solved = parameter1.isSimplified() & parameter2.isSimplified();
if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> result = solved ? solve() : null;; ObjectArrayList<Function> result = solved ? solve() : null;;
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
@ -136,14 +140,18 @@ public abstract class FunctionOperator implements Function {
final ObjectArrayList<Function> l1 = new ObjectArrayList<>(); final ObjectArrayList<Function> l1 = new ObjectArrayList<>();
final ObjectArrayList<Function> l2 = new ObjectArrayList<>(); final ObjectArrayList<Function> l2 = new ObjectArrayList<>();
if (Thread.interrupted()) throw new InterruptedException();
if (parameter1.isSimplified()) { if (parameter1.isSimplified()) {
l1.add(parameter1); l1.add(parameter1);
} else { } else {
if (Thread.interrupted()) throw new InterruptedException();
l1.addAll(parameter1.simplify()); l1.addAll(parameter1.simplify());
} }
if (Thread.interrupted()) throw new InterruptedException();
if (parameter2.isSimplified()) { if (parameter2.isSimplified()) {
l2.add(parameter2); l2.add(parameter2);
} else { } else {
if (Thread.interrupted()) throw new InterruptedException();
l2.addAll(parameter2.simplify()); l2.addAll(parameter2.simplify());
} }
@ -164,8 +172,9 @@ public abstract class FunctionOperator implements Function {
* @return The solved function. * @return The solved function.
* @throws Error * @throws Error
* Errors during computation, like a/0 or similar. * Errors during computation, like a/0 or similar.
* @throws InterruptedException
*/ */
protected abstract ObjectArrayList<Function> solve() throws Error; protected abstract ObjectArrayList<Function> solve() throws Error, InterruptedException;
@Override @Override
public abstract FunctionOperator clone(); public abstract FunctionOperator clone();

View File

@ -96,7 +96,7 @@ public abstract class FunctionSingle implements Function {
} }
@Override @Override
public final ObjectArrayList<Function> simplify() throws Error { public final ObjectArrayList<Function> simplify() throws Error, InterruptedException {
final boolean simplified = parameter.isSimplified(); final boolean simplified = parameter.isSimplified();
ObjectArrayList<Function> result = simplified ? solve() : null; ObjectArrayList<Function> result = simplified ? solve() : null;
@ -125,11 +125,12 @@ public abstract class FunctionSingle implements Function {
* @return The solved function. * @return The solved function.
* @throws Error * @throws Error
* Errors during computation, like a/0 or similar. * Errors during computation, like a/0 or similar.
* @throws InterruptedException
*/ */
protected abstract ObjectArrayList<Function> solve() throws Error; protected abstract ObjectArrayList<Function> solve() throws Error, InterruptedException;
@Override @Override
public boolean isSimplified() { public boolean isSimplified() throws InterruptedException {
return parameter.isSimplified() ? !isSolvable() : false; return parameter.isSimplified() ? !isSolvable() : false;
} }
@ -139,8 +140,9 @@ public abstract class FunctionSingle implements Function {
* *
* @return <strong>true</strong> if this function can be solved, otherwise * @return <strong>true</strong> if this function can be solved, otherwise
* <strong>false</strong>. * <strong>false</strong>.
* @throws InterruptedException
*/ */
protected abstract boolean isSolvable(); protected abstract boolean isSolvable() throws InterruptedException;
@Override @Override
public abstract FunctionSingle clone(); public abstract FunctionSingle clone();

View File

@ -27,7 +27,7 @@ public class Division extends FunctionOperator {
} }
@Override @Override
protected boolean isSolvable() { protected boolean isSolvable() throws InterruptedException {
final Function variable1 = getParameter1(); final Function variable1 = getParameter1();
final Function variable2 = getParameter2(); final Function variable2 = getParameter2();
if (FractionsRule1.compare(this)) { if (FractionsRule1.compare(this)) {
@ -75,7 +75,7 @@ public class Division extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
final Function variable1 = getParameter1(); final Function variable1 = getParameter1();
final Function variable2 = getParameter2(); final Function variable2 = getParameter2();
ObjectArrayList<Function> result = new ObjectArrayList<>(); ObjectArrayList<Function> result = new ObjectArrayList<>();

View File

@ -549,7 +549,8 @@ public class Expression extends FunctionSingle {
} }
@Override @Override
protected boolean isSolvable() { protected boolean isSolvable() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
final Function f = getParameter(); final Function f = getParameter();
if (f.isSimplified() == false) { if (f.isSimplified() == false) {
return true; return true;
@ -559,7 +560,8 @@ public class Expression extends FunctionSingle {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
final ObjectArrayList<Function> ret = new ObjectArrayList<>(); final ObjectArrayList<Function> ret = new ObjectArrayList<>();
if (getParameter().isSimplified() || !parenthesisNeeded()) { if (getParameter().isSimplified() || !parenthesisNeeded()) {
ret.add(getParameter()); ret.add(getParameter());

View File

@ -29,7 +29,7 @@ public class Multiplication extends FunctionOperator {
} }
@Override @Override
protected boolean isSolvable() { protected boolean isSolvable() throws InterruptedException {
final Function variable1 = getParameter1(); final Function variable1 = getParameter1();
final Function variable2 = getParameter2(); final Function variable2 = getParameter2();
if (variable1 instanceof Number & variable2 instanceof Number) { if (variable1 instanceof Number & variable2 instanceof Number) {
@ -60,7 +60,7 @@ public class Multiplication extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
ObjectArrayList<Function> result = new ObjectArrayList<>(); ObjectArrayList<Function> result = new ObjectArrayList<>();
if (NumberRule1.compare(this)) { if (NumberRule1.compare(this)) {
result = NumberRule1.execute(this); result = NumberRule1.execute(this);

View File

@ -72,11 +72,12 @@ public class Number implements Function {
return ret; return ret;
} }
public Number pow(Number f) throws Error { public Number pow(Number f) throws Error, InterruptedException {
Number ret = new Number(root, BigDecimal.ONE); Number ret = new Number(root, BigDecimal.ONE);
if (Utils.isIntegerValue(f.term)) { if (Utils.isIntegerValue(f.term)) {
final BigInteger bi = f.term.toBigInteger().abs(); final BigInteger bi = f.term.toBigInteger().abs();
for (BigInteger i = BigInteger.ZERO; i.compareTo(bi) < 0; i = i.add(BigInteger.ONE)) { for (BigInteger i = BigInteger.ZERO; i.compareTo(bi) < 0; i = i.add(BigInteger.ONE)) {
if (Thread.interrupted()) throw new InterruptedException();
ret = ret.multiply(new Number(root, getTerm())); ret = ret.multiply(new Number(root, getTerm()));
} }
if (f.term.signum() == -1) { if (f.term.signum() == -1) {

View File

@ -57,7 +57,7 @@ public class Power extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
final ObjectArrayList<Function> result = new ObjectArrayList<>(); final ObjectArrayList<Function> result = new ObjectArrayList<>();
if (UndefinedRule1.compare(this)) { if (UndefinedRule1.compare(this)) {
result.addAll(UndefinedRule1.execute(this)); result.addAll(UndefinedRule1.execute(this));

View File

@ -43,7 +43,7 @@ public class Root extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
final ObjectArrayList<Function> result = new ObjectArrayList<>(); final ObjectArrayList<Function> result = new ObjectArrayList<>();
if (mathContext.exactMode) { if (mathContext.exactMode) {
if (parameter1 instanceof Number && ((Number) parameter1).equals(new Number(mathContext, 2))) { if (parameter1 instanceof Number && ((Number) parameter1).equals(new Number(mathContext, 2))) {

View File

@ -40,7 +40,7 @@ public class RootSquare extends FunctionSingle {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
final ObjectArrayList<Function> result = new ObjectArrayList<>(); final ObjectArrayList<Function> result = new ObjectArrayList<>();
if (mathContext.exactMode) { if (mathContext.exactMode) {
Number exponent = new Number(mathContext, BigInteger.ONE); Number exponent = new Number(mathContext, BigInteger.ONE);

View File

@ -25,7 +25,7 @@ public class Subtraction extends FunctionOperator {
} }
@Override @Override
protected boolean isSolvable() { protected boolean isSolvable() throws InterruptedException {
if (parameter1 instanceof Number & parameter2 instanceof Number) { if (parameter1 instanceof Number & parameter2 instanceof Number) {
return true; return true;
} }
@ -57,7 +57,7 @@ public class Subtraction extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
ObjectArrayList<Function> result = new ObjectArrayList<>(); ObjectArrayList<Function> result = new ObjectArrayList<>();
if (VariableRule1.compare(this)) { if (VariableRule1.compare(this)) {
result = VariableRule1.execute(this); result = VariableRule1.execute(this);

View File

@ -27,7 +27,7 @@ public class Sum extends FunctionOperator {
} }
@Override @Override
protected boolean isSolvable() { protected boolean isSolvable() throws InterruptedException {
if (parameter1 instanceof Number & parameter2 instanceof Number) { if (parameter1 instanceof Number & parameter2 instanceof Number) {
return true; return true;
} }
@ -56,7 +56,7 @@ public class Sum extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
if (parameter1 == null || parameter2 == null) { if (parameter1 == null || parameter2 == null) {
throw new Error(Errors.SYNTAX_ERROR); throw new Error(Errors.SYNTAX_ERROR);
} }

View File

@ -42,7 +42,7 @@ public class SumSubtraction extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
if (parameter1 == null || parameter2 == null) { if (parameter1 == null || parameter2 == null) {
throw new Error(Errors.SYNTAX_ERROR); throw new Error(Errors.SYNTAX_ERROR);
} }

View File

@ -32,7 +32,7 @@ public class Equation extends FunctionOperator {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
if (parameter1 == null || parameter2 == null) { if (parameter1 == null || parameter2 == null) {
throw new Error(Errors.SYNTAX_ERROR); throw new Error(Errors.SYNTAX_ERROR);
} }

View File

@ -36,7 +36,7 @@ public class EquationsSystem extends FunctionDynamic {
} }
@Override @Override
public ObjectArrayList<Function> solve() throws Error { public ObjectArrayList<Function> solve() throws Error, InterruptedException {
final ObjectArrayList<Function> ret = new ObjectArrayList<>(); final ObjectArrayList<Function> ret = new ObjectArrayList<>();
if (functions.length == 1) { if (functions.length == 1) {
if (functions[0].isSimplified()) { if (functions[0].isSimplified()) {

View File

@ -17,7 +17,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
*/ */
public class FractionsRule11 { public class FractionsRule11 {
public static boolean compare(Function f) { public static boolean compare(Function f) throws InterruptedException {
final Division fnc = (Division) f; final Division fnc = (Division) f;
Function a; Function a;
Function c; Function c;

View File

@ -17,7 +17,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
*/ */
public class FractionsRule12 { public class FractionsRule12 {
public static boolean compare(Function f) { public static boolean compare(Function f) throws InterruptedException {
final Division fnc = (Division) f; final Division fnc = (Division) f;
Function a; Function a;
Function c; Function c;

View File

@ -16,7 +16,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
*/ */
public class FractionsRule14 { public class FractionsRule14 {
public static boolean compare(Function f) { public static boolean compare(Function f) throws InterruptedException {
final Multiplication fnc = (Multiplication) f; final Multiplication fnc = (Multiplication) f;
Function a; Function a;
Function b; Function b;

View File

@ -18,11 +18,11 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
*/ */
public class DivisionRule1 { public class DivisionRule1 {
public static boolean compare(Division f) { public static boolean compare(Division f) throws InterruptedException {
return f.getParameter1().isSimplified() && f.getParameter2().isSimplified() && (f.getParameter1() instanceof Multiplication || f.getParameter2() instanceof Multiplication) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null; return f.getParameter1().isSimplified() && f.getParameter2().isSimplified() && (f.getParameter1() instanceof Multiplication || f.getParameter2() instanceof Multiplication) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null;
} }
public static ObjectArrayList<Function> execute(Division f) throws Error { public static ObjectArrayList<Function> execute(Division f) throws Error, InterruptedException {
System.out.println(f); System.out.println(f);
final MathContext root = f.getMathContext(); final MathContext root = f.getMathContext();
Function result; Function result;
@ -73,10 +73,11 @@ public class DivisionRule1 {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static ObjectArrayList<Function>[] getDivisionElements(Division division) { private static ObjectArrayList<Function>[] getDivisionElements(Division division) throws InterruptedException {
final ObjectArrayList<Function> elementsNumerator = new ObjectArrayList<>(); final ObjectArrayList<Function> elementsNumerator = new ObjectArrayList<>();
Function numMult = division.getParameter1(); Function numMult = division.getParameter1();
while (numMult instanceof Multiplication) { while (numMult instanceof Multiplication) {
if (Thread.interrupted()) throw new InterruptedException();
elementsNumerator.add(((Multiplication) numMult).getParameter1()); elementsNumerator.add(((Multiplication) numMult).getParameter1());
numMult = ((Multiplication) numMult).getParameter2(); numMult = ((Multiplication) numMult).getParameter2();
} }
@ -85,6 +86,7 @@ public class DivisionRule1 {
final ObjectArrayList<Function> elementsDenominator = new ObjectArrayList<>(); final ObjectArrayList<Function> elementsDenominator = new ObjectArrayList<>();
Function denomMult = division.getParameter2(); Function denomMult = division.getParameter2();
while (denomMult instanceof Multiplication) { while (denomMult instanceof Multiplication) {
if (Thread.interrupted()) throw new InterruptedException();
elementsDenominator.add(((Multiplication) denomMult).getParameter1()); elementsDenominator.add(((Multiplication) denomMult).getParameter1());
denomMult = ((Multiplication) denomMult).getParameter2(); denomMult = ((Multiplication) denomMult).getParameter2();
} }
@ -93,7 +95,7 @@ public class DivisionRule1 {
return new ObjectArrayList[] { elementsNumerator, elementsDenominator }; return new ObjectArrayList[] { elementsNumerator, elementsDenominator };
} }
private static int[] getFirstWorkingDivisionCouple(ObjectArrayList<Function>[] elements) { private static int[] getFirstWorkingDivisionCouple(ObjectArrayList<Function>[] elements) throws InterruptedException {
final int[] size = new int[] { elements[0].size(), elements[1].size() }; final int[] size = new int[] { elements[0].size(), elements[1].size() };
Function a; Function a;
Function b; Function b;
@ -103,6 +105,7 @@ public class DivisionRule1 {
for (int i = 0; i < size[0]; i++) { for (int i = 0; i < size[0]; i++) {
a = elements[0].get(i); a = elements[0].get(i);
for (int j = 0; j < size[1]; j++) { for (int j = 0; j < size[1]; j++) {
if (Thread.interrupted()) throw new InterruptedException();
b = elements[1].get(j); b = elements[1].get(j);
Function testFunc; Function testFunc;
testFunc = new Division(a.getMathContext(), a, b); testFunc = new Division(a.getMathContext(), a, b);

View File

@ -17,11 +17,11 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
*/ */
public class MultiplicationMethod1 { public class MultiplicationMethod1 {
public static boolean compare(Function f) { public static boolean compare(Function f) throws InterruptedException {
return ((Multiplication) f).getParameter1().isSimplified() && ((Multiplication) f).getParameter2().isSimplified() && !(((Multiplication) f).getParameter1() instanceof Number && ((Multiplication) f).getParameter2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null; return ((Multiplication) f).getParameter1().isSimplified() && ((Multiplication) f).getParameter2().isSimplified() && !(((Multiplication) f).getParameter1() instanceof Number && ((Multiplication) f).getParameter2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null;
} }
public static ObjectArrayList<Function> execute(Function f) throws Error { public static ObjectArrayList<Function> execute(Function f) throws Error, InterruptedException {
Function result; Function result;
final MathContext root = f.getMathContext(); final MathContext root = f.getMathContext();
final ObjectArrayList<Function> elements = getMultiplicationElements(f); final ObjectArrayList<Function> elements = getMultiplicationElements(f);
@ -32,6 +32,7 @@ public class MultiplicationMethod1 {
final int size = elements.size(); final int size = elements.size();
Function prec = new Multiplication(root, elem1, elem2); Function prec = new Multiplication(root, elem1, elem2);
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
if (Thread.interrupted()) throw new InterruptedException();
if (i != workingElementCouple[0] & i != workingElementCouple[1]) { if (i != workingElementCouple[0] & i != workingElementCouple[1]) {
final Function a = prec; final Function a = prec;
final Function b = elements.get(i); final Function b = elements.get(i);
@ -46,9 +47,10 @@ public class MultiplicationMethod1 {
return results; return results;
} }
private static ObjectArrayList<Function> getMultiplicationElements(Function mult) { private static ObjectArrayList<Function> getMultiplicationElements(Function mult) throws InterruptedException {
final ObjectArrayList<Function> elements = new ObjectArrayList<>(); final ObjectArrayList<Function> elements = new ObjectArrayList<>();
while (mult instanceof Multiplication) { while (mult instanceof Multiplication) {
if (Thread.interrupted()) throw new InterruptedException();
elements.add(((Multiplication) mult).getParameter1()); elements.add(((Multiplication) mult).getParameter1());
mult = ((Multiplication) mult).getParameter2(); mult = ((Multiplication) mult).getParameter2();
} }
@ -56,7 +58,7 @@ public class MultiplicationMethod1 {
return elements; return elements;
} }
private static int[] getFirstWorkingMultiplicationCouple(ObjectArrayList<Function> elements) { private static int[] getFirstWorkingMultiplicationCouple(ObjectArrayList<Function> elements) throws InterruptedException {
final int size = elements.size(); final int size = elements.size();
Function a; Function a;
Function b; Function b;
@ -70,6 +72,7 @@ public class MultiplicationMethod1 {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
a = elements.get(i); a = elements.get(i);
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
if (Thread.interrupted()) throw new InterruptedException();
b = elements.get(j); b = elements.get(j);
if (i != j) { if (i != j) {
Function testFunc; Function testFunc;

View File

@ -22,12 +22,12 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
*/ */
public class SumMethod1 { public class SumMethod1 {
public static boolean compare(Function f) { public static boolean compare(Function f) throws InterruptedException {
final MathContext root = f.getMathContext(); final MathContext root = f.getMathContext();
return (f instanceof Sum || f instanceof Subtraction) && ((FunctionOperator) f).getParameter1().isSimplified() && ((FunctionOperator) f).getParameter2().isSimplified() && !(((FunctionOperator) f).getParameter1() instanceof Number && ((FunctionOperator) f).getParameter2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null; return (f instanceof Sum || f instanceof Subtraction) && ((FunctionOperator) f).getParameter1().isSimplified() && ((FunctionOperator) f).getParameter2().isSimplified() && !(((FunctionOperator) f).getParameter1() instanceof Number && ((FunctionOperator) f).getParameter2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null;
} }
public static ObjectArrayList<Function> execute(Function f) throws Error { public static ObjectArrayList<Function> execute(Function f) throws Error, InterruptedException {
Function result; Function result;
final MathContext root = f.getMathContext(); final MathContext root = f.getMathContext();
final ObjectArrayList<Function> elements = getSumElements(f); final ObjectArrayList<Function> elements = getSumElements(f);
@ -39,6 +39,7 @@ public class SumMethod1 {
Function prec = new Sum(root, elem1, elem2); Function prec = new Sum(root, elem1, elem2);
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
if (i != workingElementCouple[0] & i != workingElementCouple[1]) { if (i != workingElementCouple[0] & i != workingElementCouple[1]) {
if (Thread.interrupted()) throw new InterruptedException();
final Function a = prec; final Function a = prec;
final Function b = elements.get(i); final Function b = elements.get(i);
if (b instanceof Negative) { if (b instanceof Negative) {
@ -60,10 +61,11 @@ public class SumMethod1 {
return results; return results;
} }
private static ObjectArrayList<Function> getSumElements(Function sum) { private static ObjectArrayList<Function> getSumElements(Function sum) throws InterruptedException {
final MathContext root = sum.getMathContext(); final MathContext root = sum.getMathContext();
final ObjectArrayList<Function> elements = new ObjectArrayList<>(); final ObjectArrayList<Function> elements = new ObjectArrayList<>();
while (sum instanceof Sum || sum instanceof Subtraction) { while (sum instanceof Sum || sum instanceof Subtraction) {
if (Thread.interrupted()) throw new InterruptedException();
if (sum instanceof Sum) { if (sum instanceof Sum) {
elements.add(((FunctionOperator) sum).getParameter2()); elements.add(((FunctionOperator) sum).getParameter2());
} else { } else {
@ -75,7 +77,7 @@ public class SumMethod1 {
return elements; return elements;
} }
private static int[] getFirstWorkingSumCouple(MathContext root, ObjectArrayList<Function> elements) { private static int[] getFirstWorkingSumCouple(MathContext root, ObjectArrayList<Function> elements) throws InterruptedException {
final int size = elements.size(); final int size = elements.size();
Function a; Function a;
Function b; Function b;
@ -85,6 +87,7 @@ public class SumMethod1 {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
a = elements.get(i); a = elements.get(i);
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
if (Thread.interrupted()) throw new InterruptedException();
b = elements.get(j); b = elements.get(j);
if (i != j) { if (i != j) {
Function testFunc; Function testFunc;

View File

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.