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
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.
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 methods Person "John", 85, 185, 110 NumberOfPerson "Thomas", 78, 190, 150 Name Age Length Weight "Anna", 65, 175, 130 "Lidy", 55, 160, 123 NumberOfPerson = 4
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 getNumberOfPerson() … getNumberOfPerson() :- … getName() :- setName(string Name) :- NumberOfPerson person.cl Name Age Length Weight getName() setName(string Name) … person.i person.pro
Classes and Objects Create a project ‘ch08class’ Add ‘person’ class to the project Check ‘Create Interface’
Classes and Objects Build up ‘person’ class In the ‘person.i‘ file, add 2 predicates Add constructor to the ‘person.cl’ file + person type +
Classes and Objects Add implementation to ‘person.pro’ file, Build! + lowercase identifier + constructor member assignment
Classes and Objects Add menu/menu-item and connect TaskMenu.mnu Object/CreatObject TaskWindow.win onObjectCreateObject
Classes and Objects Build / Try Press <F5> if there’s an error.
Classes and Objects Add Dialog to create ‘person’ object Create a package named ‘uiForms’ Create ‘createPerson’ dialog under uiForms
Classes and Objects Edit ‘createPerson‘ dialog Add ClickResponder to createObject_ctl name_ctl createObject_ctl
Classes and Objects In the TaskWindow.win change the code in ‘onObjectCreateObject’ clause
Classes and Objects Build Run add a line to constructor in ‘person.pro’ file. Build Run
Classes and Objects Add class predicate to person class Declare class predicate at the ‘person.cl’ file. +
Classes and Objects Add implementation to ‘person.pro’ Run! +
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
Classes and Modules Add predicate to ‘output.cl’ file. No type definition in the ‘output.cl’ file. No type for output + Overloading, Polymorphism
Classes and Modules Add implementation to ‘output.pro’ file. +
Classes and Modules Build / Run! Change constructor in the ‘person.pro’ file. Build / Run!
Holding Objects Garbage Collection Add 3 more menu options Unreferenced object’s memory is reclaimed. Keep the object reference for it being reachable. Add 3 more menu options
Holding Objects Edit ‘person.pro’ file. + +
Holding Objects Edit ‘person.pro’ file. (continued) + +
Holding Objects Build / Run! Add declaration to ‘person.i’ file. Test ‘Create Object’ menu item. +
Holding Objects Complete menu items Add class predicate to ‘person.cl’ file.
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([]).
Holding Objects Add connection to menu items TaskWindow.win ‘Show All’ and ‘Show Single’ predicates onObjectShowAll : window::menuItemListener. clauses onObjectShowAll(_Source, _MenuTag) :- person::showAllPersons(). onObjectShowSingle : window::menuItemListener. onObjectShowSingle(_Source, _MenuTag) :- person::getAllNames(NamesList), b_true = vpiCommonDialogs::listSelect("Select a name", NamesList, 0, Name, _Index), !, person::showSinglePerson(Name). onObjectShowSingle(_Source, _MenuTag) :- !. + +
Holding Objects Add declaration to ‘person.cl’ file. +
Holding Objects Add implementation to ‘person.pro’ file. + clauses getAllNames(NamesList) :- getNames(createdObjects, NamesList). class predicates getNames : (person* CreatedObjects, string* NamesList) procedure (i, o). getNames(ObjectsList, NamesList) :- [Head | Tail] = ObjectsList, !, Name = Head:getName(), getNames(Tail, TailNamesList), NamesList = [Name | TailNamesList]. getNames([], []) :- !.
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). getData(Name, ObjectsList, Number) :- [Head | Tail] = ObjectsList, Name <> Head:getName(), !, getData(Name, Tail, Number). getData(_Name, ObjectsList, Number) :- [Head | _] = ObjectsList, Number = Head:getNumber().
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(_, _).
Holding Objects Add declaration to ‘person.cl’ file. +
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), !, createdObjects := [Object | createdObjects]. deletePerson(_).
Holding Objects Rebuild All Run!