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

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

Embed Size (px)

Citation preview

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

AI/ES(Artificial Intelligence / Expert System)

Visual Prolog: Part 7

2012. Fall.SME., Pukyong Nat’l Univ.

Kim, Minsoo

Page 2: 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

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

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

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

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

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

IO Stream• Start a new project ‘consoleIO’

– Console application

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

IO Stream– Add a class ‘database’ as a module

• Uncheck the ‘Create Interface’ box

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

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

+

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

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

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

IO Stream• After compile, change ‘main.pro’ file.

• Build & Run!– Check ‘person.txt’ file under Exe

directory.

+

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

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.

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

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.

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

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.

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

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

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

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.

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

IO Stream• Add new class ‘readExamples’ to

project– Uncheck the ‘Create Interface’ box.

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

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

+

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

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

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

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

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

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

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

General Input and Output• Stream class hierarchy in VIP

– OuputStream• OutputStream_console• OutputStream_file

– InputStream• OutputStream_console• OutputStream_file

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

General Input and Output• Add class ‘copyStream’ to project.

– Uncheck the ‘Create Interface’ box.

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

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

+

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

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

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

General Input and Output– Change run clause to test the predicates.

• Build & Run check file under Exe folder.

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

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)

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

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

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

General Input and Output• Interface outputStream

– flush()– nl()– save(factDB)– write(…)– writeBytes(pointer Value, byteCount size)– writeQuoted(…)– writef(string FormatString [formatString],

…)

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

General Input and Output• Class inputStream_file

– openFile(string)• Opens Unicode file for input.

– openFile(string, mode)– openFile8(string)

• Opens ANSI file for input.

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

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)