예제 #1
0
파일: OWLFile.cs 프로젝트: roddickchen/NCDK
        public new static EntryDictionary Unmarshal(TextReader reader)
        {
            EntryDictionary dict = new OWLFile();

            try
            {
                var text = reader.ReadToEnd();

                var doc  = XDocument.Parse(text);
                var root = doc.Root;
                Debug.WriteLine($"Found root element: {root.Name}");

                // Extract ownNS from root element
                //            final string ownNS = root.GetBaseURI();
                string ownNS = root.Attribute("xmlns").Value;
                dict.NS = ownNS;

                Debug.WriteLine($"Found ontology namespace: {ownNS}");

                // process the defined facts
                var entries = root.Elements();
                //Trace.TraceInformation("Found #elements in OWL dict:", entries.Count());
                foreach (var entry in entries)
                {
                    if (entry.Name.NamespaceName.Equals(ownNS, StringComparison.Ordinal))
                    {
                        Entry dbEntry = Unmarshal(entry, ownNS);
                        dict.AddEntry(dbEntry);
                        Debug.WriteLine($"Added entry: {dbEntry}");
                    }
                    else
                    {
                        Debug.WriteLine($"Found a non-fact: {entry.Name}");
                    }
                }
            }
            catch (XmlException ex)
            {
                Trace.TraceError($"Dictionary is not well-formed: {ex.Message}");
                Debug.WriteLine("Error at line " + ex.LineNumber, ", column " + ex.LinePosition);
                dict = null;
            }
            catch (IOException ex)
            {
                Trace.TraceError($"Due to an IOException, the parser could not check:{ex.Message}");
                Debug.WriteLine(ex);
                dict = null;
            }
            return(dict);
        }
예제 #2
0
        private static EntryDictionary ReadDictionary(string databaseLocator, string type, Dictionary <string, EntryDictionary> cache)
        {
            EntryDictionary dictionary;

            // to distinguish between OWL: QSAR & REACT
            if (type.Contains("_React"))
            {
                databaseLocator += "." + type.Substring(0, type.Length - 6);
            }
            else
            {
                databaseLocator += "." + type;
            }
            if (cache.ContainsKey(databaseLocator))
            {
                return(cache[databaseLocator]);
            }
            else
            {
                Trace.TraceInformation($"Reading dictionary from {databaseLocator}");
                try
                {
                    var reader = new StreamReader(ResourceLoader.GetAsStream(databaseLocator));
                    switch (type)
                    {
                    case "owl":
                        dictionary = OWLFile.Unmarshal(reader);
                        break;

                    case "owl_React":
                        dictionary = OWLReact.Unmarshal(reader);
                        break;

                    default:
                        // assume XML using Castor
                        dictionary = EntryDictionary.Unmarshal(reader);
                        break;
                    }
                }
                catch (Exception exception)
                {
                    dictionary = null;
                    Trace.TraceError($"Could not read dictionary {databaseLocator}");
                    Debug.WriteLine(exception);
                }
                cache[databaseLocator] = dictionary;
                return(dictionary);
            }
        }