view src/main/java/suikwasha/distributedalgorithm/machines/SimpleMachineBuilder.java @ 2:8e1f63faa2fd default tip

added Franklin's Algorithm
author suikwasha
date Tue, 23 Oct 2012 16:49:26 +0900
parents 38a110b13db1
children
line wrap: on
line source

package suikwasha.distributedalgorithm.machines;


import fj.data.List;
import suikwasha.distributedalgorithm.framework.Context;
import suikwasha.distributedalgorithm.framework.Algorithm;
import suikwasha.distributedalgorithm.framework.Machine;
import suikwasha.distributedalgorithm.framework.MachineBuilder;
import suikwasha.distributedalgorithm.framework.Port;

public class SimpleMachineBuilder implements MachineBuilder
{
	public Machine createMachine(Algorithm _algo)
	{
		return new MachineImpl(_algo);
	}
	
	private static class MachineImpl extends Machine
	{
		private List<Port> ports;
		private final Algorithm algo;
		
		public MachineImpl(Algorithm _algo)
		{
			ports = List.nil();
			algo = _algo;
		}
		
		public void addPort(Port _p)
		{
			ports = ports.snoc(_p);
		}
		
		public void run()
		{
			try{
				algo.execute(new ContextImpl(ports));
			}catch(Exception _e){
				_e.printStackTrace();
			}
		}
	}
	
	private static class ContextImpl implements Context
	{
		public List<Port> ports;
		
		public ContextImpl(List<Port> _ports)
		{
			ports = _ports;
		}
		
		public Iterable<Port> getPorts()
		{
			return ports;
		}
	}
}