Beispiel #1
0
        /// <summary>
        /// Translate an OWLname to a CNL (DL) name
        /// </summary>
        /// <param name="owlname">the OWL name</param>
        /// <param name="lex"></param>
        /// <param name="ns2pfx"></param>
        /// <param name="madeFor">the type of this entity (Concept, Instance, Role,...)</param>
        /// <returns></returns>
        public CogniPy.CNL.DL.DlName ToDL(OwlName owlname, CNL.EN.endict lex, Func <string, string> ns2pfx, EntityKind madeFor)
        {
            var    parts     = owlname.Split();
            string defaultNs = ns2pfx(null);

            if (string.IsNullOrWhiteSpace(parts.ns))
            {
                parts.ns = defaultNs;
            }

            string pfx = null;

            if (!IRIParser.AreNamespacesEqual(defaultNs, parts.ns))
            {
                pfx = ns2pfx(parts.ns);
            }

            var dlp = new DlName.Parts()
            {
                term = pfx
            };

            if (parts.name.StartsWith("⊤"))
            {
                dlp.name = "⊤";
                if (parts.name.Contains("."))
                {
                    dlp.name = "[" + dlp.name + "]";
                }
                dlp.quoted = false;
            }
            else
            {
                var  name_part  = parts.name;
                bool isVBigName = parts.global;
                var  sufix      = "";
                if (madeFor == EntityKind.SWRLVariable)
                {
                    var a = name_part.Split('_');
                    if (a.Length == 2)
                    {
                        name_part = a.First();
                        sufix     = a.Last();
                    }
                }
                Match mth = null;
                if (!isVBigName)
                {
                    mth = regexp.Match(name_part);
                }
                Match mth2 = null;
                if (isVBigName || !mth.Success)
                {
                    mth2 = vbregexp.Match(name_part);
                }
                if (name_part != "" &&
                    (
                        (mth != null && mth.Success &&
                         (
                             ((madeFor == EntityKind.SWRLVariable || madeFor == EntityKind.Instance) && char.IsUpper(name_part.First())) ||
                             ((madeFor == EntityKind.Concept || madeFor == EntityKind.DataRole || madeFor == EntityKind.SWRLVariable || madeFor == EntityKind.Role) && char.IsLower(name_part.First()))
                         )) ||
                        (mth2 != null && mth2.Success && madeFor == EntityKind.Instance)
                    ))
                {
                    var grps = (mth != null && mth.Success) ? mth.Groups["g"].Captures : mth2.Groups["g"].Captures;
                    var urps = (mth != null && mth.Success) ? mth.Groups["u"].Captures : mth2.Groups["u"].Captures;
                    dlp.quoted = false;
                    StringBuilder sb      = new StringBuilder();
                    var           first   = true;
                    bool          success = true;
                    foreach (var n in grps)
                    {
                        var ss = n.ToString();
                        if (first)
                        {
                            first = false;
                            if (madeFor == EntityKind.Role || madeFor == EntityKind.DataRole)
                            {
                                var nn = lex.toDL_Simple(ss, CNL.EN.endict.WordKind.PastParticiple);
                                if (lex.toN_Simple(nn, CNL.EN.endict.WordKind.PastParticiple) != ss)
                                {
                                    success    = false;
                                    dlp.quoted = true;
                                    dlp.name   = parts.name + (string.IsNullOrEmpty(sufix) ? "" : "_" + sufix);
                                    break;
                                }
                                sb.Append(nn);
                                continue;
                            }
                        }
                        else
                        {
                            sb.Append("-");
                        }
                        sb.Append(madeFor == EntityKind.Instance && !(urps.Count > 0) ? char.ToUpper(ss[0]) : char.ToLower(ss[0]));
                        sb.Append(ss.Substring(1));
                    }
                    if (success)
                    {
                        dlp.name = sb.ToString() + (string.IsNullOrEmpty(sufix) ? "" : "_" + sufix);
                        if (urps.Count > 0)
                        {
                            dlp.name = "[" + dlp.name + "]";
                        }
                    }
                }
                else
                {
                    dlp.quoted = true;
                    dlp.name   = parts.name + (string.IsNullOrEmpty(sufix) ? "" : "_" + sufix);
                }
            }

            if (madeFor == EntityKind.Instance)
            {
                dlp.name = IRIParser.decodeIRI(dlp.name);
            }

            dlp.local = ((madeFor == EntityKind.Instance) && !parts.global);

            return(dlp.Combine());
        }
Beispiel #2
0
 public Parts Split()
 {
     if (cache == null)
     {
         cache = new Dictionary <string, Parts>();
     }
     try
     {
         string key = iri.toString();
         if (cache.ContainsKey(key))
         {
             return(cache[key]);
         }
         else
         {
             if (iri.getScheme() == "file")
             {
                 string shortForm = iri.getFragment();
                 string ns        = iri.getNamespace();
                 bool   isGlobal  = shortForm.StartsWith(globalInstanceIndicator);
                 if (isGlobal)
                 {
                     shortForm = shortForm.Substring(1);
                 }
                 cache[key] = new Parts()
                 {
                     ns = ns, name = shortForm, global = isGlobal
                 };
                 return(cache[key]);
             }
             else
             {
                 Uri u = new Uri(key, UriKind.RelativeOrAbsolute);
                 if (u.IsAbsoluteUri)
                 {
                     if (!System.String.IsNullOrWhiteSpace(u.Fragment))
                     {
                         if (u.Fragment.StartsWith("#"))
                         {
                             string shortForm = Uri.UnescapeDataString(u.Fragment.Substring(1));
                             string ns        = key.Substring(0, key.Length - IRIParser.encodeToIRI(shortForm).Length - 1) + "#";
                             bool   isGlobal  = shortForm.StartsWith(globalInstanceIndicator);
                             if (isGlobal)
                             {
                                 shortForm = shortForm.Substring(1);
                             }
                             cache[key] = new Parts()
                             {
                                 ns = ns, name = shortForm, global = isGlobal
                             };
                             return(cache[key]);
                         }
                     }
                     if (!System.String.IsNullOrWhiteSpace(u.Segments[u.Segments.Count() - 1]))
                     {
                         string shortForm        = Uri.UnescapeDataString(u.Segments[u.Segments.Count() - 1]);
                         var    segmFullyEncoded = IRIParser.encodeToIRI(IRIParser.decodeIRI(u.Segments[u.Segments.Count() - 1]));
                         string ns       = u.OriginalString.Substring(0, u.OriginalString.Length - segmFullyEncoded.Length);
                         bool   isGlobal = shortForm.StartsWith(globalInstanceIndicator);
                         if (isGlobal)
                         {
                             shortForm = shortForm.Substring(1);
                         }
                         cache[key] = new Parts()
                         {
                             ns = ns, name = shortForm, global = isGlobal
                         };
                         return(cache[key]);
                     }
                 }
                 else
                 {
                     string str = IRIParser.decodeIRI(iri.toString());
                     if (str.Contains('#'))
                     {
                         var    pr        = str.Split(new char[] { '#' });
                         string shortForm = pr[1];
                         string ns        = pr[0] + "#";
                         bool   isGlobal  = shortForm.StartsWith(globalInstanceIndicator);
                         if (isGlobal)
                         {
                             shortForm = shortForm.Substring(1);
                         }
                         return(new Parts()
                         {
                             ns = ns, name = shortForm, global = isGlobal
                         });
                     }
                     else
                     {
                         bool isGlobal = str.StartsWith(globalInstanceIndicator);
                         if (isGlobal)
                         {
                             str = str.Substring(1);
                         }
                         return(new Parts()
                         {
                             name = str, global = isGlobal
                         });
                     }
                 }
             }
         }
         return(null);
     }
     catch (Exception)
     {
         string str = IRIParser.decodeIRI(iri.toString());
         if (str.Contains('#'))
         {
             var    pr        = str.Split(new char[] { '#' });
             string shortForm = pr[1];
             string ns        = pr[0] + "#";
             bool   isGlobal  = shortForm.StartsWith(globalInstanceIndicator);
             if (isGlobal)
             {
                 shortForm = shortForm.Substring(1);
             }
             return(new Parts()
             {
                 ns = ns, name = shortForm, global = isGlobal
             });
         }
         else
         {
             bool isGlobal = str.StartsWith(globalInstanceIndicator);
             if (isGlobal)
             {
                 str = str.Substring(1);
             }
             return(new Parts()
             {
                 name = str, global = isGlobal
             });
         }
     }
 }