How assert dynamic facts

Discussions related to Visual Prolog
User avatar
marMar
Posts: 24
Joined: 16 Feb 2012 16:08

How assert dynamic facts

Unread post by marMar » 20 Feb 2012 15:42

Hello friends,

I mar again. My user is blocked so I had to register a new. In this occasion i am in Spain for doing pre doctoral studies. I started again with my project in visual prolog.

In order to make my program accepts any number of variables I have used -following your guidance- the following structure.

Code: Select all

domains     binding = b(string NameVar, real ValueVar).     environment = binding*.   facts     generalData : (environment Environment).
An environment could look like this:

Code: Select all

[b("V1", 17.5), b("V2", 13.1), b("V3", 0), ....]).

I would like to allow the user insert the number of variables that he wants, but I do not know how to do this.

I have tried to do the following:

Code: Select all

         addLDCData(VarName, VarValue):-                                                             assertz(generalData ([b(VarName,Value)])),                                                              saveDatabaseLDC().
But in this way I get the following clauses:

Code: Select all

clauses generalData ([b("V0",0.0013)]).

And I need that the facts contains a list of structures such as:

Code: Select all

[b("V1", 17.5), b("V2", 13.1), b("V3", 0), ....]).
In other words, the user inserts 4 variables and I should be the following clauses:

Code: Select all

[b("V1", 17.5), b("V2", 13.1), b("V3", 0), b("V4", 0)]).
How I can do this?
you understand me?
Thank you very much.
:?:

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

Unread post by Tonton Luc » 20 Feb 2012 20:07

Hi,

One way :idea:

Code: Select all

addLDCData(VarName, VarValue):-     generalData(L),     retract(generalData(L)),     New_env_list = list::append(L,[b(VarName,Value)]),     assertz(generalData (New_env_list)),     saveDatabaseLDC(),     !. addLDCData(VarName, VarValue):-     assertz(generalData ([b(VarName,Value)|L])),     saveDatabaseLDC().

User avatar
marMar
Posts: 24
Joined: 16 Feb 2012 16:08

Unread post by marMar » 21 Feb 2012 9:00

Thank you very much for helping me, but
I'm not sure to understand your code.

When I try to use your code, the program gives me errors such as: L not complete bound. Also always fails when it gets to the line generalData (L), he does not know who is L.

I also do not understand you intend on lines

Code: Select all

  generalData (L),   retract (generalData (L)),
You could explain to me the code you have given me.

My intention is:

I have a window that allows the user to insert variables and their values. Look at the figure I've attached (you do not pay attention to fuzszyfication patterns.)

When the user clicks the ok option runs the following code:

Code: Select all

onOkClick (_Source) = button :: defaultAction: -          VarName = edit_Name: getText (),      VarValue = toterm (edit_Value: getText ()),      lDCClass :: addLDCData (VarName, varValue).
Where addLDCData is a procedure to insert a clause. For example:

Code: Select all

clauses generalData ([b ("V1", 17.5), b ("V2", 13.1), b ("V3", 0) ...... b ("VN", N)])
N represents the number of variables that you want to insert. While the user is inserting variable the program should be accumulating these values ​​in a clause. Can you give me any idea how to accomplish this?

I hope you understand my English, sorry I am Cuban :? . Please help me to get this part.
Attachments
Dibujo.JPG
Dibujo.JPG (38.43 KiB) Viewed 3722 times

User avatar
George
VIP Member
Posts: 313
Joined: 19 Sep 2011 8:54

Unread post by George » 21 Feb 2012 9:35

Code: Select all

addLDCData(VarName, VarValue):-     generalData(L), %no need this line     retract(generalData(L)),     New_env_list = list::append(L,[b(VarName,Value)]),     assertz(generalData (New_env_list)),     saveDatabaseLDC(),     !. addLDCData(VarName, VarValue):-     assertz(generalData ([b(VarName,Value)|L])), %no need "L" at this place     saveDatabaseLDC().

Code: Select all

addLDCData(VarName, VarValue):-     retract(generalData(L)),     New_env_list = list::append(L,[b(VarName,Value)]),     assertz(generalData(New_env_list)),     saveDatabaseLDC(),     !. addLDCData(VarName, VarValue):-     assertz(generalData ([b(VarName,Value)])), %no need to put variable "L" at this place because, this clause only invoke when the generalData doesn't contain anything.     saveDatabaseLDC().
Try this one.. This will works..


To get the data in list use below code,

Code: Select all

predicates getLDCData : () -> environment Environment.   clauses getLDCData() = Environment:-         generalData(LDCData),         Environment = LDCData,         !. getLDCData() = [].
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282

User avatar
marMar
Posts: 24
Joined: 16 Feb 2012 16:08

Unread post by marMar » 21 Feb 2012 10:03

:lol: :D
Thankkkkkkkk youuuuuuuuuu :P

You could guide me how I can do this same thing but in place of the user to insert variables and values, making the program can read data from a CSV data source? I use personal edition because I have no other possibilities.

Is necessary to have the commercial version to use data sources such as SQL, MySQL or CSV?

User avatar
marMar
Posts: 24
Joined: 16 Feb 2012 16:08

Re:

Unread post by marMar » 21 Feb 2012 10:46

On one occasion you commented to me that it was possible to read data from Excel. I will quote the comments you gave me:

"you can export a sheet in a CSV file (with a specific separator), an from Prolog application, you can easily convert the CSV file to a database.
You just have to read each line of the CSV file and transform it to a Prolog database (using something like string::split_delimiter). "

My question is this:

I can do this with the personal edition?
If the answer is Yes, there is an example that guide me?

Thank you very much for your help.

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

Unread post by Tonton Luc » 21 Feb 2012 12:13

Hi,

1) sorry for my wrong code in my previous post => it was a bad copy & paste. But Ananth rectify it well (Tks).

2) :idea: to read a CSV file : http://discuss.visual-prolog.com/viewto ... t=readline + make a search of "readline" on this forum.

User avatar
marMar
Posts: 24
Joined: 16 Feb 2012 16:08

Re:

Unread post by marMar » 21 Feb 2012 12:18

Tonton Luc wrote:Hi,

1) sorry for my wrong code in my previous post => it was a bad copy & paste. But Ananth rectify it well (Tks).

2) :idea: to read a CSV file : http://discuss.visual-prolog.com/viewto ... t=readline + make a search of "readline" on this forum.
CSV reading is not required commercial version? :roll:

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

Unread post by Tonton Luc » 21 Feb 2012 16:29

CSV is a simple txt file (you can open it with Notepad), so I think you can read it easyly from a VP7 Personnal Edition application.

User avatar
marMar
Posts: 24
Joined: 16 Feb 2012 16:08

Re:

Unread post by marMar » 21 Feb 2012 16:35

Tonton Luc wrote:CSV is a simple txt file, you can open it with Notepad, so I think you can read it easyly from a VP7 Personnal Edition.
There any example that can help me to read and write CSV?Sad
thanks

Steve Lympany
VIP Member
Posts: 119
Joined: 31 Mar 2001 23:01

Unread post by Steve Lympany » 21 Feb 2012 22:29

Hi,
I jut found some old code - it may be useful.

To read a spreadsheet, select the cells, copy to the Windows clipboard., then "paste" into your VIP app (vpi::cbputstring). Then decode it:

Code: Select all

domains         split_tab=split_tab;                         no_split_tab. predicates     make_list_from_string   :(split_tab Split_tab,string S,string_list SL)               procedure (i,i,o).     make_lists_from_string  :(string S,string_list_list SLL)               procedure (i,o).   clauses         make_list_from_string(SPlit_Tab,STR,SLIST):-             STR1=string::replaceall(STR,"\r","",string::caseinsensitive),                 if Split_tab=split_tab then                         STR2=string::replaceall(STR1,"\t","\n",string::caseinsensitive)                 else                         STR2=STR1                 end if,         SLIST=string::split_delimiter(STR2,"\n").   clauses         make_lists_from_string(STR,LIST_LIST):-                 LIST_OF_LINES=string::split_delimiter(STR,"\n"),                 split_lines(LIST_OF_LINES,LIST_LIST). class predicates         split_lines:(string_list,string_list*)procedure (i,o). clauses         split_lines([],[]):-!.         split_lines([LINE|LIST_OF_LINES],[LIST|LISTS]):-                 make_list_from_string(split_tab,LINE,LIST),                 split_lines(LIST_OF_LINES,LISTs).
s

Harrison Pratt
VIP Member
Posts: 295
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 22 Feb 2012 16:55

This may help while you're working on your project:

http://www.softpedia.com/get/System/Fil ... SVed.shtml

Post Reply