oaawebl.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 219 行

JAVA
219
字号
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/

package com.sri.oaa2.agt.webloaa2;

import java.io.*;
import java.util.*;
import webl.util.*;
import webl.lang.*;
import webl.lang.expr.*;
import webl.util.*;

public class OaaWebL
{
	static boolean pause = false, counters = false;
	static Machine machine = null;
	static boolean globalDone = false;
    
	static public void Usage(String msg) {
		System.out.println(msg);
		System.out.println();
		System.out.println("WebL 3.0h (alpha)");
		System.out.println("Usage: webl {options} scriptname {args}");
		System.out.println("Options:");
		System.out.println("  -D         Print debugging output to console");
		System.out.println("  -Llogfile  Log debugging output to file");
		System.out.println("  -P         Pause at exit waiting for a keystroke");
		System.out.println("  -C         Print performance counters at finish");
		System.out.println();
		System.exit(1);
	}

	static public Machine getMachine() {
		return machine;
	}

	static private void Pause() {
		if (pause) {        
			OaaWebL.printMessage("Ready...");
				try {
					int wasTyped = System.in.read();
					// This test is performed to determine if WebLOaa is
					// beign run through StartIt. 
					// When running WebLOaa through StartIt, 
					// "System.in.read" immediately returns with
					// the 10 value, and therefore, the program ends.
					// To prevent this behavior from happening, 
					// detect if StartIt is used (wasTyped=10) and start
					// waiting untils the program gets killed by StartIt.
					
					//if (wasTyped == 10) {
					if (true) {
						WebLWaiter w = new WebLWaiter();
						w.pause();
					}
				} catch (IOException e) {
					printError("IOException " + e);
				}
			}    
    }
    
    static public void main(String args[]) {                       
			Counter.Init();
			if (args.length < 1) 
				Usage("No script specified");
        
			int arg0 = 0;
			boolean debug = false;
			String  debugfile = null;
			// check for options
			String arg = args[arg0];
			while (arg.charAt(0) == '-') {
				if (arg.length() < 2) Usage("Illegal option " + arg);
				switch (arg.charAt(1)) {
					case 'D':
						debug = true;
						break;
					case 'L':
						debugfile = args[arg0].substring(2);
						break;
					case 'P':
						pause = true;
						break;
					case 'C':
						counters = true;
						break;
					default:
						Usage("Illegal option " + arg);
				}
				arg = args[++arg0];
      }
        
        // set up the default output logs
        try {
					Logger logger = new ConsoleLog(debug);               
					if (debugfile != null)
						logger.ChainTo(new FileLog(debugfile, false, true));
                
					Log.SetLogger(logger);
        } catch (IOException e) {
					printError("An IOException ocurred trying to create file " + debugfile + "," + e);
					System.exit(1);
        }
        
            // Load properties
        try {
					LoadProperties("webl.properties");
        } catch (IOException e) {
					Log.println("An IOException ocurred while reading webl.properties, " + e);
					System.exit(1);
        }                 
        
        // allocate a fresh execution machine
				try {
					machine = new Machine("Startup.webl");
        } catch (FileNotFoundException e) {
					Log.println("Panic: Unable to locate file, " + e);
					System.exit(1);
        } catch (IOException e) {
					Log.println("Panic: Error while running Startup.webl, " + e);
					System.exit(1);
        } catch (WebLException e) {
					Log.println("Panic: exception while running Startup.webl, " + e.report());
					System.exit(1);
        }
        
        machine.SetARGS(args, arg0);
        
				try {
					// run the actual script
					InputStream in = null;


					String myDir = args[arg0].substring(0, args[arg0].lastIndexOf(System.getProperty("file.separator")));
					// Add to the WebL file finder the path where our scripts are.
					// DEBUG
					//System.out.println("Adding " + myDir);
					FileLocator.AddSearchDirs(myDir);
 
					try {
						in = FileLocator.Find(args[arg0]);
						if (in != null) {
							AutoStreamReader s = new AutoStreamReader(in, "", "");
							BufferedReader di = new BufferedReader(s);
							machine.Exec(args[arg0], di);
						} else {
							Log.println("File not found: " + args[arg0]);
							Pause();
							System.exit(1);
						}
					} finally {
						if (in != null)
							in.close();
					}
        } catch (IOException e) {
					Log.println("IOException " + e);
					e.printStackTrace();
					Pause();
					System.exit(1);
        } catch (WebLException e) {
					Log.println(e.report());
					Pause();
					System.exit(1);
        } catch (WebLReturnException e) {
					Log.println("A return statement was executed outside of a function or method, " + e);
					Pause();
					System.exit(1);
				}
        
		if (counters) 
			Counter.Report();
		Pause();
		System.exit(0);
	}       
        
	static public void LoadProperties(String name) throws IOException {
		Properties props = new Properties(System.getProperties());
		InputStream in = FileLocator.Find(name);
		if (in != null) {
			props.load(new BufferedInputStream(in));
			System.setProperties(props);
		}
	}
   
  static public void printMessage(String inMessage) {
    System.out.println("WebL OAA2 : " + inMessage);
  }
  
  static public void printError(String inMessage) {
    System.out.println("WebL OAA2 ERROR : " + inMessage);
  }
}


// See  static private void Pause() for
// explanation

class WebLWaiter {
  public synchronized void pause(){
    while(!OaaWebL.globalDone){
      try{  
        wait(100);
      }catch(InterruptedException e) {
      }
    };
  }
}

⌨️ 快捷键说明

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