- Timestamp:
- 11/10/14 04:24:04 (10 years ago)
- Location:
- branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3
- Files:
-
- 2 added
- 1 deleted
- 11 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IMessagePort.cs
r11529 r11530 26 26 27 27 namespace HeuristicLab.Core.Networks { 28 public interface IGenericPort : IParameterizedPort { 29 new PortParameterCollection Parameters { get; } 30 31 IConnectedPort ConnectedPort { get; set; } 28 public interface IMessagePort : IParameterizedPort { 29 IMessagePort ConnectedPort { get; set; } 32 30 bool PortConnectionValid { get; } 33 31 bool LogMessages { get; set; } 34 32 MessageCollection Messages { get; } 35 33 34 bool CanConnectToPort(IPort port); 35 36 36 event EventHandler ConnectedPortChanged; 37 37 event EventHandler PortConnectionValidChanged; 38 38 event EventHandler LogMessagesChanged; 39 40 bool CanConnectToPort(IPort port);41 void CloneConnectedPortParameters();42 39 43 40 IMessage PrepareMessage(); … … 46 43 Task SendMessageAsync(IMessage message); 47 44 Task SendMessageAsync(IMessage message, CancellationToken token); 45 void ReceiveMessage(IMessage message, CancellationToken token); 48 46 47 event EventHandler<EventArgs<IMessage, CancellationToken>> MessageSent; 49 48 event EventHandler<EventArgs<IMessage, CancellationToken>> MessageReceived; 50 49 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IParameterizedPort.cs
r11529 r11530 26 26 IKeyedItemCollection<string, IPortParameter> Parameters { get; } 27 27 28 void CloneParametersFromPort(IParameterizedPort port); 29 28 30 event EventHandler InterfaceChanged; 29 31 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/MessagePort.cs
r11529 r11530 23 23 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 24 24 using System; 25 using System.Collections.Generic; 25 26 using System.Drawing; 26 27 using System.Linq; … … 29 30 30 31 namespace HeuristicLab.Core.Networks { 31 [Item(" GenericPort", "A generic port of an optimization network node.")]32 [Item("MessagePort", "A port of a network node for sending and receiving messages.")] 32 33 [StorableClass] 33 public class GenericPort : ParameterizedPort, IGenericPort, IConnectedPort {34 public class MessagePort : ParameterizedPort, IMessagePort { 34 35 public override Image ItemImage { 35 36 get { … … 44 45 45 46 [Storable] 46 protected I ConnectedPort connectedPort;47 public I ConnectedPort ConnectedPort {47 protected IMessagePort connectedPort; 48 public IMessagePort ConnectedPort { 48 49 get { return connectedPort; } 49 50 set { … … 87 88 88 89 [StorableConstructor] 89 protected GenericPort(bool deserializing) : base(deserializing) { }90 protected GenericPort(GenericPort original, Cloner cloner)90 protected MessagePort(bool deserializing) : base(deserializing) { } 91 protected MessagePort(MessagePort original, Cloner cloner) 91 92 : base(original, cloner) { 92 93 connectedPort = cloner.Clone(original.connectedPort); … … 96 97 RegisterConnectedPortEvents(); 97 98 } 98 public GenericPort()99 : base(" GenericPort") {99 public MessagePort() 100 : base("MessagePort") { 100 101 portConnectionValid = true; 101 102 logMessages = false; 102 103 messages = new MessageCollection(); 103 104 } 104 public GenericPort(string name)105 public MessagePort(string name) 105 106 : base(name) { 106 107 portConnectionValid = true; … … 108 109 messages = new MessageCollection(); 109 110 } 110 public GenericPort(string name, string description)111 public MessagePort(string name, string description) 111 112 : base(name, description) { 112 113 portConnectionValid = true; … … 121 122 122 123 public override IDeepCloneable Clone(Cloner cloner) { 123 return new GenericPort(this, cloner);124 return new MessagePort(this, cloner); 124 125 } 125 126 … … 127 128 if (port == null) return true; 128 129 129 var cp = port as IConnectedPort; 130 if (cp == null) return false; 131 132 // check connected port input parameters 133 foreach (var input in cp.Parameters.Where(p => p.Type.HasFlag(PortParameterType.Input))) { 134 IPortParameter param; 135 Parameters.TryGetValue(input.Name, out param); 136 if ((param == null) && (input.DefaultValue == null)) return false; 137 if (!param.Type.HasFlag(PortParameterType.Output)) return false; 138 if (!input.DataType.IsAssignableFrom(param.DataType)) return false; 139 } 140 // check local port input parameters 141 foreach (var input in Parameters.Where(p => p.Type.HasFlag(PortParameterType.Input))) { 142 IPortParameter param; 143 cp.Parameters.TryGetValue(input.Name, out param); 144 if ((param == null) && (input.DefaultValue == null)) return false; 145 if (!param.Type.HasFlag(PortParameterType.Output)) return false; 146 if (!input.DataType.IsAssignableFrom(param.DataType)) return false; 147 } 130 var p = port as IMessagePort; 131 if (p == null) return false; 132 133 // check if the local parameters are compatible to the input parameters of the port 134 if (!ParametersCompatibleToTarget(Parameters, p.Parameters)) return false; 135 // check if the parameters of the port are compatible to the local input parameters 136 if (!ParametersCompatibleToTarget(p.Parameters, Parameters)) return false; 137 148 138 return true; 149 }150 public void CloneConnectedPortParameters() {151 Parameters.Clear();152 foreach (var p in connectedPort.Parameters) {153 var param = (IPortParameter)p.Clone();154 if (!(param.Type.HasFlag(PortParameterType.Input) && param.Type.HasFlag(PortParameterType.Output))) {155 param.Type = ~param.Type; // bitwise negation: input -> output, output -> input156 }157 Parameters.Add(param);158 }159 139 } 160 140 … … 163 143 var message = new Message(); 164 144 165 // collect output parameters from local port 166 message.Values.AddRange( 167 Parameters 168 .Where(p => p.Type.HasFlag(PortParameterType.Output)) 169 .Select(p => p.CreateMessageValue()) 170 ); 171 172 // collect remaining input parameters from connected port 145 // collect parameters from local port 146 message.Values.AddRange(Parameters.Select(p => p.CreateMessageValue())); 147 148 // collect remaining parameters from connected port 173 149 if (ConnectedPort != null) { 174 150 message.Values.AddRange( 175 151 ConnectedPort.Parameters 176 .Where(p => p.Type.HasFlag(PortParameterType.Input) &&!message.Values.ContainsKey(p.Name))152 .Where(p => !message.Values.ContainsKey(p.Name)) 177 153 .Select(p => p.CreateMessageValue()) 178 154 ); 179 155 } 180 181 // collect output parameters from connected port182 if (ConnectedPort != null) {183 message.Values.AddRange(184 ConnectedPort.Parameters185 .Where(p => p.Type.HasFlag(PortParameterType.Output) && !message.Values.ContainsKey(p.Name))186 .Select(p => p.CreateMessageValue())187 );188 }189 190 // collect remaining input parameters from local port191 message.Values.AddRange(192 Parameters193 .Where(p => p.Type.HasFlag(PortParameterType.Input) && !message.Values.ContainsKey(p.Name))194 .Select(p => p.CreateMessageValue())195 );196 156 197 157 return message; … … 270 230 } 271 231 #endregion 232 233 #region Helpers 234 protected virtual bool ParametersCompatibleToTarget(IEnumerable<IPortParameter> source, IEnumerable<IPortParameter> target) { 235 // checks if the source parameters are compatible to the input parameters of the target 236 foreach (var input in target.Where(p => p.Type.HasFlag(PortParameterType.Input))) { 237 var param = source.Where(p => input.Name == p.Name).FirstOrDefault(); 238 if (param == null) { 239 if (input.DefaultValue == null) return false; 240 } else { 241 if (!param.Type.HasFlag(PortParameterType.Output)) return false; 242 if (!input.DataType.IsAssignableFrom(param.DataType)) return false; 243 } 244 } 245 return true; 246 } 247 #endregion 272 248 } 273 249 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/ParameterizedPort.cs
r11529 r11530 83 83 } 84 84 85 public virtual void CloneParametersFromPort(IParameterizedPort port) { 86 Parameters.Clear(); 87 foreach (var p in port.Parameters) { 88 var param = (IPortParameter)p.Clone(); 89 if (!(param.Type.HasFlag(PortParameterType.Input) && param.Type.HasFlag(PortParameterType.Output))) { 90 param.Type = ~param.Type; // bitwise negation: input -> output, output -> input 91 } 92 Parameters.Add(param); 93 } 94 } 95 85 96 public event EventHandler InterfaceChanged; 86 97 protected virtual void OnInterfaceChanged() { -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/HeuristicLab.Optimization.Networks-3.3.csproj
r11529 r11530 90 90 </ItemGroup> 91 91 <ItemGroup> 92 <Compile Include="Core.Networks\IUserDefinedMessagePort.cs" /> 93 <Compile Include="Core.Networks\IMessagePort.cs" /> 92 94 <Compile Include="Core.Networks\IUserDefinedNetwork.cs" /> 93 95 <Compile Include="Core.Networks\IUserDefinedNode.cs" /> 96 <Compile Include="Core.Networks\MessagePort.cs" /> 94 97 <Compile Include="Core.Networks\UserDefinedNetwork.cs" /> 95 <Compile Include="Core.Networks\GenericPort.cs" />96 <Compile Include="Core.Networks\IConnectedPort.cs" />97 <Compile Include="Core.Networks\IGenericPort.cs" />98 98 <Compile Include="Core.Networks\IMessage.cs" /> 99 99 <Compile Include="Core.Networks\IMessageValue.cs" /> … … 118 118 <Compile Include="Core.Networks\PortParameterCollection.cs" /> 119 119 <Compile Include="Core.Networks\PortParameterType.cs" /> 120 <Compile Include="Core.Networks\UserDefinedMessagePort.cs" /> 120 121 <Compile Include="Core.Networks\UserDefinedNode.cs" /> 121 122 <Compile Include="Operators\HookOperator.cs" /> -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Operators/HookOperator.cs
r11526 r11530 36 36 37 37 [Storable] 38 protected I GenericPort port;39 public I GenericPort Port {38 protected IMessagePort port; 39 public IMessagePort Port { 40 40 get { return port; } 41 41 set { -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Operators/IHookOperator.cs
r11526 r11530 27 27 public interface IHookOperator : IOperator { 28 28 new ParameterCollection Parameters { get; } 29 I GenericPort Port { get; set; }29 IMessagePort Port { get; set; } 30 30 31 31 event EventHandler PortChanged; -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPConnector.cs
r11526 r11530 65 65 66 66 protected virtual void Initialize() { 67 var parameters = new GenericPort("Parameters");67 var parameters = new MessagePort("Parameters"); 68 68 Ports.Add(parameters); 69 69 parameters.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") { Type = PortParameterType.Input }); 70 70 parameters.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") { Type = PortParameterType.Input }); 71 71 72 var ksp = new GenericPort("KSP Connector");72 var ksp = new MessagePort("KSP Connector"); 73 73 Ports.Add(ksp); 74 74 ksp.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { Type = PortParameterType.Input }); … … 77 77 ksp.Parameters.Add(new PortParameter<PathTSPTour>("Route") { Type = PortParameterType.Output }); 78 78 79 var tsp = new GenericPort("TSP Connector");79 var tsp = new MessagePort("TSP Connector"); 80 80 Ports.Add(tsp); 81 81 tsp.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { Type = PortParameterType.Output }); … … 85 85 86 86 protected virtual void RegisterEvents() { 87 var ksp = (I GenericPort)Ports["KSP Connector"];87 var ksp = (IMessagePort)Ports["KSP Connector"]; 88 88 ksp.MessageReceived += Knapsack_MessageReceived; 89 89 } … … 91 91 protected virtual void Knapsack_MessageReceived(object sender, EventArgs<IMessage, System.Threading.CancellationToken> e) { 92 92 // get parameters 93 var parametersPort = (I GenericPort)Ports["Parameters"];93 var parametersPort = (IMessagePort)Ports["Parameters"]; 94 94 var parameters = parametersPort.PrepareMessage(); 95 95 parametersPort.SendMessage(parameters, e.Value2); … … 112 112 113 113 // solve TSP 114 var tspConnectorPort = (I GenericPort)Ports["TSP Connector"];114 var tspConnectorPort = (IMessagePort)Ports["TSP Connector"]; 115 115 var tspMsg = tspConnectorPort.PrepareMessage(); 116 116 tspMsg["Coordinates"] = coords; -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetwork.cs
r11529 r11530 63 63 Nodes.Add(paramsNode); 64 64 65 var paramsPort = new GenericPort("Parameters");65 var paramsPort = new MessagePort("Parameters"); 66 66 paramsNode.Ports.Add(paramsPort); 67 67 … … 111 111 }); 112 112 113 var evaluatePort = new GenericPort("EvaluateRoute");113 var evaluatePort = new MessagePort("EvaluateRoute"); 114 114 kspNode.Ports.Add(evaluatePort); 115 115 … … 175 175 hook.Port = evaluatePort; 176 176 configPort.ConnectedPort = paramsPort; 177 evaluatePort.ConnectedPort = (I ConnectedPort)ksptspConnector.Ports["KSP Connector"];178 ((I GenericPort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;179 ((I GenericPort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;177 evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"]; 178 ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort; 179 ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort; 180 180 181 181 paramsPort.SendMessage(paramsPort.PrepareMessage()); -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks/ConfigurationPort.cs
r11526 r11530 28 28 [Item("ConfigurationPort", "A port of an optimization network node which triggers configuration changes.")] 29 29 [StorableClass] 30 public class ConfigurationPort : GenericPort, IConfigurationPort {30 public class ConfigurationPort : MessagePort, IConfigurationPort { 31 31 [StorableConstructor] 32 32 protected ConfigurationPort(bool deserializing) : base(deserializing) { } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks/ExecutionPort.cs
r11526 r11530 28 28 [Item("ExecutionPort", "A port of an optimization network node which triggers execution.")] 29 29 [StorableClass] 30 public class ExecutionPort : GenericPort, IExecutionPort {30 public class ExecutionPort : MessagePort, IExecutionPort { 31 31 [StorableConstructor] 32 32 protected ExecutionPort(bool deserializing) : base(deserializing) { } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks/IConfigurationPort.cs
r11526 r11530 23 23 24 24 namespace HeuristicLab.Optimization.Networks { 25 public interface IConfigurationPort : I GenericPort { }25 public interface IConfigurationPort : IMessagePort { } 26 26 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks/IExecutionPort.cs
r11526 r11530 23 23 24 24 namespace HeuristicLab.Optimization.Networks { 25 public interface IExecutionPort : I GenericPort { }25 public interface IExecutionPort : IMessagePort { } 26 26 }
Note: See TracChangeset
for help on using the changeset viewer.