Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 3 2012. Fall. SME., Pukyong Nat l Univ. Kim, Minsoo."— Presentation transcript:

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

2 Contents Data Modeling in Prolog A Minimal Database

3 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

4 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 brother and sister relations? Need to express gender. Modeling: Relation vs. Entity

5 Data Modeling in Prolog –Define a person predicate, change others person(Bill, male), person(Pam, female). X X X X + + + +

6 Data Modeling in Prolog –Now, test your family theory! ? brother(Jane, X), sister(Jack, Y). John, M Lisa, F Jim, M Jane, F Bill, M Sue, F Pam, F Jack, M

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

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

9 Data Modeling in Prolog –Think of these two different theories vs.

10 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.

11 A Minimal Database A data bases on a file –Person Entity in Table (Relational-Model) Using facts 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)....

12 A Minimal Database Create chap07db project –Add person package to the root Build!

13 A Minimal Database –Create person class under person package person package New In Existing Package…

14 A Minimal Database –add code(class facts) to person.pro file.

15 A Minimal Database –Add declaration to person.cl file. –Build again!

16 A Minimal Database –Add menu/menu-item and connect TaskMenu.mnu Database/FillDatabase TaskWindow.win onDatabaseFilldatabase

17 A Minimal Database Rebuild All / Execute –(if error) Rebuild All again / Press F5

18 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").

19 A Minimal Database –Add declaration to person.cl file.

20 A Minimal Database –Add menu/menu-item and connect TaskMenu.mnu TaskWindow.win Build/Try!

21 A Minimal Database Add Dialog to create a person record –Create a forms package to the project root. –Add addPerson Dialog to forms package.

22 A Minimal Database –Edit addPerson dialog Add Static Text and Edit Control Build All! name_ctl weight_ctl length_ctl iq_ctl

23 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.

24 A Minimal Database –Add declaration to person.cl file. Build!

25 A Minimal Database –Add menu/menu-item and connect TaskMenu.mnu Database/Add Person TaskWindow.win onDatabaseAddPerson Try!

26 A Minimal Database –Add ClickResponder to button addPerson.dlg OK-button Event

27 A Minimal Database –Build / Try! Change addPerson(person.pro) to prevent exception. + + + +

28 A Minimal Database Add Dialog to delete a person record –Add deletePerson Dialog to forms package.

29 A Minimal Database –Edit deletePerson.dlg dialog Add List Box and Edit Control Build All! nameListbox_ctl idListbox_ctl name_ctl id_ctl ReadOnly:True

30 A Minimal Database –Add menu/menu-item and connect TaskMenu.mnu Database/Delete Person TaskWindow.win onDatabaseDeletePerson

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

32 A Minimal Database –Add declaration to person.cl file. Build!

33 A Minimal Database –Change deletePerson.pro file Rebuild! new(Parent) :- dialog::new(Parent), generatedInitialize(), person::getAllNames(NameList), SingletonList = removeDuplicates(NameList), nameListbox_ctl:addList(SingletonList).

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

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

36 A Minimal Database OK-button ClickResponder Event onOkClick

37 A Minimal Database Build / Try! –Change deletePerson.dlg Cancel-button ClickResponder onCancelClick

38 A Minimal Database Add Dialog to change a person record –Add changePerson.dlg under forms package.

39 A Minimal Database –Edit changePerson.dlg Add List Box and Edit Control Build All! nameListbox_ctl idListbox_ctl name_ctl weight_ctl length_ctl iq_ctl id_ctl ReadOnly:True

40 A Minimal Database –Add menu/menu-item and connect TaskMenu.mnu Database/Change Person TaskWindow.win onDatabaseChangePerson

41 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(_, _, _, _, _).

42 A Minimal Database –Add declaration to person.cl file. Build!

43 A Minimal Database –Change changePerson.pro file Rebuild! new(Parent) :- dialog::new(Parent), generatedInitialize(), person::getAllNames(NameList), SingletonList = removeDuplicates(NameList), nameListbox_ctl:addList(SingletonList).

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

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

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

47 A Minimal Database Rebuild All / Try!

48 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.

49 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.

50 A Minimal Database –Add declaration to person.cl file. Build!

51 A Minimal Database –Add menu-items and connect TaskMenu.mnu Window.win

52 A Minimal Database Build and Execute!


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

Similar presentations


Ads by Google