Better history management

This commit is contained in:
Andrea Cavalli 2018-10-05 22:09:54 +02:00
parent b11d8ea4ed
commit 3808613a58
14 changed files with 132 additions and 138 deletions

View File

@ -43,7 +43,7 @@ public abstract class Block implements TreeBlock, GraphicalElement {
*/
public abstract ObjectArrayList<Block> getInnerBlocks();
public abstract int getInnerContainersCount();
public abstract ObjectArrayList<BlockContainer> getInnerContainers();
@Override
public abstract void recomputeDimensions();

View File

@ -78,8 +78,8 @@ public class BlockChar extends Block {
}
@Override
public int getInnerContainersCount() {
return 0;
public ObjectArrayList<BlockContainer> getInnerContainers() {
return null;
}
@Override

View File

@ -245,12 +245,21 @@ public class BlockContainer implements TreeContainer, GraphicalElement {
caret.skip(1);
pos++;
final int deltaCaret = caret.getRemaining();
final int caretOldPos = caret.getPosition();
removed = removed | b.delBlock(caret);
if (caret.getRemaining() == 0 || removed == false && deltaCaret >= 0 && caret.getRemaining() < 0) {
ObjectArrayList<Block> blocks = this.getBlockAt(pos - 1).get().getInnerBlocks();
int innerContainersCount = this.getBlockAt(pos - 1).get().getInnerContainersCount();
if (innerContainersCount > 0) {
innerContainersCount--;
ObjectArrayList<BlockContainer> innerContainers = this.getBlockAt(pos - 1).get().getInnerContainers();
int innerContainersBeforeCaret = 0;
int currentBlockIndex = 0;
if (innerContainers != null) {
for (BlockContainer c : innerContainers) {
currentBlockIndex += c.computeCaretMaxBound();
if (currentBlockIndex > deltaCaret) {
break;
}
innerContainersBeforeCaret++;
}
}
removeAt(pos - 1);
if (blocks != null) {
@ -262,7 +271,7 @@ public class BlockContainer implements TreeContainer, GraphicalElement {
blockNum++;
}
}
caret.setPosition(caret.getPosition() - innerContainersCount);
caret.setPosition(caretOldPos - innerContainersBeforeCaret);
removed = true;
}
}

View File

@ -136,8 +136,11 @@ public class BlockDivision extends Block {
}
@Override
public int getInnerContainersCount() {
return 2;
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(containerUp);
output.add(containerDown);
return output;
}
@Override

View File

@ -173,9 +173,13 @@ public class BlockLogarithm extends Block implements IParenthesis {
return output;
}
@Override
public int getInnerContainersCount() {
return 2;
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(containerBase);
output.add(containerNumber);
return output;
}
@Override

View File

@ -31,8 +31,10 @@ public class BlockParenthesis extends BlockParenthesisAbstract {
}
@Override
public int getInnerContainersCount() {
return 1;
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(getNumberContainer());
return output;
}
@Override

View File

@ -128,9 +128,12 @@ public abstract class BlockParenthesisAbstract extends Block implements IParenth
return containerNumber.getContent();
}
@Override
public int getInnerContainersCount() {
return 1;
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(containerNumber);
return output;
}
}

View File

@ -42,7 +42,7 @@ public class BlockPower extends Block {
}
@Override
public boolean delBlock(final Caret caret) {
public boolean delBlock(Caret caret) {
boolean removed = false;
removed = removed | containerExponent.delBlock(caret);
if (removed) {
@ -93,8 +93,10 @@ public class BlockPower extends Block {
}
@Override
public int getInnerContainersCount() {
return 1;
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(containerExponent);
return output;
}
@Override

View File

@ -99,8 +99,10 @@ public class BlockPower2 extends Block {
}
@Override
public int getInnerContainersCount() {
return 1;
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(containerExponent);
return output;
}
@Override

View File

@ -111,8 +111,10 @@ public class BlockSquareRoot extends Block {
}
@Override
public int getInnerContainersCount() {
return 1; //2
public ObjectArrayList<BlockContainer> getInnerContainers() {
ObjectArrayList<BlockContainer> output = new ObjectArrayList<>();
output.add(containerNumber);
return output;
}
@Override

View File

@ -71,8 +71,8 @@ public class BlockUndefined extends Block {
}
@Override
public int getInnerContainersCount() {
return 0;
public ObjectArrayList<BlockContainer> getInnerContainers() {
return null;
}
@Override

View File

@ -290,8 +290,8 @@ public class BlockVariable extends Block {
}
@Override
public int getInnerContainersCount() {
return 0;
public ObjectArrayList<BlockContainer> getInnerContainers() {
return null;
}
@Override

View File

@ -220,77 +220,9 @@ public class MathInputScreen extends Screen {
case STEP:
currentStep++;
return simplify(true, false);
case SIMPLIFY:
if (!step) {
currentStep = 0;
}
if (Engine.INSTANCE.getHardwareDevice().getDisplayManager().error != null) {
//TODO: make the error management a global API rather than being relegated to this screen.
Engine.getPlatform().getConsoleUtils().out().println(1, "Resetting after error...");
Engine.INSTANCE.getHardwareDevice().getDisplayManager().error = null;
calc.f = null;
calc.f2 = null;
calc.resultsCount = 0;
return true;
} else if (!computingResult) {
computingResult = true;
computingThread = new Thread(() -> {
try {
try {
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
final 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);
Engine.getPlatform().getConsoleUtils().out().println(2, "INPUT: " + expr);
final MathSolver ms = new MathSolver(expr);
final ObjectArrayList<ObjectArrayList<Function>> resultSteps = ms.solveAllSteps();
resultSteps.add(0, Utils.newArrayList(expr));
final ObjectArrayList<Function> resultExpressions = resultSteps.get(resultSteps.size() - 1);
for (final Function rr : resultExpressions) {
Engine.getPlatform().getConsoleUtils().out().println(0, "RESULT: " + rr.toString());
}
final ObjectArrayList<ObjectArrayList<Block>> resultBlocks = MathParser.parseOutput(calc, resultExpressions);
result.setContentAsMultipleGroups(resultBlocks);
// showVariablesDialog(() -> {
// currentExpression = newExpression;
// simplify();
// });
this.swapInputScreen();
}
} catch (final InterruptedException ex) {
Engine.getPlatform().getConsoleUtils().out().println(ConsoleUtils.OUTPUTLEVEL_DEBUG_MIN, "Computing thread stopped.");
} catch (final Exception ex) {
if (Engine.getPlatform().getSettings().isDebugEnabled()) {
ex.printStackTrace();
}
throw new Error(Errors.SYNTAX_ERROR);
}
} catch (final Error e) {
d.errorStackTrace = Engine.getPlatform().stacktraceToString(e);
Engine.INSTANCE.getHardwareDevice().getDisplayManager().error = e.id.toString();
System.err.println(e.id);
}
computingResult = false;
});
Engine.getPlatform().setThreadName(computingThread, "Computing Thread");
Engine.getPlatform().setThreadDaemon(computingThread);
computingThread.setPriority(Thread.NORM_PRIORITY + 3);
computingThread.start();
return true;
} else {
if (computingThread != null) {
computingThread.interrupt();
computingResult = false;
return true;
}
return false;
}
return simplify(false, true);
case NUM0:
typeChar('0');
return true;
@ -420,7 +352,7 @@ public class MathInputScreen extends Screen {
if (!result.isContentEmpty()) {
result.clear();
currentStep = 0;
Keyboard.keyPressed(Key.SIMPLIFY);
simplify(false, false);
}
return true;
case debug1:
@ -576,46 +508,79 @@ public class MathInputScreen extends Screen {
*/
}
@Deprecated
protected void simplify() {
/*
try {
try {
for (final Function f : calc.f) {
if (f instanceof Equation) {
Engine.INSTANCE.getHardwareDevice().getDisplayManager().setScreen(new SolveEquationScreen(this));
return;
protected boolean simplify(final boolean step, final boolean swapScreen) {
if (!step) {
currentStep = 0;
}
}
final ObjectArrayList<Function> results = solveExpression(calc.f);
if (results.size() == 0) {
if (Engine.INSTANCE.getHardwareDevice().getDisplayManager().error != null) {
//TODO: make the error management a global API rather than being relegated to this screen.
Engine.getPlatform().getConsoleUtils().out().println(1, "Resetting after error...");
Engine.INSTANCE.getHardwareDevice().getDisplayManager().error = null;
calc.f = null;
calc.f2 = null;
calc.resultsCount = 0;
return true;
} else if (!computingResult) {
computingResult = true;
computingThread = new Thread(() -> {
try {
try {
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
final Expression expr = MathParser.parseInput(calc, userInput);
if (calc.f == null | calc.f2 == null) {
calc.f = new ObjectArrayList<>();
calc.f2 = new ObjectArrayList<>();
} else {
calc.resultsCount = results.size();
Collections.reverse(results);
// add elements to al, including duplicates
final Set<Function> hs = new LinkedHashSet<>();
hs.addAll(results);
results.clear();
results.addAll(hs);
calc.f2 = results;
calc.f.clear();
calc.f2.clear();
}
calc.f.add(expr);
Engine.getPlatform().getConsoleUtils().out().println(2, "INPUT: " + expr);
final MathSolver ms = new MathSolver(expr);
final ObjectArrayList<ObjectArrayList<Function>> resultSteps = ms.solveAllSteps();
resultSteps.add(0, Utils.newArrayList(expr));
final ObjectArrayList<Function> resultExpressions = resultSteps.get(resultSteps.size() - 1);
for (final Function rr : resultExpressions) {
Engine.getPlatform().getConsoleUtils().out().println(0, "RESULT: " + rr.toString());
}
final ObjectArrayList<ObjectArrayList<Block>> resultBlocks = MathParser.parseOutput(calc, resultExpressions);
result.setContentAsMultipleGroups(resultBlocks);
// showVariablesDialog(() -> {
// currentExpression = newExpression;
// simplify();
// });
if (swapScreen == true) {
this.swapInputScreen();
}
}
} catch (final InterruptedException ex) {
Engine.getPlatform().getConsoleUtils().out().println(ConsoleUtils.OUTPUTLEVEL_DEBUG_MIN, "Computing thread stopped.");
} catch (final Exception ex) {
if (Utils.debugOn) {
if (Engine.getPlatform().getSettings().isDebugEnabled()) {
ex.printStackTrace();
}
throw new Error(Errors.SYNTAX_ERROR);
}
} 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");
d.errorStackTrace = Engine.getPlatform().stacktraceToString(e);
Engine.INSTANCE.getHardwareDevice().getDisplayManager().error = e.id.toString();
System.err.println(e.id);
}
*/
computingResult = false;
});
Engine.getPlatform().setThreadName(computingThread, "Computing Thread");
Engine.getPlatform().setThreadDaemon(computingThread);
computingThread.setPriority(Thread.NORM_PRIORITY + 3);
computingThread.start();
return true;
} else {
if (computingThread != null) {
computingThread.interrupt();
computingResult = false;
return true;
}
return false;
}
}
@SuppressWarnings("unused")

View File

@ -39,6 +39,8 @@ public class MathContext {
this.variablesValues.add(new VariableValue(varVal, this));
}
this.resultsCount = calc.resultsCount;
this.exactMode = calc.exactMode;
this.angleMode = calc.angleMode;
}
@Deprecated