ai/es (artificial intelligence / expert system) visual prolog: part 7 2012. fall. sme., pukyong nat...
Post on 29-Dec-2015
220 Views
Preview:
TRANSCRIPT
AI/ES(Artificial Intelligence / Expert System)
Visual Prolog: Part 7
2012. Fall.SME., Pukyong Nat’l Univ.
Kim, Minsoo
Contents• IO Stream• General Input and Output
IO Stream• Input and Output
– Input/Ouput/Error Stream• stdIO class
– Console application Old MS-DOS style• console class’s default mapping
– Input stream keyboard– Output/Error stream console screen
• stdIO class is mapped to console by default– GUI application Graphic Windows
• Output Stream Message Window– vpiMessages class– stdIO class
• Error Stream Error and Warning Window
IO Stream• Stream: a sequence of bytes
– 1100101001010101… 11001010, 01010101, …• Binary mode: no interpretation• Text mode(interpreted mode): read stream as
letters– VIP supports two codes: ANSI, Unicode
• ANSI code: an extension of ASCII code– Control codes: ANSI escape
sequence(ox1B5B…)• Unicode: 16bits base
– Try to deliver a code that can be used for every language in the world
IO Stream• Start a new project ‘consoleIO’
– Console application
IO Stream– Add a class ‘database’ as a module
• Uncheck the ‘Create Interface’ box
IO Stream– Add declaration to ‘database.cl’ file.
+
IO Stream– Add implementation to ‘database.pro’
file.+
class facts - persons person : (string Name).
clauses storeNames(N) = N :- stdIO::write("\nInput Name (<Enter> '0' to stop): "), '0' = stdIO::readChar(), !. storeNames(I) = Number :- stdIO::ungetChar(), Term = stdIO::readLine(), assert(person(Term)), N = I + 1, Number = storeNames(N).
fill() :- N = storeNames(0), stdIO::writef("\n%d terms were stored.\nPress any key.\n", N), stdIO::save(persons), _ = stdIO::readChar(), DBFile = outputStream_file::create8("person.txt"), stdIO::setOutputstream(DBFile), stdIO::save(persons), stdIO::flush().
IO Stream• After compile, change ‘main.pro’ file.
• Build & Run!– Check ‘person.txt’ file under Exe
directory.
+
IO Stream• Predicates for Input Stream
– stdIO::getInputStream() inputStream• Returns current input stream
– stdIO::setInputStream(inputStream)• Sets current input stream as inputStream
– stdIO::read() _ Term• Reads a specified term from the input stream• To define term’s domain use hasDomain/2
predicate– stdIO::readBytes(byteCount size)
binary• Reads a number of bytes from the input
stream and stores them into the binary variable
• Returned bytes’s length can be shorter than size when it encounters the end of stream before.
IO Stream• Predicates for Input Stream
(continued)– stdIO::readChar() char– stdIO:readLine() string
• Read until it encounter \n or zero-character(string terminative symbol) or end of stream.
– stdIO::readString(charCount) string• Tries to read given number of characters from
the input stream and stores them into the string.
– stdIO::ungetChar()• Returns last read character back to the
current input stream.
IO Stream• Predicates for Output Stream
– stdIO::flush()• Forces the contents of the internal buffer of
the current output stream to be written to the resource(or file) that it is connected to.
– stdIO::getOutputStream() outputStream– stdIO::setOutputStream(outputStream)– stdIO::nl()– stdIO::write(…), writeQuoted(…)– stdIO::writef(string Format
[formatString], …)• Produces output that is formatted
automatically.
IO Stream• Predicates for Error Stream
– stdIO::flush_error()– stdIO::getErrorStream()– stdIO::setErrorStream(outputStream)– stdIO::nl_error()– stdIO::write_error(…)– stdIO::writeQuoted_error(…)– stdIO::writef_error(…)
IO Stream• Special purpose predicates
– stdIO::consult(factDB)• Reads facts from the current input stream and
asserts them in the internal database (factDB)– stdIO::save(factDB)
• Saves the contents of the facts section with the name specified by factDB variable)
– stdIO::save_error(factDB)– stdIO::endOfStream()
• Checks whether a current position of input stream reaches the end of stream.
IO Stream• Add new class ‘readExamples’ to
project– Uncheck the ‘Create Interface’ box.
IO Stream– Add declaration to ‘readExamples.cl’ file.
+
IO Stream– Add implementation to
‘readExamples.pro’.
+
domains person = person(string Name). class facts intFact : integer := 1. stringFact : string := "This is a string". functorFact : person := person("Thomas").
clauses writeSomeTerms() :- stdIO::write("intFact = ", intFact, "\nstringFact = ", stringFact), stdIO::write("\nfunctorFact = ", functorFact, "\n").
readSomeTerms(IntFile, StringFile, FunctorFile) :- intFact := IntFile:read(), stringFact := StringFile:read(), functorFact := FunctorFile:read().
IO Stream– Create following 3 files under Exe
directory• Create as text files. 123 456 intSource.txt
"here is a string""here is another string"
stringSource.txt
person("Another Thomas")person("Anna")
functorSource.txt
IO Stream– Change the run clause in ‘main.pro’ file.
• Build & Run!
+
run():- console::init(), readExamples::writeSomeTerms(), IntFile = inputStream_file::openFile8("intSource.txt"), StringFile = inputStream_file::openFile8("stringSource.txt"), FunctorFile = inputStream_file::openFile8("functorSource.txt"), stdIO::write("All inputStream is open. Press <Enter>."), _ = stdIO::readChar(), readExamples::readSomeTerms(IntFile, StringFile, FunctorFile), readExamples::writeSomeTerms(), readExamples::readSomeTerms(IntFile, StringFile, FunctorFile), readExamples::writeSomeTerms(), _ = stdIO::readChar().
General Input and Output• Stream class hierarchy in VIP
– OuputStream• OutputStream_console• OutputStream_file
– InputStream• OutputStream_console• OutputStream_file
General Input and Output• Add class ‘copyStream’ to project.
– Uncheck the ‘Create Interface’ box.
General Input and Output– Add declaration ‘copyStream.cl’ file.
+
General Input and Output– Add implementation to ‘copyStream.pro’
+
class predicates copyStream : (inputStream InputFile, outputStream OutputFile) procedure (i, i). repeat : () multi (). clauses copyStream(InputFile, OutputFile) :- repeat(), String = InputFile:readLine(), stdIO::write(String, "\n"), OutputFile:write(String, "\n"), InputFile:endOfStream(), !. copyStream(_InputFile, _OutputFile). repeat(). repeat() :- repeat(). copy(InFile, OutFile) :- InputFile = inputStream_file::openFile8(InFile), OutputFile = outputStream_file::create(OutFile), copyStream(InputFile, OutputFile), InputFile:close(), OutputFile:close().
General Input and Output– Change run clause to test the predicates.
• Build & Run check file under Exe folder.
General Input and Output• Interface stream
– close()– getCRLFconversion() boolean
• Specifies whether the stream data will be handled with CRLF LF conversion (if true).
– setCRLFconversion(boolean)– getMode() mode
• binary, ANSI, Unicode– setMode(mode)– getPosition() unsigned64– setPosition(unsigned64)
General Input and Output• Interface inputStream
– consult(factDB)– reconsult(factDB)
• Retract the facts from DB and then consult.– endOfStream()– read() _ Value– readBytes(byteCount) binary– readChar() char– readLine() string– readString(charCount) string– ungetChar()
General Input and Output• Interface outputStream
– flush()– nl()– save(factDB)– write(…)– writeBytes(pointer Value, byteCount size)– writeQuoted(…)– writef(string FormatString [formatString],
…)
General Input and Output• Class inputStream_file
– openFile(string)• Opens Unicode file for input.
– openFile(string, mode)– openFile8(string)
• Opens ANSI file for input.
General Input and Output• Class outputStream_file
– append(string)• Opens Unicode file for output, setting the
stream position to the end of file.– append(string, mode)– append8(string)
• Opens ANSI file output, setting the stream position to the end of file.
– create(string), create(string, mode)– create8(string)– openFile(string), openFile(string, mode)– openFile8(string)
top related