Commits

Anonymous committed 9ed8a24

Work on CellType definition and mapping conventions.

  • Participants
  • Parent commits 8932701

Comments (0)

Files changed (10)

UserProjects/sprucely/CSharp/Composer/Composer/Common/TypeUtilities.cs

 {
 	public class TypeUtilities
 	{
+		public static IEnumerable<T> GetEnumValues<T>()
+			where T: struct
+		{
+			Type enumType = typeof(T);
+
+			// Can't use type constraints on value types, so have to do check like this
+			if (enumType.BaseType != typeof(Enum))
+				throw new ArgumentException("T must be of type System.Enum");
+
+			return Enum.GetValues(enumType) as IEnumerable<T>;
+		}
+
 		public static IEnumerable CreateDynamicallyTypedList(IEnumerable source)
 		{
 			Type type = GetCommonBaseClass(source);

UserProjects/sprucely/CSharp/Composer/Composer/Composer.csproj

     </Compile>
     <Compile Include="Common\Collections\ReadOnlyDictionary.cs" />
     <Compile Include="Common\TypeUtilities.cs" />
-    <Compile Include="Graph\Initialization\CellTypes.cs" />
     <Compile Include="UI\Behaviors\CommandRelay.cs" />
     <Compile Include="UI\Behaviors\DragAndDrop\DefaultDragHandler.cs" />
     <Compile Include="UI\Behaviors\DragAndDrop\DefaultDropHandler.cs" />

UserProjects/sprucely/CSharp/Composer/Composer/Graph/CellType.cs

 
 namespace Composer.Graph
 {
-	public class CellType
+	public enum CellType
 	{
-		public virtual int Id { get; set; }
+		Effector_Message = 0
+	}
 
-		public virtual string Name { get; set; }
-		public virtual string Description { get; set; }
-		public virtual CellCategory CellCategory { get; set; }
-
+	public class MappableCellType : MappableEnumBase
+	{
 	}
 }

UserProjects/sprucely/CSharp/Composer/Composer/Graph/CellVertex.cs

 		public virtual CellType CellType { get; set; }
 	}
 
+	public class Effector : CellVertex
+	{
+		public Effector()
+		{
+			ActivationCount = 1;
+		}
+
+		public virtual int ActivationCount { get; set; }
+	}
+
 }

UserProjects/sprucely/CSharp/Composer/Composer/Graph/Initialization/CellTypes.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Linq.Expressions;
-using Composer.Common;
-using System.Collections.Concurrent;
-using Composer.Common.Collections;
-
-namespace Composer.Graph.Initialization
-{
-	public class CellTypes
-	{
-		#region < Fields >
-
-		private static int _nextId = 0;
-		private static readonly ConcurrentDictionary<string, CellType> _mapNameToCellType
-			= new ConcurrentDictionary<string, CellType>();
-
-		public static readonly ReadOnlyDictionary<string, CellType> MapNameToCellType
-			= new ReadOnlyDictionary<string, CellType>(_mapNameToCellType);
-
-		public static readonly CellType Undefined = 
-			CreateCellType(
-				() => Undefined, 
-				(CellCategory)0, 
-				"Error: No cell type has been defined");
-		
-		public static readonly CellType Effector_Message = 
-			CreateEffectorType(
-				() => Effector_Message, 
-				"Passes a signal between components");
-
-		public static readonly CellType Effector_DoSomething =
-			CreateEffectorType(
-				() => Effector_DoSomething,
-				"Does something");
-
-		#endregion < Fields >
-
-
-		#region Methods
-
-		private static CellType CreateEffectorType(Expression<Func<CellType>> fieldExpression, string description)
-		{
-			return CreateCellType(fieldExpression, CellCategory.Effector, description);
-		}
-
-		private static CellType CreateSensorType(Expression<Func<CellType>> fieldExpression, string description)
-		{
-			return CreateCellType(fieldExpression, CellCategory.Sensor, description);
-		}
-
-		private static CellType CreateHybridType(Expression<Func<CellType>> fieldExpression, string description)
-		{
-			return CreateCellType(fieldExpression, CellCategory.Hybrid, description);
-		}
-
-		private static CellType CreateCellType(Expression<Func<CellType>> fieldExpression,
-			CellCategory cellCategory, string description)
-		{
-			var cellTypeName = TypeInfo.NameOf(fieldExpression);
-			return _mapNameToCellType.GetOrAdd(cellTypeName, new CellType()
-			{
-				CellCategory = cellCategory,
-				Id = _nextId++,
-				Name = cellTypeName,
-				Description = description
-			});
-		} 
-
-		#endregion
-	}
-}

UserProjects/sprucely/CSharp/Composer/Composer/Graph/SessionExtensions.cs

 {
 	public static class SessionExtensions
 	{
-		public static CellType GetCellType_Effector_Message(this ISession session)
-		{
-			return session.GetCellType(() => GetCellType_Effector_Message(session));
-		}
 
-		private static CellType GetCellType(this ISession session, Expression<Func<CellType>> funcNameExpr)
-		{
-			var cellTypeName = TypeInfo.NameOf(funcNameExpr).Substring(12);
-			return session.Query<CellType>()
-					.Where(x => x.Name == cellTypeName)
-					.FirstOrDefault();
-
-		}
 	}
 }

UserProjects/sprucely/CSharp/Composer/Composer/ORM/Mappings.cs

 		}
 	}
 
-
-	public class CellTypeMap : ClassMap<CellType>
+	public class CellTypeMap : EnumMapBase<MappableCellType>
 	{
 		public CellTypeMap()
 		{
-			Cache.ReadOnly();
 			Table("CellType");
-			Id(x => x.Id)
-				.GeneratedBy.Assigned();
-			Map(x => x.CellCategory)
-				.Column("Id_CellCategory")
-				.CustomType<int>()
-				.Not.Nullable();
-			Map(x => x.Name)
-				.Unique()
-				.Not.Nullable()
-				.CustomSqlType("VARCHAR(100)");
-			Map(x => x.Description)
-				.Unique()
-				.Not.Nullable()
-				.CustomSqlType("VARCHAR(200)");
 		}
 	}
 
-	public class CellVertexMap : SubclassMap<CellVertex>
+	public abstract class CellVertexMap<T> : SubclassMap<T>
+		where T : CellVertex
 	{
 		public CellVertexMap()
 		{
-			Table("Vertex_Cell");
 			KeyColumn("Id_Vertex");
 			DiscriminatorValue((int)VertexType.Cell);
 
-			References(x => x.CellType)
-				.Not.Nullable()
-				.Column("Id_CellType");
+			Map(x => x.CellType)
+				.Column("Id_CellType")
+				.CustomType<int>()
+				.Not.Nullable();
+		}
+	}
+
+	public class EffectorMap : CellVertexMap<Effector>
+	{
+		public EffectorMap()
+		{
+			Table("Vertex_Effector");
 		}
 	}
 

UserProjects/sprucely/CSharp/Composer/Composer/ORM/SessionHelper.cs

 
 using Composer.Common;
 using Composer.Graph;
-using Composer.Graph.Initialization;
 
 using FluentNHibernate.Cfg;
 using FluentNHibernate.Cfg.Db;
 		private static void InitializeDb(ISessionFactory sessionFactory)
 		{
 			InitLookups(sessionFactory);
-			InitCellTypes(sessionFactory);
 			InitComponents(sessionFactory);
 		}
 
 			//}
 		}
 
-		private static void InitCellTypes(ISessionFactory sessionFactory)
-		{
-			using (var session = sessionFactory.OpenSession())
-			using (var tx = session.BeginTransaction())
-			{
-				CellTypes.MapNameToCellType.Values
-					.Apply(x => session.Save(x));
-				tx.Commit();
-			}
-		}
-
-
-
 		private static void InitLookups(ISessionFactory sessionFactory)
 		{
 			using (var session = sessionFactory.OpenSession())
 			using (var tx = session.BeginTransaction())
 			{
-				foreach (VertexType vt in Enum.GetValues(typeof(VertexType)))
+				foreach (var vt in TypeUtilities.GetEnumValues<VertexType>())
 				{
 					session.Save(new MappableVertexType { Id = (int)vt, Name = vt.ToString() });
 				}
-				
-				foreach (EdgeType et in Enum.GetValues(typeof(EdgeType)))
+
+				foreach (var et in TypeUtilities.GetEnumValues<EdgeType>())
 				{
 					session.Save(new MappableEdgeType { Id = (int)et, Name = et.ToString() });
 				}
 
-				foreach (CellCategory et in Enum.GetValues(typeof(CellCategory)))
+				foreach (var cc in TypeUtilities.GetEnumValues<CellCategory>())
 				{
-					session.Save(new MappableCellCategory { Id = (int)et, Name = et.ToString() });
+					session.Save(new MappableCellCategory { Id = (int)cc, Name = cc.ToString() });
 				}
+
+				foreach (var ct in TypeUtilities.GetEnumValues<CellType>())
+				{
+					session.Save(new MappableCellType { Id = (int)ct, Name = ct.ToString() });
+				}
+	
 				tx.Commit();
 			}
 		}

UserProjects/sprucely/CSharp/Composer/Composer/UI/Screens/ObjectExplorer/ObjectExplorerViewModel.cs

 using Composer.UI.Screens.ObjectExplorer;
 
 using ReactiveUI;
-using Composer.Graph.Initialization;
+using Composer.Graph;
 
 namespace Composer.UI.Screens
 {
 			base.OnInitialize();
 
 			AvailableObjects.AddRange(
-				CellTypes.MapNameToCellType.Values
+				TypeUtilities.GetEnumValues<CellType>()
 				.Select(x => new CellTypeInfo(x)));
 		}
 

UserProjects/sprucely/CSharp/Composer/Composer/UI/Screens/ObjectExplorer/ObjectInfo.cs

 	public class CellTypeInfo : IObjectInfo
 	{
 		public CellType CellType { get; private set; }
-		public string Name { get { return CellType.Name; } }
-		public string Description { get { return CellType.Description; } }
+		public string Name { get { return CellType.ToString(); } }
+		public string Description { get { return "Description of " + CellType; } }
 
 		public CellTypeInfo(CellType cellType)
 		{