/// <summary> /// Generate a DTMI /// </summary> /// <param name="resource"></param> /// <returns>Returns a DTMI Id</returns> private static string GenerateDTMI(OntologyResource resource) { string id = null; try { string nodeType = resource.Resource.NodeType.ToString(); if (nodeType == "Uri") { //Get the IRI id = resource.Resource.ToString(); // Find the suffix after the # in the IRI when pattern is https://brickschema.org/schema/1.1/Brick#Equipment if (id.Contains("#")) { int index = id.LastIndexOf("#"); id = id.Substring(index + 1); } else // Or find the suffix after the last / in the IRI when pattern is http://webprotege.stanford.edu/Pizza { int index = id.LastIndexOf("/"); id = id.Substring(index + 1); } id = $"dtmi:{_dtmiPrefix}:{id};{_modelVersion}"; // Check to ensure id is <= 128 characters if (id.Count() > 128) { throw new System.Exception($"{id} is > 128 characters."); } } else { // Console.WriteLine($"Not a URI -> {resource.ToString()}"); } } catch (Exception e) { Console.WriteLine($"Could not generate DTMI for {resource.ToString()}"); Console.WriteLine($"{e.Message}"); id = null; } return(id); }
/// <summary> /// Get a label for an ontology resource, if it exists; else get the full URI. /// </summary> /// <param name="ontologyResource"></param> /// <returns></returns> internal static string GetLabel(OntologyResource ontologyResource) { string enLabel = ""; // Iterate over all resource labels foreach (ILiteralNode label in ontologyResource.Label) { // If a language-agnostic label is found, return it immediately if (label.Language == "") { return(label.Value); } // If an english-language label is found, store it for later if (label.Language == "en") { enLabel = label.Value; } } // Fallback option -- use the English label if (enLabel != "") { return(enLabel); } else { // Fallback to the fallback option -- use resource IRI, either by extracting local name // (if it is a URI node) or using whatever representation the resource provides if (ontologyResource.Resource.NodeType == NodeType.Uri) { IUriNode uriNode = (IUriNode)ontologyResource.Resource; return(GetLocalName(uriNode.Uri)); } else { return(ontologyResource.ToString()); } } }