Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Oct 22, 2020
1 parent 590b7a8 commit 1eab200
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 63 deletions.
16 changes: 5 additions & 11 deletions ide/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ plugins {
kotlin("jvm")
id("org.openjfx.javafxplugin") version Versions.org_openjfx_javafxplugin_gradle_plugin
id("com.github.johnrengelman.shadow") version Versions.com_github_johnrengelman_shadow_gradle_plugin
// id("org.beryx.jlink") version "2.22.1"
}

javafx {
Expand Down Expand Up @@ -56,9 +55,11 @@ val shadowJar = tasks.getByName<ShadowJar>("shadowJar") {
archiveVersion.set(project.version.toString())
archiveClassifier.set("redist")
sourceSets.main {
runtimeClasspath.filter { it.exists() }.map { if (it.isDirectory) it else zipTree(it) }.forEach {
from(it)
}
runtimeClasspath.filter { it.exists() }
.map { if (it.isDirectory) it else zipTree(it) }
.forEach {
from(it)
}
}
from(files("${rootProject.projectDir}/LICENSE"))

Expand All @@ -69,13 +70,6 @@ val shadowJar = tasks.getByName<ShadowJar>("shadowJar") {
}
}

// jlink{
// launcher {
// name = "2p-ide"
// }
// // imageZip.set(project.file("${project.buildDir}/image-zip/2p-ide.zip"))
// }

if (!githubToken.isNullOrBlank()) {
rootProject.configure<GithubReleaseExtension> {
releaseAssets(*(releaseAssets.toList() + shadowJar).toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import javafx.scene.control.TreeItem

class LibraryView(library: AliasedLibrary) : TreeItem<String>(library.alias) {
init {
isExpanded = true
isExpanded = false

val functionsChild = TreeItem("Functions")
val predicatesChild = TreeItem("Predicates")
Expand Down
15 changes: 15 additions & 0 deletions ide/src/main/kotlin/it/unibo/tuprolog/ui/gui/ListCellView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package it.unibo.tuprolog.ui.gui

import javafx.scene.Node
import javafx.scene.control.ListCell

class ListCellView<T : Any>(private val viewGenerator: (T) -> Node) : ListCell<T>() {
override fun updateItem(item: T?, empty: Boolean) {
super.updateItem(item, empty)
graphic = if (empty || item == null) {
null
} else {
viewGenerator(item)
}
}
}
56 changes: 25 additions & 31 deletions ide/src/main/kotlin/it/unibo/tuprolog/ui/gui/PrologIDEController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import javafx.scene.control.Alert
import javafx.scene.control.Button
import javafx.scene.control.ButtonType
import javafx.scene.control.Label
import javafx.scene.control.ListCell
import javafx.scene.control.ListView
import javafx.scene.control.MenuItem
import javafx.scene.control.ProgressBar
Expand Down Expand Up @@ -194,32 +193,11 @@ class PrologIDEController : Initializable {
model.onWarning.subscribe(this::onWarning)
model.onError.subscribe(this::onError)
model.onFileLoaded.subscribe(this::onFileLoaded)
model.onNewSolver.subscribe(this::onNewSolver)
model.onNewStaticKb.subscribe(this::onNewStaticKb)

lsvSolutions.setCellFactory {
object : ListCell<Solution>() {
override fun updateItem(item: Solution?, empty: Boolean) {
super.updateItem(item, empty)
if (empty || item == null) {
graphic = null
} else {
graphic = SolutionView.of(item)
}
}
}
}

lsvWarnings.setCellFactory {
object : ListCell<PrologWarning>() {
override fun updateItem(item: PrologWarning?, empty: Boolean) {
super.updateItem(item, empty)
if (empty || item == null) {
graphic = null
} else {
graphic = Label(item.message)
}
}
}
}
lsvSolutions.setCellFactory { ListCellView { SolutionView.of(it) } }
lsvWarnings.setCellFactory { ListCellView { Label(it.message) } }

tbcFunctor.cellValueFactory = PropertyValueFactory(Operator::functor.name)
tbcSpecifier.cellValueFactory = PropertyValueFactory(Operator::specifier.name)
Expand All @@ -231,6 +209,15 @@ class PrologIDEController : Initializable {
trvLibraries.root = TreeItem("Loaded libraries:")

model.newFile()
model.reset()
}

private fun onNewSolver(e: SolverEvent<Unit>) = onUiThread {
updateContextSensitiveView(e)
}

private fun onNewStaticKb(e: SolverEvent<Unit>) = onUiThread {
updateContextSensitiveView(e)
}

private fun onFileLoaded(e: Pair<File, String>) = onUiThread {
Expand Down Expand Up @@ -265,8 +252,7 @@ class PrologIDEController : Initializable {
""
}

private fun updatingContextSensitiveView(event: SolverEvent<*>, action: () -> Unit) {
action()
private fun updateContextSensitiveView(event: SolverEvent<*>) {
if (event.operators != lastEvent?.operators) {
tbvOperators.items.setAll(event.operators)
tabOperators.showNotification()
Expand All @@ -276,9 +262,12 @@ class PrologIDEController : Initializable {
tabFlags.showNotification()
}
if (event.libraries != lastEvent?.libraries) {
trvLibraries.root.children.setAll(
event.libraries.libraries.map { LibraryView(it) }
)
with(trvLibraries.root) {
children.setAll(event.libraries.libraries.map { LibraryView(it) })
if (children.isNotEmpty()) {
isExpanded = true
}
}
tabLibraries.showNotification()
}
if (event.staticKb != lastEvent?.staticKb) {
Expand All @@ -291,6 +280,11 @@ class PrologIDEController : Initializable {
}
}

private fun updatingContextSensitiveView(event: SolverEvent<*>, action: () -> Unit) {
action()
updateContextSensitiveView(event)
}

private fun onStderrPrinted(output: String) = onUiThread {
txaStderr.text += output
tabStderr.showNotification()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package it.unibo.tuprolog.ui.gui
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.exception.TuPrologException
import it.unibo.tuprolog.solve.Solution
import it.unibo.tuprolog.solve.Solver
import it.unibo.tuprolog.solve.TimeDuration
import it.unibo.tuprolog.solve.exception.PrologWarning
import it.unibo.tuprolog.solve.primitive.Solve
import org.reactfx.EventStream
import java.io.File
import java.util.concurrent.ExecutorService
Expand Down Expand Up @@ -60,6 +62,8 @@ interface PrologIDEModel {

fun stop()

fun reset()

var query: String

// var goal: Struct
Expand All @@ -74,6 +78,10 @@ interface PrologIDEModel {

val onQueryChanged: EventStream<String>

val onNewSolver: EventStream<SolverEvent<Unit>>

val onNewStaticKb: EventStream<SolverEvent<Unit>>

val onNewQuery: EventStream<SolverEvent<Struct>>

val onResolutionStarted: EventStream<SolverEvent<Int>>
Expand Down
57 changes: 37 additions & 20 deletions ide/src/main/kotlin/it/unibo/tuprolog/ui/gui/PrologIDEModelImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import it.unibo.tuprolog.solve.exception.PrologWarning
import it.unibo.tuprolog.theory.Theory
import it.unibo.tuprolog.theory.parsing.parseAsTheory
import it.unibo.tuprolog.ui.gui.PrologIDEModel.State
import it.unibo.tuprolog.utils.Cached
import org.reactfx.EventSource
import java.io.File
import java.util.EnumSet
Expand Down Expand Up @@ -82,11 +83,20 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
}
}

private val solver = MutableSolver.classicWithDefaultBuiltins(
stdOut = OutputChannel.of { onStdoutPrinted.push(it) },
stdErr = OutputChannel.of { onStderrPrinted.push(it) },
warnings = OutputChannel.of { onWarning.push(it) },
)
private val solver = Cached.of {
MutableSolver.classicWithDefaultBuiltins(
stdOut = OutputChannel.of { onStdoutPrinted.push(it) },
stdErr = OutputChannel.of { onStderrPrinted.push(it) },
warnings = OutputChannel.of { onWarning.push(it) },
).also {
onNewSolver.push(SolverEvent(Unit, it))
}
}

override fun reset() {
solver.invalidate()
solver.regenerate()
}

private var solutions: Iterator<Solution>? = null
private var solutionCount = 0
Expand All @@ -102,7 +112,7 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
override fun solveAll() {
solveImpl {
state = State.COMPUTING
onResolutionStarted.push(SolverEvent(++solutionCount, solver))
onResolutionStarted.push(SolverEvent(++solutionCount, solver.value))
nextAllImpl()
}
}
Expand All @@ -112,7 +122,7 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
try {
solutions = newResolution()
solutionCount = 0
onNewQuery.push(SolverEvent(lastGoal!!, solver))
onNewQuery.push(SolverEvent(lastGoal!!, solver.value))
continuation()
} catch (e: ParseException) {
onError.push(e)
Expand All @@ -121,22 +131,25 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
}

private fun newResolution(): Iterator<Solution> {
lastGoal = query.parseAsStruct(solver.operators)
val theory = currentFile?.let { getFile(it) }?.parseAsTheory(solver.operators) ?: Theory.empty()
solver.loadStaticKb(theory)
return solver.solve(lastGoal!!, timeout).iterator()
solver.value.let { s ->
lastGoal = query.parseAsStruct(s.operators)
val theory = currentFile?.let { getFile(it) }?.parseAsTheory(s.operators) ?: Theory.empty()
s.loadStaticKb(theory)
onNewStaticKb.push(SolverEvent(Unit, s))
return s.solve(lastGoal!!, timeout).iterator()
}
}

override var timeout: TimeDuration = 5000

private fun nextImpl() {
executor.execute {
onResolutionStarted.push(SolverEvent(++solutionCount, solver))
onResolutionStarted.push(SolverEvent(++solutionCount, solver.value))
val sol = solutions!!.next()
onResolutionOver.push(SolverEvent(solutionCount, solver))
onNewSolution.push(SolverEvent(sol, solver))
onResolutionOver.push(SolverEvent(solutionCount, solver.value))
onNewSolution.push(SolverEvent(sol, solver.value))
state = if (!sol.isYes || !solutions!!.hasNext()) {
onQueryOver.push(SolverEvent(sol.query, solver))
onQueryOver.push(SolverEvent(sol.query, solver.value))
State.IDLE
} else {
State.SOLUTION
Expand All @@ -149,10 +162,10 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
val sol = solutions!!.next()
// onResolutionOver.push(solutionCount)
solutionCount++
onNewSolution.push(SolverEvent(sol, solver))
onNewSolution.push(SolverEvent(sol, solver.value))
if (!solutions!!.hasNext() || state != State.COMPUTING) {
onResolutionOver.push(SolverEvent(solutionCount, solver))
onQueryOver.push(SolverEvent(sol.query, solver))
onResolutionOver.push(SolverEvent(solutionCount, solver.value))
onQueryOver.push(SolverEvent(sol.query, solver.value))
state = State.IDLE
} else {
nextAllImpl()
Expand All @@ -170,15 +183,15 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol
override fun nextAll() {
ensuringStateIs(State.SOLUTION) {
state = State.COMPUTING
onResolutionStarted.push(SolverEvent(+solutionCount, solver))
onResolutionStarted.push(SolverEvent(solutionCount, solver.value))
nextAllImpl()
}
}

override fun stop() {
ensuringStateIs(State.SOLUTION) {
state = State.IDLE
onQueryOver.push(SolverEvent(lastGoal!!, solver))
onQueryOver.push(SolverEvent(lastGoal!!, solver.value))
}
}

Expand All @@ -200,6 +213,10 @@ internal class PrologIDEModelImpl(override val executor: ExecutorService) : Prol

override val onNewQuery: EventSource<SolverEvent<Struct>> = EventSource()

override val onNewSolver: EventSource<SolverEvent<Unit>> = EventSource()

override val onNewStaticKb: EventSource<SolverEvent<Unit>> = EventSource()

override val onResolutionStarted: EventSource<SolverEvent<Int>> = EventSource()

override val onNewSolution: EventSource<SolverEvent<Solution>> = EventSource()
Expand Down

0 comments on commit 1eab200

Please sign in to comment.