📄 agentwriter.java
字号:
/*
* The contents of this file are subject to the BT "ZEUS" Open Source
* Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
* except in compliance with the Licence. You may obtain a copy of the Licence
* from $ZEUS_INSTALL/licence.html or alternatively from
* http://www.labs.bt.com/projects/agents/zeus/licence.htm
*
* Except as stated in Clause 7 of the Licence, software distributed under the
* Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the Licence for the specific language governing rights and
* limitations under the Licence.
*
* The Original Code is within the package zeus.*.
* The Initial Developer of the Original Code is British Telecommunications
* public limited company, whose registered office is at 81 Newgate Street,
* London, EC1A 7AJ, England. Portions created by British Telecommunications
* public limited company are Copyright 1996-9. All Rights Reserved.
*
* THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
*/
/**
Change log
-----------
Simon 22/08/00 added an exception catcher as part of work for fix #109719
Simon 24/08/00 added code that puts a -name option into the option list, and then
sets the agents name if it is used
*/
package zeus.generator.code;
import java.util.*;
import javax.swing.JTextArea;
import java.io.*;
import zeus.concepts.*;
import zeus.util.*;
import zeus.generator.GeneratorModel;
/**
AgentWriter is the class that is called by the generator to produce the Java code
which is compiled into the agents
*/
public class AgentWriter extends Writer {
public AgentWriter(GenerationPlan genplan, GeneratorModel genmodel,
String directory, JTextArea textArea) {
super(genplan,genmodel,directory,textArea);
}
private boolean hasTasks(AgentDescription agent)
{
String[] tasks = agent.getTasks();
if (tasks.length == 0) return false;
for(int j = 0; j < tasks.length; j++ )
{
AbstractTask t = genmodel.getTask(tasks[j]);
switch( t.getType() )
{
case AbstractTask.PRIMITIVE:
return true;
case AbstractTask.SUMMARY:
return true;
case AbstractTask.SCRIPT:
return true;
}
}
return false;
}
public boolean hasRules(AgentDescription agent)
{
String[] tasks = agent.getTasks();
if (tasks.length == 0) return false;
for(int j = 0; j < tasks.length; j++ )
{
AbstractTask t = genmodel.getTask(tasks[j]);
switch( t.getType() )
{
case AbstractTask.BEHAVIOUR:
return true;
case AbstractTask.SCRIPT:
return true;
}
}
return false;
}
public void write()
{
PrintWriter out;
AgentInfo[] agentInfo = genplan.getSelectedAgents();
if ( agentInfo.length == 0 ) return;
textArea.append("\n*** Agent code generation started ***\n\n");
for(int i = 0; i < agentInfo.length; i++ ) {
AgentDescription agent = genmodel.getAgent(agentInfo[i].id);
String name = genmodel.getAgentName(agentInfo[i].id);
textArea.append("Generating code for agent " + name + "\n");
try {
out = createFile(name + ".java");
out.println(standard_disclaimer);
out.println("/*");
out.println("This code was automatically generated by " +
"ZeusAgentGenerator version " +
SystemProps.getProperty("version.id"));
out.println(" DO NOT MODIFY!!");
out.println("*/\n");
out.println("import java.util.*;");
out.println("import java.io.*;");
out.println("import zeus.util.*;");
out.println("import zeus.concepts.*;");
out.println("import zeus.actors.*;");
out.println("import zeus.agents.*;");
out.println();
out.println("public class " + name + " {");
out.println(" protected static void version() {");
out.println(" System.err.println(\"ZeusAgent - " + name +
" version: " + SystemProps.getProperty("version.id") +
"\");");
out.println(" System.exit(0);");
out.println(" }");
out.println();
out.println(" protected static void usage() {");
out.println(" System.err.println(\"Usage: java " +
name + " -s <dns_file> -o <ontology_file> " +
"[-gui ViewerProg] [-e <ExternalProg>] " +
"[-r ResourceProg] [-name <AgentName>] [-debug] [-h] [-v]\");");
out.println(" System.exit(0);");
out.println(" }");
out.println();
out.println(" public static void main(String[] arg) {");
// added by Simon 21/08/00 for #109719 (1.04)
out.println(" try { ");
out.println(" ZeusAgent agent;");
out.println(" String external = null;");
out.println(" String dns_file = null;");
out.println(" String resource = null;");
out.println(" String gui = null;");
out.println(" String ontology_file = null;");
out.println(" Vector nameservers = null;");
out.println(" String name = new String (\"" + name + "\");");
out.println(" Bindings b = new Bindings(\"" + name + "\");");
out.println(" FileInputStream stream = null;");
out.println(" ZeusExternal user_prog = null;");
out.println();
out.println();
out.println(" for( int j = 0; j < arg.length; j++ ) {");
out.println(" if ( arg[j].equals(\"-s\") && " +
" ++j < arg.length )");
out.println(" dns_file = arg[j];");
out.println(" else if ( arg[j].equals(\"-e\") && " +
" ++j < arg.length )");
out.println(" external = arg[j];");
out.println(" else if ( arg[j].equals(\"-r\") && " +
" ++j < arg.length )");
out.println(" resource = arg[j];");
out.println(" else if ( arg[j].equals(\"-o\") && " +
" ++j < arg.length )");
out.println(" ontology_file = arg[j];");
out.println(" else if ( arg[j].equals(\"-gui\") && " +
" ++j < arg.length )");
out.println(" gui = arg[j];");
out.println(" else if ( arg[j].equals(\"-debug\") ) {");
out.println(" Core.debug = true;");
out.println(" Core.setDebuggerOutputFile(\"" + name + ".log" + "\");");
out.println(" }");
out.println(" else if ( arg[j].equals(\"-v\") )");
out.println(" version();");
// added by Simon 24/08/00, frankly @ way past my bedtime
out.println(" else if ( arg[j].equals(\"-name\") &&" +
" ++j < arg.length )");
out.println(" name = name + arg[j];"); // altered to remove '_'
//zzzzz
out.println(" else if ( arg[j].equals(\"-h\") )");
out.println(" usage();");
out.println(" else");
out.println(" usage();");
out.println(" }");
out.println();
// added by Simon 24/08/00
out.println(" b = new Bindings(name);");
out.println(" if ( ontology_file == null ) {");
out.println(" System.err.println(\"Ontology Database " +
"file must be specified with -o option\");");
out.println(" usage();");
out.println(" }");
out.println(" if ( dns_file == null ) {");
out.println(" System.err.println(\"Domain nameserver " +
"file must be specified with -s option\");");
out.println(" usage();");
out.println(" }");
out.println();
out.println(" nameservers = ZeusParser.addressList(" +
"new FileInputStream(dns_file));");
out.println(" if ( nameservers == null || " +
"nameservers.isEmpty() ) ");
out.println(" throw new IOException();");
out.println();
out.println(" agent = new ZeusAgent(name,ontology_file,nameservers," +
agent.getPlannerWidth() + "," + agent.getPlannerLength() + "," +
hasTasks(agent) + "," + hasRules(agent) + ");" );
out.println();
out.println(" AgentContext context = agent.getAgentContext();");
out.println(" OntologyDb db = context.OntologyDb();");
out.println();
out.println();
out.println("/*\n Initialising Extensions\n*/");
out.println(" Class c;");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -