private static GroupDefinition CreateImportingGroup() { return(new GroupDefinition("b") { InternalConnections = Enumerable.Empty <PartImportToPartExportMap>(), Parts = new List <GroupPartDefinition> { new GroupPartDefinition( TypeIdentity.CreateDefinition(typeof(string)), 0, new Dictionary <ExportRegistrationId, SerializableExportDefinition>(), new Dictionary <ImportRegistrationId, SerializableImportDefinition> { { new ImportRegistrationId(typeof(string), 0, "PartContract1"), PropertyBasedImportDefinition.CreateDefinition( "PartContract1", TypeIdentity.CreateDefinition(typeof(int)), ImportCardinality.ExactlyOne, false, CreationPolicy.Any, typeof(ImportOnPropertyWithType).GetProperty("ImportingProperty")) } }, new Dictionary <ScheduleActionRegistrationId, ScheduleActionDefinition>(), new Dictionary <ScheduleConditionRegistrationId, ScheduleConditionDefinition>()), new GroupPartDefinition( TypeIdentity.CreateDefinition(typeof(string)), 1, new Dictionary <ExportRegistrationId, SerializableExportDefinition>(), new Dictionary <ImportRegistrationId, SerializableImportDefinition> { { new ImportRegistrationId(typeof(string), 1, "PartContract2"), PropertyBasedImportDefinition.CreateDefinition( "PartContract2", TypeIdentity.CreateDefinition(typeof(string)), ImportCardinality.ExactlyOne, false, CreationPolicy.Any, typeof(ImportOnPropertyWithEnumerable).GetProperty("ImportingProperty")) } }, new Dictionary <ScheduleActionRegistrationId, ScheduleActionDefinition>(), new Dictionary <ScheduleConditionRegistrationId, ScheduleConditionDefinition>()), }, GroupImports = new List <GroupImportDefinition> { GroupImportDefinition.CreateDefinition( "ContractName", new GroupRegistrationId("b"), null, new List <ImportRegistrationId> { new ImportRegistrationId(typeof(string), 0, "PartContract1"), new ImportRegistrationId(typeof(string), 1, "PartContract2"), }) }, }); }
public void ConnectWithUnknownExportingGroup() { using (var source = new CancellationTokenSource()) { var commands = new Mock <ICompositionCommands>(); { commands.Setup(c => c.Add(It.IsAny <GroupCompositionId>(), It.IsAny <GroupDefinition>())) .Returns( Task.Factory.StartNew( () => { }, source.Token, TaskCreationOptions.None, new CurrentThreadTaskScheduler())); } var connector = new Mock <IConnectGroups>(); var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var layer = new ProxyCompositionLayer(commands.Object, connector.Object, systemDiagnostics); var exportingGroup = new GroupDefinition("Group1"); var task = layer.Add(exportingGroup); task.Wait(); var importDefinition = GroupImportDefinition.CreateDefinition( "a", new GroupRegistrationId("b"), null, Enumerable.Empty <ImportRegistrationId>()); Assert.Throws <UnknownPartGroupException>(() => layer.Connect(task.Result, importDefinition, new GroupCompositionId())); } }
public void Connect() { using (var source = new CancellationTokenSource()) { var commands = new Mock <ICompositionCommands>(); { commands.Setup(c => c.Add(It.IsAny <GroupCompositionId>(), It.IsAny <GroupDefinition>())) .Returns( Task.Factory.StartNew( () => { }, source.Token, TaskCreationOptions.None, new CurrentThreadTaskScheduler())); commands.Setup(c => c.Connect(It.IsAny <GroupConnection>())) .Returns( Task.Factory.StartNew( () => { }, source.Token, TaskCreationOptions.None, new CurrentThreadTaskScheduler())); } var connector = new Mock <IConnectGroups>(); { connector.Setup( c => c.GenerateConnectionFor( It.IsAny <GroupDefinition>(), It.IsAny <GroupImportDefinition>(), It.IsAny <GroupDefinition>())) .Callback <GroupDefinition, GroupImportDefinition, GroupDefinition>( (importingGroup, importDef, exportingGroup) => { }) .Returns(Enumerable.Empty <PartImportToPartExportMap>()); } var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var layer = new ProxyCompositionLayer(commands.Object, connector.Object, systemDiagnostics); var exportingDefinition = new GroupDefinition("Group1"); var addTask = layer.Add(exportingDefinition); var exportingId = addTask.Result; var importingDefinition = new GroupDefinition("Group2"); addTask = layer.Add(importingDefinition); var importingId = addTask.Result; var importDefinition = GroupImportDefinition.CreateDefinition( "a", new GroupRegistrationId("b"), null, Enumerable.Empty <ImportRegistrationId>()); var connectTask = layer.Connect(importingId, importDefinition, exportingId); connectTask.Wait(); Assert.IsTrue(layer.IsConnected(importingId, importDefinition)); Assert.IsTrue(layer.IsConnected(importingId, importDefinition, exportingId)); Assert.AreEqual(exportingId, layer.ConnectedTo(importingId, importDefinition)); } }
public void ReloadFromDataset() { var importingId = new GroupCompositionId(); var exportingId = new GroupCompositionId(); var importingGroup = new GroupDefinition("Group1"); var exportingGroup = new GroupDefinition("Group2"); var importDefinition = GroupImportDefinition.CreateDefinition( "a", new GroupRegistrationId("b"), null, Enumerable.Empty <ImportRegistrationId>()); var state = new GroupCompositionState( new List <Tuple <GroupCompositionId, GroupDefinition> > { new Tuple <GroupCompositionId, GroupDefinition>(importingId, importingGroup), new Tuple <GroupCompositionId, GroupDefinition>(exportingId, exportingGroup), }, new List <Tuple <GroupCompositionId, GroupImportDefinition, GroupCompositionId> > { new Tuple <GroupCompositionId, GroupImportDefinition, GroupCompositionId>(importingId, importDefinition, exportingId) }); using (var source = new CancellationTokenSource()) { var commands = new Mock <ICompositionCommands>(); { commands.Setup(c => c.CurrentState()) .Returns( Task <GroupCompositionState> .Factory.StartNew( () => state, source.Token, TaskCreationOptions.None, new CurrentThreadTaskScheduler())); } var connector = new Mock <IConnectGroups>(); var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var layer = new ProxyCompositionLayer(commands.Object, connector.Object, systemDiagnostics); var task = layer.ReloadFromDataset(); task.Wait(); Assert.IsTrue(layer.Contains(importingId)); Assert.AreEqual(importingGroup, layer.Group(importingId)); Assert.IsTrue(layer.Contains(exportingId)); Assert.AreEqual(exportingGroup, layer.Group(exportingId)); Assert.IsTrue(layer.IsConnected(importingId, importDefinition)); Assert.IsTrue(layer.IsConnected(importingId, importDefinition, exportingId)); Assert.AreEqual(exportingId, layer.ConnectedTo(importingId, importDefinition)); } }
public void ConnectedToWithUnknownImportingGroup() { var commands = new Mock <ICompositionCommands>(); var connector = new Mock <IConnectGroups>(); var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var layer = new ProxyCompositionLayer(commands.Object, connector.Object, systemDiagnostics); var importDefinition = GroupImportDefinition.CreateDefinition( "a", new GroupRegistrationId("b"), null, Enumerable.Empty <ImportRegistrationId>()); Assert.IsNull(layer.ConnectedTo(null, importDefinition)); Assert.IsNull(layer.ConnectedTo(new GroupCompositionId(), null)); Assert.IsNull(layer.ConnectedTo(new GroupCompositionId(), importDefinition)); }
public void CanConnectToWithNonExistingExportGroup() { var importingId = new GroupCompositionId(); var exportingId = new GroupCompositionId(); var importEngine = new Mock <IConnectGroups>(); var graph = new Mock <ICompositionLayer>(); { graph.Setup(g => g.Contains(It.IsAny <GroupCompositionId>())) .Returns <GroupCompositionId>(id => !id.Equals(exportingId)); } var selector = new GroupSelector(importEngine.Object, graph.Object); Assert.IsFalse( selector.CanConnectTo( importingId, GroupImportDefinition.CreateDefinition("myContract", new GroupRegistrationId("a"), null, null), exportingId)); }
/// <summary> /// Registers a group with the currently stored data. /// </summary> /// <param name="name">The name of the newly created group.</param> /// <returns>The registration ID of the group.</returns> public GroupRegistrationId Register(string name) { var definition = new GroupDefinition(name); definition.Parts = m_Objects.SelectMany(p => p.Value).ToList(); definition.InternalConnections = m_Connections.Select( p => new PartImportToPartExportMap(p.Key, (IEnumerable <ExportRegistrationId>)p.Value)); if (m_Schedule != null) { definition.Schedule = ScheduleDefinition.CreateDefinition( definition.Id, m_Schedule, m_Actions.ToDictionary(p => p.Value, p => p.Key), m_Conditions.ToDictionary(p => p.Value, p => p.Key)); } if (m_GroupExport != null) { definition.GroupExport = GroupExportDefinition.CreateDefinition( m_GroupExport.ContractName, definition.Id, NonLinkedExports()); } if (m_GroupImports.Count > 0) { definition.GroupImports = m_GroupImports.Select( i => GroupImportDefinition.CreateDefinition( i.Value.ContractName, definition.Id, i.Value.InsertPoint, i.Value.ObjectImports)) .ToList(); } Clear(); m_Repository.AddGroup(definition, m_FileInfo); return(definition.Id); }
public void MatchingGroupsWithGroupImport() { var groups = new List <GroupDefinition> { new GroupDefinition("Group1"), new GroupDefinition("Group2"), new GroupDefinition("Group3"), }; var importEngine = new Mock <IConnectGroups>(); { importEngine.Setup(i => i.MatchingGroups(It.IsAny <GroupImportDefinition>(), It.IsAny <IDictionary <string, object> >())) .Returns(new List <GroupDefinition> { groups[0], groups[1] }); } var selector = new GroupSelector(importEngine.Object, new Mock <ICompositionLayer>().Object); var selectedGroups = selector.MatchingGroups(GroupImportDefinition.CreateDefinition("myContract", groups[0].Id, null, null)); Assert.AreEqual(2, selectedGroups.Count()); }
public void Connect() { var lockKey = new DatasetLockKey(); var datasetLock = new Mock <ITrackDatasetLocks>(); { datasetLock.Setup(d => d.LockForWriting()) .Returns(lockKey) .Verifiable(); datasetLock.Setup(d => d.RemoveWriteLock(It.IsAny <DatasetLockKey>())) .Callback <DatasetLockKey>(key => Assert.AreSame(lockKey, key)) .Verifiable(); } var originalConnection = new GroupConnection( new GroupCompositionId(), new GroupCompositionId(), GroupImportDefinition.CreateDefinition( "a", new GroupRegistrationId("a"), null, Enumerable.Empty <ImportRegistrationId>()), Enumerable.Empty <PartImportToPartExportMap>()); var storage = new Mock <IStoreGroupsAndConnections>(); { storage.Setup(s => s.Connect(It.IsAny <GroupConnection>())) .Callback <GroupConnection>( id => { Assert.AreSame(originalConnection, id); }); } var commands = new CompositionCommands(datasetLock.Object, storage.Object); var task = commands.Connect(originalConnection); task.Wait(); datasetLock.Verify(d => d.LockForWriting(), Times.Once()); datasetLock.Verify(d => d.RemoveWriteLock(It.IsAny <DatasetLockKey>()), Times.Once()); }
public void CanConnectToWithNonmatchingGroups() { var groups = new List <GroupDefinition> { new GroupDefinition("Group1"), new GroupDefinition("Group2"), new GroupDefinition("Group3"), }; var importEngine = new Mock <IConnectGroups>(); { importEngine.Setup(i => i.Accepts(It.IsAny <GroupImportDefinition>(), It.IsAny <GroupExportDefinition>())) .Returns(false); importEngine.Setup(i => i.ExportPassesSelectionCriteria(It.IsAny <GroupExportDefinition>(), It.IsAny <IDictionary <string, object> >())) .Returns(true); } var selector = new GroupSelector(importEngine.Object, new Mock <ICompositionLayer>().Object); Assert.IsFalse( selector.CanConnectTo( new GroupCompositionId(), GroupImportDefinition.CreateDefinition("myContract", groups[0].Id, null, null), new GroupCompositionId())); }
public void CurrentState() { var lockKey = new DatasetLockKey(); var datasetLock = new Mock <ITrackDatasetLocks>(); { datasetLock.Setup(d => d.LockForReading()) .Returns(lockKey) .Verifiable(); datasetLock.Setup(d => d.RemoveReadLock(It.IsAny <DatasetLockKey>())) .Callback <DatasetLockKey>(key => Assert.AreSame(lockKey, key)) .Verifiable(); } var groups = new Dictionary <GroupCompositionId, Tuple <GroupDefinition, Tuple <GroupImportDefinition, GroupCompositionId> > > { { new GroupCompositionId(), new Tuple <GroupDefinition, Tuple <GroupImportDefinition, GroupCompositionId> >( new GroupDefinition("a"), new Tuple <GroupImportDefinition, GroupCompositionId>( GroupImportDefinition.CreateDefinition( "c", new GroupRegistrationId("c"), null, Enumerable.Empty <ImportRegistrationId>()), new GroupCompositionId())) }, { new GroupCompositionId(), new Tuple <GroupDefinition, Tuple <GroupImportDefinition, GroupCompositionId> >( new GroupDefinition("d"), new Tuple <GroupImportDefinition, GroupCompositionId>( GroupImportDefinition.CreateDefinition( "e", new GroupRegistrationId("e"), null, Enumerable.Empty <ImportRegistrationId>()), new GroupCompositionId())) }, }; var storage = new Mock <IStoreGroupsAndConnections>(); { storage.Setup(s => s.Groups()) .Returns(groups.Keys); storage.Setup(s => s.Group(It.IsAny <GroupCompositionId>())) .Returns <GroupCompositionId>(id => groups[id].Item1); storage.Setup(s => s.SatisfiedImports(It.IsAny <GroupCompositionId>())) .Returns <GroupCompositionId>(id => new List <Tuple <GroupImportDefinition, GroupCompositionId> > { groups[id].Item2 }); } var commands = new CompositionCommands(datasetLock.Object, storage.Object); var task = commands.CurrentState(); var results = task.Result; Assert.That( results.Groups, Is.EquivalentTo( groups.Select(p => new Tuple <GroupCompositionId, GroupDefinition>(p.Key, p.Value.Item1)))); Assert.That( results.Connections, Is.EquivalentTo( groups.Select( p => new Tuple <GroupCompositionId, GroupImportDefinition, GroupCompositionId>( p.Key, p.Value.Item2.Item1, p.Value.Item2.Item2)))); datasetLock.Verify(d => d.LockForReading(), Times.Once()); datasetLock.Verify(d => d.RemoveReadLock(It.IsAny <DatasetLockKey>()), Times.Once()); }
private static GroupDefinition CreateImportingDefinition() { var groupName = "Import"; return(new GroupDefinition(groupName) { InternalConnections = Enumerable.Empty <PartImportToPartExportMap>(), Parts = new List <GroupPartDefinition> { new GroupPartDefinition( TypeIdentity.CreateDefinition(typeof(List <string>)), 0, new Dictionary <ExportRegistrationId, SerializableExportDefinition>(), new Dictionary <ImportRegistrationId, SerializableImportDefinition> { { new ImportRegistrationId(typeof(string), 0, "PartContract1"), PropertyBasedImportDefinition.CreateDefinition( "PartContract1", TypeIdentity.CreateDefinition(typeof(int)), ImportCardinality.ExactlyOne, false, CreationPolicy.Any, typeof(ImportOnPropertyWithType).GetProperty("ImportingProperty")) } }, new Dictionary <ScheduleActionRegistrationId, ScheduleActionDefinition>(), new Dictionary <ScheduleConditionRegistrationId, ScheduleConditionDefinition>()), new GroupPartDefinition( TypeIdentity.CreateDefinition(typeof(List <double>)), 1, new Dictionary <ExportRegistrationId, SerializableExportDefinition>(), new Dictionary <ImportRegistrationId, SerializableImportDefinition> { { new ImportRegistrationId(typeof(string), 1, "PartContract2"), ConstructorBasedImportDefinition.CreateDefinition( "PartContract2", TypeIdentity.CreateDefinition(typeof(string)), ImportCardinality.ExactlyOne, CreationPolicy.Any, typeof(Version).GetConstructor( new[] { typeof(string) }).GetParameters().First()) } }, new Dictionary <ScheduleActionRegistrationId, ScheduleActionDefinition>(), new Dictionary <ScheduleConditionRegistrationId, ScheduleConditionDefinition>()), }, GroupImports = new List <GroupImportDefinition> { GroupImportDefinition.CreateDefinition( "ContractName", new GroupRegistrationId(groupName), null, new List <ImportRegistrationId> { new ImportRegistrationId(typeof(string), 0, "PartContract1"), new ImportRegistrationId(typeof(string), 1, "PartContract2"), }) }, }); }