Ejemplo n.º 1
0
        /// <summary>
        /// Translate a CNL(DL) name to a OWL name
        /// </summary>
        /// <param name="dl">the CNL name</param>
        /// <param name="lex"></param>
        /// <param name="pfx2ns"></param>
        /// <param name="madeFor">the type of this entity (Concept, Instance, Role,...)</param>
        /// <returns></returns>
        public OwlName FromDL(CogniPy.CNL.DL.DlName dl, CNL.EN.endict lex, Func <string, string> pfx2ns, EntityKind madeFor)
        {
            var owlParts = new OwlName.Parts();
            var dlParts  = dl.Split();

            if (dlParts.term == null)
            {
                owlParts.ns = pfx2ns(null);// OWLOntologyXMLNamespaceManager namespaceManager namespaceManager.getDefaultNamespace();
            }
            else
            {
                owlParts.ns = pfx2ns(dlParts.term);//namespaceManager.getNamespaceForPrefix(dlParts.term);
                if (owlParts.ns == null)
                {
                    owlParts.ns = "http://ontorion.com/unknown.owl/" + dlParts.term + "#";
                }
            }
            if (!dlParts.quoted)
            {
                var    name_part = dlParts.name;
                string sufix     = "";
                if (madeFor == EntityKind.SWRLVariable)
                {
                    var a = name_part.Split('_');
                    if (a.Length == 2)
                    {
                        name_part = a.First();
                        sufix     = a.Last();
                    }
                }

                var           pp         = name_part.Split(new char[] { '-' });
                StringBuilder sb         = new StringBuilder();
                bool          first      = true;
                string        oldpp      = "";
                bool          wereDigits = false;
                foreach (var p in pp)
                {
                    if (first)
                    {
                        first = false;
                        if (madeFor == EntityKind.Role || madeFor == EntityKind.DataRole)
                        {
                            var nn = lex.toN_Simple(p, CNL.EN.endict.WordKind.PastParticiple);
                            sb.Append(nn);
                        }
                        else
                        {
                            sb.Append((madeFor == EntityKind.Instance || (madeFor == EntityKind.SWRLVariable /* && p.Length==1 */)) ?
                                      char.ToUpper(p[0]) : char.ToLower(p[0]));
                            sb.Append(p.Substring(1));
                        }
                    }
                    else
                    {
                        if (oldpp.Length > 1 && char.IsLetter(oldpp[0]) && char.ToUpper(oldpp.Last()) == oldpp.Last() && char.IsLetter(p[0]))
                        {
                            sb.Append(exoticUTFForUnderscore);
                        }
                        if (char.IsDigit(p[0]))
                        {
                            if (wereDigits)
                            {
                                sb.Append("-");
                            }
                            sb.Append(p);
                            wereDigits = true;
                        }
                        else
                        {
                            sb.Append(char.ToUpper(p[0]));
                            sb.Append(p.Substring(1));
                            wereDigits = false;
                        }
                    }
                    oldpp = p;
                }
                owlParts.name = sb.ToString() + (string.IsNullOrEmpty(sufix) ? "" : "_" + sufix);
            }
            else
            {
                owlParts.name = dlParts.name;
            }

            owlParts.global = (madeFor == EntityKind.Instance && !dlParts.local);

            return(owlParts.Combine());
        }
Ejemplo n.º 2
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());
        }