using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using CategoryTheory;
using DiagramUI;
using BaseTypes;
using FormulaEditor;
using DataPerformer.Interfaces;
namespace DataPerformer
{
/// <summary>
/// Object which assemblies variables to matrix
/// </summary>
[Serializable()]
public class MatrixAssembly : CategoryObject, ISerializable, IDataConsumer, IMeasure, IPostSetArrow, IMeasurements
{
#region Fields
/// <summary>
/// Names of scalars
/// </summary>
protected string[,] names;
/// <summary>
/// Measures of scalars
/// </summary>
protected IMeasure[,] measures;
/// <summary>
/// All measurements
/// </summary>
protected IList<IMeasurements> measurements = new List<IMeasurements>();
/// <summary>
/// Return type
/// </summary>
protected ArrayReturnType type;
/// <summary>
/// The "is updated" sign
/// </summary>
protected bool isUpdated;
/// <summary>
/// Values
/// </summary>
protected double[,] val;
/// <summary>
/// Parameter
/// </summary>
protected Func<object> parameter;
const Double a = 0;
#endregion
#region Ctor
/// <summary>
/// Default constructor
/// </summary>
public MatrixAssembly()
{
parameter = GetValue;
}
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="info">Serialization info</param>
/// <param name="context">Streaming context</param>
protected MatrixAssembly(SerializationInfo info, StreamingContext context)
: this()
{
try
{
names = info.GetValue("Names", typeof(string[,])) as string[,];
}
catch (Exception ex)
{
ex.Log();
}
}
#endregion
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
if (names == null)
{
return;
}
info.AddValue("Names", names, typeof(string[,]));
}
#endregion
#region IPostSetArrow Members
void IPostSetArrow.PostSetArrow()
{
Post();
}
#endregion
#region IDataConsumer Members
void IDataConsumer.Add(IMeasurements measurements)
{
this.measurements.Add(measurements);
}
void IDataConsumer.Remove(IMeasurements measurements)
{
this.measurements.Remove(measurements); ;
}
void IDataConsumer.UpdateChildrenData()
{
foreach (IMeasurements m in measurements)
{
m.UpdateMeasurements();
}
}
int IDataConsumer.Count
{
get { return measurements.Count; }
}
IMeasurements IDataConsumer.this[int n]
{
get { return measurements[n]; }
}
void IDataConsumer.Reset()
{
this.FullReset();
}
#endregion
#region IMeasure Members
Func<object> IMeasure.Parameter
{
get { return parameter; }
}
string IMeasure.Name
{
get { return "Matrix"; }
}
object IMeasure.Type
{
get { return type; }
}
#endregion
#region IMeasurements Members
int IMeasurements.Count
{
get { return 1; }
}
IMeasure IMeasurements.this[int n]
{
get { return this; }
}
void IMeasurements.UpdateMeasurements()
{
if (isUpdated)
{
return;
}
IDataConsumer c = this;
c.UpdateChildrenData();
for (int i = 0; i < val.GetLength(0); i++)
{
for (int j = 0; j < val.GetLength(1); j++)
{
val[i, j] = (double)measures[i, j].Parameter();
}
}
isUpdated = true;
}
bool IMeasurements.IsUpdated
{
get
{
return isUpdated;
}
set
{
isUpdated = value;
}
}
#endregion
#region Specific Members
/// <summary>
/// Names of scalar variables
/// </summary>
public string[,] Names
{
get
{
return names;
}
set
{
names = value;
Post();
}
}
/// <summary>
/// Gets value
/// </summary>
/// <returns>Value</returns>
protected object GetValue()
{
return val;
}
private void Post()
{
if (names == null)
{
return;
}
measures = new IMeasure[names.GetLength(0), names.GetLength(1)];
for (int i = 0; i < names.GetLength(0); i++)
{
for (int j = 0; j < names.GetLength(1); j++)
{
measures[i, j] = this.FindMeasure(names[i, j], false);
}
}
val = new double[measures.GetLength(0), measures.GetLength(1)];
type = new ArrayReturnType(a, new int[] { measures.GetLength(0), measures.GetLength(1) }, false);
}
#endregion
}
}