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

📄 slcodec.java

📁 JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简化了多Agent系统的实现。
💻 JAVA
📖 第 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.
 * **************************************************************
 */
package jade.content.lang.sl;

import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.abs.*;
import jade.content.schema.ObjectSchema;
import jade.content.lang.StringCodec;
//#MIDP_EXCLUDE_BEGIN
import jade.lang.acl.ISO8601;
import jade.util.leap.Iterator;
import jade.domain.FIPANames;
import jade.core.CaseInsensitiveString;

import java.util.Date;
import java.util.Vector;
import java.io.StringReader;

import java.io.BufferedReader; // only for debugging purposes in the main
import java.io.InputStreamReader; // only for debugging purposes in the main
//#MIDP_EXCLUDE_END

/**  
 * The codec class for the <b><i>FIPA-SL</i>n</b> languages. This class
 * implements the <code>Codec</code> interface and allows converting
 * back and forth between strings and frames, according to the SL
 * grammar.
 * By default the class implements full SL grammar, otherwise the proper
 * value must be used in the constructor.
 * @author Fabio Bellifemine - TILAB 
 * @author Nicolas Lhuillier - Motorola (added support for byte[] primitive)
 * @version $Date: 2005-10-28 14:19:31 +0200 (ven, 28 ott 2005) $ $Revision: 5805 $
 */
/*#MIDP_INCLUDE_BEGIN
public class SLCodec extends SimpleSLCodec {
#MIDP_INCLUDE_END*/
//#MIDP_EXCLUDE_BEGIN
public class SLCodec extends StringCodec {

    private transient SLParser parser;
    private SL0Ontology slOnto; // ontology of the content language
    private Ontology domainOnto = null; // application ontology
    /** This is the StringBuffer used by the encode method **/
    private transient StringBuffer buffer = null; 
		/** This variable is true, when meta symbols are allowed (metas are a semantics-specific extension to the SL Grammar) **/
		private boolean metaAllowed = true; //FIXME set/unset this variable to do

    /**
     * Construct a Codec object for the full SL-language (FIPA-SL).
     */
    public SLCodec() {
    	this(3);
    }
    
    /**
     * Construct a Codec object for the given profile of SL-language.
     * @param slType specify 0 for FIPA-SL0, 1 for FIPA-SL1, 2 for FIPA-SL2, any other value can be used for full FIPA-SL
     */
    public SLCodec(int slType) {
	super((slType==0 ? FIPANames.ContentLanguage.FIPA_SL0 :
	       (slType==1 ? FIPANames.ContentLanguage.FIPA_SL1 :
		(slType==2 ? FIPANames.ContentLanguage.FIPA_SL2 :
		 FIPANames.ContentLanguage.FIPA_SL ))));
	if ((slType < 0) || (slType > 2)) // if outside range, set to full SL
	    slType = 3;
	slOnto = (SL0Ontology) (slType == 0 ? SL0Ontology.getInstance() : 
		(slType == 1 ? SL1Ontology.getInstance() :
		(slType == 2 ? SL2Ontology.getInstance() : SLOntology.getInstance())));
	parser = new SLParser(new StringReader(""));
	parser.setSLType(slType); 
    }


    /**
     * Encodes a content into a String.
     * @param content the content as an abstract descriptor.
     * @return the content as a String.
     * @throws CodecException
     */
    public String encode(AbsContentElement content) throws CodecException {
	return encode(null, content);
    }

    /**
     * Encodes a content into a String.
     * @param ontology the ontology 
     * @param content the content as an abstract descriptor.
     * @return the content as a String.
     * @throws CodecException
     */
    public synchronized String encode(Ontology ontology, AbsContentElement content) throws CodecException {
    	try {
    		domainOnto = ontology;
    		buffer = new StringBuffer("(");
    		if (content instanceof AbsContentElementList) {
    			for (Iterator i=((AbsContentElementList)content).iterator(); i.hasNext(); ) {
    				AbsObject o = (AbsObject)i.next();
    				encodeAndAppend(o);
    				buffer.append(' ');
    			}
    		} else encodeAndAppend(content);
    		buffer.append(')');
    		return buffer.toString();
    	} finally {
    		buffer = null; //frees the memory
    	}
    }

 

    /**
     * Encode a string, taking care of quoting separated words and
     * escaping strings, if necessary.
     * And append it to the buffer.
     **/
    private void encodeAndAppend(String val) {
    	// if the slotName is a String of words then quote it. If it is a meta (i.e. startsWith "??") do not quote it.
    	String out = ( (SimpleSLTokenizer.isAWord(val) || (metaAllowed && val.startsWith("??")) ) ? val : SimpleSLTokenizer.quoteString(val));
    	buffer.append(out);
    }


    /** Encode the passed Abstract Predicate and append its encoding to buffer **/
    private void encodeAndAppend(AbsPredicate val) throws CodecException {
    	String propositionSymbol = val.getTypeName();
    	if (val.getCount() > 0) { // predicate with arguments
    		String[] slotNames = getSlotNames(val);
    		buffer.append('(');
    		if (slOnto.isUnaryLogicalOp(propositionSymbol)) {
    			// Unary logical operator of the SL language (NOT)
    			buffer.append(propositionSymbol);
    			buffer.append(' ');
    			try {
    				encodeAndAppend((AbsPredicate)val.getAbsObject(slotNames[0]));
    			} catch (RuntimeException e) {
    				throw new CodecException("A UnaryLogicalOp requires a formula argument",e);
    			}
    		} else if (slOnto.isBinaryLogicalOp(propositionSymbol)) {
    			// Bynary logical operator of the SL language (AND, OR)
    			buffer.append(propositionSymbol);
    			buffer.append(' ');
    			try {
    				encodeAndAppend((AbsPredicate)val.getAbsObject(slotNames[0]));
    				buffer.append(' ');
    				encodeAndAppend((AbsPredicate)val.getAbsObject(slotNames[1]));
    			} catch (RuntimeException e) {
    				throw new CodecException("A BinaryLogicalOp requires 2 formula arguments",e);
    			}
    		} else if (slOnto.isQuantifier(propositionSymbol)) {
    			// Quantifier operator of the SL language (EXISTS, FORALL)
    			buffer.append(propositionSymbol);
    			buffer.append(' ');
    			try {
    				encodeAndAppend((AbsVariable)val.getAbsObject(slotNames[0])); //FIXME. The hypothesis is that the first slot is the variable
    				buffer.append(' ');
    				encodeAndAppend((AbsPredicate)val.getAbsObject(slotNames[1]));
    			} catch (RuntimeException e) {
    				throw new CodecException("A Quantifier requires a variable and a formula arguments",e);
    			}
    		} else if (slOnto.isModalOp(propositionSymbol)) {
    			// Modal operator of the SL language (B, I, U, PG)
    			buffer.append(propositionSymbol);
    			buffer.append(' ');
    			try {
    				encodeAndAppend((AbsTerm)val.getAbsObject(slotNames[0]));
    				buffer.append(' ');
    				encodeAndAppend((AbsPredicate)val.getAbsObject(slotNames[1]));
    			} catch (RuntimeException e) {
    				throw new CodecException("A ModalOp requires a term and a formula arguments",e);
    			}
    		} else if (slOnto.isActionOp(propositionSymbol)) {
    			// Action operator of the SL language (DONE, FEASIBLE)
    			buffer.append(propositionSymbol);
    			buffer.append(' ');
    			try {
    				encodeAndAppend((AbsTerm)val.getAbsObject(slotNames[0])); //FIXME check it is an action expression
    				AbsPredicate ap = (AbsPredicate)val.getAbsObject(slotNames[1]);
    				if (ap != null) { // Second argument is optional
    					buffer.append(' ');
    					encodeAndAppend(ap);
    				}
    			} catch (RuntimeException e) {
    				throw new CodecException("An ActionOp requires an actionexpression and (optionally) a formula arguments",e);
    			}
    		} else if (slOnto.isBinaryTermOp(propositionSymbol)) {
    			// Binary term operator of the SL language (RESULT, =)
    			buffer.append(propositionSymbol);
    			buffer.append(' ');
    			try {
    				encodeAndAppend((AbsTerm)val.getAbsObject(slotNames[0]));
    				buffer.append(' ');
    				encodeAndAppend((AbsTerm)val.getAbsObject(slotNames[1]));
    			} catch (RuntimeException e) {
    				throw new CodecException("A BinaryTermOp requires 2 term arguments",e);
    			}
    		} else {
    			encodeAndAppend(propositionSymbol);
    			// Predicate in the ontology
    			try {
    				encodeSlotsByOrder(val, slotNames);
    			} catch (RuntimeException e) {
    				throw new CodecException("SL allows predicates with term arguments only",e);
    			}
    		}
    		buffer.append(')');
    	} else
    		// Proposition
    		encodeAndAppend(propositionSymbol);  
    }

    private void encodeAndAppend(AbsIRE val) throws CodecException {
    	buffer.append('(');
      encodeAndAppend(val.getTypeName());
      buffer.append(' ');
      encodeAndAppend(val.getTerm());
      buffer.append(' ');
      encodeAndAppend(val.getProposition());
      buffer.append(')'); 
    }
 
    private void encodeAndAppend(AbsVariable val) throws CodecException {
    	String var = val.getName();
    	if (var.charAt(0) == '?') {
    		encodeAndAppend(var);
    	} else {
					buffer.append('?');
					encodeAndAppend(var);
			}
    }
    
    private void encodeAndAppend(AbsConcept val) throws CodecException {
    	String functionSymbol = val.getTypeName();
    	buffer.append('(');
    	String[] slotNames = getSlotNames(val);
    	if (slOnto.isSLFunctionWithoutSlotNames(functionSymbol)) { 
    		// A Functional operator of the SL language (ACTION, + ...)
    		// The form is: functionSymbol Term*
    		buffer.append(functionSymbol);
    		try {
    			encodeSlotsByOrder(val, slotNames);
    		} catch (RuntimeException e) {
    			throw new CodecException("A FunctionalOperator requires 1 or 2 Term arguments",e);
    		}
    	} else { 
    		// A generic term in the ontology. The form can be both 
    		// functionSymbol Parameter* or functionSymbol Term*. Get the 
    		// preferred way from the ontology.
    		encodeAndAppend(functionSymbol);
    		try {
    			// FIXME: To improve performances the two operations that imply
    			// retrieving a schema from the ontology (getting slot names and
    			// getting the preferred encoding type) should be carried out at 
    			// the same time.
    			if (getEncodingByOrder(val)) {
    				encodeSlotsByOrder(val, slotNames);
    			}
    			else {
    				encodeSlotsByName(val, slotNames);
    			}
    		} catch (RuntimeException e) {
    			throw new CodecException("A FunctionalTerm requires Terms arguments",e);
    		}
    	}
    	
    	buffer.append(')');
    }


    private void encodeAndAppend(AbsAggregate val) throws CodecException {
    	buffer.append('(');
			encodeAndAppend(val.getTypeName());
			for (Iterator i=val.iterator(); i.hasNext(); ) {
				buffer.append(' ');
				encodeAndAppend((AbsObject)i.next());
			}
	  	buffer.append(')');
    }


    private void encodeAndAppend(AbsPrimitive val) throws CodecException {
    	Object v = val.getObject();
    	if (v instanceof Date)
    		buffer.append(ISO8601.toString((Date)v));
    	else if (v instanceof Number) 

⌨️ 快捷键说明

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