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

📄 agentwriter.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            out.println();
            out.println("         if ( resource != null ) {");
            out.println("            c = Class.forName(resource);");
            out.println("            ExternalDb oracle = (ExternalDb) c.newInstance();");
            out.println("            context.set(oracle);");
            out.println("            oracle.set(context);");
            out.println("         }");
            out.println("         if ( gui != null ) {");
            out.println("            c = Class.forName(gui);");
            out.println("            ZeusAgentUI ui = (ZeusAgentUI)c.newInstance();");
            out.println("            context.set(ui);");
            out.println("            ui.set(context);");
            out.println("         }");
            out.println();

            ProtocolInfo[] protocol = agent.getProtocols();
            out.println("/*\n         Initialising ProtocolDb\n*/");
            out.println("         ProtocolInfo info;");
            for(int j = 0; j < protocol.length; j++ ) {
               StrategyInfo[] strategy = protocol[j].getConstraints();
               for(int k = 0; k < strategy.length; k++ ) {
                  String[] array = strategy[k].getAgents();
                  Vector stringVector = new Vector();
                  for(int l = 0; l < array.length; l++ ) {
                     String a_name = genmodel.getAgentName(array[l]);
                     if ( a_name == null )
                        textArea.append("Warning: agent " + name +
                           " refers to an undefined agent\n");
                     else
                        stringVector.addElement(a_name);
                  }
                  array = Misc.stringArray(stringVector);
                  strategy[k].setAgents(array);
               }
               protocol[j].setConstraints(strategy);

               out.println("         info = ZeusParser.protocolInfo(db,\"" +
                     Misc.escape(protocol[j].toString()) + "\");");
               out.println("         if ( info.resolve(b) )");
               out.println("            agent.addProtocol(info);");
            }
            out.println();

            String[] tasks = agent.getTasks();
            if (tasks.length != 0) {
               out.println("/*\n         Initialising TaskDb\n*/");
               out.println("         AbstractTask t;");
               for(int j = 0; j < tasks.length; j++ ) {
                  String a_name = genmodel.getTaskName(tasks[j]);
                  if ( a_name == null )
                     textArea.append("Warning: agent " + name + " refers to an undefined task\n");
                  else {
                     AbstractTask t = genmodel.getTask(tasks[j]);
                     switch( t.getType() ) {
                        case AbstractTask.PRIMITIVE:
                             t = new PrimitiveTask((PrimitiveTask)t);
                             t.setName(a_name);
                             out.println("         t = ZeusParser.primitiveTask(db,\"" +
                                Misc.escape(t.toString()) + "\");");
                             break;
                        case AbstractTask.SUMMARY:
                             t = new SummaryTask((SummaryTask)t);
                             t.setName(a_name);
                             out.println("         t = ZeusParser.summaryTask(db,\"" +
                                Misc.escape(t.toString()) + "\");");
                             break;
                        case AbstractTask.BEHAVIOUR:
                             out.println("         stream = new FileInputStream(\"" +
                                a_name + ".clp" + "\");");
                             out.println("         t = ZeusParser.reteKB(db,stream);");
                             out.println("         stream.close();");
                             break;
                        case AbstractTask.SCRIPT:
                             // t = new Script((Script)t);
                             t.setName(a_name);
                             out.println("         t = ZeusParser.script(db,\"" +
                                Misc.escape(t.toString()) + "\");");
                             break;
                        default:
                             Core.ERROR(null,1,this);
                     }
                     out.println("         if ( t.resolve(b) )");
                     out.println("            agent.addTask(t);");

		     //Add restrictions
		     List restrictions = agent.getRestrictions();
		     for(Iterator r = restrictions.iterator() ; r.hasNext() ;){
		       Restriction item = (Restriction)r.next();
		       if(item.getTaskName().equals(a_name)) {
			 out.println("         t.addRestriction(\"" +
				     item.getFactName() + "\", \"" +
				     item.getAttributeName() + "\", \"" +
				     item.getRestriction() + "\");");
		       }
		     }
                  }
               }
            }

            Acquaintance[] others = agent.getAcquaintances();
            out.println("/*\n         Initialising OrganisationalDb\n*/");
            out.println("         AbilityDbItem item;");
            for(int j = 0; j < others.length; j++ ) {
               String a_name = genmodel.getAgentName(others[j].getName());
               if ( a_name == null )
                  textArea.append("Warning: agent " + name +
                     " refers to an undefined acquaintance\n");
               else {
                  out.println("         agent.addRelation(\"" + a_name +
                     "\",\"" + others[j].getRelation() + "\");");

                  AbilitySpec[] List = others[j].getAbilities();
                  AbilityDbItem dbItem;
                  for(int k = 0; k < List.length; k++ ) {
                     dbItem = new AbilityDbItem(a_name,List[k]);
                     out.println("         item = ZeusParser.abilityDbItem(db,\"" +
                        Misc.escape(dbItem.toString()) + "\");");
                     out.println("         if ( item.resolve(b) )");
                     out.println("            agent.addAbility(item);");
                  }
               }
               out.println();
            }

            Fact[] facts = agent.getInitialFacts();
            out.println("/*\n         Initialising ResourceDb\n*/");
            out.println("         Fact f1;");
            for(int j = 0; j < facts.length; j++ ) {
               if ( !facts[j].isDeterminate() )
                  textArea.append("Warning: " + facts[j].getId() +
                         " is not grounded");
                  out.println("         f1 = ZeusParser.fact(db,\"" +
                              Misc.escape(facts[j].toString()) + "\");");
                  out.println("         if ( f1.resolve(b) )");
                  out.println("            agent.addFact(f1);");
            }
            out.println();

            out.println();
            out.println("/*\n         Initialising External User Program\n*/");
            out.println();
            out.println("         if ( external != null ) {");
            out.println("            c = Class.forName(external);");
            out.println("            user_prog = (ZeusExternal) c.newInstance();");
            out.println("            context.set(user_prog);");
            out.println("         }");
            out.println();

            if (hasRules(agent)) {
               out.println("/*\n         Activating Rete Engine\n*/");
               out.println("         context.ReteEngine().run();");
            }

            out.println();
            out.println("/*\n         Activating External User Program\n*/");
            out.println();
            out.println("         if ( user_prog != null )");
            out.println("            user_prog.exec(context);");
            out.println();
            out.println("      }");
            // added by simon 21/08/00 
            // catch for ClassNotFoundeException to give a nice error message
            out.println(" catch (ClassNotFoundException cnfe) { "); 
            out.print("     System.out.println(\"Java cannot find some of the classes that "); 
            out.print("are needed to run this agent. Please ensure that you have the following"); 
            out.print ("in your classpath : zeus_install_dir\\\\lib\\\\zeus.jar, "); 
          out.print ("zeus_install_dir\\\\lib\\\\gnu-regexp.jar, "); 
            out.print ("java_install_dir\\\\jre\\\\rt.jar "); 
            out.print (" Where zeus_install_dir is the directory that you have installed "); 
            out.print (" Zeus in , and java_install_dir is the directory that you have ");
            out.println (" installed Java in\");"); 
            out.println("   cnfe.printStackTrace();}");
            out.println("      catch(Exception e) {");
            out.println("         e.printStackTrace();");
            // line removed by simon 21/08/00. 
            // if the agent is brought down with a exit(0) the whole jvm will close 
            // further work - deregister instead, and finalise objects.
            //out.println("         System.exit(0);");
            out.println("      }");
          
                        
            out.println("   }");
            out.println("}");
            out.flush();
            out.close();

            agentInfo[i].status = GenerationPlan.NO_SAVE_NEEDED;
            agentInfo[i].generate = false;
         }
         catch(IOException e) {
            textArea.append("Error generating agent " + name);
         }
      }
      textArea.append("Generating " +
         SystemProps.getProperty("application.gif") + " file\n");
      try {
         out = createFile(SystemProps.getProperty("application.gif"));

         String name, filename;
         char sys_char = File.separatorChar;
         char zeus_char = SystemProps.getProperty("file.separator").charAt(0);

         String[] identifiers = genmodel.getAgentIds();

         for(int i = 0; i < identifiers.length; i++ ) {
            name = genmodel.getAgentName(identifiers[i]);
            filename = genmodel.getAgentIcon(identifiers[i]);
            filename = Misc.relativePath(directory,filename);
            filename = filename.replace(sys_char,zeus_char);
            out.println(name + " = " + Misc.escape(filename));
         }
         out.flush();
         out.close();
      }
      catch(IOException e) {
         textArea.append("Error generating " +
            SystemProps.getProperty("application.gif") + " file\n");
      }
      textArea.append("\n*** Agent Code generation completed ***\n");
   }
}

⌨️ 快捷键说明

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