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

📄 slparser.java

📁 人工智能中Agent开发包。多 Agent 系统是处理自治 Agent 之间知识层的协作问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Generated By:JavaCC: Do not edit this line. SLParser.java */
package JADE_SL.lang.sl;

import JADE_SL.abs.*;
import JADE_SL.onto.Ontology;
import JADE_SL.CaseInsensitiveString;
import JADE_SL.lang.Codec;
import JADE_SL.*;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
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.)
* @version $Date: 2003/10/09 12:55:36 $ $Revision: 1.1.1.1 $
**/
class SLParser implements SLParserConstants {


     /* 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;
  /**
   * @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.
  */
  AbsContentElement parse(Ontology o, String content) throws ParseException, TokenMgrError{
    curOntology = o;
    ReInit(new StringReader(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 ...");

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

    while (true) {
        System.out.println("insert an SL expression to parse: ");
      try {
        AbsContentElementList result = theParser.Content();
        System.out.println("\n\n RESULT=\n"+result.toString());
        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 LBRACE2:
      case WORD2:
      case STRING_LITERAL2:
        ;
        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;
    default:
      jj_la1[3] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  {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");
  }

//FIXME. The IRE has been constrained to contain a Variable instead of a Term.
  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");}
    /* t=Term()*/ var = Variable();
    prop = Wff();
   ire = new AbsIRE(t.image);
   ire.setVariable(var); 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); {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

  final public AbsTerm Term() throws ParseException {
  Token v; AbsTerm val=null;
    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 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;
    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:
      s = String();
        // if it is true or false, then converts the String into a Boolean
        if (CaseInsensitiveString.equalsIgnoreCase(s, "true") ||
            CaseInsensitiveString.equalsIgnoreCase(s, "false") )
                val = AbsPrimitive.wrap(Boolean.getBoolean(s));
        else
                val = AbsPrimitive.wrap(s);
      break;
    case INTEGER:
    case HEXINTEGER:
    case FLOATONE:
    case FLOATTWO:
      val = Number();
      break;
    case DATETIME:
      t = jj_consume_token(DATETIME);
        try {
          Date d=ISO8601.toDate(t.image);
          val = AbsPrimitive.wrap(d);
        } catch (Exception e) {
          val = AbsPrimitive.wrap(t.image);
        }
      break;
    default:
      jj_la1[7] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

  final public AbsConcept FunctionalTerm_NoBrace() throws ParseException {
  Token t; AbsTerm term1, term2; AbsConcept val=null; String s;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case ARITHMETICOP:
      t = jj_consume_token(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);
        }
      break;
    case WORD:
    case STRING_LITERAL:
    case WORD2:
    case STRING_LITERAL2:
      s = String();
      if ( (SLCodec.SET.equalsIgnoreCase(s)) || (SLCodec.SEQUENCE.equalsIgnoreCase(s)))
         val = new AbsAggregate(s);
      else
         val = new AbsConcept(s);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case LBRACE:
      case VARIABLE:
      case INTEGER:
      case HEXINTEGER:
      case FLOATONE:
      case FLOATTWO:
      case WORD:
      case STRING_LITERAL:
      case PARAMETERNAME:
      case DATETIME:
      case LBRACE2:
      case WORD2:
      case STRING_LITERAL2:
        FunctionalTermParameters(val);
        break;
      default:
        jj_la1[8] = jj_gen;
        ;
      }
      break;
    default:
      jj_la1[9] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
  {if (true) return val;}
    throw new Error("Missing return statement in function");
  }

// artificial production, needed to avoid a bad warning from javacc
// val can be either an AbsConcept or an AbsAggregate
  final public void FunctionalTermParameters(AbsConcept val) throws ParseException {
  AbsTerm t; int slotNumber=0;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case LBRACE:
    case VARIABLE:
    case INTEGER:
    case HEXINTEGER:
    case FLOATONE:
    case FLOATTWO:
    case WORD:
    case STRING_LITERAL:
    case DATETIME:
    case LBRACE2:
    case WORD2:
    case STRING_LITERAL2:
       String slotNames[] = null;
         try {
           slotNames = curOntology.getSchema(val.getTypeName()).getNames();
         } catch (Exception e) {
         }
      label_2:
      while (true) {
        t = Term();
         try {
               val.set(slotNames[slotNumber], t);
         } catch (Exception e) {
               val.set(Codec.UNNAMEDPREFIX+slotNumber,t);
         }
        slotNumber++;
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case LBRACE:
        case VARIABLE:
        case INTEGER:
        case HEXINTEGER:
        case FLOATONE:
        case FLOATTWO:
        case WORD:
        case STRING_LITERAL:
        case DATETIME:
        case LBRACE2:
        case WORD2:
        case STRING_LITERAL2:
          ;
          break;
        default:
          jj_la1[10] = jj_gen;
          break label_2;
        }
      }
      break;
    case PARAMETERNAME:
      label_3:

⌨️ 快捷键说明

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