Download presentation
Published byJarrod Vinton Modified over 10 years ago
1
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 4
2012. Fall. SME., Pukyong Nat’l Univ. Kim, Minsoo
2
Contents Classes and Objects Classes and Modules Holding Objects
3
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.
4
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
5
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
6
Classes and Objects Create a project ‘ch08class’
Add ‘person’ class to the project Check ‘Create Interface’
7
Classes and Objects Build up ‘person’ class
In the ‘person.i‘ file, add 2 predicates Add constructor to the ‘person.cl’ file + person type +
8
Classes and Objects Add implementation to ‘person.pro’ file, Build!
+ lowercase identifier + constructor member assignment
9
Classes and Objects Add menu/menu-item and connect
TaskMenu.mnu Object/CreatObject TaskWindow.win onObjectCreateObject
10
Classes and Objects Build / Try Press <F5> if there’s an error.
11
Classes and Objects Add Dialog to create ‘person’ object
Create a package named ‘uiForms’ Create ‘createPerson’ dialog under uiForms
12
Classes and Objects Edit ‘createPerson‘ dialog
Add ClickResponder to createObject_ctl name_ctl createObject_ctl
13
Classes and Objects In the TaskWindow.win change the code in ‘onObjectCreateObject’ clause
14
Classes and Objects Build Run
add a line to constructor in ‘person.pro’ file. Build Run
15
Classes and Objects Add class predicate to person class
Declare class predicate at the ‘person.cl’ file. +
16
Classes and Objects Add implementation to ‘person.pro’ Run! +
17
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
18
Classes and Modules Add predicate to ‘output.cl’ file.
No type definition in the ‘output.cl’ file. No type for output + Overloading, Polymorphism
19
Classes and Modules Add implementation to ‘output.pro’ file. +
20
Classes and Modules Build / Run!
Change constructor in the ‘person.pro’ file. Build / Run!
21
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
22
Holding Objects Edit ‘person.pro’ file. + +
23
Holding Objects Edit ‘person.pro’ file. (continued) + +
24
Holding Objects Build / Run! Add declaration to ‘person.i’ file.
Test ‘Create Object’ menu item. +
25
Holding Objects Complete menu items
Add class predicate to ‘person.cl’ file.
26
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([]).
27
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) :- !. + +
28
Holding Objects Add declaration to ‘person.cl’ file. +
29
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([], []) :- !.
30
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().
31
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(_, _).
32
Holding Objects Add declaration to ‘person.cl’ file. +
33
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(_).
34
Holding Objects Rebuild All Run!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.