Presentation is loading. Please wait.

Presentation is loading. Please wait.

AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 7 2012. Fall. SME., Pukyong Nat ’ l Univ. Kim, Minsoo.

Similar presentations


Presentation on theme: "AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 7 2012. Fall. SME., Pukyong Nat ’ l Univ. Kim, Minsoo."— Presentation transcript:

1 AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 7 2012. Fall. SME., Pukyong Nat ’ l Univ. Kim, Minsoo

2 Contents IO Stream General Input and Output

3 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

4 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

5 IO Stream Start a new project ‘consoleIO’ –Console application

6 IO Stream –Add a class ‘database’ as a module Uncheck the ‘Create Interface’ box

7 IO Stream –Add declaration to ‘database.cl’ file. + +

8 IO Stream –Add implementation to ‘database.pro’ file. + + class facts - persons person : (string Name). clauses storeNames(N) = N :- stdIO::write("\nInput Name ( '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().

9 IO Stream After compile, change ‘main.pro’ file. Build & Run! –Check ‘person.txt’ file under Exe directory. + +

10 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.

11 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.

12 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.

13 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(…)

14 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.

15 IO Stream Add new class ‘readExamples’ to project –Uncheck the ‘Create Interface’ box.

16 IO Stream –Add declaration to ‘readExamples.cl’ file. + +

17 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().

18 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

19 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."), _ = stdIO::readChar(), readExamples::readSomeTerms(IntFile, StringFile, FunctorFile), readExamples::writeSomeTerms(), readExamples::readSomeTerms(IntFile, StringFile, FunctorFile), readExamples::writeSomeTerms(), _ = stdIO::readChar().

20 General Input and Output Stream class hierarchy in VIP –OuputStream OutputStream_console OutputStream_file –InputStream OutputStream_console OutputStream_file

21 General Input and Output Add class ‘copyStream’ to project. –Uncheck the ‘Create Interface’ box.

22 General Input and Output –Add declaration ‘copyStream.cl’ file. + +

23 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().

24 General Input and Output –Change run clause to test the predicates. Build & Run  check file under Exe folder.

25 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)

26 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()

27 General Input and Output Interface outputStream –flush() –nl() –save(factDB) –write(…) –writeBytes(pointer Value, byteCount size) –writeQuoted(…) –writef(string FormatString [formatString], …)

28 General Input and Output Class inputStream_file –openFile(string) Opens Unicode file for input. –openFile(string, mode) –openFile8(string) Opens ANSI file for input.

29 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)


Download ppt "AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 7 2012. Fall. SME., Pukyong Nat ’ l Univ. Kim, Minsoo."

Similar presentations


Ads by Google