using System; using System.Collections.Generic; using System.Linq; namespace Moserware.Skills.FactorGraphs { public abstract class FactorGraphLayerBase { public abstract IEnumerable> UntypedFactors { get; } public abstract void BuildLayer(); public virtual Schedule CreatePriorSchedule() { return null; } public virtual Schedule CreatePosteriorSchedule() { return null; } // HACK public abstract void SetRawInputVariablesGroups(object value); public abstract object GetRawOutputVariablesGroups(); } public abstract class FactorGraphLayer : FactorGraphLayerBase where TParentGraph : FactorGraph where TBaseVariable : Variable where TInputVariable : TBaseVariable where TFactor : Factor where TOutputVariable : TBaseVariable { private readonly List _LocalFactors = new List(); private readonly List> _OutputVariablesGroups = new List>(); private IList> _InputVariablesGroups = new List>(); protected FactorGraphLayer(TParentGraph parentGraph) { ParentFactorGraph = parentGraph; } protected IList> InputVariablesGroups { get { return _InputVariablesGroups; } } // HACK public TParentGraph ParentFactorGraph { get; private set; } public IList> OutputVariablesGroups { get { return _OutputVariablesGroups; } } public IList LocalFactors { get { return _LocalFactors; } } public override IEnumerable> UntypedFactors { get { return _LocalFactors.Cast>(); } } public override void SetRawInputVariablesGroups(object value) { var newList = value as IList>; if (newList == null) { // TODO: message throw new ArgumentException(); } _InputVariablesGroups = newList; } public override object GetRawOutputVariablesGroups() { return _OutputVariablesGroups; } protected Schedule ScheduleSequence( IEnumerable itemsToSequence, string nameFormat, params object[] args) where TSchedule : Schedule { string formattedName = String.Format(nameFormat, args); return new ScheduleSequence(formattedName, itemsToSequence); } protected void AddLayerFactor(TFactor factor) { _LocalFactors.Add(factor); } // Helper utility protected double Square(double x) { return x*x; } } }