Example #1
0
        /// <summary>
        /// Given a DomainModel object, find the path to the file resource.
        /// This will traverse the UsesResource connection to a Resource object,
        /// then return that Resource object's path.
        /// </summary>
        /// <param name="domainModel"></param>
        /// <param name="path">The path to the resource, relative to the component's resource folder</param>
        /// <param name="pathConvention"></param>
        /// <returns>True if the resource was found</returns>
        public static bool TryGetResourcePath(CyPhy.DomainModel domainModel, out string path, PathConvention pathConvention = PathConvention.REL_TO_COMP_DIR)
        {
            if (false == (domainModel.ParentContainer is CyPhy.Component) &&
                (pathConvention == PathConvention.REL_TO_PROJ_ROOT || pathConvention == PathConvention.ABSOLUTE))
            {
                throw new NotSupportedException(String.Format("{0} doesn't support PathConvention of type {1} if domainModel.ParentContainer is not a CyPhy.Component",
                                                              System.Reflection.MethodBase.GetCurrentMethod().Name,
                                                              pathConvention.ToString()));
            }

            // Starting with the DomainModel object, we'll find a Resource object to which it has a relation.
            // Then we'll return the path of that Resource object.

            List <CyPhy.Resource> l_Resources = new List <CyPhy.Resource>();

            foreach (CyPhy.UsesResource ur in domainModel.DstConnections.UsesResourceCollection)
            {
                l_Resources.Add(ur.DstEnds.Resource);
            }
            foreach (CyPhy.UsesResource ur in domainModel.SrcConnections.UsesResourceCollection)
            {
                l_Resources.Add(ur.SrcEnds.Resource);
            }

            path = "";

            // If no Resources found, return false.
            if (l_Resources.Count == 0)
            {
                return(false);
            }

            // At least one Resource was found, so return the path of the first.
            var resourcePath = l_Resources.FirstOrDefault().Attributes.Path;

            if (pathConvention == PathConvention.REL_TO_COMP_DIR)
            {
                path = resourcePath;
            }
            else if (pathConvention == PathConvention.REL_TO_PROJ_ROOT)
            {
                var compDirPath = GetComponentFolderPath(domainModel.ParentContainer as CyPhy.Component);
                path = Path.Combine(compDirPath, resourcePath);
            }
            else if (pathConvention == PathConvention.ABSOLUTE)
            {
                var compDirPath  = GetComponentFolderPath(domainModel.ParentContainer as CyPhy.Component);
                var projRootPath = domainModel.Impl.Project.GetRootDirectoryPath();
                path = Path.Combine(projRootPath, compDirPath, resourcePath);
            }
            else
            {
                throw new NotSupportedException(String.Format("{0} doesn't support PathConvention of type {1}",
                                                              System.Reflection.MethodBase.GetCurrentMethod().Name,
                                                              pathConvention.ToString()));
            }

            return(true);
        }
Example #2
0
 /// <summary>
 /// Given a DomainModel object, find the path to the file resource.
 /// This will traverse the UsesResource connection to a Resource object,
 /// then return that Resource object's path.
 /// </summary>
 /// <param name="path">The path to the resource, relative to the component's resource folder</param>
 /// <param name="pathConvention"></param>
 /// <returns>True if the resource was found</returns>
 public static bool TryGetResourcePath(this CyPhy.DomainModel domainModel, out string path, ComponentLibraryManager.PathConvention pathConvention = ComponentLibraryManager.PathConvention.REL_TO_COMP_DIR)
 {
     return(ComponentLibraryManager.TryGetResourcePath(domainModel, out path, pathConvention));
 }