ÎãÂê04b ¥Ù¡¼¥¹¥×¥í¥»¥¹´Ö¤Î¥¹¥È¥ê¡¼¥à¼õ¤±ÅϤ·

(1)³µÍ×

¥¨¡¼¥¸¥§¥ó¥È¤¬»ý¤Ä¥Ù¡¼¥¹¥×¥í¥»¥¹´Ö¤Ç¥¹¥È¥ê¡¼¥à¤ò¼õ¤±ÅϤ¹ÊýË¡¤ò¼¨¤¹¡£
(Ãí°Õ¡§¥Í¡¼¥à¥µ¡¼¥ÐNameServer.jar¤Îµ¯Æ°¤¬É¬Íס£)

(2)¥¨¡¼¥¸¥§¥ó¥È

¥¨¡¼¥¸¥§¥ó¥È̾¥Õ¥¡¥¤¥ë̾ÀâÌÀ
TimeSenderTimeSender.dash¥¹¥È¥ê¡¼¥à¤òÁ÷¿®¤¹¤ë¥¨¡¼¥¸¥§¥ó¥È
TimeSender.class
¥Ù¡¼¥¹¥×¥í¥»¥¹
TimeReceiverTimeReceiver.dash¥¹¥È¥ê¡¼¥à¤ò¼õ¿®¤¹¤ë¥¨¡¼¥¸¥§¥ó¥È
TimeReceiver.class
¥Ù¡¼¥¹¥×¥í¥»¥¹

(2.1)TimeSender

¡üÃμ±µ­½Ò¥Õ¥¡¥¤¥ë (TimeSender.dash)
(agent TimeSender
 
 (property
  (create :author "yu" :modifiedBy "taka")
 )
 
 (initial_facts
 )
 
 (knowledge
    (rule startup
      (Msg :performative __INIT_I)
      (Status :name ?name)
      -->
      (inspect nonstop)
      (loadBP baseProcess.dashSample.TimeSender)  // BPÆɤ߹þ¤ß
      (startBP)                          // run()¸Æ¤Ó½Ð¤·
      (control startup ())               // startup()¸Æ¤Ó½Ð¤·
      (control setWindowTitle(?name))    // setWindowTitle()¸Æ¤Ó½Ð¤·
      (bind ?t (current-time))
      //¥­¡¼¤È¤Ê¤ë¥Õ¥¡¥¯¥È¤òºîÀ®
      (make (key :name ?name :time ?t))
    )
    
    //¥Ñ¥¤¥×¤ÎºîÀ®¤È¥¹¥È¥ê¡¼¥àÁ÷¿®³«»Ï
    (rule sendStream
      (key :name ?name :time ?t) = ?arg
      -->
      (bind ?key (toString ?arg))
      //¥­¡¼¤òÍѤ¤¤Æ¥¹¥È¥ê¡¼¥àÁ÷¼õ¿®ÍѤΥѥ¤¥×¤òºîÀ®¤·¡¢
      //¥¹¥È¥ê¡¼¥àÁ÷¿®³«»Ï
      (control startOutputStream(?key))
    )
    
    //¥­¡¼Í×µá¤ò¼õ¤±¼è¤ê¡¢¥­¡¼¤òÁ÷¤ë
    (rule receive-keyRequest
      (Msg :performative keyRequest :from ?sender ) = ?msg
      (key :name ?name :time ?t) = ?arg
      -->
      (bind ?key (toString ?arg))
      (send :performative sendKey :to ?sender :content (?key))
      (remove ?msg)
    )
  )
)

¡ü¥Ù¡¼¥¹¥×¥í¥»¥¹ (TimeSender.java)
package baseProcess.dashSample;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import dash.*;
import java.io.*;

public class TimeSender extends JFrame implements DashBP {

	private DashAgent agent;

	JTextArea textarea;
	String key;
	BufferedWriter out;
	OutputStream os = null;
	//¥ë¡¼¥×Íѥե饰
	boolean flag = true;
	//Ʊ´üÍÑ¥ª¥Ö¥¸¥§¥¯¥È
	Object obj = new Object();

	/** ¥³¥ó¥¹¥È¥é¥¯¥¿¡£¥¢¥¯¥·¥ç¥ó(loadBP)¼Â¹Ô»þ¤Ë¸Æ¤Ð¤ì¤ë¡£ */
	public TimeSender() {
		super("TimeSender");
	}

	/** ¥¢¥¯¥·¥ç¥ó(loadBP)¼Â¹Ô»þ¤Ë¸Æ¤Ð¤ì¤ë¡£*/
	public void setAgent(DashAgent agent) {
		this.agent = agent;
	}

	/** ¥¢¥¯¥·¥ç¥ó(startBP)¼Â¹Ô»þ¤Ë¸Æ¤Ð¤ì¤ë */
	public void run() {
		//½ÐÎÏ¥¹¥È¥ê¡¼¥à¼èÆÀ¤Þ¤ÇÂÔ¤Ä
		synchronized (obj) {
			try {
				obj.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//½ÐÎÏ¥¹¥È¥ê¡¼¥à¼èÆÀ¸å¤Î½èÍý
		out = new BufferedWriter(new OutputStreamWriter(os));
		while (flag) {
			Date now = new Date();
			textarea.setText(now.toString());
			try {
				out.write(now.toString() + "\n");
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/** ¥¨¡¼¥¸¥§¥ó¥È¤Î½ªÎ»»þ¤Ë¸Æ¤Ð¤ì¤ë */
	public void finalizeBP() {
		flag = false;
		if (out != null) {
			//Pipe¤ÎÀÚÃǽèÍý¤ò¼Â¹Ô
			agent.pm.close(key);
		}
		dispose();
	}

	/** ¥ë¡¼¥ëstartupÆâ¤Ç¸Æ¤Ó½Ð¤µ¤ì¤ë */
	public void startup() {
		setup();
		// ¤È¤­¤É¤­null¤Ê¤³¤È¤¬¤¢¤ë
		while (textarea == null)
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
			}
		textarea.append("µ¯Æ°\n");
	}

	/** ¥ë¡¼¥ëstartupÆâ¤Ç¸Æ¤Ó½Ð¤µ¤ì¤ë */
	public void setWindowTitle(Object[] str1) {
		setTitle("sWin:" + str1[0]);
	}

	//PipeºîÀ®¡¢¥¹¥È¥ê¡¼¥àÁ÷¿®³«»Ï
	public void startOutputStream(Object[] str) {
		key = (String) str[0];
		// ¥­¡¼¤òÍѤ¤¤ÆPipe¤ÎºîÀ®½èÍý¤ò¼Â¹Ô
		agent.pm.create(key);

		synchronized (obj) {
			// ¥­¡¼¤òÍѤ¤¤Æ½ÐÎÏ¥¹¥È¥ê¡¼¥à¤ò¼èÆÀ
			os = agent.pm.getOutputStream(key);
			obj.notify();
		}
	}

	/** ¥¦¥£¥ó¥É¥¦¤òºî¤ë */
	private void setup() {
		textarea = new JTextArea(6, 20);
		JScrollPane sc = new JScrollPane(textarea);
		sc.setBorder(new TitledBorder("Output"));
		getContentPane().add(sc, BorderLayout.CENTER);
		pack();
		setVisible(true);
	}
}

(2.2)TimeReceiver

¡üÃμ±µ­½Ò¥Õ¥¡¥¤¥ë (TimeReceiver.dash)
(agent TimeReceiver
 
 (property
  (create :author "yu" :modifiedBy "taka")
 )
 
 (initial_facts
 )
 
  (knowledge
    (rule startup
      (Msg :performative __INIT_I)
      (Status :name ?name)
      -->
      (inspect nonstop)
      (loadBP baseProcess.dashSample.TimeReceiver)  // BPÆɤ߹þ¤ß
      (startBP)                          // run()¸Æ¤Ó½Ð¤·
      (control startup ())
    )
 
    // ¥Ù¡¼¥¹¥×¥í¥»¥¹¤«¤é¤Î¥¤¥Ù¥ó¥È¤ò¼õ¤±¼è¤ê¡¢
    // TimeSender¤Ë¥­¡¼¤òÍ׵᤹¤ë
    (rule event-handling
      (Msg :performative __Event :content (?string)) = ?msg
      -->
      (bind ?agents (lookup :rname TimeSender))
      (bind ?agent (shift ?agents))
      (send :performative keyRequest :to ?agent:name :content ())
      (remove ?msg)
     )
    
    // TimeSender¤«¤é¡¢¥­¡¼¤ò¼õ¤±¼è¤ë
    (rule receive-sendKey
     (Msg :performative sendKey :from ?sender :content (?key)) = ?msg
     -->
     //¥­¡¼¤òÍѤ¤¤Æ¥¹¥È¥ê¡¼¥à¼õ¿®¤ò³«»Ï
     (control startInputStream(?key))
     (remove ?msg)
    )
  )
)

¡ü¥Ù¡¼¥¹¥×¥í¥»¥¹ (TimeReceiver.java)
package baseProcess.dashSample;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import dash.*;
import java.io.*;

public class TimeReceiver extends JFrame implements DashBP, ActionListener {

	private DashAgent agent;

	JTextArea textarea;
	JButton startbutton;
	JButton stopbutton;
	BufferedReader in;
	InputStream is = null;
	//¥ë¡¼¥×Íѥե饰
	boolean flag = true;
	//Ʊ´üÍÑ¥ª¥Ö¥¸¥§¥¯¥È
	Object obj = new Object();

	/** ¥³¥ó¥¹¥È¥é¥¯¥¿¡£¥¢¥¯¥·¥ç¥ó(loadBP)¼Â¹Ô»þ¤Ë¸Æ¤Ð¤ì¤ë¡£ */
	public TimeReceiver() {
		super("TimeReceiver");
	}

	/** ¥¢¥¯¥·¥ç¥ó(loadBP)¼Â¹Ô»þ¤Ë¸Æ¤Ð¤ì¤ë¡£*/
	public void setAgent(DashAgent agent) {
		this.agent = agent;
	}

	/** ¥¢¥¯¥·¥ç¥ó(startBP)¼Â¹Ô»þ¤Ë¸Æ¤Ð¤ì¤ë
	 * 
	 * */
	public void run() {
		//ÆþÎÏ¥¹¥È¥ê¡¼¥à¼èÆÀ¤Þ¤ÇÂÔ¤Ä	
		synchronized (obj) {
			try {
				obj.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//ÆþÎÏ¥¹¥È¥ê¡¼¥à¼èÆÀ¸å¤Î½èÍý
		in = new BufferedReader(new InputStreamReader(is));
		try {
			while (flag) {
				String ss = in.readLine();
				if (ss != null)
					textarea.append(ss + "\n");
			}
		} catch (IOException e) {
			finalizeBP();
			//System.out.println("Pipe¤ÎÀܳ¤¬ÀÚ¤ì¤Þ¤·¤¿");
		}
	}

	//¥¹¥È¥ê¡¼¥à¼õ¿®³«»Ï
	public void startInputStream(Object[] str) {
		synchronized (obj) {
			//ÆþÎÏ¥¹¥È¥ê¡¼¥à¤ò¼èÆÀ
			is = agent.pm.getInputStream((String) str[0]);
			obj.notify();
		}
	}

	/** ¥¨¡¼¥¸¥§¥ó¥È¤Î½ªÎ»»þ¤Ë¸Æ¤Ð¤ì¤ë */
	public void finalizeBP() {
		flag = false;
		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		dispose();
	}

	/** start¥Ü¥¿¥ó¤ò²¡¤·¡¢¥¨¡¼¥¸¥§¥ó¥È¤Ë¥¤¥Ù¥ó¥È¤ò¾å¤²¤ë */
	public void actionPerformed(ActionEvent ae) {
		//System.out.println(((JButton)ae.getSource()).getText());
		if (agent != null) {
			if (((JButton) ae.getSource()).getText().equals("Start")) {
				agent.raiseEvent("start");
			} else if (((JButton) ae.getSource()).getText().equals("Finish")) {
				dispose();
			}
		}
	}

	/** ¥ë¡¼¥ëstartupÆâ¤Ç¸Æ¤Ó½Ð¤µ¤ì¤ë */
	public void startup() {
		setup();
		// ¤È¤­¤É¤­null¤Ê¤³¤È¤¬¤¢¤ë
		while (textarea == null)
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
			}
		textarea.append("µ¯Æ°\n");
	}

	/** ¥¦¥£¥ó¥É¥¦¤òºî¤ë */
	private void setup() {
		textarea = new JTextArea(6, 20);
		JScrollPane sc = new JScrollPane(textarea);
		sc.setBorder(new TitledBorder("Input"));

		startbutton = new JButton("Start");
		startbutton.addActionListener(this);

		stopbutton = new JButton("Finish");
		stopbutton.addActionListener(this);

		JPanel panel = new JPanel();
		panel.add(startbutton);
		panel.add(stopbutton);

		GridLayout layout = new GridLayout(1, 2);
		panel.setLayout(layout);

		getContentPane().add(sc, BorderLayout.CENTER);
		getContentPane().add(panel, BorderLayout.SOUTH);

		pack();
		setVisible(true);
	}
}

(3)½èÍý¤Îή¤ì


­¡TimeSender¥¨¡¼¥¸¥§¥ó¥È¡¢TimeReceiver¥¨¡¼¥¸¥§¥ó¥È¤òµ¯Æ°¤¹¤ë

­¢TimeSender¥¨¡¼¥¸¥§¥ó¥È¤Ï¡¢¥­¡¼¤È¤Ê¤ë¥Õ¥¡¥¯¥È¤òÀ¸À®¤·¡¢¤½¤Î¥­¡¼¤òÍѤ¤¤Æ¥¹¥È¥ê¡¼¥àÁ÷¼õ¿®ÍѤΥѥ¤¥×¤òºîÀ®¤¹¤ë¡Ê agent.pm.create(key) ¡Ë

­£TimeSender¥¨¡¼¥¸¥§¥ó¥È¤Ï¡¢¥­¡¼¤òÍѤ¤¤Æ½ÐÎÏ¥¹¥È¥ê¡¼¥à¤ò¼èÆÀ¡Ê os = agent.pm.getOutputStream(key) ¡Ë¤·¡¢¥¹¥È¥ê¡¼¥à¤òή¤¹


­¤TimeReceiver¥¨¡¼¥¸¥§¥ó¥È¤Ï¡¢¥Ù¡¼¥¹¥×¥í¥»¥¹¤«¤é¼õ¿®³«»Ï¤Î¥¤¥Ù¥ó¥È¤ò¼õ¤±¼è¤ë¤È¡¢TimeSender¥¨¡¼¥¸¥§¥ó¥È¤ËÂФ·¥­¡¼Í×µá¥á¥Ã¥»¡¼¥¸¤òÅꤲ¡¢¥­¡¼¤ò¤â¤é¤¦

­¥TimeReceiver¥¨¡¼¥¸¥§¥ó¥È¤Ï¡¢¥­¡¼¤ò¤â¤È¤ËÆþÎÏ¥¹¥È¥ê¡¼¥à¤ò¼èÆÀ¡Ê is = agent.pm.getInputStream(key) ¡Ë¤·¡¢¥¹¥È¥ê¡¼¥à¤ò¼õ¿®¤¹¤ë