📄 internaldomain.java
字号:
package JSHOP2;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.LinkedList;import java.util.Vector;/** Each domain at compile time is represented as an instance of this class. * * @author Okhtay Ilghami * @author <a href="http://www.cs.umd.edu/~okhtay">http://www.cs.umd.edu/~okhtay</a> * @version 1.0.3*/public class InternalDomain{ /** The number of solution plans per planning problem that the user has * requested from this object. */ private int planNo; /** A <code>Vector</code> of axioms seen so far in the domain description. * Each member is of type <code>InternalAxiom</code>. */ private Vector<InternalAxiom> axioms; /** A <code>Vector</code> of <code>String</code> names of user-defined * external code calls that must be imported before being used in the * domain description. */ private Vector<String> calcs; /** A <code>Vector</code> of <code>String</code> names of compound tasks seen * so far in the domain. */ private Vector<String> compoundTasks; /** A <code>Vector</code> of <code>String</code> names of constant symbols * seen so far in the domain. */ private Vector<String> constants; /** The number of constant symbols already seen in the planning domain. Any * number of constant symbols in the planning problem more than this * indicates presence of constant symbols that appear exclusively in the * problem description. */ private int constantsSize; /** The new line character in the platform JSHOP2 is running on. */ final static String endl = System.getProperty("line.separator"); /** A <code>Vector</code> of methods seen so far in the domain description. * Each member is of type <code>InternalMethod</code>. */ private Vector<InternalMethod> methods; /** The <code>String</code> name of the domain. */ private String name; /** A <code>Vector</code> of operators seen so far in the domain description. * Each member is of type <code>InternalOperator</code>. */ private Vector<InternalOperator> operators; /** The parser object that will parse this domain. */ private JSHOP2Parser parser; /** A <code>Vector</code> of <code>String</code> names of primitive tasks * seen so far in the domain. */ private Vector<String> primitiveTasks; /** The <code>String</code> name of the planning problem. */ private String probName; /** To initialize this domain. * * @param fin * the file from which the domain description is to be read. * @param planNoIn * the number of solution plans per planning problem that the user * has requested from this object. * @throws IOException */ public InternalDomain(File fin, int planNoIn) throws IOException { planNo = planNoIn; axioms = new Vector<InternalAxiom>(); calcs = new Vector<String>(); compoundTasks = new Vector<String>(); constants = new Vector<String>(); methods = new Vector<InternalMethod>(); operators = new Vector<InternalOperator>(); //-- Initialize the lexer and the parser associated with this object. JSHOP2Lexer lexer = new JSHOP2Lexer(new FileInputStream(fin)); parser = new JSHOP2Parser(lexer); parser.initialize(lexer, this); primitiveTasks = new Vector<String>(); } /** To add an axiom to the list of axioms read from the file. * * @param axiom * the axiom to be added. */ public void addAxiom(InternalAxiom axiom) { axioms.add(axiom); } /** To add a <code>String</code> used as a name of a compound task in the * domain description to the list of compound task names, in case it has not * been added before. * * @param s * the <code>String</code> to be added. * @return * the index assigned to this name. */ public int addCompoundTask(String s) { int index; //-- If this name has not been added before, add it to the end of the //-- Vector and return its index. if ((index = compoundTasks.indexOf(s)) == -1) { compoundTasks.add(s); return compoundTasks.size() - 1; } //-- Otherwise, just return its index. return index; } /** To add a <code>String</code> used as a constant symbol in the domain * description to the list of constant symbols, in case it has not been * added before. * * @param s * the <code>String</code> to be added. * @return * the index assigned to this name. */ public int addConstant(String s) { int index; //-- If this name has not been added before, add it to the end of the //-- Vector and return its index. if ((index = constants.indexOf(s)) == -1) { constants.add(s); return constants.size() - 1; } //-- Otherwise, just return its index. return index; } /** To add the <code>String</code> name of an external code call to the list * of such code calls. * * @param what * the name of the code call being added. */ public void addCalc(String what) { if (!calcs.contains(what)) calcs.add(what); } /** To add a method to the list of methods read from the file. * * @param method * the method to be added. */ public void addMethod(InternalMethod method) { methods.add(method); } /** To add an operator to the list of operators read from the file. * * @param op * the operator to be added. */ public void addOperator(InternalOperator op) { operators.add(op); } /** To add a <code>String</code> used as a name of a primitive task in the * domain description to the list of primitive task names, in case it has not * been added before. * * @param s * the <code>String</code> to be added. * @return * the index assigned to this name. */ public int addPrimitiveTask(String s) { int index; //-- If this name has not been added before, add it to the end of the //-- Vector and return its index. if ((index = primitiveTasks.indexOf(s)) == -1) { primitiveTasks.add(s); return primitiveTasks.size() - 1; } //-- Otherwise, just return its index. return index; } /** This function writes the Java code necessary to produce this domain at * run time in the appropriate file. * * @param varsMaxSize * the maximum number of variables seen in any variable scope in * this domain. * @throws IOException */ public void close(int varsMaxSize) throws IOException { //-- To hold the String to be written. String s; //-- JSHOP2 classes should be imported first. s = "import JSHOP2.*;" + endl + endl; //-- Produce the classes that represent the operators. for (int i = 0; i < operators.size(); i++) s += operators.get(i).toCode(); //-- Produce the classes that represent the methods. for (int i = 0; i < methods.size(); i++) s += methods.get(i).toCode(); //-- Produce the classes that represent the axioms. for (int i = 0; i < axioms.size(); i++) s += axioms.get(i).toCode(); //-- Produce the class that represents the domain itself. s += "public class " + name + " extends Domain" + endl + "{" + endl; //-- Take care of the user-defined external code calls first by //-- instantiating an object of that class to do the calculations. for (int i = 0; i < calcs.size(); i++) { String imp = (String)calcs.get(i); s += "\tpublic static " + imp + " calculate" + imp + " = new " + imp + "();" + endl + endl; } //-- Produce the constructor for the class that represents this domain. s += "\tpublic " + name + "()" + endl + "\t{" + endl; //-- To initialize an array of the variable symbols the size of which is //-- equal to the maximum number of variables seen in any scope in the //-- domain. This way, all the variable symbols that have the same index //-- will point to the same thing rather than pointing to duplicate copies. s += "\t\tTermVariable.initialize(" + varsMaxSize + ");" + endl + endl; //-- Produce the array that maps constant symbols to integers. s += vectorToCode(constants, "constants"); //-- Produce the array that maps compound tasks to integers. s += vectorToCode(compoundTasks, "compoundTasks"); //-- Produce the array that maps primitive tasks to integers. s += vectorToCode(primitiveTasks, "primitiveTasks"); //-- Allocate an array of type 'Method[]'. The size of the array is the //-- number of compound tasks in the domain, and each element of the array //-- represents all the methods that can be used to decompose the //-- corresponding compound task. s += "\t\tmethods = new Method[" + compoundTasks.size() + "][];" + endl + endl; //-- For each compound task, for (int i = 0; i < compoundTasks.size(); i++) { //-- To store the number of methods that can decompose this compound //-- task. int j = 0; //-- To iterate over the methods. //-- First iterate over the methods to find out how many methods can //-- decompose this compound task. for (InternalMethod m : methods) { if (m.getHead().getHead() == i) j++; } //-- Allocate an array of right size. s += "\t\tmethods[" + i + "] = new Method[" + j + "];" + endl; j = 0; //-- Next, iterate over the methods again, this time to add the methods //-- that can decompose this compound task to the array. for (InternalMethod m : methods) { if (m.getHead().getHead() == i) s += "\t\tmethods[" + i + "][" + j++ + "] = new Method" + m.getCnt() + "();" + endl; } s += endl; } //-- Allocate an array of type 'Operator[]'. The size of the array is the //-- number of primitive tasks in the domain, and each element of the array //-- represents all the operators that can be used to achieve the //-- corresponding primitive task. s += endl + "\t\tops = new Operator[" + primitiveTasks.size() + "][];" + endl + endl; //-- For each primitive task, for (int i = 0; i < primitiveTasks.size(); i++) { //-- To store the number of operators that can achieve this primitive //-- task. int j = 0; //-- To iterate over the operators. //-- First iterate over the operators to find out how many operators can //-- achieve this primitive task. for (InternalOperator o : operators) { if (o.getHead().getHead() == i) j++; } //-- Allocate an array of the right size. s += "\t\tops[" + i + "] = new Operator[" + j + "];" + endl; j = 0; //-- Next, iterate over the operators again, this time to add the //-- operators that can achieve this primitive task to the array. for (InternalOperator o : operators) { if (o.getHead().getHead() == i) s += "\t\tops[" + i + "][" + j++ + "] = new Operator" + o.getCnt() + "();" + endl; } s += endl; } //-- Allocate an array of type 'Axiom[]'. The size of the array is the //-- number of constant symbols in the domain, and each element of the //-- array represents all the axioms that can be used to prove predicates //-- which start with the corresponding constant symbol. s += "\t\taxioms = new Axiom[" + constants.size() + "][];" + endl + endl; //-- For each constant symbol, for (int i = 0; i < constants.size(); i++) { //-- To store the number of axioms that can prove predicates that start //-- with this constant symbol. int j = 0; //-- To iterate over the axioms. //-- First iterate over the axioms to find out how many axioms can be //-- used to prove the predicates that start with this constant symbol. for (InternalAxiom a : axioms) { if (a.getHead().getHead() == i) j++; } //-- Allocate an array of the right size. s += "\t\taxioms[" + i + "] = new Axiom[" + j + "];" + endl; j = 0; //-- Next, iterate over the axioms again, this time to add the axioms //-- that can be used to prove the predicates that start with this //-- constant symbol to the array. for (InternalAxiom a : axioms) { if (a.getHead().getHead() == i) s += "\t\taxioms[" + i + "][" + j++ + "] = new Axiom" + a.getCnt() + "();" + endl; } s += endl; } //-- Close the constructor and the class. s += "\t}" + endl + "}";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -