How to get a data from text file

Discussions related to Visual Prolog
danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

How to get a data from text file

Unread post by danepa » 30 Sep 2011 7:12

If i have a database in personDB.txt

the content of personDB.txt :

Code: Select all

clauses user(1,"Danepa","Indonesia",1,10,1988,"xxx"). user(2,"Alex","Europe",5,2,1990,"xxx"). user(3,"Michael","China",1,4,1996,"xxx"). user(4,"Peter","Germany",15,11,1980,"xxx").
it means :

Code: Select all

user(ID,Name,State,Birthdate,Monthdate,Yeardate,password).

how can i get the data in ID = 3? and how can i get the data from maximum ID on this database?
because i can't use maximum() function.




And i have an error, if i close the program, and then open the program and create an ID again, the database will reset from zero data. how can i assert it without delete the last data?

this is my code :

Code: Select all

class facts - userDB    user : (string Nama, string Alamat, integer TglLahir, integer Blnlahir, integer ThnLahir, string Password).   buatID(Nama, Alamat, TglLahir, BlnLahir, ThnLahir, Password) :-         assert(user(Nama, Alamat, TglLahir, BlnLahir, ThnLahir, Password)),         saveDatabase().       saveDatabase() :-         file::existFile("personDB.txt"), !,         file::delete("personDB.txt"),         file::save("personDB.txt", userDB).          saveDatabase() :-         file::save("personDB.txt", userDB).
Daniel

dominique f pannier
VIP Member
Posts: 98
Joined: 23 Sep 2002 23:01

Unread post by dominique f pannier » 30 Sep 2011 9:18

Hi,
how can i get the data in ID = 3
predicates

clauses
getUserData(
user(3,"Michael","China",1,4,1996,"xxx").
Regards
Dominique Pannier

dominique f pannier
VIP Member
Posts: 98
Joined: 23 Sep 2002 23:01

Unread post by dominique f pannier » 30 Sep 2011 9:38

Hi,
sorry, keybord error.
how can i get the data in ID = 3?

Code: Select all

predicates      p : ().      getUserData : (unsigned, ID, string S1, string S2, unsigned Day, unsigned Month, unsigned Year, string Value). clauses        p():-           ...          getUserData(3, S1, S2, Day, Month, Year, Value),           ...              getUserData(ID, S1, S2, Day, Month, Year, Value):-           user(ID, S1, S2, Day, Month, Year, Value),           !.
how can i get the data from maximum ID on this database?

Code: Select all

predicates      getIDMax : () -> unsigned IDMAX. clauses      getIDMax() = IDMAX :-            LID = [ID||user(ID, _, _, _, _, _, _)],            [Head|_] = list::sort(LID, descending()),            IDMAX = Head,            !.
how can i assert it without delete the last data?
When you load your program, the first thing to do is to load the file you have previously saved :

Code: Select all

class facts     exceptionDumper : predicate{exception::traceId} := exceptionDump::dumpToStdOutput. predicates      loaddUsersFile : (). clauses      loaddUsersFile():-         try             FileName = concat(directory::getCurrentDirectory(), "\\personDB.txt"),             consult(FileName, userDB)         catch TraceId do             exceptionDumper(TraceId),             stdIO::writef("\n\n")         end try.
Then, when you assert a new fact, it is appended at the bottom of your database, because assert has the same effect than assertz . If you want to write your new fact at the top of the database, use the predicate asserta. And your last saveDatabase predicate is the good one : you need not to delete the file, it is overwritten.
Regards
Dominique Pannier

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 1 Oct 2011 0:49

dominique f pannier wrote:
how can i assert it without delete the last data?
When you load your program, the first thing to do is to load the file you have previously saved :

Code: Select all

class facts     exceptionDumper : predicate{exception::traceId} := exceptionDump::dumpToStdOutput. predicates      loaddUsersFile : (). clauses      loaddUsersFile():-         try             FileName = concat(directory::getCurrentDirectory(), "\\personDB.txt"),             consult(FileName, userDB)         catch TraceId do             exceptionDumper(TraceId),             stdIO::writef("\n\n")         end try.
Then, when you assert a new fact, it is appended at the bottom of your database, because assert has the same effect than assertz . If you want to write your new fact at the top of the database, use the predicate asserta. And your last saveDatabase predicate is the good one : you need not to delete the file, it is overwritten.
When i insert the code, i have an error again :
error c229: Undeclared identifier 'concat/2->'
error c229: Undeclared identifier 'consult/2'
Daniel

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 1 Oct 2011 1:07

dominique f pannier wrote:Hi,
sorry, keybord error.
how can i get the data in ID = 3?

Code: Select all

predicates      p : ().      getUserData : (unsigned, ID, string S1, string S2, unsigned Day, unsigned Month, unsigned Year, string Value). clauses        p():-           ...          getUserData(3, S1, S2, Day, Month, Year, Value),           ...              getUserData(ID, S1, S2, Day, Month, Year, Value):-           user(ID, S1, S2, Day, Month, Year, Value),           !.
how can i get the data from my database personDB.txt?

i want to check it for Login form, for Name and password.

My database : Name, Address, Day, Month, Year, Password
Daniel

dominique f pannier
VIP Member
Posts: 98
Joined: 23 Sep 2002 23:01

Unread post by dominique f pannier » 3 Oct 2011 8:00

Hi,
error c229: Undeclared identifier 'concat/2->' 'consult/2'
Theses predicates belong to other classes : class string for concat, class file for consult. Befor using it, you have to open the scope of theses class in the implementation :

Code: Select all

implement your_class open string, file
how can i get the data from my database personDB.txt?
...My database : Name, Address, Day, Month, Year, Password
If your database is stored in personDBtxt, then the facts of the database are :

Code: Select all

user(ID,Name,State,Birthdate,Monthdate,Yeardate,password).
If your question is to get data from personDB.txt, like I told you you have first to load the file. Then the facts are transfered in the memory and you can access them either directly, either with a predicate like

Code: Select all

getUserData(ID, S1, S2, Day, Month, Year, Value)
If your question is how someone can get data from personDB.txt and check them, it is another problem.
Regards
Dominique Pannier

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 4 Oct 2011 5:58

dominique f pannier wrote:Hi,
error c229: Undeclared identifier 'concat/2->' 'consult/2'
Theses predicates belong to other classes : class string for concat, class file for consult. Befor using it, you have to open the scope of theses class in the implementation :

Code: Select all

implement your_class open string, file
how can i get the data from my database personDB.txt?
...My database : Name, Address, Day, Month, Year, Password
If your database is stored in personDBtxt, then the facts of the database are :

Code: Select all

user(ID,Name,State,Birthdate,Monthdate,Yeardate,password).
If your question is to get data from personDB.txt, like I told you you have first to load the file. Then the facts are transfered in the memory and you can access them either directly, either with a predicate like

Code: Select all

getUserData(ID, S1, S2, Day, Month, Year, Value)
If your question is how someone can get data from personDB.txt and check them, it is another problem.
For easier, can you tell me how to use login form with database (ID,Name,State,Birthdate,Monthdate,Yeardate,password) in personDB.txt? what's the code?
(The input from user, just Name and password.)

Thanks.
Daniel

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 6 Oct 2011 5:26

I already upload my file, can u help me to solve my problem?

http://www.mediafire.com/?fda4qpbx5m899e5
(I'm sorry, i cannot attach here, i don't know why)

My problem :
1. I want to use Input1, 2, 3, etc to calculate P1, P2, P3, etc...
2. How to assert when I make an ID and it can not be removed, it will be assert in bottom of database list.
3. How to use login form, so it must check name and password to enter the other form.

thanks.

My yahoo messenger :
dan_epa@yahoo.com

If you can help me, please add my YM, so we can communicate further. Thanks.
Daniel

dominique f pannier
VIP Member
Posts: 98
Joined: 23 Sep 2002 23:01

Unread post by dominique f pannier » 6 Oct 2011 9:08

Hi,
I don't have any time to guide you to build your whole solution, I can only help you with VIP language.
But if you right unserstand how VIP is working, I think there is no problem you cannot solve.

First :
How to assert when I make an ID and it can not be removed, it will be assert in bottom of database list.
I think you have not really right understood how an internal database is working.

1.- you declare your database in a facts section (if you want to use it in objects) or in class facts section (if you need not objects) :

Code: Select all

facts - userDadabase  user : (unsigned ID,string Name, string State, unsigned Birthdate, unsigned Monthdate, unsigned Yeardate,string Password) nondeterm.    
2.- As long as you did not add a fact, your database is empty.

Code: Select all

predicates      addUser : (unsigned ID,string Name, string State, unsigned Birthdate, unsigned Monthdate, unsigned Yeardate,string Password). clauses      add(ID, Name, State, B, M, Y, Pass):-           assert(user(ID, Name, State, B, M, Y, Pass)).
3.- When you add a fact, it is added at the bottom or at the top of the database : nothing is destroyed if you don't retract it with the retract predicate (for instance), your base is in the memory. Before leaving your program, you save your database

Code: Select all

 predicates       saveUser : (). clauses      saveUser():-           file::save("personDB.txt", userDB).
4.- When you launch your program again, you load your database :

Code: Select all

predicates      loadUser : (). clauses      loadUser():-           try               FileName = concat(directory::getCurrentDirectory(), "\\personDB.txt"),               consult(FileName, userDB)           catch TraceId do               exceptionDumper(TraceId),               stdIO::writef("\n\n")           end try.
5.- At this moment, the facts are stored in memory. You can again add at the top or the bottom of the database. Of course, you can save and load as often as you want.

Second :
How to use login form, so it must check name and password to enter the other form
If you want to use a login form, i suggest you to study the example program formWindowDemo, which can show you how to use an editControl object in order to get a text entered by a user and a button to collect it. Then, if you want to check the user and his password, you call an ad hoc predicate you built in the package into which you handle your database. It will be an object predicate (declared in the interface file - .i) if you want to use objects, a class predicate declared in the class file - .cl) if you do not want, anyway written in the implementation file(. pro).
Regards
Dominique Pannier

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 7 Oct 2011 7:59

dominique f pannier wrote:Hi,
I don't have any time to guide you to build your whole solution, I can only help you with VIP language.
But if you right unserstand how VIP is working, I think there is no problem you cannot solve.

First :
How to assert when I make an ID and it can not be removed, it will be assert in bottom of database list.
I think you have not really right understood how an internal database is working.

1.- you declare your database in a facts section (if you want to use it in objects) or in class facts section (if you need not objects) :

Code: Select all

facts - userDadabase  user : (unsigned ID,string Name, string State, unsigned Birthdate, unsigned Monthdate, unsigned Yeardate,string Password) nondeterm.    
2.- As long as you did not add a fact, your database is empty.

Code: Select all

predicates      addUser : (unsigned ID,string Name, string State, unsigned Birthdate, unsigned Monthdate, unsigned Yeardate,string Password). clauses      add(ID, Name, State, B, M, Y, Pass):-           assert(user(ID, Name, State, B, M, Y, Pass)).
3.- When you add a fact, it is added at the bottom or at the top of the database : nothing is destroyed if you don't retract it with the retract predicate (for instance), your base is in the memory. Before leaving your program, you save your database

Code: Select all

 predicates       saveUser : (). clauses      saveUser():-           file::save("personDB.txt", userDB).
4.- When you launch your program again, you load your database :

Code: Select all

predicates      loadUser : (). clauses      loadUser():-           try               FileName = concat(directory::getCurrentDirectory(), "\\personDB.txt"),               consult(FileName, userDB)           catch TraceId do               exceptionDumper(TraceId),               stdIO::writef("\n\n")           end try.
5.- At this moment, the facts are stored in memory. You can again add at the top or the bottom of the database. Of course, you can save and load as often as you want.

Second :
How to use login form, so it must check name and password to enter the other form
If you want to use a login form, i suggest you to study the example program formWindowDemo, which can show you how to use an editControl object in order to get a text entered by a user and a button to collect it. Then, if you want to check the user and his password, you call an ad hoc predicate you built in the package into which you handle your database. It will be an object predicate (declared in the interface file - .i) if you want to use objects, a class predicate declared in the class file - .cl) if you do not want, anyway written in the implementation file(. pro).
okay, i can assert it in bottom of file. The next answer, how can i compare user input (name and password) with my database personDB.txt?

Code: Select all

class predicates    isMember : (string, string*) determ.   class facts - userDB    user : (string Nama, string Alamat, integer TglLahir, integer Blnlahir, integer ThnLahir, string Password). % from my database personDB.txt   clauses     onOkClick(_Source) = button::defaultAction :-          Nama = nama_ctl:gettext(),          Password = pass_ctl:gettext(),          login(Nama,Password).   login(Nama,Password) :-     DBNama=[Nama||user(Nama,_,_,_,_,_)],           if list::isMember(Nama,DBNama) then          S=string::format("Name '%' found",Nama),          vpicommondialogs::note(S)       else          S=string::format("Name '%' not found",Nama),          vpicommondialogs::note(S)       end if.       isMember(Nama, [_|Tail]) :-   isMember(Nama,Tail),   isMember(Nama, [Nama]).
I can find the name in the list from my database, but, how can i compare the password so that the name and password is correct.
Daniel

dominique f pannier
VIP Member
Posts: 98
Joined: 23 Sep 2002 23:01

Unread post by dominique f pannier » 7 Oct 2011 9:06

Hi,
Your algorithm is right, but :

1.- you need not to declare and write the isMember predicate : you don't use it, because you use the built-in predicate ine the file class : list::isMember.

2.- the error origin is in that part of your code:

Code: Select all

login(Nama,Password) :-     DBNama=[Nama||user(Nama,_,_,_,_,_)],           if list::isMember(DBNama,Nama) then
a) if you use a comprehension list, you should not use the variable Nama inside, which will be bound with Nama in the head of your predicate : in that case, only one fact will be collected...

b) you invert the variables in the isMember predicate, which is not in keeping with the declaration of this predicate. Then your code begins :

Code: Select all

login(Nama,Password) :-     DBNama=[X||user(X,_,_,_,_,_)],           if list::isMember(Nama,DBNama) then
But It could be more simple yet :

Code: Select all

login(Nama,Password) :-      user(Nama, _, _, _, _, Password), !,      ...
In that case, you use the backtracking system, which binds automatically your variables if the fact exists in the database.
Regards
Dominique Pannier

dominique f pannier
VIP Member
Posts: 98
Joined: 23 Sep 2002 23:01

Unread post by dominique f pannier » 7 Oct 2011 9:17

Sorry,
when reading again :
you invert the variables in the isMember predicate
is false : I made a bad reading of your code. But the rest of the post is OK.
Regards
Dominique Pannier

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 7 Oct 2011 9:30

dominique f pannier wrote: But It could be more simple yet :

Code: Select all

login(Nama,Password) :-      user(Nama, _, _, _, _, Password), !,      ...
In that case, you use the backtracking system, which binds automatically your variables if the fact exists in the database.
i've try it, but, nothing happen, it will always not found.
Daniel

User avatar
Tonton Luc
VIP Member
Posts: 501
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc » 7 Oct 2011 17:55

Hi,
Try this :

Code: Select all

login(Nama,Password) :-     user(Nama, _, _, _, _, Password),     S=string::format("Welcome '%' ",Nama),     vpicommondialogs::note(S),     !. login(Nama,_) :-     S=string::format("sorry but you're not welcome '%'\n...because your name is not in personDB.txt.",Nama),     vpicommondialogs::error(S).
...but before to use this login clause, in BuatID.pro, you need to consult your database using somethink like :

Code: Select all

predicates     onShow : window::showListener. clauses     onShow(_Source, _Data):-         YourDataBase = "personDB.txt",         FileName = fileName::setPath(YourDataBase,directory::getCurrentDirectory()),         if file::existFile(YourDataBase) then             try                 consult(FileName, userDB)             catch _ do                 vpiCommonDialogs::error("This program cann't started :",string::format("Please verify the structure of this following file :\n\n%",FileName)),                 applicationWindow::get():destroy()             end try         else             vpiCommonDialogs::error("This program cann't started :",string::format("This following file doesn't exist :\n\n%",FileName)),             applicationWindow::get():destroy()         end if         .

danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

Re:

Unread post by danepa » 17 Oct 2011 3:23

Tonton Luc wrote:Hi,
Try this :

Code: Select all

login(Nama,Password) :-     user(Nama, _, _, _, _, Password),     S=string::format("Welcome '%' ",Nama),     vpicommondialogs::note(S),     !. login(Nama,_) :-     S=string::format("sorry but you're not welcome '%'\n...because your name is not in personDB.txt.",Nama),     vpicommondialogs::error(S). [/quote]   i've been try this, but there is an error : error c631: The predicate 'buatID::login/2 (i,i)', which is declared as 'procedure', is actually 'determ'   and if i change the predicate to be determ predicate, it will   error c631: The predicate 'login::onOkClick/1-> (i)', which is declared as 'procedure', is actually 'determ'
Daniel

Post Reply