Verified Commit 14c30974 authored by Marcin Moskal's avatar Marcin Moskal
Browse files

Add query print loop

parent b8aa9fbc
......@@ -12,47 +12,62 @@ trait REPL extends App with LazyLogging {
val parser: NDLParser = new NDLParser
type Domain = parser.sExpParser.Domain
type Result = Iterator[QueryExecutor[Domain]#Result]
type Evaluation = (KnowledgeBase[Domain], Result)
def executeQuery: QueryExecutor[Domain] => Query[Domain] => Result
def readInput(): String = StdIn.readLine()
def write: Any => Unit = Predef.print
def writeln: Any => Unit = println
def read(): parser.sExpParser.ParseResult[NDL[Domain]] = {
val input: String = readInput()
val parsed = parser.parser(new CharSequenceReader(input))
logger.debug("parse result: {}", parsed)
logger.trace("parse result: {}", parsed)
parsed
}
def eval(kb: KnowledgeBase[Domain], in: NDL[Domain]): (KnowledgeBase[Domain], Result) = {
def eval(kb: KnowledgeBase[Domain], in: NDL[Domain]): Evaluation = {
in match {
case fact: Fact[Domain] =>
logger.debug("eval(): expanding kb")
logger.trace("eval(): expanding kb")
(kb.addFact(fact), Iterator.empty)
case q: Query[Domain] =>
logger.debug("eval(): query")
logger.trace("eval(): query")
(kb, executeQuery(kb)(q))
}
}
def print: Evaluation => Unit = { case r@(_, bindingIterator) =>
if (bindingIterator.hasNext) {
write(queryPrompt)
readInput()
// FIXME implement binding .toString()
writeln(bindingIterator.next().toList.map(x => s"(${x.name} ${x.value})").mkString(", "))
print(r)
}
}
val prompt: String = "> "
val queryPrompt: String = "? "
def mode: String
@tailrec
final def loop(kb: KnowledgeBase[Domain]): Unit = {
print(prompt)
write(prompt)
val str = read()
if (str.successful) {
val (newKb, queryResult) = eval(kb, str.get)
logger.debug("kb after eval: {}", newKb.facts)
logger.debug("evaluated query: {}", queryResult.toList)
val e@(newKb, queryResult) = eval(kb, str.get)
logger.trace("kb after eval: {}", newKb.facts)
logger.trace("evaluated query: {}", queryResult.toList)
print(e)
loop(newKb)
} else {
loop(kb)
}
}
println(s"NDL REPL: $mode")
writeln(s"NDL REPL: $mode")
loop(new KnowledgeBase[Domain](Set.empty))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment