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 kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>

Binary file not shown.

13
pom.xml
View File

@ -7,6 +7,17 @@
<version>1.0-SNAPSHOT</version>
<name>WarpPI Calculator</name>
<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>
<dependency>
<groupId>junit</groupId>
@ -32,7 +43,7 @@
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.1</version>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>

View File

@ -142,11 +142,11 @@ public class TestGPU {
this.d = d;
r = d.getRenderer();
exampleFont = d.loadFont("ex");
exampleFont = d.loadFont("norm");
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
c = new NormalInputContainer(new InputContext(), false, 0, 200);

View File

@ -535,7 +535,7 @@ public class Utils {
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) {
if (itm.isSimplified() == false) {
return false;

View File

@ -237,10 +237,10 @@ public final class DisplayManager implements RenderingLoop {
private void load_fonts() throws IOException {
fonts = new BinaryFont[7];
fonts[0] = engine.loadFont("big");
fonts[1] = engine.loadFont("small");
fonts[2] = engine.loadFont("ex");
fonts[3] = engine.loadFont("big");
fonts[0] = engine.loadFont("smal");
fonts[1] = engine.loadFont("smallest");
fonts[2] = engine.loadFont("norm");
fonts[3] = engine.loadFont("smal");
//4
//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 + height - chh, '╰');
r.glFillColor(x + prw + 3, y + 6, 2, height - 6 * 2);
r.glFillColor(x + width - 5, y + 6, 2, height - 6 * 2);
if (small) {
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 + height - chh, '╯');
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 {
private static final BinaryFont fontBig = Utils.getFont(false);
public MathContext calc;
public InputContext ic;
public InputContainer userInput;
public OutputContainer result;
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;
public MathInputScreen() {
@ -56,7 +64,7 @@ public class MathInputScreen extends Screen {
calc = new MathContext();
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) {
e.printStackTrace();
System.exit(1);
@ -75,7 +83,6 @@ public class MathInputScreen extends Screen {
@Override
public void beforeRender(float dt) {
if (DisplayManager.INSTANCE.error == null) {
DisplayManager.INSTANCE.renderer.glClearColor(0xFFc5c2af);
} else {
@ -84,9 +91,53 @@ public class MathInputScreen extends Screen {
if (userInput.beforeRender(dt)) {
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
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);
}
@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
public boolean mustBeRefreshed() {
@ -188,76 +226,97 @@ public class MathInputScreen extends Screen {
calc.resultsCount = 0;
return true;
} else {
try {
try {
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
Expression expr = MathParser.parseInput(calc, userInput);
if (calc.f == null | calc.f2 == null) {
calc.f = new ObjectArrayList<>();
calc.f2 = new ObjectArrayList<>();
} else {
calc.f.clear();
calc.f2.clear();
}
calc.f.add(expr);
int stop = 0;
boolean done = false;
ObjectArrayList<Function> resultExpressions = new ObjectArrayList<>();
resultExpressions.add(expr.getParameter());
while (!done && stop < 3000) {
ObjectArrayList<Function> newResultExpressions = new ObjectArrayList<>();
done = true;
for (Function f : resultExpressions) {
Function newResult = null;
if (f.isSimplified() == false) {
done = false;
if (f instanceof Expression) {
ObjectArrayList<Function> fncResult = ((Expression) f).solve();
for (Function resultItem : fncResult) {
newResultExpressions.add(resultItem);
if (!computingResult) {
computingResult = true;
computingThread = new Thread(()-> {
try {
try {
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
Expression expr = MathParser.parseInput(calc, userInput);
if (calc.f == null | calc.f2 == null) {
calc.f = new ObjectArrayList<>();
calc.f2 = new ObjectArrayList<>();
} else {
calc.f.clear();
calc.f2.clear();
}
calc.f.add(expr);
int stop = 0;
boolean done = false;
ObjectArrayList<Function> resultExpressions = new ObjectArrayList<>();
resultExpressions.add(expr.getParameter());
while (!done && stop < 3000) {
if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> newResultExpressions = new ObjectArrayList<>();
done = true;
for (Function f : resultExpressions) {
if (Thread.interrupted()) throw new InterruptedException();
Function newResult = null;
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 {
List<Function> fncResult = f.simplify();
for (Function resultItem : fncResult) {
newResultExpressions.add(resultItem);
if (newResult != null) {
newResultExpressions.add(newResult);
}
}
} else {
newResult = f;
resultExpressions = newResultExpressions;
stop++;
}
if (newResult != null) {
newResultExpressions.add(newResult);
Utils.out.println(2, "INPUT: " + expr);
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;
stop++;
} catch (final InterruptedException ex) {
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);
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();
// });
} catch (final Error e) {
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);
}
} catch (final Exception ex) {
if (StaticVars.debugOn) {
ex.printStackTrace();
}
throw new Error(Errors.SYNTAX_ERROR);
computingResult = false;
});
computingThread.setName("Computing Thread");
computingThread.setDaemon(true);
computingThread.setPriority(Thread.NORM_PRIORITY + 3);
computingThread.start();
return true;
} else {
if (computingThread != null) {
computingThread.interrupt();
computingResult = false;
return true;
}
} catch (final Error e) {
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 false;
}
return true;
}
case NUM0:
typeChar('0');

View File

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

View File

@ -113,7 +113,8 @@ public abstract class FunctionOperator implements Function {
}
@Override
public boolean isSimplified() {
public boolean isSimplified() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
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
* <strong>false</strong>.
* @throws InterruptedException
*/
protected abstract boolean isSolvable();
protected abstract boolean isSolvable() throws InterruptedException;
@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();
if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> result = solved ? solve() : null;;
if (result == null || result.isEmpty()) {
@ -136,14 +140,18 @@ public abstract class FunctionOperator implements Function {
final ObjectArrayList<Function> l1 = new ObjectArrayList<>();
final ObjectArrayList<Function> l2 = new ObjectArrayList<>();
if (Thread.interrupted()) throw new InterruptedException();
if (parameter1.isSimplified()) {
l1.add(parameter1);
} else {
if (Thread.interrupted()) throw new InterruptedException();
l1.addAll(parameter1.simplify());
}
if (Thread.interrupted()) throw new InterruptedException();
if (parameter2.isSimplified()) {
l2.add(parameter2);
} else {
if (Thread.interrupted()) throw new InterruptedException();
l2.addAll(parameter2.simplify());
}
@ -164,8 +172,9 @@ public abstract class FunctionOperator implements Function {
* @return The solved function.
* @throws Error
* 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
public abstract FunctionOperator clone();

View File

@ -96,7 +96,7 @@ public abstract class FunctionSingle implements Function {
}
@Override
public final ObjectArrayList<Function> simplify() throws Error {
public final ObjectArrayList<Function> simplify() throws Error, InterruptedException {
final boolean simplified = parameter.isSimplified();
ObjectArrayList<Function> result = simplified ? solve() : null;
@ -125,11 +125,12 @@ public abstract class FunctionSingle implements Function {
* @return The solved function.
* @throws Error
* 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
public boolean isSimplified() {
public boolean isSimplified() throws InterruptedException {
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
* <strong>false</strong>.
* @throws InterruptedException
*/
protected abstract boolean isSolvable();
protected abstract boolean isSolvable() throws InterruptedException;
@Override
public abstract FunctionSingle clone();

View File

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

View File

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

View File

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

View File

@ -72,11 +72,12 @@ public class Number implements Function {
return ret;
}
public Number pow(Number f) throws Error {
public Number pow(Number f) throws Error, InterruptedException {
Number ret = new Number(root, BigDecimal.ONE);
if (Utils.isIntegerValue(f.term)) {
final BigInteger bi = f.term.toBigInteger().abs();
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()));
}
if (f.term.signum() == -1) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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