using System;
using System.Diagnostics;
using System.Collections;
using System.Xml;
using System.Windows.Forms;
using System.ComponentModel;
using AnticipatingMinds.CommonUIControls;
namespace AnticipatingMinds.Genesis.KnowledgeManagement{
/// <summary>
/// Represents a base class for rule templates.
/// </summary>
/// <remarks>
/// RuleTemplate defines an interface and a set of basic functionality for rule template.
/// Rule template can be viewed as rule factory. It is instantiated dynamicly and than is requested
/// to create rules by providing rule properties. If the same template creates more than one type of rules
/// template can add that specifies rule type to the rule properties and than read it durring rule
/// instantiation.
/// </remarks>
public abstract class RuleTemplate
{
/// <summary>
/// Defines string keys for property names in IDictionary
/// </summary>
public class PropertyName
{
public const string Name = "Name";
public const string Description = "Description";
public const string HelpUrl = "HelpUrl";
}
/// <summary>
/// Represents a handler for event raised when one of the template properties has changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Instantiates a new instance of the <see cref="RuleTemplate"/> class.
/// </summary>
/// <param name="templateId">Template id</param>
/// <param name="templateProperties">Template properties.</param>
protected RuleTemplate(string templateId, IDictionary templateProperties)
{
this.templateId = templateId;
properties = new Hashtable(templateProperties);
}
/// <summary>
/// Gets template id.
/// </summary>
public string Id
{
get
{
return templateId;
}
}
/// <summary>
/// Gets template name.
/// </summary>
/// <remarks>
/// Template name is read from properties dictionary. Property name is 'Name'
/// </remarks>
public virtual string Name
{
get
{
return GetProperty(PropertyName.Name) as String;
}
}
/// <summary>
/// Gets template description.
/// </summary>
/// <remarks>
/// Template description is read from properties dictionary. Property name is 'Description'
/// </remarks>
public virtual string Description
{
get
{
return GetProperty(PropertyName.Description) as String;
}
}
/// <summary>
/// Gets or sets template hel url.
/// </summary>
/// <remarks>
/// Template help url is read from properties dictionary. Property name is 'HelpUrl'
/// </remarks>
public virtual string HelpUrl
{
get
{
return GetProperty(PropertyName.HelpUrl) as String;
}
set
{
SetProperty(PropertyName.HelpUrl,value == null ? String.Empty:value);
}
}
/// <summary>
/// Sets property value.
/// </summary>
/// <param name="propertyName">Property name.</param>
/// <param name="value">Property value.</param>
protected void SetProperty(string propertyName, object value)
{
if(properties.Contains(propertyName))
if(properties[propertyName].Equals(value))
return;
properties[propertyName] = value;
OnPropertyChanged(propertyName);
}
/// <summary>
/// Gets property value.
/// </summary>
/// <param name="propertyName">Property name.</param>
/// <returns>Property value if property is found, null otherwise.</returns>
protected object GetProperty(string propertyName)
{
if(properties.Contains(propertyName))
return properties[propertyName];
else
return null;
}
/// <summary>
/// create a rule with property values as found in ruleProperties.
/// </summary>
/// <param name="ruleId">An id of new rule.</param>
/// <param name="ruleProperties">Properties of the rule to create.</param>
/// <returns>Returns an instance of the created rule.</returns>
public abstract Rule CreateRule(string ruleId, IDictionary ruleProperties);
/// <summary>
/// Return current state of passed rule as rule XML.
/// </summary>
/// <param name="rule"></param>
/// <returns>Return an xml elemtn that contains all of the rule properties including name and id</returns>
public virtual IDictionary GetRuleProperties(Rule rule)
{
return rule.GetProperties();
}
/// <summary>
/// Sets rule properties from properties dictionary.
/// </summary>
/// <param name="rule">Rule whose properties shall be set.</param>
/// <param name="ruleProperties">New set of rule properties.</param>
public virtual void SetRuleProperties(Rule rule,IDictionary ruleProperties)
{
rule.SetProperties(ruleProperties);
}
/// <summary>
/// Get appropriate rule property pages pre-populated from rule properties
/// </summary>
/// <param name="ruleProperties"></param>
/// <returns></returns>
public virtual PropertyPage[] GetRulePropertiesUI(IDictionary ruleProperties)
{
return noPropertyPages;
}
/// <summary>
/// Get Current state of template, returned as xml
/// </summary>
/// <returns></returns>
public virtual IDictionary GetTemplateProperties()
{
return properties;
}
/// <summary>
/// Set current state of template from xml. This method is called as soon
/// as the template is instantiated (as templates are loaded from Knowledge Base).
/// </summary>
/// <param name="templateProperties"></param>
public virtual void SetTemplateProperties(IDictionary templateProperties)
{
foreach(string key in templateProperties.Keys)
SetProperty(key,templateProperties[key]);
}
/// <summary>
/// Gets a UI for changing template properties.
/// </summary>
/// <returns>Returns an array of pages to display.</returns>
public virtual PropertyPage[] GetTemplatePropertiesUI()
{
return noPropertyPages;
}
/// <summary>
/// This function raises <see cref="PropertyChanged"/> event.
/// </summary>
/// <param name="propertyName">Name of the changed property.</param>
/// <remarks>Do not call this function directly, use
/// <see cref="SetProperty"/> function to set property value and raise this event.
/// </remarks>
protected virtual void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
private Hashtable properties;
private string templateId;
private static PropertyPage[] noPropertyPages = new PropertyPage[0];
}
}
|