📄 generatemanualconstruction.java
字号:
package com.sri.oaa2.icl;
/**
* Given a well-formed String, s--that is, one for which IclTerm.fromString(true, s)
* does not throw an exception--and where s contains no icldataq() type terms, this
* class can be used to generate a new String which represents the manual construction
* of the term represented by s.
* <p>
* For example, calling getInstance().from("a") would result in the String
* new IclStr("a")
* This is most useful when you have a long String for which you currently call
* IclTerm.fromString(). The reason is because calling fromString() is much
* slower than using the equivalent constructor calls, due to the parsing
* overhead.
*
* @author agno
*/
public class GenerateManualConstruction
{
private static final GenerateManualConstruction singleton = new GenerateManualConstruction();
GenerateManualConstruction()
{
}
/**
* Generate a String representing the construction of the IclTerm represented
* by s. The construction will use Java constructors.
*
* @param s The String to convert
* @return String a String which can be used to replace an existing call
* to IclTerm.fromString
*/
public String from(String s)
{
IclTerm term = IclTerm.fromString(true, s);
Traverse trav = new Traverse();
trav.traverse(term);
trav.getBuffer().delete(trav.getBuffer().length() - 2, trav.getBuffer().length());
return trav.getBuffer().toString();
}
/**
* Get an instance of this class.
*/
public static GenerateManualConstruction getInstance()
{
return singleton;
}
private class Traverse extends IclTermTraverser implements OaaPrologVocabTokenTypes
{
StringBuffer buffer = new StringBuffer();
Traverse()
{
}
StringBuffer getBuffer()
{
return buffer;
}
public boolean discover(IclTerm t)
{
switch(t.getType()) {
case OaaPrologVocabTokenTypes.LIST:
this.getBuffer().append(
"new IclList(Arrays.asList(new Object[] {");
break;
case OaaPrologVocabTokenTypes.GROUP:
this.getBuffer().append(
"new IclGroup(Arrays.asList(new Object[] {");
break;
case OaaPrologVocabTokenTypes.STRUCT:
this.getBuffer().append("new IclStruct(");
this.getBuffer().append("\"");
this.getBuffer().append(((IclStruct)t).getFunctor());
this.getBuffer().append("\"");
this.getBuffer().append(
", Arrays.asList(new Object[] {");
break;
case OaaPrologVocabTokenTypes.INT:
this.getBuffer().append("new IclInt(");
this.getBuffer().append(((IclInt)t).toLong());
this.getBuffer().append("L)");
this.getBuffer().append(", ");
break;
case OaaPrologVocabTokenTypes.FLOAT:
this.getBuffer().append("new IclFloat(");
this.getBuffer().append(((IclFloat)t).toDouble());
this.getBuffer().append(")");
this.getBuffer().append(", ");
break;
case OaaPrologVocabTokenTypes.VAR:
this.getBuffer().append("new IclVar(");
this.getBuffer().append("\"");
this.getBuffer().append(((IclVar)t).getName());
this.getBuffer().append("\"");
this.getBuffer().append(")");
this.getBuffer().append(", ");
break;
case OaaPrologVocabTokenTypes.STR:
this.getBuffer().append("new IclStr(");
this.getBuffer().append("\"");
this.getBuffer().append(((IclStr)t).toUnquotedString());
this.getBuffer().append("\"");
this.getBuffer().append(")");
this.getBuffer().append(", ");
break;
case OaaPrologVocabTokenTypes.ICLDATAQ:
throw new UnsupportedOperationException("GenerateManualConstruction won't work for IclDataQ");
default:
throw new UnsupportedOperationException("GenerateManualConstruction unknown type: " + t.getType());
}
return true;
}
public boolean finish(IclTerm t)
{
switch(t.getType()) {
case OaaPrologVocabTokenTypes.LIST:
if(t.size() > 0) {
this.getBuffer().delete(this.getBuffer().length() - 2, this.getBuffer().length());
}
this.getBuffer().append("}))");
this.getBuffer().append(", ");
break;
case OaaPrologVocabTokenTypes.GROUP:
if(t.size() > 0) {
this.getBuffer().delete(this.getBuffer().length() - 2, this.getBuffer().length());
}
this.getBuffer().append("}), ");
this.getBuffer().append("'");
this.getBuffer().append(((IclGroup)t).getStarter());
this.getBuffer().append("'");
this.getBuffer().append(")");
this.getBuffer().append(", ");
break;
case OaaPrologVocabTokenTypes.STRUCT:
if(t.size() > 0) {
this.getBuffer().delete(this.getBuffer().length() - 2, this.getBuffer().length());
}
this.getBuffer().append("}))");
this.getBuffer().append(", ");
break;
default:
// nothing
}
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -