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

AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 4 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 4 2012. Fall. SME., Pukyong Nat ’ l Univ. Kim, Minsoo

AI/ES(Artificial Intelligence / Expert System)

Visual Prolog: Part 4

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

Kim, Minsoo

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

Contents• Classes and Objects• Classes and Modules• Holding Objects

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

Classes and Objects• The world of object orientation

– The world consists of identifiable objects.• Person, Cat, Dog, …

– An object has certain characteristics(attributes).• Person: name, length, weight, iq, …

– Objects can do something (tasks).• Operations / methods• Person: run, smile, talk, …

– The set of objects with the same operations and attributes class• Jack, Linda, John, … Person

– Objects are related to each other.

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

Classes and Objects• Class vs. Object

– Class: User-Defined Type(Domain)• Template for object creation new()

constructor• Manager for member object class

predicates/facts– Instance Object: created individual thing

• With different identity• With same attributes and methodsPerson

NumberOfPerson

NameAgeLengthWeight

"John", 85, 185, 110

"Thomas", 78, 190, 150

"Anna", 65, 175, 130

"Lidy", 55, 160, 123

NumberOfPerson = 4

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

Classes and Objects• OOP in Visual Prolog

– Class file(*.cl): declaration of public method• class-wide predicates and facts

– Interface file(*.i): declaration of public method• Instance object’s predicates and facts

– Prolog file (*.pro): actual implementation• Actual implementation of public method• Private method declaration and

implementation

Person

NumberOfPerson

NameAgeLengthWeight

getNumberOfPerson()…

getName()setName(string Name)…

getNumberOfPerson() :- …getName() :- …setName(string Name) :- …

person.cl

person.i person.pro

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

Classes and Objects• Create a project ‘ch08class’

– Add ‘person’ class to the project• Check ‘Create Interface’

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

Classes and Objects• Build up ‘person’ class

– In the ‘person.i‘ file, add 2 predicates

– Add constructor to the ‘person.cl’ file

+

+

person type

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

Classes and Objects– Add implementation to ‘person.pro’ file,

Build!+

+

lowercase identifier

constructormember assignment

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

Classes and Objects– Add menu/menu-item and connect

• TaskMenu.mnu Object/CreatObject• TaskWindow.win onObjectCreateObject

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

Classes and Objects• Build / Try

– Press <F5> if there’s an error.

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

Classes and Objects• Add Dialog to create ‘person’ object

– Create a package named ‘uiForms’– Create ‘createPerson’ dialog under

uiForms

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

Classes and Objects– Edit ‘createPerson‘ dialog– Add ClickResponder to createObject_ctl

name_ctl createObject_ctl

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

Classes and Objects– In the TaskWindow.win change the code

in ‘onObjectCreateObject’ clause

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

Classes and Objects– add a line to constructor in ‘person.pro’

file.

• Build Run

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

Classes and Objects• Add class predicate to person class

– Declare class predicate at the ‘person.cl’ file.

+

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

Classes and Objects– Add implementation to ‘person.pro’

Run!+

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

Classes and Modules• Module: non-object constructing class

– Just a collection of class predicates.• No interface file is created for module.

• Add ‘output.cl’ class to the project.– Uncheck ‘Create Interface’ option

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

Classes and Modules– Add predicate to ‘output.cl’ file.

• No type definition in the ‘output.cl’ file.

+

No type for output

Overloading,Polymorphism

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

Classes and Modules– Add implementation to ‘output.pro’ file.

+

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

Classes and Modules– Change constructor in the ‘person.pro’

file.

• Build / Run!

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

Holding Objects• Garbage Collection

– Unreferenced object’s memory is reclaimed.

– Keep the object reference for it being reachable.

• Add 3 more menu options

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

Holding Objects– Edit ‘person.pro’ file.

+

+

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

Holding Objects– Edit ‘person.pro’ file. (continued)

+

+

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

Holding Objects– Add declaration to ‘person.i’ file.

• Build / Run!– Test ‘Create Object’ menu item.

+

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

Holding Objects• Complete menu items

– Add class predicate to ‘person.cl’ file.

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

Holding Objects– Add implementation to ‘person.pro’ file.

+

showAllPersons() :- stdIO::write("These are the persons in the database.\n"), showAllObjects(createdObjects).

class predicates showAllObjects : (person* CreatedObjects).clauses showAllObjects([Head | Tail]) :- stdIO::write("Name: ", Head:getName(), " with Number: ", Head:getNumber()), stdIO::nl, !, showAllObjects(Tail). showAllObjects([]).

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

Holding Objects– Add connection to menu items

• TaskWindow.win ‘Show All’ and ‘Show Single’

+

+

predicates onObjectShowAll : window::menuItemListener.clauses onObjectShowAll(_Source, _MenuTag) :- person::showAllPersons().

predicates onObjectShowSingle : window::menuItemListener.clauses onObjectShowSingle(_Source, _MenuTag) :- person::getAllNames(NamesList), b_true = vpiCommonDialogs::listSelect("Select a name", NamesList, 0, Name, _Index), !, person::showSinglePerson(Name). onObjectShowSingle(_Source, _MenuTag) :- !.

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

Holding Objects– Add declaration to ‘person.cl’ file.

+

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

Holding Objects– Add implementation to ‘person.pro’ file.

+

clauses getAllNames(NamesList) :- getNames(createdObjects, NamesList).

class predicates getNames : (person* CreatedObjects, string* NamesList) procedure (i, o).clauses getNames(ObjectsList, NamesList) :- [Head | Tail] = ObjectsList, !, Name = Head:getName(), getNames(Tail, TailNamesList), NamesList = [Name | TailNamesList]. getNames([], []) :- !.

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

Holding Objects– Add implementation to ‘person.pro’ file.

(cont)+

clauses showSinglePerson(Name) :- getData(Name, createdObjects, Number), !, stdIO::write("You have selected: ", Name, " with number: ", Number), stdIO::nl. showSinglePerson(_Name).

class predicates getData : (string Name, person* ObjectsList, unsigned Number) determ (i, i, o).clauses getData(Name, ObjectsList, Number) :- [Head | Tail] = ObjectsList, Name <> Head:getName(), !, getData(Name, Tail, Number). getData(_Name, ObjectsList, Number) :- [Head | _] = ObjectsList, Number = Head:getNumber().

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

Holding Objects– Add code for ‘Delete Object’ menu item.

• TaskWindow.win onObjectDeleteObject

clauses onObjectDeleteObject(_Source, _MenuTag) :- person::getAllNames(NamesList), b_true = vpiCommonDialogs::listSelect("Select a name", NamesList, 0, Name, _Index), !, person::deletePerson(Name). onObjectDeleteObject(_, _).

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

Holding Objects– Add declaration to ‘person.cl’ file.

+

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

Holding Objects– Add implementation to ‘person.pro’ file.

+

clauses deletePerson(Name) :- [Object | Tail] = createdObjects, Name = Object:getName(), !, createdObjects := Tail, stdIO::write("Person ", Name, " has been deleted.\n"). deletePerson(Name) :- [Object | Tail] = createdObjects, createdObjects := Tail, deletePerson(Name), !, createdObjects := [Object | createdObjects]. deletePerson(_).

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

Holding Objects• Rebuild All Run!