⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extendedslparser.jj

📁 java实现的P2P多agent中间件
💻 JJ
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

options {
//  LOOKAHEAD = 1;	      
//  CHOICE_AMBIGUITY_CHECK = 2;
//  OTHER_AMBIGUITY_CHECK = 1;
  STATIC = false;
//  DEBUG_PARSER = true;
//  DEBUG_LOOKAHEAD = true;
//  DEBUG_TOKEN_MANAGER = true;
  ERROR_REPORTING = true;
//  JAVA_UNICODE_ESCAPE = true;
  UNICODE_INPUT = true;
  IGNORE_CASE = true;
//  USER_TOKEN_MANAGER = false;
//  USER_CHAR_STREAM = false;
//  BUILD_PARSER = true;
//  BUILD_TOKEN_MANAGER = true;
//  SANITY_CHECK = true;
//  FORCE_LA_CHECK = true;	// Force LookAhead Checking
}

PARSER_BEGIN(ExtendedSLParser)

package jade.content.lang.sl;

import jade.content.abs.*;
import jade.content.onto.Ontology;
import jade.core.CaseInsensitiveString;
import jade.content.lang.Codec;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.io.IOException;
import java.util.Date;



/**
* ExtendedSLParser. This same parser embeds also lower profiles of SL, namely SL-0, SL-1 and SL-2.
* @author Fabio Bellifemine, TILab S.p.A. (formerly CSELT S.p.A.)
* @author Nicolas Lhuillier (Motorola) (added support for PREFIXBYTELENGTHENCODEDSTRING)
* @version $Date: 2005-05-13 18:14:10 +0200 (Fri, 13 May 2005) $ $Revision: 5696 $
**/
class ExtendedSLParser {

  private static final String META_EXCEPTION_MESSAGE = "Meta SL expressions are not allowed";
		/** This variable is true, when meta symbols are allowed (metas are a semantics-specific extension to the SL Grammar) **/
		private boolean metaAllowed = true; //FIXME to do set/unset this variable 

     /* Take a quoted FIPA SL0 String and convert to a 
      * normal Java-style String.  Remove the
      * leading/trailing quotation marks, and
      * un-escape any included quotation marks.
      * This must be the exact inverse of the 
      * escape() procedure in the SLEncoder.
     */
     private String unescape(String s) {
          StringBuffer result = new StringBuffer(s.length());
          for( int i=1; i<s.length()-1; i++)
          	if( s.charAt(i) == '\\' && s.charAt(i+1) == '\"' ) {
                  result.append("\"");
                  i++;
                } else
                  result.append(s.charAt(i));
                return result.toString();
     }


     /**
     * When an ActionExpression is parsed, if it is an AbsConcept then
     * it must be casted upto an AbsAgentAction. 
     **/
     private AbsTerm toAbsAgentAction(AbsTerm t) {
     	if ((t instanceof AbsConcept) && (!(t instanceof AbsAgentAction))) {
     		AbsAgentAction act = new AbsAgentAction(t.getTypeName());
     		String[] slotNames = t.getNames();
     		if (slotNames != null) {
     			for (int i=0; i<slotNames.length; i++)
     				act.set(slotNames[i], (AbsTerm) t.getAbsObject(slotNames[i]));
     		}
     		return act;
     	} else
     		return t;
     }
     
  /**
   * By default an object of this type implements a Full ExtendedSLParser.
   * This method allows to change this default.
   * @param slType (0 for FIPa-SL0, 1 for SL1, 2 for SL2, >2 for full SL) 
  **/ 
  void setSLType(int slType) {
     this.slType = slType;
  }

  
  Ontology curOntology = null;
  /**
   * Reinitialize the parser such as it is ready to parse a new expression.
   * @param content the content to be parsed
   * @param o the ontology, null if no ontology (this parameter is used to get the names of the slots
   * when they are encoded as unnamed slots.
  */
  void reinit(Ontology o, String content) {
    curOntology = o;
    if (content == null) content = new String();
    ReInit(new StringReader(content));
  }

  /**
   * @param content the content to be parsed
   * @param o the ontology, null if no ontology (this parameter is used to get the names of the slots
   * when they are encoded as unnamed slots.
   * @deprecated since JADE 3.4 it is preferrable to use reinit() and then call directly the method corresponding to the production rule (e.g. Content())
  */
  AbsContentElement parse(Ontology o, String content) throws ParseException, TokenMgrError{
	  reinit(o, content);
    AbsContentElementList tuple = Content();
    if (tuple.size() > 1)
      return tuple;
    else  // if there is a single ContentExpression than return just it, not the tuple
      return tuple.get(0);
  }

  /** (0 for FIPa-SL0, 1 for SL1, 2 for SL2, >2 for full SL) **/
  int slType=3;

  public static void main(String[] args) {

    ExtendedSLParser theParser = null;  
    try {
      theParser = new ExtendedSLParser(System.in);
      theParser.setSLType(Integer.parseInt(args[0]));
    } catch (Exception e) {
      System.out.println("usage: ExtendedSLParser SLLevel\n  where SLLevel can be 0 for SL0, 1 for SL1, 2 for SL2, 3 or more for full SL");
      System.exit(0);
    }
    if (theParser.slType < 3)
       System.out.println("SL-"+theParser.slType+" Parser Started ...");
    else
       System.out.println("Full-SL"+" Parser Started ...");

    SLCodec codec = new SLCodec();
    //Ontology o = new DefaultOntology();

    while (true) {
	System.out.println("insert an SL expression to parse: ");
      try {
	AbsContentElementList result = theParser.Content();
  String resultEncoded = codec.encode(result);
	System.out.println("\n\n RESULT of ExtendedSLParser.Content()=\n"+resultEncoded); 
  AbsContentElement result2 = codec.decode(resultEncoded);
	System.out.println("\n\n RESULT of SLCodec.decode(SLCodec.encode(ExtendedSLParser.Content()))=\n"+codec.encode(result2)); 
	System.out.println("\n\n");
	//result.dump();
	//System.out.println("AFTER ENCODING: "+codec.encode(result,o));
      }
      catch(Exception pe) {
	pe.printStackTrace();
	System.exit(0);
      }
    }
  }

}
 
PARSER_END(ExtendedSLParser)






/*   P R O D U C T I O N    R U L E S  */

/**
* This production rule represents the more general expression that can
* serve as content for an ACL message. Since different communicative
* acts have different content (action expressions for
* <code>request</code>, predicate for <code>inform</code>, etc.), any
* allowed SL content expression can be parsed from here.
*/
AbsContentElementList Content():
{ AbsContentElementList tuple = new AbsContentElementList();  
  AbsContentElement     val;
}
{
 LBrace() (val=ContentExpression() {tuple.add(val);})+ RBrace()
 {return tuple;} 
}

/** Left Brace in all of the possible states of the Token Manager **/
void LBrace() :
{}
{ <LBRACE> | <LBRACE2> } // lbrace2 in the OperatorState of the Token Manager

/** Right Brace in all of the possible states of the Token Manager **/
void RBrace() :
{}
{ <RBRACE> | <RBRACE2> } // rbrace2 in the OperatorState of the Token Manager


AbsContentElement ContentExpression():
{ AbsContentElement val=null; String s; }
{
 (   s=String()     { val=new AbsPredicate(s);} // true, false, PropositionSymbol
   | LBrace()       ( val=ContentExpression_NoBrace() ) RBrace()
   | s=MetaSymbol() { AbsPredicate val1=new AbsPredicate(s); val1.setIsMetaFormula(true); val=val1;} 
 )
 {val.setIsAContentExpression(true); return val;}
}


AbsContentElement ContentExpression_NoBrace() :
{ AbsContentElement val=null; }
{
 (  val=IdentifyingExpression_NoBrace()
  | val=ActionExpression_NoBrace()
  | val=Wff_NoBrace() )
 {return val;}
}


AbsIRE IdentifyingExpression_NoBrace() :
{ Token t; AbsIRE ire=null; AbsPredicate prop; AbsTerm term; AbsVariable var;}
{
 t=<REFERENTIALOP> {if (slType<2) throw new ParseException("NotFullSL_IdentifyExpression_NotParsable_UseAtLeastSL2");} term=Term() /*var=Variable()*/ prop=Wff()
 { ire = new AbsIRE(t.image); 
   /*ire.setVariable(var);*/
   ire.setTerm(term);
   ire.setProposition(prop); 
   return ire;}
}


AbsVariable Variable():
{ AbsVariable val=null; Token v;}
{
  v=<VARIABLE>
  {val = new AbsVariable(); val.setName(v.image.substring(1)); return val;}
}


AbsTerm Term():
{ Token v; AbsTerm val=null; String s;}
{
 (  val=Variable() 
  | val=Constant()
  | LBrace() 
   (   val=FunctionalTerm_NoBrace()
     | val=ActionExpression_NoBrace()
     | val=IdentifyingExpression_NoBrace()
   ) 
   RBrace() 
                     // in order to avoid adding a new class that represents a generic AbsTerm, I am reusing AbsVariable. 
                     // Of course, the methods fromObject/toObject needs to have a similar behaviour. 
  | s=MetaSymbol() { AbsVariable val1=new AbsVariable(); val1.setName(s); val1.setIsMetaTerm(true); val=val1;} 
 )
 {return val;}
}


AbsPrimitive Constant() :
{ String s; AbsPrimitive val=null; Token t; }
{
 (  s= String() { 
	// if it is true or false, then converts the String into a Boolean
        if (CaseInsensitiveString.equalsIgnoreCase(s, "true")) 
	        val = AbsPrimitive.wrap(true);	
        else if (CaseInsensitiveString.equalsIgnoreCase(s, "false"))  
	        val = AbsPrimitive.wrap(false);
	else {
        if (  (CaseInsensitiveString.equalsIgnoreCase(s,"\"true\""))
            ||(CaseInsensitiveString.equalsIgnoreCase(s,"\"false\"")) )
                  // in this case leading/trailing quotes were added by the
                  // encoder and now they must be removed. 
                  s = unescape(s);
		    val = AbsPrimitive.wrap(s);
        }
                }
  | val=Number() 
  | t=<DATETIME> { 
    try { 
      Date d=jade.lang.acl.ISO8601.toDate(t.image);
      val = AbsPrimitive.wrap(d);
    } catch (Exception e) { 
      val = AbsPrimitive.wrap(t.image); 
                 }
  }
  | t=<PREFIXBYTELENGTHENCODEDSTRING> {
    val = AbsPrimitive.wrap(t.image.getBytes());
                                      }
 )
 {return val;}
}


AbsConcept FunctionalTerm_NoBrace() :  
{ Token t; AbsTerm term1, term2; AbsConcept val=null; String s;}
{
 (  t=<ARITHMETICOP> term1=Term() term2=Term() 
    {  	val = new AbsConcept(t.image);
	try {
	   String slotNames[] = curOntology.getSchema(t.image).getNames();
	   val.set(slotNames[0], term1);
	   val.set(slotNames[1], term2);
        } catch (Exception e) {
           val.set(Codec.UNNAMEDPREFIX+"0", term1);
           val.set(Codec.UNNAMEDPREFIX+"1", term2);
        }
    }	
  | s=String() {
      if ( (SL0Vocabulary.SET.equalsIgnoreCase(s)) || (SL0Vocabulary.SEQUENCE.equalsIgnoreCase(s))) 
         val = new AbsAggregate(s);  
      else 
         val = new AbsConcept(s);}
   (FunctionalTermParameters(val))?
 )
 {return val;} 
}

// artificial production, needed to avoid a bad warning from javacc
// val can be either an AbsConcept or an AbsAggregate
void FunctionalTermParameters(AbsConcept val) :
{ AbsTerm t; int slotNumber=0;}
{
   ( { String slotNames[] = null;
         try {

⌨️ 快捷键说明

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