jason-lang / jason

Jason is a fully-fledged interpreter for an extended version of AgentSpeak, a BDI agent-oriented logic programming language.
http://jason-lang.github.io
GNU Lesser General Public License v3.0
223 stars 67 forks source link

Is .map.put in for loop working correctly? #96

Closed KonstSkouras closed 1 year ago

KonstSkouras commented 2 years ago

Hi,

I'm trying to dynamically initialize a Map, in JASON 3.0 as follows:

!initializeMap(MO, 5).

+!initializeMap(MO, N)
   <-
   .map.create(MO);
   .map.put(MO,1,2);
   .print("starting with Map", MO); 
   .type(MO, T);
   .print("type ", T);
   for(.range(I,1,N)) {
      .map.put(MO,I,I);
      .print("After put Map is ", MO);
   };
   .print("Final Map is ", MO).

However, the result is the following

[c] starting with Map{1->2}
[c] type map
[c] After put Map is {1->1}
[c] After put Map is {1->2,2->2}
[c] After put Map is {1->2,3->3}
[c] After put Map is {1->2,4->4}
[c] After put Map is {1->2,5->5}
[c] Final Map is {1->2}

So, it seems like when using the put operation in the for loop, a new map is created instead of updating the existing one. Is this the intended behavior? And if so, is there any way to dynamically update an existing map?

jomifred commented 2 years ago

Hi Skouras,

thanks for reporting this issue. It is indeed a bug! I am still thinking on how to solve it. Unfortunately it is more difficult to solve than a thought.

Meanwhile, you can use a "non for" version of your code:

+!initializeMapNoFor(MO,N) <-
   .map.create(MO);
   .map.put(MO,1,2);
   !initializeMapNoFor(MO,1,N);
   .print(MO).
+!initializeMapNoFor(MO,V,N) : V > N.
+!initializeMapNoFor(MO,V,N) <-
   .map.put(MO,V,V);
   !initializeMapNoFor(MO,V+1,N).
jomifred commented 2 years ago

A tentative to fix the bug is available in the "develop" branch of Jason. I will not place the fix in the master branch for now since it is a bit "experimental". If you use gradle to run your system, the release 3.1-SNAPSHOT contains this fix.

KonstSkouras commented 2 years ago

Thank you for your replies!