/*********************************************************************/
/* */
/* AgentService Framework */
/* www.agentservice.it */
/* */
/* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */
/* 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. */
/* */
/*********************************************************************/
using System;
using System.Runtime.Serialization;
namespace AgentService.Platform.Modules
{
///
/// Class ObjectDescription. It gives a description of the types
/// needed to instantiate an Object object
///
[Serializable]
[DataContract]
public class TypeDescriptor : ICloneable
{
///
/// Object full type name
///
private string fullTypeName;
///
/// Get object full type name
///
[DataMember]
public string FullTypeName
{
get
{
return this.fullTypeName;
}
set
{
this.fullTypeName = value;
}
}
///
/// Object full base type name
///
private string baseTypeName;
///
/// Get object full base type name
///
[DataMember]
public string BaseTypeName
{
get
{
return this.baseTypeName;
}
set
{
this.baseTypeName = value;
}
}
///
/// Name of the assembly containing the object definition
///
private string assemblyName;
///
/// Get the name of the assembly containing the object definition
///
[DataMember]
public string AssemblyName
{
get
{
return this.assemblyName;
}
set
{
this.assemblyName = value;
}
}
///
/// Stores the public key token for
/// fast comparisons.
///
private ulong fastPublicKeyToken;
///
/// Gets the public key token of the
/// assembly containing the type.
///
[DataMember]
public ulong PublicKeyToken
{
get
{
return this.fastPublicKeyToken;
}
set
{
this.fastPublicKeyToken = value;
}
}
///
/// Public key token of the assembly containing the object definition
///
// private byte[] publicKeyToken;
// ///
// /// Get the public key token of the assembly containing the object definition
// ///
// public byte[] PublicKeyToken
// {
// get
// {
// return this.publicKeyToken;
// }
// }
///
/// Default constructor: needed for binary serialization
///
public TypeDescriptor()
{
// this.publicKeyToken = null;
this.fastPublicKeyToken = 0;
}
///
/// Constructor
///
/// full type name of the Object
/// full base type name of the Object
/// name of the assembly containing Object's definition
/// public key token of the assembly containing Object's definition
public TypeDescriptor(string fullTypeName, string assemblyName, byte[] pkt)
{
this.fullTypeName = fullTypeName;
this.assemblyName = assemblyName;
this.baseTypeName = null;
//this.publicKeyToken = pkt;
this.ComputeFastPKT(pkt);
}
///
/// Constructs a TypeDescriptor with the given parameters
///
/// full type name
/// assembly file name
/// pubic key token
public TypeDescriptor(string fullTypeName, string assemblyName, ulong publicKeyToken)
{
this.fullTypeName = fullTypeName;
this.assemblyName = assemblyName;
this.baseTypeName = null;
this.fastPublicKeyToken = publicKeyToken;
}
///
/// Constructs a TypeDescriptor object for the given type.
///
/// type object
public TypeDescriptor(Type type)
{
System.Reflection.AssemblyName name = type.Assembly.GetName();
this.fullTypeName = type.FullName;
this.baseTypeName = type.BaseType.FullName;
this.assemblyName = name.Name;
//this.publicKeyToken = name.GetPublicKeyToken();
this.ComputeFastPKT(name.GetPublicKeyToken());
}
///
/// Compares two TypeDescriptor objects for equality.
/// Two TypeDescriptor are equal if they have the same
/// full type name, the same assembly name, and the
/// same public key token
///
/// type descriptor instance
/// true if the two instances have the same properties
public override bool Equals(object obj)
{
TypeDescriptor td = obj as TypeDescriptor;
if (td == null)
{
return false;
}
return ((td.fullTypeName.CompareTo(this.fullTypeName) == 0) &&
(td.baseTypeName.CompareTo(this.baseTypeName) == 0) &&
(td.assemblyName.CompareTo(this.assemblyName) == 0) &&
(td.fastPublicKeyToken == this.fastPublicKeyToken));
}
///
/// Returns the hash code for this instance.
///
///A 32-bit signed integer hash code.
public override int GetHashCode()
{
return (this.fullTypeName.GetHashCode() *
this.BaseTypeName.GetHashCode() *
this.assemblyName.GetHashCode() *
this.fastPublicKeyToken.GetHashCode());
}
///
/// Copies the public key token and puts
/// it into unsigned long variable. The
/// layout of the key is the following:
/// pkt[0] = msb, pkt[7] = lsb.
///
private void ComputeFastPKT(byte[] publicKeyToken)
{
if ((publicKeyToken == null) || (publicKeyToken.Length == 0))
{
this.fastPublicKeyToken = 0;
}
else
{
for (int ndx = 7; ndx >= 0; ndx--)
{
ulong shift = (ulong)(ndx * 8);
this.fastPublicKeyToken |= (ulong)publicKeyToken[ndx] * shift;
}
}
}
#region ICloneable Members
///
/// Clones a TypeDescriptor instance.
///
/// cloned instance
public object Clone()
{
TypeDescriptor td = new TypeDescriptor();
td.fastPublicKeyToken = this.fastPublicKeyToken;
td.fullTypeName = this.fullTypeName;
td.baseTypeName = this.baseTypeName;
td.assemblyName = this.assemblyName;
return td;
}
#endregion
}
///
/// Class FieldDescriptor. Describes the information
/// concerning a field of a class. It merely stores the
/// type and the name of a field.
///
[Serializable]
[DataContract]
public class FieldDescriptor
{
///
/// field name
///
private string name;
///
/// gets the field name
///
[DataMember]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
///
/// Indicates the access type of the field
///
[Flags]
public enum AccessType
{
ReadOnly = 1,
WriteOnly = 2,
ReadWrite = ReadOnly | WriteOnly
}
private AccessType accessType;
[DataMember]
public AccessType Access
{
get
{
return this.accessType;
}
set
{
this.accessType = value;
}
}
///
/// description of the type of the field.
///
private TypeDescriptor type;
///
/// Gets the descriptor object of the
/// type of the field.
///
[DataMember]
public TypeDescriptor Type
{
get
{
return this.type;
}
set
{
this.type = value;
}
}
public FieldDescriptor() { }
///
/// Constructs a FieldDescriptor of a field.
///
/// field name
/// descriptor object of the the type of the field
public FieldDescriptor(string name, TypeDescriptor type, AccessType act)
{
if (name == null)
throw new ArgumentNullException("name", "field name cannot be null");
this.name = name;
if (type == null)
throw new ArgumentNullException("type", "type descriptor cannot be null");
this.type = type;
this.accessType = act;
}
///
/// Constructs a FieldDescriptor of a field.
///
/// field name
/// type object of the the type of the field
public FieldDescriptor(string name, Type type, AccessType act)
{
if (name == null)
throw new ArgumentNullException("name", "field name cannot be null");
this.name = name;
if (type == null)
throw new ArgumentNullException("type", "type information cannot be null");
this.type = new TypeDescriptor(type);
this.accessType = act;
}
///
/// Compares two field descriptors. A field descriptor
/// is equivalent to another if they have the same name
/// and the same type and the same access type.
///
/// object to compare to
/// true if the two fields have the same name
/// and the same type descriptor and the same access type
public override bool Equals(object obj)
{
FieldDescriptor field = obj as FieldDescriptor;
if (field != null)
{
return ((this.name == field.name) && (this.type == field.type) && (this.accessType == field.Access));
}
return false;
}
///
/// Returns the hash code for this instance.
///
///A 32-bit signed integer hash code.
public override int GetHashCode()
{
return (this.accessType.GetHashCode() *
this.name.GetHashCode() *
this.type.GetHashCode());
}
}
///
/// Class AgentTemplateDescriptor. Describes the structure
/// of an AgentTemplate derived class. It holds the information
/// about the knowledge and behaviour types used by this template.
///
[Serializable]
[DataContract]
public class AgentTemplateDescriptor : TypeDescriptor
{
///
/// array of knowledge types used by this template
///
private KnowledgeDescriptor[] knowledges;
///
/// Gets the array of the knowledge types used
/// by this template.
///
[DataMember]
public KnowledgeDescriptor[] Knowledges
{
get
{
return this.knowledges;
}
set
{
this.knowledges = value;
}
}
///
/// array of behaviour types used by this template
///
private BehaviourDescriptor[] behaviours;
///
/// Gets the array of behaviour types used by this
/// template.
///
[DataMember]
public BehaviourDescriptor[] Behaviours
{
get
{
return this.behaviours;
}
set
{
this.behaviours = value;
}
}
public AgentTemplateDescriptor() : base() { }
///
/// Constructs an AgentTemplateDescriptor instance.
///
/// template full type name
/// template defining assembly
/// array of knowledge types used by this template
/// array of behaviour types used by this template
public AgentTemplateDescriptor(string name, string assembly, string basename, KnowledgeDescriptor[] knowledges, BehaviourDescriptor[] behaviours)
: base(name, assembly, 0)
{
this.knowledges = (knowledges == null ? new KnowledgeDescriptor[0] : knowledges);
this.behaviours = (behaviours == null ? new BehaviourDescriptor[0] : behaviours);
}
///
/// Constructs an AgentTemplateDescriptor instance.
///
/// template type information
/// array of knowledge types used by this template
/// array of behaviour types used by this template
public AgentTemplateDescriptor(Type type, KnowledgeDescriptor[] knowledges, BehaviourDescriptor[] behaviours)
: base(type)
{
this.knowledges = (knowledges == null ? new KnowledgeDescriptor[0] : knowledges);
this.behaviours = (behaviours == null ? new BehaviourDescriptor[0] : behaviours);
}
///
/// Constructs an AgentTemplateDescriptor instance.
///
/// template type descriptor information
/// array of knowledge types used by this template
/// array of behaviour types used by this template
public AgentTemplateDescriptor(TypeDescriptor type, KnowledgeDescriptor[] knowledges, BehaviourDescriptor[] behaviours)
: base(type.FullTypeName, type.AssemblyName,type.PublicKeyToken)
{
this.knowledges = (knowledges == null ? new KnowledgeDescriptor[0] : knowledges);
this.behaviours = (behaviours == null ? new BehaviourDescriptor[0] : behaviours);
}
}
///
/// Class KnowledgeDescriptor. Holds information about
/// the fields of a knowledge instance.
///
[Serializable]
[DataContract]
public class KnowledgeDescriptor : TypeDescriptor
{
///
/// knowledge fields array
///
private FieldDescriptor[] items;
///
/// Gets the knowledge fields array.
///
[DataMember]
public FieldDescriptor[] Items
{
get
{
return this.items;
}
set
{
this.items = value;
}
}
public KnowledgeDescriptor() : base() { }
///
/// Constructs a KnowledgeDescriptor instance.
///
/// knowledge type full name
///
///
public KnowledgeDescriptor(string name, string assembly, FieldDescriptor[] items)
: base(name, assembly, 0)
{
if (items == null)
throw new ArgumentNullException("items", "knowledge field array cannot be null");
this.items = items;
}
///
/// Constructs a KnowledgeDescriptor instance.
///
/// knowledge type informatino
/// array of knowledge item descriptors
public KnowledgeDescriptor(Type type, FieldDescriptor[] items)
: base(type)
{
if (items == null)
throw new ArgumentNullException("items", "knowledge field array cannot be null");
this.items = items;
}
///
/// Constructs a KnowledgeDescriptor instance.
///
/// knowledge type descriptor information
/// array of knowledge item descriptors
public KnowledgeDescriptor(TypeDescriptor type, FieldDescriptor[] items)
: base(type.FullTypeName, type.AssemblyName, type.PublicKeyToken)
{
if (items == null)
throw new ArgumentNullException("items", "knowledge field array cannot be null");
this.items = items;
}
}
///
/// Class BehaviourDescriptor. Describes a particular behaviour type
/// and the knowledges it uses.
///
[Serializable]
[DataContract]
public class BehaviourDescriptor : TypeDescriptor
{
///
/// array of knowlege field references used
///
private FieldDescriptor[] knowledges;
///
/// Gets the array of the knowledge reference
/// fields used by this behaviour.
///
[DataMember]
public FieldDescriptor[] Knowledges
{
get
{
return this.knowledges;
}
set
{
this.knowledges = value;
}
}
public BehaviourDescriptor() : base() { }
///
/// Constructs a BehaviourDescriptor instance.
///
/// behaviour type full name
/// behaviour type defining assembly
/// array of knowledge reference fields used by this behaviour
public BehaviourDescriptor(string name, string assembly, FieldDescriptor[] knowledges)
: base(name, assembly, 0)
{
this.knowledges = (knowledges == null ? new FieldDescriptor[0] : knowledges);
}
///
/// Constructs a BehaviourDescriptor instance.
///
/// behaviour type information
/// array of knowledge reference fields used by this behaviour
public BehaviourDescriptor(Type type, FieldDescriptor[] knowledges)
: base(type)
{
this.knowledges = (knowledges == null ? new FieldDescriptor[0] : knowledges);
}
///
/// Constructs a BehaviourDescriptor instance.
///
/// behaviour type descriptor
/// array of knowledge reference fields used by this behaviour
public BehaviourDescriptor(TypeDescriptor type, FieldDescriptor[] knowledges)
: base(type.FullTypeName, type.AssemblyName, type.PublicKeyToken)
{
this.knowledges = (knowledges == null ? new FieldDescriptor[0] : knowledges);
}
}
}