ModelDriven / Alf-Reference-Implementation

Open-source implementation of the Action Language for fUML (Alf) specification.
30 stars 2 forks source link

The first() operation of Queue returns null when the queue is not empty #101

Closed alperkocatas closed 1 year ago

alperkocatas commented 2 years ago

I am trying to use the Queue data structure in Alf, with the reference implementation. I can insert and remove elements from the queue, however, when I want to peek the queue using the "first" function, the value returned is null.

Here is a test code which demonstrates the problem:

private import Alf::Library::PrimitiveBehaviors::IntegerFunctions::ToString;
private import Alf::Library::PrimitiveBehaviors::IntegerFunctions::*;
private import Alf::Library::CollectionClasses::Queue;

activity QueueFunctionsTest() {
    Queue<Integer> buffer = new Queue<Integer>();
    buffer.add(1);
    buffer.add(2);
    buffer.add(3);

    Integer queueSize = buffer.size();
    if (queueSize != null)
    {
        WriteLine("Queue size is: " + ToString(queueSize));
    }

    Integer i = 0;
    while (buffer.size() != 0) {
        WriteLine("Peeking queue...");
        Integer val = buffer.first();
        if (val != null) {
            WriteLine("Value in front of the queue is: " + ToString(val));
        }
        else {
            // Problem: we don't expect to arrive here!
            WriteLine("Ooops: peeked value in front of the queue seems to be null");
        }

        WriteLine("Removing first element...");
        val = buffer.removeFirst();

        if (val != null) {
            WriteLine("Removed value in front of the queue: " + ToString(val));
        }
    }
}

Output from executing the code is:

Queue size is: 3
Peeking queue...
Ooops: peeked value in front of the queue seems to be null
Removing first element...
Removed value in front of the queue: 1
Peeking queue...
Ooops: peeked value in front of the queue seems to be null
Removing first element...
Removed value in front of the queue: 2
Peeking queue...
Ooops: peeked value in front of the queue seems to be null
Removing first element...
Removed value in front of the queue: 3

I wonder if this is the expected normal behavior, or is this a bug?

Thanks.

seidewitz commented 2 years ago

This is a bug.

The implementation for Queue::first can ultimately be found in the file org.modeldriven.alf/Libraries/alf/Alf/Library/CollectionClasses/Impl/QueueImpl.alf:

  protected firstImpl(): any[0..1] {
    this.content->First();
  }

The statement in the body of this operation should obviously begin with return. When this change is made, your sample code runs as expected.

seidewitz commented 2 years ago

This is now fixed in commit c3b5d77 on the maintenance branch, if you would like to try it out.

alperkocatas commented 2 years ago

Thanks a lot, I have applied the suggested fix to my version and it worked. I am closing the issue as it is fixed with the new commit.

seidewitz commented 2 years ago

I would prefer to leave the issue open until the fix is included in a production release.

seidewitz commented 1 year ago

Resolved in Release 1.1.0l.