ai/es (artificial intelligence / expert system) visual prolog: part 3 2012. fall. sme., pukyong nat...
TRANSCRIPT
AI/ES(Artificial Intelligence / Expert System)
Visual Prolog: Part 3
2012. Fall.SME., Pukyong Nat’l Univ.
Kim, Minsoo
Contents• Data Modeling in Prolog• A Minimal Database
Data Modeling in Prolog• How data is modeled in Prolog (PIE)
– Domain Field Data Type• char: one character between single quotes
– ‘a’, ‘b’, ‘\n’, ‘\t’, ‘\r’, ‘\\’, ‘\”’, …• string: a series of characters between double
quotes– “Hill”, “John”, “…\n…”
• integer: an integral signed number– +1, 1, -2, 5, 0, …
• unsigned: integral unsigned numbers– 3, 4, …
• real: numbers that may contain a decimal part– -3.5, +4.56, -1.23456789e+18, …
– Simple Domain vs. Complex Domain
Data Modeling in Prolog• Extending Family Theory Example
– Open & Execute ‘PIE’ Project• Load ‘FamilyTree(A1).PRO’ and reconsult.
brother and sister relations?
Need to express gender.
Modeling: ‘Relation’ vs.
‘Entity’
Data Modeling in Prolog– Define a ‘person’ predicate, change
others• person(“Bill”, “male”), person(“Pam”,
“female”).X
X
+
+
Data Modeling in Prolog– Now, test your family theory!
• ? brother(“Jane”, X), sister(“Jack”, Y).
John, M Lisa, F Jim, M
Jane, FBill, M Sue, F
Pam, F Jack, M
Data Modeling in Prolog• In Prolog (Horn clauses)
– rules: ‘Head’ is true if ‘Body’ is true.• Head :- Body
– facts: clauses with ‘empty’ bodies.• cat(“Tom”).• cat(“Tom”) :- true.
– functor: a fact builds up compound domain.• Usually to define entities with their properties.
– person(string Name, string Gender)• functor is not a function
– A functor has no computation within it.• You can declare a functor in PIE by simply
using it in your predicates.– VIP requires you to declare functor before you
use.
Data Modeling in Prolog• Try another family theory
– Load ‘FamilyTree(B1).PRO’ / Reconsult
person(Who, “male”).
parent(X, person(_, “female”).
grandFather(X, Y).
sibling(person(“Pam”, “female”), person(Sibling, Gender)).
sister(person(_, “male”), person(X, Y)).
brother(person(_, “female”), X).
ancestor(person(“Pam”, _), Who).
Data Modeling in Prolog– Think of these two different theories
vs.
Data Modeling in Prolog• How to model a theory?
– Modeling• The establishment of a relationship between
the subset of the outside real-world that is being tackled with the internal data structures of software.
• Focus on entities and their relationships.– Description of entity functor (complex
domain)– Description of relationship fact, rule
– Software building• Software will be only as good as allowed by
the modeled data.– Poor model poorly working (inefficient)
software• Use side effects to tackle goals or sub-goals.
A Minimal Database• A data bases on a file
– Person Entity in Table (Relational-Model)• Using fact’s OAV-tuple (Object-Attribute-Value)• Using functor person(ID, Name, Weight,
Length, IQ)
fact("John", "ID", 1).fact("John", "Weight", 85).fact("John", "Length", 185).fact("John", "IQ", 110).fact("Thomas", "ID", 2).fact("Thomas", "Weight", 78)....
person("1", "John", 85, 185, 110).person("2", "Thomas", 78, 190, 150).person("3", "Anna", 65, 175, 130).person("4", "Lidy", 55, 160, 123)....
A Minimal Database• Create ‘chap07db’ project
– Add ‘person’ package to the root Build!
A Minimal Database– Create ‘person’ class under person
package• person package New In Existing Package…
A Minimal Database– add code(class facts) to ‘person.pro’ file.
A Minimal Database– Add declaration to ‘person.cl’ file.
– Build again!
A Minimal Database– Add menu/menu-item and connect
• TaskMenu.mnu Database/FillDatabase• TaskWindow.win onDatabaseFilldatabase
A Minimal Database• Rebuild All / Execute
– (if error) ‘Rebuild All’ again / Press ‘F5’
A Minimal Database• Add more predicates & connect to
menu– In ‘person.pro’ file
• foreach condition do body end foreach.
listDatabaseFail() :- stdIO::write("=== person database ===\n"), person(ID, Name, Weight, Length, IQ), stdIO::write("ID: ", ID, ", Name: ", Name, ", Weight: ", Weight, ", Length: ", Length, ", IQ: ", IQ), stdIO::nl, fail.listDatabaseFail() :- stdIO::write("==================\n"). listDatabaseForEach() :- stdIO::write("=== person database ===\n"), foreach person(ID, Name, Weight, Length, IQ) do stdIO::write("ID: ", ID, ", Name: ", Name, ", Weight: ", Weight, ", Length: ", Length, ", IQ: ", IQ), stdIO::nl end foreach, stdIO::write("==================\n").
A Minimal Database– Add declaration to ‘person.cl’ file.
A Minimal Database– Add menu/menu-item and connect
• TaskMenu.mnu TaskWindow.win Build/Try!
A Minimal Database• Add Dialog to create a person record
– Create a ‘forms’ package to the project root.
– Add ‘addPerson’ Dialog to ‘forms’ package.
A Minimal Database– Edit ‘addPerson’ dialog
• Add ‘Static Text’ and ‘Edit Control’ Build All!
name_ctlweight_ctllength_ctliq_ctl
A Minimal Database– In ‘person.pro’ file.
• Add ‘addPerson’ predicate and ‘list’ library.
addPerson(Name, Weight, Length, IQ) :- findAll(ID, person(ID, _, _, _, _), IDList), NewID = toString(toTerm(maximum(IDList)) + 1), assertz(person(NewID, Name, Weight, Length, IQ)), stdIO::write("Person ", Name, " has been added with ID: ", NewID), stdIO::nl.
A Minimal Database– Add declaration to ‘person.cl’ file.
Build!
A Minimal Database– Add menu/menu-item and connect
• TaskMenu.mnu Database/Add Person• TaskWindow.win onDatabaseAddPerson Try!
A Minimal Database– Add ClickResponder to <OK> button
• addPerson.dlg OK-button Event
A Minimal Database– Build / Try!
• Change addPerson(person.pro) to prevent exception.
+
+
A Minimal Database• Add Dialog to delete a person record
– Add ‘deletePerson’ Dialog to ‘forms’ package.
A Minimal Database– Edit ‘deletePerson.dlg’ dialog
• Add ‘List Box’ and ‘Edit Control’ Build All!
nameListbox_ctl
idListbox_ctl
name_ctlid_ctl
ReadOnly:True
A Minimal Database– Add menu/menu-item and connect
• TaskMenu.mnu Database/Delete Person• TaskWindow.win onDatabaseDeletePerson
A Minimal Database– Add 4 predicates to ‘person.pro’ file.
getAllNames(NameList) :- findAll(Name, person(_, Name, _, _, _), NameList). getSomeIDs(Name, IDList) :- findAll(ID, person(ID, Name, _, _, _), IDList). getPerson(ID, Name, Weight, Length, IQ) :- person(ID, Name, Weight, Length, IQ), !.getPerson(_ID, "0", 0, 0, 0).
deletePerson(ID) :- retract(person(ID, Name, _, _, _)), !, stdIO::write("Person ", Name, " with ID: ", ID, " has been deleted.\n").deletePerson(_).
A Minimal Database– Add declaration to ‘person.cl’ file.
Build!
A Minimal Database– Change ‘deletePerson.pro’ file Rebuild!
new(Parent) :- dialog::new(Parent), generatedInitialize(), person::getAllNames(NameList), SingletonList = removeDuplicates(NameList), nameListbox_ctl:addList(SingletonList).
A Minimal Database– Edit ‘deletePerson.dlg’ dialog
• nameListbox_ctl SelectionChangeListener Event onNameListboxSelectionChanged
onNameListboxSelectionChanged(Source) :- [Name | _T] = Source:getSelectedItems(), person::getSomeIDs(Name, IDList), idListbox_ctl:clearAll(), id_ctl:setText(""), name_ctl:setText(""), idListbox_ctl:addList(IDList), !.onNameListboxSelectionChanged(_Source).
A Minimal Database• idListbox_ctl SelectionChangeListener Event
onIdListboxSelectionChanged
onIdListboxSelectionChanged(Source) :- [ID | _T] = Source:getSelectedItems(), person::getPerson(ID, Name, _, _, _), id_ctl:setText(ID), name_ctl:setText(Name), !.onIdListboxSelectionChanged(_Source).
A Minimal Database• OK-button ClickResponder Event onOkClick
A Minimal Database• Build / Try!
– Change ‘deletePerson.dlg‘• Cancel-button ClickResponder
onCancelClick
A Minimal Database• Add Dialog to change a person record
– Add ‘changePerson.dlg‘ under forms package.
A Minimal Database– Edit ‘changePerson.dlg‘
• Add ‘List Box’ and ‘Edit Control’ Build All!
nameListbox_ctlidListbox_ctl
name_ctlweight_ctllength_ctliq_ctl
id_ctl
ReadOnly:True
A Minimal Database– Add menu/menu-item and connect
• TaskMenu.mnu Database/Change Person• TaskWindow.win onDatabaseChangePerson
A Minimal Database– Add a predicate to ‘person.pro’ file.
changePerson(ID, Name, Weight, Length, IQ) :- retract(person(ID, _, _, _, _)), assertz(person(ID, Name, Weight, Length, IQ)), !, stdIO::write("Person ", Name, " with ID: ", ID, " has been changed."), stdIO::nl.changePerson(_, _, _, _, _).
A Minimal Database– Add declaration to ‘person.cl’ file.
Build!
A Minimal Database– Change ‘changePerson.pro’ file
Rebuild!
new(Parent) :- dialog::new(Parent), generatedInitialize(), person::getAllNames(NameList), SingletonList = removeDuplicates(NameList), nameListbox_ctl:addList(SingletonList).
A Minimal Database– Edit ‘changePerson.dlg’ dialog
• nameListbox_ctl SelectionChangeListener Event onNameListboxSelectionChanged
onNameListboxSelectionChanged(Source) :- [Name | _T] = Source:getSelectedItems(), person::getSomeIDs(Name, IDList), idListbox_ctl:clearAll(), id_ctl:setText(""), name_ctl:setText(""), weight_ctl:setText(""), length_ctl:setText(""), iq_ctl:setText(""), idListbox_ctl:addList(IDList), !.onNameListboxSelectionChanged(_Source).
A Minimal Database• idListbox_ctl SelectionChangeListener Event
onIdListboxSelectionChanged
onIdListboxSelectionChanged(Source) :- [ID | _T] = Source:getSelectedItems(), person::getPerson(ID, Name, Weight, Length, IQ), id_ctl:setText(ID), name_ctl:setText(Name), weight_ctl:setText(toString(Weight)), length_ctl:setText(toString(Length)), iq_ctl:setText(toString(IQ)), !.onIdListboxSelectionChanged(_Source).
A Minimal Database• OK-button ClickResponder Event onOkClick
onOkClick(_Source) = button::defaultAction :- ID = id_ctl:getText(), Name = name_ctl:getText(), Weight = toTerm(weight_ctl:getText()), Length = toTerm(length_ctl:getText()), IQ = toTerm(iq_ctl:getText()), person::changePerson(ID, Name, Weight, Length, IQ).
A Minimal Database• Rebuild All / Try!
A Minimal Database• Saving and consulting the database
– Internal database is a volatile storage.• Save an internal database to disk save/2
– Arguments: filename, internal database name– Give a name to internal database to save it!
– Change ‘person.pro’ file to name a database.
A Minimal Database– In ‘person.pro’ file.
• Add 2 predicates and ‘file’ library.
saveDatabase() :- file::existFile("personDB.txt"), !, file::delete("personDB.txt"), file::save("personDB.txt", personDB), stdIO::write("The database has been overwritten to file personDB.txt\n").saveDatabase() :- file::save("personDB.txt", personDB), stdIO::write("The database has been saved to file personDB.txt\n").
consultDatabase() :- file::consult("personDB.txt", personDB), stdIO::write("Records have been added from file personDB.txt"), stdIO::nl.
A Minimal Database– Add declaration to ‘person.cl’ file.
Build!
A Minimal Database– Add menu-items and connect
• TaskMenu.mnu Window.win
A Minimal Database• Build and Execute!