<commit_before><commit_msg>Added ontology for Serializable objects<commit_after>/**
 * ***************************************************************
 * 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.onto;
//#MIDP_EXCLUDE_FILE

import jade.content.abs.AbsObject;
import jade.content.abs.AbsPrimitive;
import jade.content.abs.AbsConcept;
import jade.content.schema.ObjectSchema;
import jade.content.schema.PrimitiveSchema;
import jade.content.schema.ConceptSchema;

import java.io.*;

/**
   This ontology allows dealing with Java Serializable objects as
   if they were instances of ontological elements.
   <br>
   <b>NOT available in MIDP</b>
   <br>
   @author <NAME> - TILAB
 */
public class SerializableOntology extends Ontology {
  // The singleton instance of this ontology
  private static final SerializableOntology theInstance = new SerializableOntology();
  
  private static final String SERIALIZABLE = "serializable";
  private static final String SERIALIZABLE_VALUE = "value";
  
  /**
   * Returns the singleton instance of the <code>SerializableOntology</code>.
   * @return the singleton instance of the <code>SerializableOntology</code>
   */
  public static Ontology getInstance() {
    return theInstance;
  }
  
  /**
     Construct a SerializableOntology object
   */
  private SerializableOntology() {
    super("Serializable-ontology", (Ontology) null, null);
  	try {
	    // Add the primitive schema for binary data
	    PrimitiveSchema byteSeqSchema = (PrimitiveSchema) BasicOntology.getInstance().getSchema(BasicOntology.BYTE_SEQUENCE);
	    add(byteSeqSchema);
	    // Add the schema for a generic Serializable object
	    ConceptSchema serializableSchema = new ConceptSchema(SERIALIZABLE);
	    serializableSchema.add(SERIALIZABLE_VALUE, byteSeqSchema);
	    add(serializableSchema);
  	}
  	catch (Exception e) {
  		// Should never happen
  		e.printStackTrace();
  	}
  }
            
	//#APIDOC_EXCLUDE_BEGIN
  /**
   */
  protected Object toObject(AbsObject abs, String lcType, Ontology globalOnto)
    			throws UnknownSchemaException, UngroundedException, OntologyException {
		if (SERIALIZABLE.equals(abs.getTypeName())) {
			try {
				AbsPrimitive absValue = (AbsPrimitive) abs.getAbsObject(SERIALIZABLE_VALUE);
				byte[] value = absValue.getByteSequence();
	      ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(value));
	      return in.readObject();
			}
			catch (Throwable t) {
				throw new OntologyException("Error in object deserialization.", t);
			}
		}
		else {
			throw new OntologyException("Abs-object "+abs+" is not serializable");
		}
	}
  	
  /**
   */
  protected AbsObject fromObject(Object obj, Ontology globalOnto) 
  			throws UnknownSchemaException, OntologyException {
  				
    // If obj is already an abstract descriptor --> just return it
  	if (obj instanceof AbsObject) {
  		return (AbsObject) obj;
  	}
  	
  	if (obj instanceof Serializable) {
  		try {
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
    		ObjectOutputStream out = new ObjectOutputStream(baos);
    		out.writeObject(obj);
    		AbsConcept absSerializable = new AbsConcept(SERIALIZABLE);
    		absSerializable.set(SERIALIZABLE_VALUE, baos.toByteArray());
    		return absSerializable;
  		}
			catch (Throwable t) {
				throw new OntologyException("Error in object deserialization.", t);
			}
		}
		else {
			throw new OntologyException("Object "+obj+" is not serializable");
		}
  } 
	//#APIDOC_EXCLUDE_END
}
{'fraction_non_alphanumeric': 0.11133648861843588, 'fraction_numerical': 0.00450755014649538, 'mean_word_length': 3.8134490238611716, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4547486', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1245, 'n_words': 393}