Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
pro
ndl
Commits
5b64aa63
Verified
Commit
5b64aa63
authored
6 years ago
by
Marcin Moskal
Browse files
Options
Download
Email Patches
Plain Diff
Add query print loop
parent
dc35fd34
repl
constraints
knowledge-base
ranges
2 merge requests
!4
REPL
,
!1
Add basic knowledge base support
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/scala/pl/edu/agh/kis/eis/ndl/repl/REPL.scala
+24
-9
src/main/scala/pl/edu/agh/kis/eis/ndl/repl/REPL.scala
with
24 additions
and
9 deletions
+24
-9
src/main/scala/pl/edu/agh/kis/eis/ndl/repl/REPL.scala
View file @
5b64aa63
...
...
@@ -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
=
{
p
ri
n
t
(
prompt
)
w
rit
e
(
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
)
}
}
p
ri
n
tln
(
s
"NDL REPL: $mode"
)
w
rit
e
ln
(
s
"NDL REPL: $mode"
)
loop
(
new
KnowledgeBase
[
Domain
](
Set
.
empty
))
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help