📄 iclutils.java
字号:
package com.sri.oaa2.icl;
import java.util.*;
import antlr_oaa.RecognitionException;
import antlr_oaa.TokenStreamException;
import org.apache.log4j.Logger;
public class IclUtils {
static Logger logger = Logger.getLogger(IclUtils.class.getName());
/**
* @deprecated use getTrueAtom()
*/
public static final IclTerm iclTrue = new IclStr("true");
/**
* Get the atom representing "true"
*/
public static IclTerm getTrueAtom() {
return new IclStr("true");
}
/**
* @deprecated use IclTerm.fromString(String) (which throws
* exceptions). You can also use fromString(bolean, String) if you
* want a runtime exception.
*/
public static IclTerm icl(String t) {
return IclTerm.fromString(true, t);
}
/**
* Generate an IclTerm from a string, throwing RuntimeException on errors,
* or displaying the errors.
*
* @param boolean failFast: if true, throw RuntimeException; otherwise, just
* display errors.
* @param String t: the string from which to generate an IclTerm
* @return IclTerm: the new IclTerm or null if it could not be parsed
*/
public static IclTerm fromString(boolean failFast, String t) {
return IclTerm.fromString(failFast, t);
}
/**
* Generate an IclTerm from a string. Thread safe. See IclTerm.fromString for
* actual implementation.
*
* @param String t: the string from which to generate an IclTerm
* @return IclTerm: the new IclTerm or null if it could not be parsed
* @throws RecognitionException if the string could not be parsed
* @throws TokenStreamException if the string could not be tokenized
*/
public static IclTerm fromString(String t) throws RecognitionException, TokenStreamException {
return IclTerm.fromString(t);
}
/**
* Removes all occurences of a functor from a parameter list.
*
* @param functor the functor to remove
* @param params the parameter list
* @return <code>true</code> if one or more functors were removed from the
* parameter list, and <code>false</code> if the input functor was not
* found in the parameter list.
*/
public static boolean removeParamValue(String functor, IclList params) {
boolean ret = false;
for (int i = 0; i < params.size(); i++) {
IclTerm param = params.getTerm(i);
if (param.toIdentifyingString().equals(functor)) {
params.removeElement(i--);
ret = true;
}
}
return ret;
}
/**
* Get a parameter value from a parameter list, using the default if the parameter
* if the given functor isn't in the list.
*
* @param String functor: the functor to look for
* @param IclTerm defaultVal: default value to use if no value found
* @param IclList params: parameter list, consisting of parameters in the form
* key(value), or key; the latter form is short for key(true)
* @return IclTerm the desired parameter value, or the default
*/
public static IclTerm getParamValue(String functor, IclTerm defaultVal, IclList params) {
if (params == null) {
return defaultVal;
}
int len = params.size();
IclTerm current = null;
String id;
for (int i = 0; i < len; ++i) {
current = params.getTerm(i);
if (current.isStruct() && (current.size() == 1)) {
id = ToFunctor.getInstance().from(current);
if (id.equals(functor)) {
return current.getTerm(0);
}
} else if (current.isStr() && (current.toIdentifyingString().equals(functor))) {
return new IclStr("true");
}
}
return defaultVal;
}
/**
* Get a parameter value from a list. A parameter list looks like:
* [key1(value1),key2(value2),...,keyn(valuen)].
* This looks for the parameter with key as given. If expectedValue
* is not null, then it also looks for the parameter that unifies
* with the given value. If it is null, then we just look for any
* parameter with the given key. If multiple matching parameters
* exist, this returns the first one.
*
* @param String key: the key of the parameter
* @param IclTerm expectedValue: the value to unify against
* @param IclTerm paramList: a parameter list.
* @return IclTerm: the found parameter as key(value), or null
*/
public static IclTerm termParamValue(String key, IclTerm expectedValue, IclTerm paramList) {
if (paramList == null) {
return null;
}
IclTerm test = null;
IclTerm currentTerm = null;
IclTerm found = null;
int i = 0;
if (expectedValue != null) {
test = new IclStruct(key);
test.add(expectedValue);
} else {
test = new IclStruct(key);
test.add(new IclVar());
}
Iterator it = paramList.iterator();
while (it.hasNext() && (found == null)) {
currentTerm = (IclTerm) (it.next());
if (currentTerm.isStr()) {
currentTerm = new IclStruct(currentTerm.toString());
currentTerm.add(IclUtils.getTrueAtom());
}
found = Unifier.getInstance().unify(currentTerm, test);
++i;
}
// found is null or some term
return found;
}
/**
* As termParamValue, but uses null for the expectedValue, and attempts to convert the
* IclTerm to an int.
*
* @throws RuntimeException: if the returned parameter cannot be converted.
*/
public static int intParamValue(String func, int defaultValue, IclTerm paramList) {
IclTerm found = termParamValue(func, null, paramList);
if (found == null) {
return defaultValue;
}
try {
return ToInt.getInstance().from(found.getTerm(0));
} catch (UnsupportedOperationException uoe) {
logger.info("IclUtils.intParamValue() term cannot be converted: " + found.toString() + ", using default value");
return defaultValue;
}
}
/**
* As termParamValue, but uses null for the expectedValue, and attempts to convert the
* IclTerm to an int.
*
* @throws RuntimeException: if the returned parameter cannot be converted.
*/
public static int intParamValue(String func, IclTerm paramList) {
IclTerm found = termParamValue(func, null, paramList);
if (found == null) {
return -1;
}
try {
return ToInt.getInstance().from(found.getTerm(0));
} catch (UnsupportedOperationException uoe) {
throw new RuntimeException("intParamValue: term cannot be converted: " + found.toString());
}
}
/**
* Get an element in the list which unifies with the given elt.. Note that
* list *must* be an IclList.
*
* @param elt: the element against which to unify
* @param list: a list to search
*/
public static IclTerm getMember(IclTerm elt, IclTerm list) {
IclTerm res = null;
if (list == null) {
return null;
}
if (elt == null) {
return null;
}
if (!list.isList()) {
return null;
}
IclTerm t;
for (ListIterator li = list.listIterator(); li.hasNext();) {
t = (IclTerm) li.next();
res = Unifier.getInstance().unify(elt, t);
if (res != null) {
return res;
}
}
return null;
}
/**
* Converts any '' marks inside a string to just '.
* Single quotes inside strings will be doubled
* when coming from Prolog.
*
* @param s the string to undouble quotes
* @return the input string with double quotes converted to single
* quotes
* @see #removeQuotes
* @see #fixQuotes
*/
public static String undoubleQuotes(String s) {
StringBuffer b = new StringBuffer(s);
int len = b.length();
StringBuffer ret = new StringBuffer(len);
if (len > 1) {
len = b.length();
int i = 0;
while (i < len) {
if ((i != (len - 1)) &&
(b.charAt(i) == '\'') &&
(b.charAt(i + 1) == '\'')) {
ret.append(b.charAt(i));
i += 2;
} else {
ret.append(b.charAt(i));
++i;
}
}
return ret.toString();
}
return s;
}
/**
* Doubles any "'" in the string, to prepare the string to be
* surrounded by "'".
*
* @param s the string to convert to double quotes
* @return the input string with quotes doubled.
* @see #undoubleQuotes
* @see #fixQuotes
*/
public static String doubleQuotes(String s) {
StringBuffer b = new StringBuffer(s);
int len = b.length();
StringBuffer ret = new StringBuffer(len);
int i = 0;
while (i < len) {
if (b.charAt(i) == '\'') {
ret.append("''");
} else {
ret.append(b.charAt(i));
}
++i;
}
return ret.toString();
}
/**
* Remove the quotes from a string.
*
* @param s the string ro remove quotes from
* @return the original string without surrounding quotes
*/
public static String removeQuotes(String s) {
StringBuffer b = new StringBuffer(s);
int len = b.length();
if (len > 0) {
char last = b.charAt(len - 1);
if ((last == '\'') || (last == '"')) {
b.deleteCharAt(len - 1);
}
if (b.length() > 0) {
char first = b.charAt(0);
if ((first == '\'') || (first == '"')) {
b.deleteCharAt(0);
}
}
}
return b.toString();
}
/**
* Converts any '' marks inside a string to just ', and
* removes any external quote marks around the string.
* (Calls removeQuotes, followed by undoubleQuotes).
*
* @param s the string to fix quotes
* @return the string with quotes fixed
* @see #removeQuotes
* @see #undoubleQuotes
*/
public static String fixQuotes(String s) {
return undoubleQuotes(removeQuotes(s));
}
// Following is the deprecated interface
/**
* @deprecated use intParamValue()
*/
public static int iclParamValueAsInt(String func, IclTerm paramList) {
return intParamValue(func, paramList);
}
/**
* @deprecated use termParamValue
*/
public static IclTerm iclParamValue(String function, IclTerm match, IclTerm paramList) {
return termParamValue(function, match, paramList);
}
/**
* @deprecated use termParamValue().getTerm(0) or getParamValue()
*/
public static IclTerm iclParamValue(String function, IclTerm paramList) {
IclTerm res = termParamValue(function, null, paramList);
if (res == null) {
return null;
}
if (!res.isStruct()) {
return null;
}
if (res.size() < 1) {
return null;
}
return res.getTerm(0);
}
/**
* @deprecated use getMember
*/
public static IclTerm iclMember(IclTerm elt, IclTerm list) {
return getMember(elt, list);
}
/**
* @deprecated use IclTerm.fromString()
*/
public static IclTerm iclNewTermFromString(String t) {
if (logger.isDebugEnabled()) {
logger.debug("IclUtils.iclNewTermFromString() with arg: [" + t + "]");
}
return IclTerm.fromString(true, t);
}
/**
* @deprecated use IclTerm.isList
*/
public static boolean iclIsList(IclTerm in) {
return in.isList();
}
/**
* @deprecated use IclTerm.isInt
*/
public static boolean iclIsInt(IclTerm in) {
return in.isInt();
}
/**
* @deprecated use IclTerm.isFloat
*/
public static boolean iclIsFloat(IclTerm in) {
return in.isFloat();
}
/**
* @deprecated use IclTerm.isStruct
*/
public static boolean iclIsStruct(IclTerm in) {
return in.isStruct();
}
/**
* @deprecated use IclTerm.isVar
*/
public static boolean iclIsVar(IclTerm in) {
return in.isVar();
}
/**
* @deprecated use IclTerm.isGroup
*/
public static boolean iclIsGroup(IclTerm in) {
return in.isGroup();
}
/**
* @deprecated use IclTerm.isStr
*/
public static boolean iclIsStr(IclTerm in) {
return in.isStr();
}
/**
* @deprecated use IclTerm.isEmptyList
*/
public static boolean iclIsEmptyList(IclTerm in) {
return in.isEmptyList();
}
/**
* @deprecated silly--will always return true
*/
public static boolean iclIsValid(IclTerm in) {
return true;
}
public static void debugP(String s) {
/*
if(debugFlag) {
System.out.println(s);
}
*/
}
/**
* debugPrint
*/
public static void debugPrint(String s, IclTerm t) {
/*
if (debugFlag) {
System.out.println(s);
if (t!=null)
t.print();
else
System.out.print("Can't print term");
System.out.println();
}
*/
}
/**
* debugPrint
*/
public static void debugPrint(String s) {
/*
if (debugFlag)
System.out.println(s);
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -