|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using CategoryTheory;
using DiagramUI;
using DiagramUI.Labels;
using DiagramUI.Attributes;
using DiagramUI.Interfaces;
using DataPerformer.Interfaces;
namespace DataPerformer.Arrows
{
/// <summary>
/// Data link
/// </summary>
/// <summary>
/// The link with data provider
/// </summary>
[Serializable()]
public class DataLink : CategoryArrow, ISerializable,
IRemovableObject, IDataLinkFactory
{
#region Fields
/// <summary>
/// Error message
/// </summary>
public static readonly string SetProviderBefore = "You should create measurements source before consumer";
/// <summary>
/// DataLink checker
/// </summary>
private static Action<DataLink> checker;
/// <summary>
/// The source of this arrow
/// </summary>
private IDataConsumer target;
/// <summary>
/// The target of this arrow
/// </summary>
private IMeasurements source;
/// <summary>
/// Linked object
/// </summary>
protected object obj;
/// <summary>
/// Data link factory
/// </summary>
private static IDataLinkFactory dataLinkFactory = new DataLink();
#endregion
#region Ctor
/// <summary>
/// Default constructor
/// </summary>
public DataLink()
{
}
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="info">Serialization info</param>
/// <param name="context">Streaming context</param>
private DataLink(SerializationInfo info, StreamingContext context)
{
}
#endregion
#region Overriden Members
/// <summary>
/// The source of this arrow
/// </summary>
public override ICategoryObject Source
{
get
{
return source as ICategoryObject;
}
set
{
if (source != null)
{
throw new Exception();
}
IDataLinkFactory f = this;
source = f.GetMeasurements(value);
}
}
/// <summary>
/// The target of this arrow
/// </summary>
public override ICategoryObject Target
{
get
{
return target as ICategoryObject;
}
set
{
if (target != null)
{
throw new Exception();
}
IDataLinkFactory f = this;
IDataConsumer t = f.GetConsumer(value);
bool check = true;
/*if ((asa != null) & (ata != null))
{
if ((asa.Length > 0) | (ata.Length > 0))
{
check = false;
}
}*/
IAssociatedObject s = source as IAssociatedObject;
if (s.Object != null & value.Object != null)
{
if (check)
{
INamedComponent ns = s.Object as INamedComponent;
INamedComponent nt = value.Object as INamedComponent;
if (nt != null & ns != null)
{
if (PureDesktopPeer.GetDifference(ns, nt) >= 0)
{
throw new Exception(SetProviderBefore);
}
}
}
target = t;
target.Add(source);
}
if (!check)
{
return;
}
try
{
if (checker != null)
{
checker(this);
}
}
catch (Exception e)
{
e.ShowError(10);
target.Remove(source);
throw e;
}
}
}
#endregion
#region IRemovableObject Members
/// <summary>
/// The post remove operation
/// </summary>
public void RemoveObject()
{
if (source == null | target == null)
{
return;
}
target.Remove(source);
}
#endregion
#region IDataLinkFactory Members
IDataConsumer IDataLinkFactory.GetConsumer(ICategoryObject source)
{
IAssociatedObject ao = source;
object o = ao.Object;
if (o is INamedComponent)
{
IDataConsumer dcl = null;
INamedComponent comp = o as INamedComponent;
IDesktop desktop = comp.Root.Desktop;
desktop.ForEach<DataLink>(delegate(DataLink dl)
{
if (dcl != null)
{
return;
}
object dt = dl.Source;
if (dt is IAssociatedObject)
{
IAssociatedObject aot = dt as IAssociatedObject;
if (aot.Object == o)
{
dcl = dl.source as IDataConsumer;
}
}
});
if (dcl != null)
{
return dcl;
}
}
IDataConsumer dc = DataConsumerWrapper.Create(source);
if (dc == null)
{
CategoryException.ThrowIllegalTargetException();
}
return dc;
}
IMeasurements IDataLinkFactory.GetMeasurements(ICategoryObject target)
{
IAssociatedObject ao = target;
object o = ao.Object;
if (o is INamedComponent)
{
IMeasurements ml = null;
INamedComponent comp = o as INamedComponent;
IDesktop d = null;
INamedComponent r = comp.Root;
if (r != null)
{
d = r.Desktop;
}
else
{
d = comp.Desktop;
}
if (d != null)
{
d.ForEach<DataLink>(delegate(DataLink dl)
{
if (ml != null)
{
return;
}
object dt = dl.Target;
if (dt is IAssociatedObject)
{
IAssociatedObject aot = dt as IAssociatedObject;
if (aot.Object == o)
{
ml = dl.Target as IMeasurements;
}
}
});
if (ml != null)
{
return ml;
}
}
}
IMeasurements m = MeasurementsWrapper.Create(target);
if (m == null)
{
CategoryException.ThrowIllegalTargetException();
}
return m;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:
1) Noncommutative geometry
http://front.math.ucdavis.edu/author/P.Ivankov
2) Literary work (Russian only)
http://zhurnal.lib.ru/editors/3/3d_m/
3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1