Package brute

Source Code of brute.BruteLinda

package brute;
import java.util.ArrayList;
import java.util.Iterator;

import linda.ILinda;
import linda.LindaArgs;



public class BruteLinda implements ILinda {
 
  ArrayList<LindaArgs> tuplespace = new ArrayList<LindaArgs>();

  @Override
  public void output(LindaArgs args) {
    synchronized (tuplespace) {
      tuplespace.add(args)
    }
  }

  @Override
  public void input(LindaArgs pattern) {
    while (true) {
      synchronized (tuplespace) {
        for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
          LindaArgs tuple = iterator.next();
          if (tuple.matchTuples(pattern)) {
            LindaArgs result = new LindaArgs(tuple);
            iterator.remove();
           
            pattern.applyResult(result);
            return;
          }
        }
      }
    }
  }

  @Override
  public void read(LindaArgs pattern) {
    while (true) {
      synchronized (tuplespace) {
        for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
          LindaArgs tuple = iterator.next();
          if (tuple.matchTuples(pattern)) {
            LindaArgs result = new LindaArgs(tuple);

            pattern.applyResult(result);
            return;
          }
        }
      }
    }
  }

  @Override
  public boolean tryInput(LindaArgs pattern) {
    synchronized (tuplespace) {
      for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
        LindaArgs tuple = iterator.next();
        if (tuple.matchTuples(pattern)) {
          LindaArgs result = new LindaArgs(tuple);
          iterator.remove();

          pattern.applyResult(result);
          return true;
        }
      }
    }

    return false;
  }

  @Override
  public boolean tryRead(LindaArgs pattern) {
    synchronized (tuplespace) {
      for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
        LindaArgs tuple = iterator.next();
        if (tuple.matchTuples(pattern)) {
          LindaArgs result = new LindaArgs(tuple);

          pattern.applyResult(result);
          return true;
        }
      }
    }
    return false;
  }

}
TOP

Related Classes of brute.BruteLinda

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.