⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testagent.cs

📁 SRI international 发布的OAA框架软件
💻 CS
字号:
using System;
using com.sri.sedc.javanetbridge;
using jnb.com.sri.sedc.javanetbridge;
using jnb.com.sri.oaa2.lib;
using jnb.com.sri.oaa2.icl;
using jnb.com.sri.oaa2.com;
using jnb.java.lang;

namespace com.sri.oaa2.test
{
	/// <summary>
	/// TestAgent is used in conjunction with the OAA2 unit tests. It solves the same
	/// solvables as the Java TestAgent solves. See oaalib/test/README.txt for documentation
	/// on how to run the unit tests with the test agent.
	/// This class extends the OAAEventListener_CallbackImpl class. OAAEventListener_CallbackImpl
	/// implements a callback mechanism for OAAEventListener.
	/// </summary>
	class TestAgent : OAAEventListener_CallbackImpl
	{
		const String AGENT_NAME = "TestAgent";
		const String CONNECTION_ID = "parent";
		private Random random = new Random();
		private String onekstring, fivekstring, tenkstring, twentykstring, fiftykstring, sixtykstring;
		private JavaNetBridgeBean bean;
		private Class iclDataQClass;

		private LibOaa myOaa;

		public TestAgent(JavaNetBridgeBean bean) 
		{
			this.bean = bean;
			onekstring = makeString(1024);
			fivekstring = makeString(5*1024);
			tenkstring = makeString(10*1024);
			twentykstring = makeString(20*1024);
			fiftykstring = makeString(50*1024);
			sixtykstring = makeString(60*1024);
			iclDataQClass = new IclDataQ().getClass();
		}

		/// <summary>
		/// The main entry point for the TestAgent application.
		/// </summary>
		[MTAThread]
		static void Main(string[] args)
		{
			// These commands can be used to change the ip address and port of the proxy agent.
			// They must be called before any .NET APIs are acccessed below. By default it is
			// assumed the java proxy is running on this machine on port 4016.
			//JavaNetBridgeInteropClass.setConnectAddress(System.Net.IPAddress.Parse("128.18.233.111"));
			//JavaNetBridgeInteropClass.setConnectPort(4080);

			// AXBridgeBean is the main bean interface. This is how to retrieve it:
			JavaNetBridgeBean bean = Oaa2JavaInterop.getSingleton().getJavaNetBridgeBean();
			// Don't trace method calls. This slows down calls significantly. Tracing
			// method calls is useful for debugging, so this can be turned on for 
			// debugging.
			bean.setTraceMethods(false);
			
			// Setting the OAA_LISTEN system property enables direct connect.
			// Alternatively, the command-line parameters -oaa_listen "tcp('localhost',4040)"
			// could be specified when starting the agent. Ex:
			//     agent.init(new string[] { "-oaa_listen", "tcp('localhost',4040)" });
			JavaSystem.setProperty("OAA_LISTEN", "tcp('localhost',4040)");

			TestAgent agent = new TestAgent(bean);
			agent.init(args);

			// Loop forever. The add agent will run until the app is terminated.
			while (true) 
			{
				System.Threading.Thread.Sleep(10000);
				JavaSystem.gc();
				System.GC.Collect();
			}
		}

		private void debug(String msg)
		{
			Console.WriteLine(AGENT_NAME + ": " + msg);
		}
		
		public override bool doOAAEvent(IclTerm goal, IclList parameters, IclList answers)
		{
			if (goal.toIdentifyingString().Equals("setData")) 
			{
				int length = 0;
				IclTerm inTerm = goal.getTerm(0);
				if (iclDataQClass.isInstance(inTerm)) 
				{
					IclDataQ dataTerm = (IclDataQ)inTerm;
					byte[] data = dataTerm.getData();
					length = data.Length;
				}
				// Don't actually send the data back...
				answers.add(new IclStruct(goal.toIdentifyingString(), new IclVar("Data"), new IclInt(length)));
			} 
			else if (goal.toIdentifyingString().Equals("makeString")) 
			{
				int size = new IclInt(goal.getTerm(0)).toInt();
				String s = makeString(size);
				answers.add(new IclStruct(goal.toIdentifyingString(), new IclInt(size),
					new IclStr(s)));
				return true;
			} 
			else if (goal.toIdentifyingString().Equals("copyString") ||
				goal.toIdentifyingString().Equals("copyData")) 
			{
				IclTerm inTerm = goal.getTerm(0);
				IclTerm retTerm = null;
				if (iclDataQClass.isInstance(inTerm)) 
				{
					IclDataQ dataTerm = (IclDataQ)inTerm;
					byte[] data = dataTerm.getData();
					retTerm = new IclDataQ(data);
				} else {
					retTerm = IclTerm.newIclTerm(inTerm.clone());
				}
				IclTerm outClone = IclTerm.newIclTerm(inTerm.clone());
				answers.add(new IclStruct(goal.toIdentifyingString(), outClone,
					retTerm));
				return true;
			} 
			else if (goal.toIdentifyingString().Equals("doNothing")) 
			{
				answers.add(new IclStruct(goal.toIdentifyingString(), new IclVar("Nothing")));
				return true;
			} else if (goal.toIdentifyingString().Equals("add")) 
			{
				int Num1 = ((IclInt)goal.getTerm(0)).toInt();
				int Num2 = ((IclInt)goal.getTerm(1)).toInt();
				int Sum = Num1 + Num2;

				/* The answers returned must "match" the goal, but with
				variables filled in.  So since requests will arrive
				as  "add(1,2,X)", we should return "add(1,2,3)".
				OAA is currently not set up to handle functions,
					ie. "add(1,2)"  returns "3", because some languages
					(e.g. Prolog) do not work well in this mode.
				Note: OAA can handle if there are multiple answers
					to a question, which is why answers is a list.
				Example:  "sqrt(4, X)" should return two answers,
					"sqrt(4, 2)" and "sqrt(4, -2)"
				*/
				answers.add(
						new IclStruct("add", new IclInt(Num1), new IclInt(Num2),
								new IclInt(Sum)));

				return true;
			}

			return true;
		}

		public String getAgentCapabilities()
		{
			return "[add(Num1,Num2,Sum), doNothing(Nothing), copyData(A,B), " +
				"copyString(A,B), setData(Data,RetLength), makeString(Len,Ret)]";
		}

		public String getAgentName()
		{
			return AGENT_NAME;
		}

		public void init(string[] args)
		{
			LibComTcpProtocol protocol = new LibComTcpProtocol();
			LibCom com = new LibCom(protocol, args);
			myOaa = new LibOaa(com);

			if (!myOaa.oaaSetupCommunication(getAgentName()))
			{
				throw new System.Exception("Failed to setup OAA communication");
			}

			debug("Connection Made");

			IclTerm solveables = IclTerm.fromString(true, getAgentCapabilities());

			debug("Registering " + getAgentName());
			if (!myOaa.oaaRegister(CONNECTION_ID, getAgentName(), solveables, new IclList()))
			{
				throw new System.Exception("Failed to register");
			}

			myOaa.oaaRegisterCallback("app_do_event", this);

			myOaa.oaaReady(true);
		}

		String makeString(int size) 
		{
			if (size == 1024 && onekstring != null) 
			{
				return onekstring;
			} 
			else if (size == 5*1024 && fivekstring != null) 
			{
				return fivekstring;
			} 
			else if (size == 10*1024 && tenkstring != null) 
			{
				return tenkstring;
			} 
			else if (size == 20*1024 && twentykstring != null) 
			{
				return twentykstring;
			} 
			else if (size == 50*1024 && fiftykstring != null) 
			{
				return fiftykstring;
			} 
			else if (size == 60*1024 && sixtykstring != null) 
			{
				return sixtykstring;
			}

			debug("Creating string of size " + size);

			char[] chars = new char[size];

			for (int indx = 0; indx < size; indx++) 
			{
				chars[indx] = (char)('a' + Math.Round(random.NextDouble()*('z' - 'a')));
			}

			return new string(chars);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -