/*********************************************************************/
/* */
/* 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;
using AgentService.Platform.AMS;
using System.Collections.Generic;
namespace AgentService.Platform.DF
{
///
/// Class ServiceDescription. Holds the description of a service.
///
[Serializable()]
[DataContract]
public class ServiceDescription
{
public static readonly int AllSubTypes = 0;
# region Members
///
/// cached name used for the the
/// ToString() method.
///
protected string cachedName;
///
/// name of the service
///
private string name;
///
/// Gets the name of the service
///
[DataMember]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
///
/// type of the service
///
private string type;
///
/// Gets the service type
///
[DataMember]
public string Type
{
get
{
return this.type;
}
set
{
this.type = value;
}
}
///
/// service subtype
///
private int subType;
///
/// Gets the service subtype
///
[DataMember]
public int SubType
{
get
{
return this.subType;
}
set
{
this.subType = value;
}
}
///
/// owner of the service
///
private AID owner;
///
/// Gets the owner of the service
///
[DataMember]
public AID Owner
{
get
{
return this.owner;
}
set
{
this.owner = value;
}
}
///
/// list of interaction protocols supported by the service
///
private List protocols;
///
/// Gets the collection of protocols supported by the service
///
[DataMember]
public List Protocols
{
get
{
return this.protocols;
}
set
{
this.protocols = value;
}
}
///
/// list of ontologies supported by the service
///
private AgentService.Ontology.OntologyTypeDescriptorCollection ontologies;
///
/// Gets the list of ontologies supported by the srrvice
///
[DataMember]
public AgentService.Ontology.OntologyTypeDescriptorCollection Ontologies
{
get
{
return this.ontologies;
}
set
{
this.ontologies = value;
}
}
///
/// list of content languages supported by the service
///
private List languages;
///
/// Gets the list of content languages supported by the service
///
[DataMember]
public List Languages
{
get
{
return this.languages;
}
set
{
this.languages = value;
}
}
///
/// list of properties that discriminate the service
///
private List properties;
///
/// Gets the properties that discriminate the service.
///
[DataMember]
public List Properties
{
get
{
return this.properties;
}
set
{
this.properties = value;
}
}
///
/// Flag indicating if the service is published by the platform.
/// A service published by the platform is known also outside the
/// platform.
///
private bool bPublish;
///
/// Gets the value of the publishing flag
///
[DataMember]
public bool AllowPublishing
{
get
{
return this.bPublish;
}
set
{
this.bPublish = value;
}
}
# endregion
# region Constructor
public ServiceDescription() { }
///
/// Constructs a ServiceDescription object
///
/// name of the service
/// type of the service
/// flags indicating if the service allows publishing
/// subtype of the service
public ServiceDescription(string name, string type, int subtype, bool bPublish)
{
if (name == null)
throw new ArgumentNullException("name", "service name cannot be null");
if (type == null)
throw new ArgumentNullException("type", "service type cannot be null");
if (owner == null)
throw new ArgumentNullException("owner", "service owner cannot be null");
this.name = name;
this.type = type;
this.subType = subtype;
this.protocols = new List();
this.ontologies = new AgentService.Ontology.OntologyTypeDescriptorCollection();
this.languages = new List();
this.properties = new List(); ;
this.bPublish = bPublish;
}
///
/// Constructs a ServiceDescription object with the needed information
///
/// Service name
/// Service type
/// Name of the agent tha provide the service
public ServiceDescription(string name, string type)
{
if (name == null)
throw new ArgumentNullException("name", "service name cannot be null");
if (type == null)
throw new ArgumentNullException("type", "service type cannot be null");
this.name = name;
this.type = type;
this.subType = ServiceDescription.AllSubTypes;
this.protocols = new List();
this.ontologies = new AgentService.Ontology.OntologyTypeDescriptorCollection();
this.languages = new List();
this.properties = new List();
this.bPublish = true;
}
///
/// Creates a cache copy of the full name for the service description.
///
protected void makeCachedName()
{
this.cachedName = string.Format("{0}#{1}#{2}", this.name, this.type, this.subType);
}
///
/// Returns true if the given service description matcvhes the
/// current ServiceDescription, by following the comparison criteria
///
/// service descirption candidate
/// flags that describe how to perform the search
/// true if the candidate matches the current instance with the given flags
public bool Match(ServiceDescription sd, ServiceComparison flags)
{
string lowerName = this.name.ToLower();
string lowerNameOther = sd.name.ToLower();
bool bMatchNames = (lowerName.CompareTo(lowerNameOther) == 0);
if (flags == (ServiceComparison.ByName | ServiceComparison.ByType | ServiceComparison.BySubType))
{
if ((this.type != null) && (sd.type != null))
{
bool bMatchTypes = (this.type.ToLower().CompareTo(sd.type.ToLower()) == 0);
bool bMatchSubTypes = (this.subType == sd.subType);
return (bMatchNames && bMatchTypes && bMatchSubTypes);
}
if ((this.type == null) && (sd.type == null))
{
// if we have null types, we do not
// check further for sub types
return bMatchNames;
}
return false;
}
if (flags == (ServiceComparison.ByName | ServiceComparison.ByType))
{
if ((this.type != null) && (sd.type != null))
{
bool bMatchTypes = (this.type.ToLower().CompareTo(sd.type.ToLower()) == 0);
return (bMatchNames && bMatchTypes);
}
if ((this.type == null) && (sd.type == null))
{
return bMatchNames;
}
return false;
}
if (flags == ServiceComparison.ByName)
{
return bMatchNames;
}
return false;
}
///
/// Returns true if two ServiceDescription objects are equal. The Equals
/// method compares two services by comparing the names of the two services.
/// String comparison is case insensitive.
///
/// the other ServiceDescription object
/// boolean value indicating equality
public override bool Equals(object other)
{
ServiceDescription sd = other as ServiceDescription;
if (sd == null)
return false;
return (this.name.ToLower().CompareTo(sd.name.ToLower()) == 0);
}
///
/// Gets a string representation of the
/// ServiceDescription instance.
///
/// string representation
public override string ToString()
{
return this.cachedName;
}
///
/// Gets the hash code of the service description.
/// The hash code is computed by using the string
/// returned from the ToString() method.
///
/// hash code of the Service Description
public override int GetHashCode()
{
return this.cachedName.GetHashCode();
}
# endregion
# region Methods
///
/// Sets the agent that owns the service.
///
/// agent identifier of the owner
internal void SetOwner(AID aid)
{
this.owner = aid;
}
# endregion
}
}