Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/input-error-handling #70

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main/scala/satify/model/State.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package satify.model

import satify.model
import satify.model.errors.Error
import satify.model.expression.Expression
import satify.model.problems.Problem

Expand All @@ -25,13 +26,24 @@ trait State:
type Problem = problems.Problem
val problem: Option[Problem] = None

/** Error representation */
type Error = errors.Error
val error: Option[Error] = None

/** Factory for [[State]] instances. */
object State:
/** Creates a new empty application state.
* @return a new [[State]] instance.
*/
def apply(): State = StateImpl()

/** Creates a new application state containing only the the input and the occurred error.
* @param input the input string
* @param error the [[Error]]
* @return a new [[State]] instance.
*/
def apply(input: String, error: Error): State = StateImpl(Some(input), None, None, None, None, Some(error))

/** Creates a new application state with input expression and its CNF.
* @param exp the input [[Expression]]
* @param cnf the [[CNF]]
Expand Down Expand Up @@ -68,5 +80,6 @@ object State:
override val expression: Option[Expression] = None,
override val cnf: Option[CNF] = None,
override val solution: Option[Solution] = None,
override val problem: Option[Problem] = None
override val problem: Option[Problem] = None,
override val error: Option[Error] = None
) extends State
5 changes: 5 additions & 0 deletions src/main/scala/satify/model/errors/Error.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package satify.model.errors

enum Error:
case InvalidInput
case Unknown
12 changes: 9 additions & 3 deletions src/main/scala/satify/update/Update.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package satify.update
import satify.dsl.Reflection.reflect
import satify.model.*
import satify.model.CNF.Symbol
import satify.model.errors.Error
import satify.model.errors.Error.InvalidInput
import satify.model.expression.Expression
import satify.model.problems.NQueens.*
import satify.model.problems.ProblemChoice.{GraphColoring, NurseScheduling, NQueens as NQueensChoice}
Expand All @@ -20,9 +22,13 @@ object Update:
message match
case Input(char) => model
case Solve(input) =>
val exp = reflect(input)
given CNFConverter = exp => tseitin(exp)
State(input, exp, Solver().solve(exp))
try
val exp = reflect(input)
given CNFConverter = exp => tseitin(exp)
State(input, exp, Solver().solve(exp))
catch
case e: Exception =>
State(input, InvalidInput)
case SolveProblem(problem, parameter) =>
val exp: Expression = problem match
case NQueensChoice => NQueens(parameter).exp
Expand Down
24 changes: 20 additions & 4 deletions src/main/scala/satify/view/View.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package satify.view

import satify.model.errors.Error
import satify.model.errors.Error.*
import satify.model.{CNF, Solution, State}
import satify.view.ComponentUtils.{createInputTextArea, createNextSection, createOutputTextArea}
import satify.view.ComponentUtils.{createErrorDialog, createInputTextArea, createNextSection, createOutputTextArea}
import satify.view.Constants.{cnfOutputDialogName, solOutputDialogName}

import scala.swing.*

object View:
def view(model: State): Set[Component] =
import model.*
updateExpression(input.get) ++ updateCnf(cnf) ++ updateSolution(model, solution)
if error.isDefined then updateError(error.get, input.get)
else updateExpression(input.get) ++ updateCnf(cnf) ++ updateSolution(model, solution)

/** Update the solution components
* @param model the current state
* @param sol the solution to show
* @return
* @return a set of components to add to the GUI
*/
private def updateSolution(model: State, sol: Option[Solution]): Set[Component] =
if sol.isDefined then
Expand All @@ -39,7 +42,20 @@ object View:
else Set()

/** Update the expression text area
* @param exp the component to show
* @param exp the exp to show in new component
* @return a set of components to add to the GUI
*/
private def updateExpression(exp: String): Set[Component] = Set(createInputTextArea(exp))

/** Update the error and show it in a dialog
* @param error the error to show in a popup dialog
* @param input the input to show in new component
* @return a set of components to add to the GUI
*/
private def updateError(error: Error, input: String): Set[Component] =
var errorDialog: Dialog = null
error match
case InvalidInput => errorDialog = createErrorDialog("Invalid input")
case Unknown => errorDialog = createErrorDialog("Unknown error")
errorDialog.open()
Set(createInputTextArea(input))