jshop2parser.java

来自「SHOP2 一个人工智能里面关于任务分解和任务规划的系统。JSHOP2是其jav」· Java 代码 · 共 1,647 行 · 第 1/3 页

JAVA
1,647
字号
				//-- Create the object that represents the operator, and add it to the		//-- list of the operators in the domain.		domain.addOperator(new InternalOperator(p, pre, del, add, cost));				//-- The scope for the variables in an operator is within that operator,		//-- so as soon as we get out of the operator body we should empty our		//-- list of variables after updating the value of 'varsMaxSize'.		if (vars.size() > varsMaxSize)		varsMaxSize = vars.size();				vars.clear();				match(RP);	}		public final void axiom() throws RecognitionException, TokenStreamException {				Token  ah = null;		Token  bn = null;						//-- A Vector each element of which represents one branch of the axiom,		//-- (i.e., one way to prove its head).		Vector<LogicalPrecondition> a = new Vector<LogicalPrecondition>();				//-- The current branch label.		String branchLabel;				//-- A Vector of String labels of branches.		Vector<String> labels = new Vector<String>();				//-- The current branch of the axiom being considered.		LogicalPrecondition lPre;				//-- The head of the axiom (i.e., the predicate it can be used to prove).		Predicate p;				match(LP);		ah = LT(1);		match(AXIOM);		p=la();		{		int _cnt34=0;		_loop34:		do {			if ((_tokenSet_2.member(LA(1)))) {								//-- Generate a default label for this branch.				branchLabel = "Axiom" + domain.getAxiomNo() + "Branch" + labels.size();								{				switch ( LA(1)) {				case ID:				{					bn = LT(1);					match(ID);										branchLabel = bn.getText();										break;				}				case LP:				case NIL:				case VARID:				{					break;				}				default:				{					throw new NoViableAltException(LT(1), getFilename());				}				}				}				lPre=lp();								//-- Add the current branch of the axiom to the list of its branches				//-- and its label to the list of labels.				a.add(lPre);				labels.add(branchLabel);							}			else {				if ( _cnt34>=1 ) { break _loop34; } else {throw new NoViableAltException(LT(1), getFilename());}			}						_cnt34++;		} while (true);		}				p.setVarCount(vars.size());				//-- Create the object that represents the axiom, and add it to the list		//-- of the axioms in the domain.		domain.addAxiom(new InternalAxiom(p, a, labels));				//-- The scope for the variables in an axiom is within that axiom, so as		//-- soon as we get out of the operator body we should empty our list of		//-- variables after updating the value of 'varsMaxSize'.		if (vars.size() > varsMaxSize)		varsMaxSize = vars.size();				vars.clear();				match(RP);	}		public final List  terml() throws RecognitionException, TokenStreamException {		List retVal;								//-- A LinkedList to store the terms in the term list.		LinkedList<Term> list = new LinkedList<Term>();				//-- The current term.		Term tn;				{		_loop57:		do {			if ((_tokenSet_3.member(LA(1)))) {				tn=term();								//-- Add the current term to the list of terms seen so far.				list.addFirst(tn);							}			else {				break _loop57;			}					} while (true);		}				//-- Create the object that represents this term list.		retVal = List.MakeList(list);				return retVal;	}		public final LogicalPrecondition  lp() throws RecognitionException, TokenStreamException {		LogicalPrecondition retVal;				Token  vn = null;				//-- The logical expression associated with this logical precondition.		LogicalExpression lExp;				//-- The name of the function used in a :sort-by logical precondition.		String func = null;						if ((_tokenSet_4.member(LA(1))) && (_tokenSet_5.member(LA(2)))) {			lExp=le();						//-- Create the object that represents this logical precondition.			retVal = new LogicalPrecondition(lExp, false);					}		else if ((LA(1)==LP) && (LA(2)==FIRST)) {			match(LP);			match(FIRST);			lExp=le();			match(RP);						//-- Create the object that represents this logical precondition.			retVal = new LogicalPrecondition(lExp, true);					}		else if ((LA(1)==LP) && (LA(2)==SORT)) {			match(LP);			match(SORT);			vn = LT(1);			match(VARID);			{			switch ( LA(1)) {			case ID:			case DIV:			case EQUAL:			case LESS:			case LESSEQ:			case MEMBER:			case MINUS:			case MORE:			case MOREEQ:			case MULT:			case NOTEQ:			case PLUS:			case POWER:			{				func=fid();				break;			}			case LP:			case NIL:			case VARID:			{				break;			}			default:			{				throw new NoViableAltException(LT(1), getFilename());			}			}			}			lExp=le();			match(RP);						String s = vn.getText().toLowerCase();						//-- Add the variable to the variable list.			if (!vars.contains(s))			vars.add(s);						//-- If no function is specified, use '<' as the default function.			if (func == null || func.equals("StdLib.less"))			func = "new CompLess(" + vars.indexOf(s) + ")";			else if (func.equals("StdLib.more"))			func = "new CompMore(" + vars.indexOf(s) + ")";			else			func = "new " + func + "(" + vars.indexOf(s) + ")";						//-- Create the object that represents this logical precondition.			retVal = new LogicalPrecondition(lExp, func);					}		else {			throw new NoViableAltException(LT(1), getFilename());		}				return retVal;	}		public final Vector  da() throws RecognitionException, TokenStreamException {		Vector retVal;				Token  var = null;				//-- The current delete/add element.		DelAddElement delAdd = null;				//-- The return value, a Vector of delete/add elements.		retVal = new Vector();				//-- The first element is set to NULL, meaning that this delete/add list is		//-- a real list of predicates, and not a variable.		retVal.add(null);						switch ( LA(1)) {		case LP:		{			match(LP);			{			_loop21:			do {				if ((LA(1)==LP||LA(1)==VARID)) {					delAdd=dae();										//-- Add the next delete/add element to the delete/add list.					retVal.add(delAdd);									}				else {					break _loop21;				}							} while (true);			}			match(RP);			break;		}		case VARID:		{			var = LT(1);			match(VARID);						//-- In case the delete/add list is a variable (to be instantiated to a			//-- list of predicates at run time) and not a real list, just clear the			//-- Vector (of its first element set to NULL) to signal this, and add			//-- the index of the variable to the Vector.			retVal.clear();			retVal.add(new Integer(vars.indexOf(var.getText().toLowerCase())));						break;		}		case NIL:		{			match(NIL);			break;		}		default:		{			throw new NoViableAltException(LT(1), getFilename());		}		}		return retVal;	}		public final Term  term() throws RecognitionException, TokenStreamException {		Term retVal;				Token  vn = null;		Token  in = null;		Token  num = null;				//-- The name of the function called in a call term.		String func;				//-- The term list.		List list;				//-- The current term.		Term tn;				retVal = null;						switch ( LA(1)) {		case VARID:		{			vn = LT(1);			match(VARID);						//-- Add the variable symbol to the variable list.			if (!vars.contains(vn.getText().toLowerCase()))			vars.add(vn.getText().toLowerCase());						//-- Create the object that represents this variable symbol.			retVal = new TermVariable(vars.indexOf(vn.getText().toLowerCase()));						break;		}		case ID:		{			in = LT(1);			match(ID);						//-- Add the constant symbol to the list of constant symbols in the			//-- domain.			int index = domain.addConstant(in.getText().toLowerCase());						//-- Create the object that represents this constant symbol.			retVal = new TermConstant(index);						break;		}		case NUM:		{			num = LT(1);			match(NUM);						//-- Create the object that represents this numerical term.			retVal = new TermNumber(Double.parseDouble(num.getText().toLowerCase()));						break;		}		case NIL:		{			match(NIL);						//-- Empty list term.			retVal = TermList.NIL;						break;		}		default:			if ((LA(1)==LP) && (_tokenSet_6.member(LA(2)))) {				match(LP);				list=terml();				{				switch ( LA(1)) {				case DOT:				{					match(DOT);					tn=term();										//-- Append the current term to the end of the term list.					retVal = new TermList(list.append(tn));										break;				}				case RP:				{					break;				}				default:				{					throw new NoViableAltException(LT(1), getFilename());				}				}				}								//-- If retVal is not already created, create it as a list term.				if (retVal == null)				retVal = new TermList(list);								match(RP);			}			else if ((LA(1)==LP) && (LA(2)==CALL)) {				match(LP);				match(CALL);				func=fid();				list=terml();				match(RP);								//-- If this function call is not one implemented in the standard				//-- library, add it to the list of user-defined external code calls.				if (!func.startsWith("StdLib."))				{				domain.addCalc(func);				func = domain.getName() + ".calculate" + func;				}								//-- Create the object that represents this call term.				retVal = new TermCall(list, func);							}		else {			throw new NoViableAltException(LT(1), getFilename());		}		}		return retVal;	}		public final DelAddElement  dae() throws RecognitionException, TokenStreamException {		DelAddElement retVal;						//-- The logical atom to be deleted, added, protected or unprotected.		Predicate p;						if ((LA(1)==LP||LA(1)==VARID) && (_tokenSet_7.member(LA(2)))) {			p=la();						//-- The atomic delete/add element.			retVal = new DelAddAtomic(p);					}		else if ((LA(1)==LP) && (LA(2)==PROTECTION)) {			match(LP);			match(PROTECTION);			p=la();			match(RP);						//-- The protection delete/add element.			retVal = new DelAddProtection(p);					}		else if ((LA(1)==LP) && (LA(2)==FORALL)) {			match(LP);			match(FORALL);						//-- The precondition of the ForAll delete/add element.			LogicalExpression exp;						//-- A Vector of atoms to be deleted/added under this ForAll delete/add			//-- element.			Vector<Predicate> atoms = new Vector<Predicate>();						{			switch ( LA(1)) {			case LP:			{				{				match(LP);				{				_loop26:				do {					if ((LA(1)==VARID)) {						match(VARID);					}					else {						break _loop26;					}									} while (true);				}				match(RP);				}				break;			}			case NIL:			{				match(NIL);				break;			}			default:			{				throw new NoViableAltException(LT(1), getFilename());			}			}			}			exp=le();			{			switch ( LA(1)) {			case LP:			{				{				match(LP);				{				_loop30:				do {					if ((LA(1)==LP||LA(1)==VARID)) {						p=la();												//-- Add the current predicate to the list of atoms to be						//-- deleted/added under this ForAll delete/add element.						atoms.add(p);											}					else {						break _loop30;					}									} while (true);				}				match(RP);				}				break;			}			case NIL:			{				match(NIL);				break;			}			default:			{				throw new NoViableAltException(LT(1), getFilename());			}			}			}						//-- Create the delete/add element and return it.			retVal = new DelAddForAll(exp, atoms);						match(RP);		}		else {			throw new NoViableAltException(LT(1), getFilename());		}				return retVal;	}		public final LogicalExpression  le() throws RecognitionException, TokenStreamException {		LogicalExpression retVal;

⌨️ 快捷键说明

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