📄 libcomutils.java
字号:
/*
* @(#)LibComUtils.java 08/1999
*
* 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.
*/
/*
* file: LibComUtils.java
* purpose: Utilities for connection related operations
*
* Copyright (C) 1998, SRI International. All rights reserved.
*/
package com.sri.oaa2.com;
import java.io.*;
import com.sri.oaa2.icl.*;
import antlr_oaa.TokenStreamException;
import antlr_oaa.RecognitionException;
import org.apache.log4j.Logger;
// For parsing command line arguments
/**
* Interface to LibComSocket
*/
public class LibComUtils {
private static long gensym_id = 0;
/**
* Generates a unique identifier.
*
* @param name the identifier prefix name
* @return a unique identifier
*/
public synchronized static String gensym(String prefix) {
gensym_id++;
return prefix + gensym_id;
}
/**
* Temporarly in here, should be moved into LibOaa.java.
* Applications only (Applets can not read files and don't have cmd line...)
* Needs a lot of work! */
static public IclTerm oaaResolveVariable(String varName, String[] cmdArgs) {
IclTerm currentTerm = null;
IclTerm testTerm = new IclStruct(varName, new IclVar("X"));
// Tries to unify the incoming variable first with the command line
if (cmdArgs != null) {
int i = 0;
String arg;
while(i < cmdArgs.length) {
arg = cmdArgs[i++];
try {
currentTerm = IclTermCache.getNoAnon(arg.substring(1));
}
catch(Exception re) {
currentTerm = new IclStr(arg.substring(1));
}
// DEBUG
// System.out.println("Resolve : Current Term " + currentTerm + " against " + varName);
// First, checks if term is an IclStruct
if ((currentTerm != null) && currentTerm.isStruct()) {
IclTerm ans = Unifier.getInstance().unify(testTerm, currentTerm);
// variable found!
if (ans != null)
return ans.getTerm(0);
}
// Then, checks if term is an atom (IclStr)
if ((currentTerm != null) && currentTerm.isStr()) {
IclTerm ans = Unifier.getInstance().unify(new IclStr(varName), currentTerm);
// variable found!
if (ans != null) {
// The option -oaa does not take any paramters
if (ans.toString().equals("oaa"))
return ans;
else {
return IclTerm.fromString(true, cmdArgs[i++]);
}
}
}
}
}
// If not found, looks for the setup.pl file
String fileName = "setup.pl";
// Add appropriate prefix to filename: Get the current directory
String localDirPath = System.getProperty("user.dir") + System.getProperty("file.separator");
String globalDirPath = "";
// For the rootPath, we need information about the system
// Under UNIX
// os.name = Solaris
// os.arch = sparc
// Under Windows
// os.name = Windows 95
// os.arch = x86
if (System.getProperty("os.name").startsWith("Windows"))
globalDirPath = "c:";
else
globalDirPath = System.getProperty("user.home");
globalDirPath = globalDirPath + System.getProperty("file.separator");
// Collects data from setup.pl
if (fileName != null) {
if (new java.io.File(localDirPath+fileName).exists())
fileName = localDirPath + fileName;
else {
if (new java.io.File(globalDirPath+fileName).exists())
fileName = globalDirPath+fileName;
}
try {
BufferedReader setup = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName)));
try{
String currentLine = setup.readLine();
while((currentLine!=null)) {
currentLine = currentLine.trim();
// DEBUG
//System.out.println("Current line : " + currentLine);
// Removes the dot at the end, due the Prolog syntax (Fact)
if (currentLine.endsWith("."))
currentLine = currentLine.substring(0,currentLine.length()-1);
// Process the current line
if (!currentLine.startsWith("%") && !currentLine.equals("")) {
// Build a term representing the current line
currentTerm = IclTermCache.get(currentLine);
// Adds a new term to the list of terms (if relevant)
if ((currentTerm != null) && currentTerm.isStruct()) {
IclTerm ans = Unifier.getInstance().unify(testTerm, currentTerm);
// variable found!
if (ans != null) {
return ans.getTerm(0);
}
}
}
currentLine = setup.readLine();
}
}catch(EOFException eof) {
ComPrintError("EOF Error " + eof.toString());
}
setup.close();
}catch(IOException eio) {
ComPrintError("IO Error " + eio.toString());
}
} // End filename != null
// Check system property
String listenProp = System.getProperty(varName.toUpperCase());
if (listenProp != null && !listenProp.equals("")) {
try {
IclTerm ret = IclTerm.fromString(listenProp);
return ret;
} catch (RecognitionException e) {
ComPrintError(e.toString());
} catch (TokenStreamException e) {
ComPrintError(e.toString());
}
}
// variable not found
return null;
}
static void ComPrintError (String s) {
System.out.println("LibCom Error --> " + s);
}
/**
* Resolves the input address if it contains variables.
*
* @param address address to resolve
* @param mCmdLine the command line areguments
* @param params parameters. This method checks if the
* <code>resolve_vars</code> parameter is "false" (default->"true"). If
* this parameter is "False", the address is not resolved.
* @param logger a logger to log errors and debug output
*/
public static IclTerm resolveAddress(IclTerm address, String[] mCmdLine,
IclTerm params, Logger logger) {
if (params != null) {
if (params.isList()) {
IclTerm resolve_vars = IclUtils.getParamValue("resolve_vars",
new IclStr("true"), (IclList)params);
// Only resolve the address if resolve_vars is not false
if (resolve_vars.toIdentifyingString().equals("false")) {
return address; // Do not resolve the address
}
} else {
logger.error("resolveAddress: params is not a list: " +
params.getClass().getName());
new Exception().printStackTrace();
}
}
IclTerm ret = address;
IclTerm addressStruct = null;
try {
addressStruct = IclTermCache.getNoAnon("tcp(Host,Port)");
} catch (Exception e) { // rethrow RuntimeException
throw new RuntimeException(e.toString());
}
IclTerm connectionInfo = Unifier.getInstance().unify(addressStruct, address);
IclTerm host = null;
IclTerm port = null;
if (connectionInfo != null) {
ret = connectionInfo;
host = ret.getTerm(0);
port = ret.getTerm(1);
// If variable address, look it up from command line
if (host.isVar() || port.isVar()) {
IclTerm newAddress = LibComUtils.oaaResolveVariable("oaa_connect", mCmdLine);
if (newAddress != null) {
ret = newAddress;
host = ret.getTerm(0);
port = ret.getTerm(1);
}
}
// If still a variable look it up in the system properties
if (host.isVar() || port.isVar()) {
String prop = System.getProperty("OAA_CONNECT");
if (prop != null) {
try {
IclTerm newAddress = IclTerm.fromString(prop);
if (newAddress != null) {
logger.debug("host and port are vars, resolved");
ret = newAddress;
host = ret.getTerm(0);
port = ret.getTerm(1);
}
} catch (RecognitionException e) {
logger.error("IclTerm.fromString failed with \"" +
prop + "\"", e);
} catch (TokenStreamException e) {
logger.error("IclTerm.fromString failed with \"" +
prop + "\"", e);
}
}
}
// If still a variable, look it up from setup file (diff name on cmdline and setupfile)
if (host.isVar() || port.isVar()) {
IclTerm newAddress = LibComUtils.oaaResolveVariable("default_facilitator", mCmdLine);
if (newAddress != null) {
logger.debug("host and port are vars, resolved");
ret = newAddress;
} else {
logger.debug("host and port are vars, could not resolve");
}
}
}
return ret;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -