/// <summary> /// Adds the component to componentAssembly_mo as an instance. /// </summary> /// <param name="componentAssembly_mo">ComponentAssembly that contains the component.</param> /// <param name="parentComponentAssembly_mo">ComponentAssembly that contains componentAssembly_mo.</param> /// <param name="component">CyPhy Component to add.</param> private void AddComponent(ComponentAssembly componentAssembly_mo, ComponentAssembly parentComponentAssembly_mo, CyPhy.Component component) { var ca = componentAssembly_mo.Impl; var modelicaModel = this.GetModelicaModelFromComponent(component); // Make sure there are margins to the top- and left-border int canvasX = component.GenericAspect.X > ScaleFactor * ComponentIconSize ? component.GenericAspect.X : ScaleFactor * ComponentIconSize; int canvasY = component.GenericAspect.Y > ScaleFactor * ComponentIconSize ? component.GenericAspect.Y : ScaleFactor * ComponentIconSize; // Make sure there are margins to the bottom- and right-border componentAssembly_mo.CanvasXMax = canvasX + (ScaleFactor * ComponentIconSize) > componentAssembly_mo.CanvasXMax ? canvasX + (ScaleFactor * ComponentIconSize) : componentAssembly_mo.CanvasXMax; componentAssembly_mo.CanvasYMax = canvasY + (ScaleFactor * ComponentIconSize) > componentAssembly_mo.CanvasYMax ? canvasY + (ScaleFactor * ComponentIconSize) : componentAssembly_mo.CanvasYMax; var componentInstance_mo = new ComponentInstance(component) { InstanceOf = this.treeComponents[component.DerivedFrom().ID], CanvasX = canvasX, CanvasY = canvasY }; foreach (var parameter in component.Children.ParameterCollection) { // META-3622 if (String.IsNullOrWhiteSpace(parameter.Attributes.Dimension) || parameter.Attributes.Dimension.Trim() == "1") { var vf = parameter.SrcConnections.ValueFlowCollection.FirstOrDefault(); if (vf != null && vf.ParentContainer.ID == ca.ID) { var parameter_mo = this.GetArgumentParameter(ca.ID, parameter, vf); componentInstance_mo.Parameters.Add(parameter_mo); } } } // META-2189 Overwrite ModelicaParameters connected to properties with formulated values in the design. foreach (var modelicaParameter in modelicaModel.Children.ModelicaParameterCollection) { var connection = modelicaParameter.SrcConnections.ModelicaParameterPortMapCollection.FirstOrDefault(); if (connection != null && connection.SrcEnds.Property != null) { double temp; var value = modelicaParameter.Attributes.Value.Trim(); if (double.TryParse(modelicaParameter.Attributes.Value, out temp) || value == "true" || value == "false" || (value.StartsWith("\"") && value.EndsWith("\""))) { var parameter_mo = new UnitParameter(); parameter_mo.Name = modelicaParameter.Name; parameter_mo.Value = new ModelicaValue(modelicaParameter.Attributes.Value.Trim()); componentInstance_mo.Parameters.Add(parameter_mo); } } } componentAssembly_mo.ComponentInstances.Add(componentInstance_mo); string componentModelicaURI = componentAssembly_mo.Name + "." + componentInstance_mo.Name; var parentCA = parentComponentAssembly_mo; while (parentCA != null) { componentModelicaURI = parentCA.Name + "." + componentModelicaURI; parentCA = parentCA.ParentComponentAssembly; } this.ExtractPCCInputs(component, componentModelicaURI); string modelType; if (modelicaModel is CyPhy.CyberModel) { modelType = "ControllerModel"; } else { modelType = "PlantModel"; } var componentInfo = new ComponentInfo(component.Impl as GME.MGA.IMgaFCO, modelType, this.traceability); this.instanceURIMap.Add(string.Format("{0}.TestBenches.{1}.{2}", MainPackage, this.testBench_mo.Name, componentModelicaURI), componentInfo); if (this.modelURIMap.ContainsKey(componentInstance_mo.InstanceOf.FullName) == false) { this.modelURIMap.Add(componentInstance_mo.InstanceOf.FullName, componentInfo); } if (this.extendsURIMap.ContainsKey(componentInstance_mo.InstanceOf.Extends) == false) { var componentExtendInfo = new ComponentInfo(modelicaModel.Impl as GME.MGA.IMgaFCO, modelType, this.traceability); this.extendsURIMap.Add(componentInstance_mo.InstanceOf.Extends, componentExtendInfo); } // Iterate over all ports and look for connections FROM the port. These connections will generate equations in the // component assembly if the connections is not entering a ModelicaModel. // Connections from ModelicaConnectors (Components) foreach (var port in component.Children.ModelicaConnectorCollection) { foreach (var dstPort in port.DstConnections.PortCompositionCollection .Select(c => c.DstEnds.ModelicaConnector) .Where(p => p.ParentContainer is CyPhy.ModelicaModel == false)) { CyPhy.ModelicaConnector srcConnector = this.GetModelicaModelConnectorFromComponentPort(port); if (srcConnector == null) { continue; } CyPhy.ModelicaConnector dstConnector = null; if (dstPort.ParentContainer is CyPhy.Component) { dstConnector = this.GetModelicaModelConnectorFromComponentPort(dstPort); } else if (dstPort.ParentContainer is CyPhy.ComponentAssembly) { dstConnector = dstPort; } // The Fidelty Selected might not have this Connection - don't make it. // Or it might be a floating connector with no purpose. if (dstConnector == null) { continue; } var connection_mo = new Connection(srcConnector.Name, dstConnector.Name) { SrcInstanceName = component.Name, Type = srcConnector.Attributes.Class }; if (dstPort.ParentContainer.ID != ca.ID) { connection_mo.DstInstanceName = dstPort.ParentContainer.Name; } componentAssembly_mo.Connections.Add(connection_mo); } } // Connections from Connectors (Components) foreach (var portContainer in component.Children.ConnectorCollection) { if (portContainer.Children.ModelicaConnectorCollection.Count() != 1) { continue; } foreach (var dstPortContainer in portContainer.DstConnections.ConnectorCompositionCollection.Select(c => c.DstEnds.Connector)) { if (dstPortContainer.Children.ModelicaConnectorCollection.Count() != 1) { continue; } CyPhy.ModelicaConnector srcConnector = this.GetModelicaModelConnectorFromComponentPortContainer(portContainer); if (srcConnector == null) { continue; } CyPhy.ModelicaConnector dstConnector = null; string dstConnectorName = "ERROR No Name Assigned"; if (dstPortContainer.ParentContainer is CyPhy.Component) { var portComponent = CyPhyClasses.Component.Cast(dstPortContainer.ParentContainer.Impl); if (this.ComponentIsDynamic(portComponent) == false) { this.Logger.WriteWarning("'{0}' does not contain any dynamic-components, connections to it skipped.", portComponent.Name); continue; } dstConnector = this.GetModelicaModelConnectorFromComponentPortContainer(dstPortContainer); if (dstConnector != null) { dstConnectorName = dstConnector.Name; } } else if (dstPortContainer.ParentContainer is CyPhy.ComponentAssembly) { var portCA = dstPortContainer.ParentContainer.Impl; if (this.ContainsDynamicComponents(CyPhyClasses.ComponentAssembly.Cast(portCA)) == false) { this.Logger.WriteWarning("'{0}' does not contain any dynamic-components, connections to it skipped.", portCA.Name); continue; } dstConnector = dstPortContainer.Children.ModelicaConnectorCollection.FirstOrDefault(); dstConnectorName = dstPortContainer.Name.Replace(' ', '_'); } // The Fidelty Selected might not have this Connection - don't make it. // Or it might be a floating connector with no purpose. if (dstConnector == null) { continue; } var connection_mo = new Connection(srcConnector.Name, dstConnectorName) { SrcInstanceName = component.Name, Type = srcConnector.Attributes.Class }; if (dstPortContainer.ParentContainer.ID != ca.ID) { connection_mo.DstInstanceName = dstPortContainer.ParentContainer.Name; } componentAssembly_mo.Connections.Add(connection_mo); } } // Add limit-checks for the component to the test-bench foreach (var modelicaModelLimit in modelicaModel.Children.LimitCheckCollection) { // TODO: When META-1124 and META-1129 both are merged, merge Limit and LimitCheck class. var limit = new Limit(modelicaModelLimit) { ComponentModelicaUri = componentModelicaURI }; this.testBench_mo.Limits.Add(limit); var limitCheck = new LimitCheck() { ParentKind = modelicaModelLimit.ParentContainer.Kind, LimitFullPath = modelicaModelLimit.Path, LimitName = modelicaModelLimit.Name, VariableName = modelicaModelLimit.Attributes.VariableName, Type = modelicaModelLimit.Attributes.LimitType.ToString(), Value = modelicaModelLimit.Attributes.Value, VariableFullPath = string.Format("{0}.{1}", componentModelicaURI, modelicaModelLimit.Attributes.VariableName), ModelicaRecordName = string.Format("Limit{0}", this.testBench_mo.Limits.Count) }; this.LimitDefintion.LimitChecks.Add(limitCheck); } }