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

📄 slparser.java

📁 java实现的P2P多agent中间件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* Generated By:JavaCC: Do not edit this line. SLParser.java */
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;



/**
* SLParser. 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 SLParser implements SLParserConstants {

  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 SLParser.
   * 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) {

    SLParser theParser = null;
    try {
      theParser = new SLParser(System.in);
      theParser.setSLType(Integer.parseInt(args[0]));
    } catch (Exception e) {
      System.out.println("usage: SLParser 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 SLParser.Content()=\n"+resultEncoded);
  AbsContentElement result2 = codec.decode(resultEncoded);
        System.out.println("\n\n RESULT of SLCodec.decode(SLCodec.encode(SLParser.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);
      }
    }
  }

/*   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.
*/
  final public AbsContentElementList Content() throws ParseException {
  AbsContentElementList tuple = new AbsContentElementList();
  AbsContentElement     val;
    LBrace();
    label_1:
    while (true) {
      val = ContentExpression();
                                    tuple.add(val);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case LBRACE:
      case WORD:
      case STRING_LITERAL:
      case METASYMBOL:
      case LBRACE2:
      case WORD2:
      case STRING_LITERAL2:
      case METASYMBOL2:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
    }
    RBrace();
  {if (true) return tuple;}
    throw new Error("Missing return statement in function");
  }

/** Left Brace in all of the possible states of the Token Manager **/
  final public void LBrace() throws ParseException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case LBRACE:
      jj_consume_token(LBRACE);
      break;
    case LBRACE2:
      jj_consume_token(LBRACE2);
      break;
    default:
      jj_la1[1] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  }

                         // lbrace2 in the OperatorState of the Token Manager

/** Right Brace in all of the possible states of the Token Manager **/
  final public void RBrace() throws ParseException {
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case RBRACE:
      jj_consume_token(RBRACE);
      break;
    case RBRACE2:
      jj_consume_token(RBRACE2);
      break;
    default:
      jj_la1[2] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  }

                         // rbrace2 in the OperatorState of the Token Manager
  final public AbsContentElement ContentExpression() throws ParseException {
  AbsContentElement val=null; String s;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case WORD:
    case STRING_LITERAL:
    case WORD2:
    case STRING_LITERAL2:
      s = String();
                      val=new AbsPredicate(s);
      break;
    case LBRACE:
    case LBRACE2:
      LBrace();
      val = ContentExpression_NoBrace();
      RBrace();
      break;
    case METASYMBOL:
    case METASYMBOL2:
      s = MetaSymbol();
                      AbsPredicate val1=new AbsPredicate(s); val1.setIsMetaFormula(true); val=val1;
      break;
    default:
      jj_la1[3] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  val.setIsAContentExpression(true); {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

  final public AbsContentElement ContentExpression_NoBrace() throws ParseException {
  AbsContentElement val=null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case REFERENTIALOP:
      val = IdentifyingExpression_NoBrace();
      break;
    case ACTION:
    case ACTIONOPLL:
      val = ActionExpression_NoBrace();
      break;
    case WORD:
    case STRING_LITERAL:
    case MODALOP:
    case ACTIONOP:
    case UNARYLOGICALOP:
    case BINARYLOGICALOP:
    case QUANTIFIER:
    case WORD2:
    case STRING_LITERAL2:
      val = Wff_NoBrace();
      break;
    default:
      jj_la1[4] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

  final public AbsIRE IdentifyingExpression_NoBrace() throws ParseException {
  Token t; AbsIRE ire=null; AbsPredicate prop; AbsTerm term; AbsVariable var;
    t = jj_consume_token(REFERENTIALOP);
                    if (slType<2) {if (true) 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);
   {if (true) return ire;}
    throw new Error("Missing return statement in function");
  }

  final public AbsVariable Variable() throws ParseException {
  AbsVariable val=null; Token v;
    v = jj_consume_token(VARIABLE);
   val = new AbsVariable(); val.setName(v.image.substring(1)); {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

  final public AbsTerm Term() throws ParseException {
  Token v; AbsTerm val=null; String s;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case VARIABLE:
      val = Variable();
      break;
    case INTEGER:
    case HEXINTEGER:
    case FLOATONE:
    case FLOATTWO:
    case WORD:
    case STRING_LITERAL:
    case DATETIME:
    case PREFIXBYTELENGTHENCODEDSTRING:
    case WORD2:
    case STRING_LITERAL2:
      val = Constant();
      break;
    case LBRACE:
    case LBRACE2:
      LBrace();
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case WORD:
      case STRING_LITERAL:
      case ARITHMETICOP:
      case WORD2:
      case STRING_LITERAL2:
        val = FunctionalTerm_NoBrace();
        break;
      case ACTION:
      case ACTIONOPLL:
        val = ActionExpression_NoBrace();
        break;
      case REFERENTIALOP:
        val = IdentifyingExpression_NoBrace();
        break;
      default:
        jj_la1[5] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      RBrace();
      break;
    case METASYMBOL:
    case METASYMBOL2:
      s = MetaSymbol();
                     AbsVariable val1=new AbsVariable(); val1.setName(s); val1.setIsMetaTerm(true); val=val1;
      break;
    default:
      jj_la1[6] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

  final public AbsPrimitive Constant() throws ParseException {
  String s; AbsPrimitive val=null; Token t;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case WORD:
    case STRING_LITERAL:
    case WORD2:
    case STRING_LITERAL2:

⌨️ 快捷键说明

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