
// ConsumerAgent.java
// Michele Loreti and the jSpace Developers, 2017
// Modified by Andrew Davison, ad@coe.psu.ac.th, May 2023

/* Read item tuples from a space. Each tuple has the
   form ("ITEM", <item name>, <item number>)

  Usage:
    > compile.bat ConsumerAgent.java

    Part of the application made up of:
       * ProducerConsumer.java
       * ProducerAgent.java
       * ConsumerAgent.java

  Other version: 
    * RemoteProducerConsumer.java
*/

import java.util.*;

import org.jspace.*;


public class ConsumerAgent implements Runnable
{
  private Space space;
  private int numItems;

  public ConsumerAgent(Space space, int n)
  {
    this.space = space;
    numItems = n;
  }


  public void run()
  {
    try {
      for (int i=0; i < numItems; i++ ) {
        Object[] data =
          space.get(new ActualField("ITEM"),
                    new FormalField(String.class),
                    new FormalField(Integer.class));
             // randomly select any item from space
        System.out.println("        CONSUMED: " + 
                           data[1] + " " + data[2]);
      }
    }
    catch (InterruptedException e) 
    {  System.out.println("         CONSUMER error: " + e); }
  } /* run */


}  // end of ConsumerAgent class
