public async Task <ActionResult <OClass> > PostOClass(OClass oClass)
        {
            _context.OClass.Add(oClass);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetOClass", new { id = oClass.Id }, oClass));
        }
        public async Task <IActionResult> PutOClass(Guid id, OClass oClass)
        {
            if (id != oClass.Id)
            {
                return(BadRequest());
            }

            _context.Entry(oClass).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OClassExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #3
0
        public bool AddToTree(OClass ontologyClass)
        {
            bool bAdded = false;
            OClass currentRoot = null;

            if (ontologyClass.IsSubClassOf != null)
            {
                foreach (OClass root in rootClasses)
                {
                    if (root.ContainsOntologyClass(ontologyClass.IsSubClassOf))
                    {
                        currentRoot = root;
                        bAdded = currentRoot.AddOClassChild(ontologyClass);
                        if (bAdded) break;
                    }
                }
            }

            if (!bAdded)
            {
                #region otherwise create new root entry
                rootClasses.Add(ontologyClass);
                currentRoot = ontologyClass;
                bAdded = true;
                #endregion
            }

            if (currentRoot == null)
            {
                return false;
            }

            if (bAdded && currentRoot != null)
            {
                // cross check tree structure vs. updated tree
                List<OClass> lClassesToRemove = new List<OClass>();
                foreach (OClass otherRoot in rootClasses)
                {
                    if (!currentRoot.Equals(otherRoot) && otherRoot.IsSubClassOf != null && currentRoot.ContainsOntologyClass(otherRoot.IsSubClassOf))
                    {
                        currentRoot.AddOClassChild(otherRoot);
                        lClassesToRemove.Add(otherRoot);
                    }
                }

                foreach (OClass classToRemove in lClassesToRemove)
                {
                    this.RemoveFromTree(classToRemove);
                }
            }
            else
            {
                Console.WriteLine("should not happen.");
            }

            return bAdded;
        }
Exemple #4
0
 public ODataItem(OClass originClass)
 {
     currentClass = originClass;
     allowedProperties = new List<string>();
     allowedProperties = currentClass.GetAttributeNames();
     updateProperties = new Dictionary<string, List<string>>();
     foreach (string prop in allowedProperties)
     {
         updateProperties.Add(prop, new List<string>());
     }
 }
Exemple #5
0
        public void Whatever()
        {
            GameThread.IgnoreThreadRestrictions = true;
            using (var engine = new GameEngine(null))
            {
                engine.EngineInitialized.WaitOne();
                var o = new OClass(engine);

                engine.Javascript.Execute("var a = new Array();");
                var obj = engine.Javascript.ExecuteAndReturn("a");
            }
        }
Exemple #6
0
        private void InitializeJS()
        {
            this.AssertRunningOnThisThread();

            Javascript = new JavascriptEngine();
            Javascript.Script.console = new
            {
                log = new Action <object>(ScriptLog)
            };
            O = new OClass(this);
#pragma warning disable CC0021 // Use nameof
            Javascript.AddObject("O", O);
#pragma warning restore CC0021 // Use nameof
            O.Init();
            if (Resources != null)
            {
                Javascript.Execute(Resources.ReadEntryPoint());
            }
            ((ManualResetEvent)EngineInitialized).Set();
        }
Exemple #7
0
        public void Spec()
        {
            GameThread.IgnoreThreadRestrictions = true;
            using (var engine = new GameEngine(null))
            {
                engine.EngineInitialized.WaitOne();
                var o = new OClass(engine);
                engine.Javascript.Execute("O.state.test = {}");
                engine.Javascript.Execute("O.state.test.jimmy = 12;");
                var val = engine.Javascript.Script.O.state.test.jimmy;
                //    var list = engine.StateHistory.GetLatestChanges()
                //        .Where(x => x.Name.StartsWith("O.state.test"))
                //        .ToArray();

                //    Assert.AreEqual(12, val);
                //    Assert.AreEqual("O.state.test", list[0].Name);
                //    Assert.AreEqual("{}", list[0].Change);
                //    Assert.AreEqual("O.state.test.jimmy", list[1].Name);
                //    Assert.AreEqual("12", list[1].Change);
            }
        }
Exemple #8
0
        public bool RemoveFromTree(OClass oClassToRemove)
        {
            #region if class-to-remove is a root-element
            if (rootClasses.Contains(oClassToRemove))
            {
                return rootClasses.Remove(oClassToRemove);
            }
            #endregion

            #region if class-to-remove is nested into tree structure - walk through and delete within
            foreach (OClass rootClass in rootClasses)
            {
                if (rootClass.ContainsOntologyClass(oClassToRemove))
                {
                    return rootClass.RemoveOClassChild(oClassToRemove);
                }
            }
            #endregion

            return false;
        }
Exemple #9
0
 public int GetOntologyClassLevel(OClass refOClass)
 {
     int iLevel;
     foreach (OClass rootClass in rootClasses)
     {
         iLevel = rootClass.GetOntologyClassLevel(refOClass);
         if (iLevel >= 0)
             return iLevel;
     }
     return -1;
 }
Exemple #10
0
 public OClass GetOntologyClass(OClass refOClass)
 {
     OClass retClass;
     foreach (OClass rootClass in rootClasses)
     {
         retClass = rootClass.GetOntologyClass(refOClass);
         if (retClass != null)
             return retClass;
     }
     return null;
 }
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;
            StreamWriter sw = new StreamWriter(now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + ".log");
            while (true)
            {
                #region define logging actions
                Action<string> LogDevNull = new Action<string>((msg) => { });
                Action<string> LogError = new Action<string>((msg) =>
                {
                    Console.WriteLine(DateTime.Now + " " + msg);
                });
                Action<string> LogMessage = new Action<string>((msg) =>
                {
                    sw.WriteLine(DateTime.Now + " " + msg);
                    sw.Flush();
                    Console.WriteLine(DateTime.Now + " " + msg);
                });
                #endregion

                #region check file existence and load ontology
                Ontology thisOntology = null;
                try
                {
                    string ontologyFilename = "dbpedia_3.6.owl";
                    de.sones.solutions.lib.xml.XmlElement rootElement = XmlParser.loadXml(new StreamReader(ontologyFilename), LogError);
                    if (rootElement == null)
                    {
                        LogError("Null or empty XmlStructure in file: '" + ontologyFilename + "'");
                        break;
                    }
                    thisOntology = OwlParser.CreateOntologyFromXml(rootElement, LogDevNull);

                    #region add Thing manually!
                    OClass thing = new OClass("http://www.w3.org/2002/07/owl#Thing");

                    OProperty oPropertyName = new OProperty();
                    oPropertyName.ID = "Name";
                    oPropertyName.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyName);

                    OProperty oPropertyLongAbstract = new OProperty();
                    oPropertyLongAbstract.ID = "LongAbstract";
                    oPropertyLongAbstract.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyLongAbstract);

                    OProperty oPropertyShortAbstract = new OProperty();
                    oPropertyShortAbstract.ID = "ShortAbstract";
                    oPropertyShortAbstract.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyShortAbstract);

                    thisOntology.AddOntologyClass(thing);
                    #endregion
                }
                catch (Exception a)
                {
                    LogError("Error parsing ontology!");
                    LogError(a.Message);
                    LogError(a.StackTrace);

                    break;
                }
                #endregion

                #region ask for langs to import --> de, en
                List<String> lLangs = new List<string>();
                Console.WriteLine("Which language data should be added to the database?");
                Console.WriteLine("Enter language id and press <Enter>");
                Console.WriteLine("An empty line quits the iteration process.");
                String currentLine = null;
                bool bFirst = true;
                while ((currentLine = Console.ReadLine().Trim()) != "")
                {
                    lLangs.Add(currentLine);

                    bFirst = true;
                    Console.Write("CurrentlySelected: ");
                    foreach (String lang in lLangs)
                    {
                        if (bFirst)
                            bFirst = false;
                        else Console.Write(", ");
                        Console.Write(lang);
                    }
                    Console.WriteLine();
                }
                #endregion

                #region load dictExistingNodes
                Dictionary<string, List<string>> dictExistingNodes = new Dictionary<string, List<string>>();
                try
                {
                    StreamReader sr = new StreamReader("ExistingNodes.dict");
                    String line = null;
                    String[] strToken = null;
                    char[] cToken = { ';' };
                    List<string> lData = null;
                    while ((line = sr.ReadLine()) != null)
                    {
                        strToken = line.Split(cToken, StringSplitOptions.RemoveEmptyEntries);
                        if (strToken.Length != 4)
                        {
                            LogError("ERROR: load dictExistingNodes: reading line: " + line);
                            continue;
                        }
                        else
                        {
                            lData = new List<string>();
                            lData.Add(strToken[1]);
                            lData.Add(strToken[2]);
                            lData.Add(strToken[3]);
                            dictExistingNodes.Add(strToken[0], lData);
                        }
                    }
                }
                catch (Exception a)
                {
                    LogError("Error load dictExistingNodes");
                    LogError(a.Message);
                    LogError(a.StackTrace);
                    break;
                }
                LogMessage("dictExistingNodes.Count=" + dictExistingNodes.Count);
                #endregion

                #region write data to XmlBulkImport format
                try
                {
                    #region local vars
                    String[] token;
                    String Key, Type, Subject, Filename;
                    List<string> Value = null;
                    // long VertexID;
                    char[] sepChars = { '=' };
                    ODataItem dataItem = null;
                    Dictionary<string, List<string>> dictNodeAttributes;
                    uint fileCounter = 0;
                    uint lineCount = 0;
                    #endregion

                    #region begin writing xml structure (header)
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    XmlWriter writer = XmlWriter.Create(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.XmlFile, settings);

                    writer.WriteStartDocument();  // <?xml version="1.0" encoding="UTF-8"?> ???

                    // <BulkImport xmlns="http://schema.sones.com/graphds/xmlbulkimport.xsd">
                    writer.WriteStartElement("BulkImport", "http://schema.sones.com/graphds/xmlbulkimport.xsd");

                    writer.WriteStartElement("Import");   // <Import>
                    #endregion

                    #region process all data files
                    LogMessage("begin 'process all data files'");
                    foreach (string dirname in Directory.EnumerateDirectories(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir))
                    {
                        foreach (string filename in Directory.EnumerateFiles(dirname))
                        {
                            Subject = filename.Substring(filename.LastIndexOf(Path.DirectorySeparatorChar) + 1, (filename.Length - filename.LastIndexOf(Path.DirectorySeparatorChar) - 1));

                            #region transfer all atributes from filestream to dictionary (for better handling)
                            dictNodeAttributes = new Dictionary<string, List<string>>();
                            foreach (string line in File.ReadAllLines(filename))
                            {
                                token = line.Split(sepChars);
                                if (token.Length < 2)
                                {
                                    LogError("Invalid line content: " + line);
                                    continue;
                                }
                                Key = token[0];

                                Value = new List<string>();
                                for (int i = 1; i < token.Length; i++)
                                {
                                    Value.Add(token[i]);
                                }

                                if (RemoveEvilCharactersForFile(Key).Equals(Subject))
                                    Key = RemoveEvilCharactersForFile(Key);
                                if (!dictNodeAttributes.ContainsKey(Key))
                                    dictNodeAttributes.Add(Key, Value);
                                else
                                {
                                    foreach (String value in Value)
                                    {
                                        dictNodeAttributes[Key].Add(value);
                                    }
                                }
                            }
                            #endregion

                            #region check data validity before starting evaluation
                            dataItem = null;
                            if (!dictNodeAttributes.ContainsKey(Subject) || dictNodeAttributes[Subject] == null)
                            {
                                LogError("Data file contains no Type of the Instance!" + filename); // Subject);
                                continue;
                            }
                            #endregion

                            #region process data
                            else
                            {
                                #region write <Insert VertexType="..." VertexId="...">
                                // <Insert VertexType="User" VertexID="-9223372036854775808">
                                Type = dictNodeAttributes[Subject][0];
                                writer.WriteStartElement("Insert");
                                writer.WriteAttributeString("VertexType", GqlStringExtension.RemoveEvilCharacters(Type));
                                writer.WriteAttributeString("VertexID", dictNodeAttributes["VertexID"][0]); // VertexID.ToString());
                                #endregion

                                #region write all other attributes
                                foreach (String key in dictNodeAttributes.Keys)
                                {
                                    if (!key.Equals(Subject) && !key.Equals("VertexID"))
                                    {
                                        #region get attribute's data-type information from Ontology
                                        OClass oClass = thisOntology.GetOClass(Type);
                                        string searchkey = key;
                                        if (key.LastIndexOf('_') > 0)
                                        {
                                            searchkey = key.Substring(0, key.LastIndexOf('_'));
                                        }
                                        OProperty oProp = oClass.GetProperty(searchkey);
                                        #endregion

                                        #region <SetValue ..> for all attributes
                                        if (oProp == null || oProp.Range == null || !oProp.Range.StartsWith("http://dbpedia.org/ontology/"))  // undefined attribute
                                        {
                                            // <SetValue Key="Name" Value="SGFydHdpZw=="/>

                                            #region write undefined attribute
                                            if (oProp == null || oProp.Range == null)
                                            {
                                                StringBuilder sbValue = new StringBuilder();
                                                foreach (String value in dictNodeAttributes[key])
                                                {
                                                    sbValue.Append(value);
                                                }

                                                writer.WriteStartElement("SetValue");
                                                writer.WriteAttributeString("AttributeName", GqlStringExtension.RemoveEvilCharacters(key));
                                                writer.WriteAttributeString("Value", Convert.ToBase64String(Encoding.Default.GetBytes(sbValue.ToString())));
                                                writer.WriteEndElement();  // </SetValue>
                                            }
                                            #endregion
                                            #region write ontology attribute (includes attribute data-type handling)
                                            else
                                            {
                                                String value = null;
                                                try
                                                {
                                                    switch (oProp.Range)
                                                    {
                                                        #region W3.org number data types
                                                        case "http://www.w3.org/2001/XMLSchema#integer":
                                                        case "http://www.w3.org/2001/XMLSchema#nonNegativeInteger":
                                                        case "http://www.w3.org/2001/XMLSchema#positiveInteger":
                                                        case "http://www.w3.org/2001/XMLSchema#gYear":
                                                            {
                                                                value = Convert.ToBase64String(Encoding.Default.GetBytes(Convert.ToInt64(dictNodeAttributes[key][0]).ToString(new CultureInfo("en-US"))));//.ToString()).ToString()));
                                                                break;
                                                            }
                                                        case "http://www.w3.org/2001/XMLSchema#double":
                                                        case "http://www.w3.org/2001/XMLSchema#float":
                                                            {
                                                                value = Convert.ToBase64String(Encoding.Default.GetBytes(Convert.ToDouble(dictNodeAttributes[key][0]).ToString(new CultureInfo("en-US"))));
                                                                break;
                                                            }
                                                        case "http://www.w3.org/2001/XMLSchema#boolean":
                                                            {
                                                                if (dictNodeAttributes[key][0].ToString().Equals("True") || dictNodeAttributes[key][0].ToString().Equals("1"))
                                                                    value = Convert.ToBase64String(Encoding.Default.GetBytes("True"));
                                                                else
                                                                    value = Convert.ToBase64String(Encoding.Default.GetBytes("False"));
                                                                break;
                                                            }

                                                        #endregion
                                                        #region dbpedia
                                                        case "http://dbpedia.org/datatype/squareKilometre":
                                                        case "http://dbpedia.org/datatype/squareMetre":
                                                        case "http://dbpedia.org/datatype/kilometrePerSecond":
                                                        case "http://dbpedia.org/datatype/kilometrePerHour":
                                                        case "http://dbpedia.org/datatype/kilogramPerCubicMetre":
                                                        case "http://dbpedia.org/datatype/gramPerKilometre":
                                                        case "http://dbpedia.org/datatype/day":
                                                        case "http://dbpedia.org/datatype/hour":
                                                        case "http://dbpedia.org/datatype/minute":
                                                        case "http://dbpedia.org/datatype/second":
                                                        case "http://dbpedia.org/datatype/cubicMetre":
                                                        case "http://dbpedia.org/datatype/cubicKilometre":
                                                        case "http://dbpedia.org/datatype/cubicCentimetre":
                                                        case "http://dbpedia.org/datatype/kilometre":
                                                        case "http://dbpedia.org/datatype/metre":
                                                        case "http://dbpedia.org/datatype/centimetre":
                                                        case "http://dbpedia.org/datatype/millimetre":
                                                        case "http://dbpedia.org/datatype/inhabitantsPerSquareKilometre":
                                                        case "http://dbpedia.org/datatype/kelvin":
                                                        case "http://dbpedia.org/datatype/cubicMetrePerSecond":
                                                        case "http://dbpedia.org/datatype/kilogram":
                                                        case "http://dbpedia.org/datatype/megabyte":
                                                        case "http://dbpedia.org/datatype/litre":
                                                        case "http://dbpedia.org/datatype/engineConfiguration":
                                                        case "http://dbpedia.org/datatype/kilowatt":
                                                        case "http://dbpedia.org/datatype/newtonMetre":
                                                        #endregion
                                                        #region currencies
                                                        case "http://dbpedia.org/datatype/usDollar":
                                                        case "http://dbpedia.org/datatype/euro":
                                                        case "http://dbpedia.org/datatype/bermudianDollar":
                                                        case "http://dbpedia.org/datatype/nicaraguanCórdoba":
                                                        case "http://dbpedia.org/datatype/poundSterling":
                                                        case "http://dbpedia.org/datatype/japaneseYen":
                                                        case "http://dbpedia.org/datatype/swedishKrona":
                                                        case "http://dbpedia.org/datatype/canadianDollar":
                                                        case "http://dbpedia.org/datatype/liberianDollar":
                                                        case "http://dbpedia.org/datatype/norwegianKrone":
                                                        case "http://dbpedia.org/datatype/namibianDollar":
                                                        case "http://dbpedia.org/datatype/ukrainianHryvnia":
                                                        case "http://dbpedia.org/datatype/czechKoruna":
                                                        case "http://dbpedia.org/datatype/swissFranc":
                                                        case "http://dbpedia.org/datatype/malaysianRinggit":
                                                        case "http://dbpedia.org/datatype/newZealandDollar":
                                                        case "http://dbpedia.org/datatype/danishKrone":
                                                        case "http://dbpedia.org/datatype/philippinePeso":
                                                        case "http://dbpedia.org/datatype/southKoreanWon":
                                                        case "http://dbpedia.org/datatype/hongKongDollar":
                                                        case "http://dbpedia.org/datatype/australianDollar":
                                                        case "http://dbpedia.org/datatype/indianRupee":
                                                        case "http://dbpedia.org/datatype/russianRouble":
                                                        case "http://dbpedia.org/datatype/singaporeDollar":
                                                        case "http://dbpedia.org/datatype/icelandKrona":
                                                        case "http://dbpedia.org/datatype/bosniaAndHerzegovinaConvertibleMarks":
                                                        case "http://dbpedia.org/datatype/polishZłoty":
                                                        case "http://dbpedia.org/datatype/latvianLats":
                                                        case "http://dbpedia.org/datatype/croatianKuna":
                                                        case "http://dbpedia.org/datatype/iranianRial":
                                                        case "http://dbpedia.org/datatype/egyptianPound":
                                                        case "http://dbpedia.org/datatype/lithuanianLitas":
                                                        case "http://dbpedia.org/datatype/pakistaniRupee":
                                                        case "http://dbpedia.org/datatype/bhutaneseNgultrum":
                                                        case "http://dbpedia.org/datatype/romanianNewLeu":
                                                        case "http://dbpedia.org/datatype/bangladeshiTaka":
                                                        case "http://dbpedia.org/datatype/nigerianNaira":
                                                        case "http://dbpedia.org/datatype/saudiRiyal":
                                                        case "http://dbpedia.org/datatype/brazilianReal":
                                                        case "http://dbpedia.org/datatype/turkishLira":
                                                        case "http://dbpedia.org/datatype/kazakhstaniTenge":
                                                        case "http://dbpedia.org/datatype/unitedArabEmiratesDirham":
                                                        case "http://dbpedia.org/datatype/mexicanPeso":
                                                        case "http://dbpedia.org/datatype/newTaiwanDollar":
                                                        case "http://dbpedia.org/datatype/hungarianForint":
                                                        case "http://dbpedia.org/datatype/falklandIslandsPound":
                                                        case "http://dbpedia.org/datatype/belizeDollar":
                                                        case "http://dbpedia.org/datatype/chileanPeso":
                                                        case "http://dbpedia.org/datatype/renminbi":
                                                        case "http://dbpedia.org/datatype/thaiBaht":
                                                        case "http://dbpedia.org/datatype/papuaNewGuineanKina":
                                                        case "http://dbpedia.org/datatype/kuwaitiDinar":
                                                        case "http://dbpedia.org/datatype/israeliNewSheqel":
                                                        case "http://dbpedia.org/datatype/sriLankanRupee":
                                                        case "http://dbpedia.org/datatype/peruvianNuevoSol":
                                                        case "http://dbpedia.org/datatype/estonianKroon":
                                                        case "http://dbpedia.org/datatype/southAfricanRand":
                                                        case "http://dbpedia.org/datatype/argentinePeso":
                                                        case "http://dbpedia.org/datatype/jamaicanDollar":
                                                        case "http://dbpedia.org/datatype/qatariRial":
                                                        #endregion
                                                            {
                                                                value = Convert.ToBase64String(Encoding.Default.GetBytes(Convert.ToDouble(dictNodeAttributes[key][0]).ToString(new CultureInfo("en-US"))));
                                                                break;
                                                            }
                                                        #region date
                                                        case "http://www.w3.org/2001/XMLSchema#date":
                                                            {
                                                                value = Convert.ToBase64String(Encoding.Default.GetBytes(Convert.ToDateTime(dictNodeAttributes[key][0]).ToString(new CultureInfo("en-US"))));
                                                                break;
                                                            }
                                                        case "http://www.w3.org/2001/XMLSchema#string":
                                                        case "http://www.w3.org/2001/XMLSchema#anyURI":
                                                        #endregion
                                                        #region workaround simple types
                                                        case "http://dbpedia.org/datatype/valvetrain":
                                                        case "http://dbpedia.org/datatype/fuelType":
                                                        #endregion
                                                            {
                                                                StringBuilder sbValue = new StringBuilder();
                                                                foreach (String strValue in dictNodeAttributes[key])
                                                                {
                                                                    sbValue.Append(strValue);
                                                                }

                                                                value = Convert.ToBase64String(Encoding.Default.GetBytes(sbValue.ToString()));
                                                                break;
                                                            }
                                                        default:
                                                            {
                                                                break;
                                                            }
                                                    }
                                                }
                                                catch (Exception a)
                                                {
                                                    StringBuilder sbValue = new StringBuilder();
                                                    foreach (String strValue in dictNodeAttributes[key])
                                                    {
                                                        sbValue.Append(strValue);
                                                    }

                                                    LogError("Invalid data content!: " + sbValue);
                                                    value = null;
                                                }
                                                if (value != null)
                                                {
                                                    writer.WriteStartElement("SetValue");
                                                    writer.WriteAttributeString("AttributeName", GqlStringExtension.RemoveEvilCharacters(key));
                                                    writer.WriteAttributeString("Value", value);
                                                    writer.WriteEndElement();  // </SetValue>
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion
                                        #region <MultiLink ..> for all attributes that represent edges
                                        else
                                        {
                                            writer.WriteStartElement("MultiLink");   // <MultiLink Key="Friends">
                                            writer.WriteAttributeString("AttributeName", GqlStringExtension.RemoveEvilCharacters(key));

                                            foreach (String value in dictNodeAttributes[key])
                                            {
                                                if (dictExistingNodes.ContainsKey(value))
                                                {
                                                    writer.WriteStartElement("Link");

                                                    // <Link VertexType="User" VertexID="-9223372036854775806"/>
                                                    // writer.WriteAttributeString("VertexType",  GqlStringExtension.RemoveEvilCharacters(oProp.Range));

                                                    writer.WriteAttributeString("VertexType", GqlStringExtension.RemoveEvilCharacters(dictExistingNodes[value][2]));
                                                    writer.WriteAttributeString("VertexID", dictExistingNodes[value][1]);

                                                    writer.WriteEndElement();
                                                }
                                            }
                                            writer.WriteEndElement(); // </MultiLink>
                                        }
                                        #endregion
                                    }
                                }
                                writer.WriteEndElement();  // </Insert>
                                #endregion
                            }  // end else   ( corr. if:(!dictNodeAttributes.ContainsKey(Subject) || dictNodeAttributes[Subject].Count < 1))
                            #endregion

                            #region some debug/processing info
                            fileCounter++;
                            if (fileCounter % 10000 == 0)
                            {
                                LogMessage("processed " + fileCounter + " items.");
                            }
                            #endregion
                        }  // string filename in Directory.EnumerateFiles(dirname)
                    }  // foreach (string dirname in Directory.EnumerateDirectories(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir))
                    LogMessage("end 'process all data files'");
                    #endregion

                    #region evaluate labels
                    LogMessage("begin 'evaluate labels'");
                    Dictionary<string, long> dictLabelIds = new Dictionary<string, long>();
                    Dictionary<string, Dictionary<string, List<long>>> dictInstanceLabels = new Dictionary<string, Dictionary<string, List<long>>>();
                    long currentLabelVertexID = long.MinValue;

                    foreach (String lang in lLangs)
                    {
                        if (Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang))
                        {
                            Filename = "labels_" + lang + ".nt";
                            if (!File.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang + Path.DirectorySeparatorChar + Filename))
                            {
                                continue;
                            }
                            uint insertedLabels = 0;
                            LogMessage("begin read labels from " + Filename);

                            using (StreamReader srNodes = new StreamReader(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang + Path.DirectorySeparatorChar + Filename))
                            {
                                #region if input stream is empty --> do error handling
                                if (srNodes == null)
                                {
                                    LogError("Error reading Nodes file: '" + Filename + "'");
                                    return;
                                }
                                #endregion

                                #region init local vars
                                String strCurrentTriple;
                                Triple currentTriple;
                                #endregion

                                #region for each triple (line)
                                while ((strCurrentTriple = srNodes.ReadLine()) != null)
                                {
                                    currentTriple = NTripleParser.Split(strCurrentTriple, LogError);

                                    #region some debug info
                                    if (lineCount % 1000 == 0)
                                    {
                                        Console.Write(".");
                                    }
                                    if (lineCount % 100000 == 0)
                                    {
                                        LogMessage("EvaluateLabels('" + lang + "'): lineCount=" + lineCount);
                                        GC.Collect();
                                        GC.Collect();
                                        GC.WaitForFullGCComplete();
                                    }
                                    lineCount++;
                                    #endregion

                                    if (currentTriple == null || currentTriple.TripleObject == null)
                                        continue;

                                    if (!dictExistingNodes.ContainsKey(currentTriple.Subject))
                                    {
                                        continue;
                                    }

                                    if (!dictLabelIds.ContainsKey(currentTriple.TripleObject))
                                    {
                                        dictLabelIds.Add(currentTriple.TripleObject, currentLabelVertexID);
                                        currentLabelVertexID++;

                                        // <Insert VertexType="Label" VertexID="-9223372036854775808">
                                        writer.WriteStartElement("Insert");
                                        writer.WriteAttributeString("VertexType", "Label");
                                        writer.WriteAttributeString("VertexID", dictLabelIds[currentTriple.TripleObject].ToString()); // VertexID.ToString());

                                        // <SetValue AttributeName="Name" Value="SGFydHdpZw=="/>
                                        writer.WriteStartElement("SetValue");
                                        writer.WriteAttributeString("AttributeName", "Name");
                                        writer.WriteAttributeString("Value", Convert.ToBase64String(Encoding.Default.GetBytes(currentTriple.TripleObject)));
                                        writer.WriteEndElement(); // </SetValue>
                                        writer.WriteEndElement();  // </Insert>

                                        insertedLabels++;
                                    }

                                    if (!dictInstanceLabels.ContainsKey(currentTriple.Subject))
                                    {
                                        dictInstanceLabels.Add(currentTriple.Subject, new Dictionary<string, List<long>>());
                                    }

                                    if (!dictInstanceLabels[currentTriple.Subject].ContainsKey(lang))
                                    {
                                        dictInstanceLabels[currentTriple.Subject].Add(lang, new List<long>());
                                    }
                                    dictInstanceLabels[currentTriple.Subject][lang].Add(currentLabelVertexID - 1);
                                }  // while ((strCurrentTriple = srNodes.ReadLine()) != null)
                                #endregion
                            }  // using (StreamReader srNodes = new StreamReader(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang + Path.DirectorySeparatorChar + Filename))
                            LogMessage("end read labels from " + Filename + " inserted " + insertedLabels + "entries");
                        }  // if (Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang))
                    }  // foreach (String lang in lLangs)
                    LogMessage("end 'evaluate labels'");
                    #endregion

                    #region create all instance entries in dictionary
                    LogMessage("begin 'create all instance entries in dictionary'");
                    Dictionary<string, List<KeyValuePair<long, string>>> dictInstances = new Dictionary<string, List<KeyValuePair<long, string>>>();
                    Dictionary<string, long> dictInstanceVertexIDs = new Dictionary<string, long>();
                    long lInstanceVertexID_count = long.MinValue;
                    foreach (String instance in dictExistingNodes.Keys)
                    {
                        List<KeyValuePair<long, string>> lNodeIds = new List<KeyValuePair<long, string>>();
                        lNodeIds.Add(new KeyValuePair<long, string>(Int64.Parse(dictExistingNodes[instance][1]), dictExistingNodes[instance][2]));
                        dictInstances.Add(instance, lNodeIds);
                        dictInstanceVertexIDs.Add(instance, lInstanceVertexID_count);
                        lInstanceVertexID_count++;
                    }
                    LogMessage("end 'create all instance entries in dictionary'");
                    #endregion

                    #region evaluate disambiguations
                    LogMessage("begin evaluate disambiguations");
                    foreach (String lang in lLangs)
                    {
                        if (Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang))
                        {
                            Filename = "disambiguations_" + lang + ".nt";
                            if (!File.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang + Path.DirectorySeparatorChar + Filename))
                            {
                                continue;
                            }

                            using (StreamReader srNodes = new StreamReader(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang + Path.DirectorySeparatorChar + Filename))
                            {
                                #region if input stream is empty --> do error handling
                                if (srNodes == null)
                                {
                                    LogError("Error reading Nodes file: '" + Filename + "'");
                                    return;
                                }
                                #endregion

                                #region init local vars
                                String strCurrentTriple;
                                Triple currentTriple;
                                #endregion

                                #region for each triple (line), add or append dictInstances dictionary
                                while ((strCurrentTriple = srNodes.ReadLine()) != null)
                                {
                                    currentTriple = NTripleParser.Split(strCurrentTriple, LogError);

                                    if (dictExistingNodes.ContainsKey(currentTriple.Subject))
                                    {
                                        Console.WriteLine("should not happen currentTriple.Subject='" + currentTriple.Subject + "'");
                                        continue;
                                    }

                                    if (!dictExistingNodes.ContainsKey(currentTriple.TripleObject))
                                    {
                                        continue;
                                    }
                                    if (!dictInstanceVertexIDs.ContainsKey(currentTriple.Subject))
                                    {
                                        dictInstanceVertexIDs.Add(currentTriple.Subject, lInstanceVertexID_count);
                                        lInstanceVertexID_count++;
                                    }

                                    if (!dictInstances.ContainsKey(currentTriple.Subject))
                                    {
                                        dictInstances.Add(currentTriple.Subject, new List<KeyValuePair<long, string>>());
                                    }

                                    // string vertexType = "httpwwww3org200207owlThing";
                                    if (dictExistingNodes.ContainsKey(currentTriple.TripleObject))
                                    {
                                        // vertexType = dictExistingNodes[currentTriple.TripleObject][2];
                                        dictInstances[currentTriple.Subject].Add(new KeyValuePair<long, string>(Int64.Parse(dictExistingNodes[currentTriple.TripleObject][1]), dictExistingNodes[currentTriple.TripleObject][2]));
                                    }
                                    // dictInstances[currentTriple.Subject].Add(new KeyValuePair<long, string>(dictInstanceVertexIDs[currentTriple.TripleObject], vertexType));

                                } // while ((strCurrentTriple = srNodes.ReadLine()) != null)
                                #endregion
                            }  // using (StreamReader srNodes = new StreamReader(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang + Path.DirectorySeparatorChar + Filename))
                        }  // if (Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang))
                    }  // foreach (String lang in lLangs)
                    LogMessage("end evaluate disambiguations");
                    #endregion

                    #region create Instance xml bulk import format
                    LogMessage("begin create Instance xml bulk import format");
                    foreach (string currentInstance in dictInstances.Keys)
                    {
                        if (dictInstances[currentInstance].Count < 1)
                        {
                            continue;
                        }

                        writer.WriteStartElement("Insert");
                        writer.WriteAttributeString("VertexType", "Instance");
                        writer.WriteAttributeString("VertexID", dictInstanceVertexIDs[currentInstance].ToString());

                        #region add instance name
                        // <SetValue AttributeName="Name" Value="SGFydHdpZw=="/>
                        writer.WriteStartElement("SetValue");
                        writer.WriteAttributeString("AttributeName", "Name");
                        writer.WriteAttributeString("Value", Convert.ToBase64String(Encoding.Default.GetBytes(currentInstance)));
                        writer.WriteEndElement(); // </SetValue>
                        #endregion

                        #region add labels (for all langs)
                        if (dictInstanceLabels.ContainsKey(currentInstance))
                        {
                            Dictionary<string, List<long>> dictLangLabels = dictInstanceLabels[currentInstance];

                            if (dictLangLabels.Keys.Count == 0)
                                LogMessage("'dictLangLabels.Keys.Count == 0' for '" + currentInstance + "'");

                            foreach (string currentLang in dictLangLabels.Keys)
                            {
                                List<long> lLabels = dictLangLabels[currentLang];

                                if (lLabels.Count == 0)
                                    LogMessage("'lLabels.Count == 0' for currentLang='" + currentLang + "' and currentInstance='" + currentInstance + "'");
                                else
                                {
                                    writer.WriteStartElement("MultiLink");   // <MultiLink Key="Label_de">
                                    writer.WriteAttributeString("AttributeName", "Labels_" + currentLang);

                                    foreach (long currentLabelId in lLabels)
                                    {
                                        writer.WriteStartElement("Link");

                                        // <Link VertexType="User" VertexID="-9223372036854775806"/>
                                        // writer.WriteAttributeString("VertexType",  GqlStringExtension.RemoveEvilCharacters(oProp.Range));

                                        writer.WriteAttributeString("VertexType", "Label");
                                        writer.WriteAttributeString("VertexID", currentLabelId.ToString());

                                        writer.WriteEndElement();
                                    }
                                    writer.WriteEndElement(); // </MultiLink>
                                }
                            }
                        }
                        else LogMessage("!dictInstanceLabels.ContainsKey('" + currentInstance + "')");
                        #endregion

                        #region add disambiguation
                        if (dictInstances[currentInstance].Count > 0)
                        {
                            writer.WriteStartElement("MultiLink");   // <MultiLink Key="Label_de">
                            writer.WriteAttributeString("AttributeName", "RefersToEntry");

                            foreach (KeyValuePair<long, string> currentThingId in dictInstances[currentInstance])
                            {
                                writer.WriteStartElement("Link");

                                // <Link VertexType="User" VertexID="-9223372036854775806"/>
                                // writer.WriteAttributeString("VertexType",  GqlStringExtension.RemoveEvilCharacters(oProp.Range));

                                writer.WriteAttributeString("VertexType", GqlStringExtension.RemoveEvilCharacters(currentThingId.Value));
                                writer.WriteAttributeString("VertexID", currentThingId.Key.ToString());

                                writer.WriteEndElement();
                            }

                            writer.WriteEndElement(); // </MultiLink>
                        }  // if (dictInstances[currentInstance].Count > 0)
                        #endregion

                        writer.WriteEndElement(); // </Insert>
                    }  // foreach (string currentInstance in dictInstances.Keys)
                    LogMessage("end create Instance xml bulk import format");

                    #endregion

                    #region finish writing xml-structure
                    writer.WriteEndElement();  // </Import>
                    writer.WriteEndElement();  // </BulkImport>
                    writer.Flush();
                    writer.Close();
                    #endregion
                }
                catch (Exception a)
                {
                    LogError("Error reading data files.");
                    LogError(a.Message);
                    LogError(a.StackTrace);
                }
                #endregion

                break; // default behaviour
            } // while (true)
            sw.Close();

            Console.WriteLine("Press <Enter> to quit.");
            Console.ReadLine();
        }
Exemple #12
0
        // List<OClass> lOClasses = null;
        public bool AddOntologyClass(OClass newClass)
        {
            if (ClassTree == null)
                ClassTree = new OClassTree();

            if (newClass.ID == null /* && newClass.About == null*/)
                return false;

            bool bAdded = ClassTree.AddToTree(newClass);
            if (!bAdded)
                Console.WriteLine("Error! Didn't add <owl:class>: " + newClass);

            return true;
        }
        static void Main(string[] args)
        {
            while (true)
            {
                #region extract ontology filename from command line args
                String ontologyFilename = null;
                String gqlFilename = null;
                if (args.Length != 2)
                {
                    Console.WriteLine("Wrong number of arguments!");
                    Console.WriteLine("Usage: CreateSchemaFromOntology <OntologyFilename> <GqlFilename>");
                    Console.WriteLine("  e.g. CreateSchemaFromOntology dbpedia_3.6.owl dbpedia.gql");
                    Console.WriteLine("File to import should reside at the execution directory: " + Environment.CurrentDirectory);
                }
                else
                {
                    ontologyFilename = args[0];
                    gqlFilename = args[1];
                }
                #endregion

                #region check file existence and load ontology
                Ontology ontology = null;
                try
                {
                    XmlElement rootElement = XmlParser.loadXml(new StreamReader(ontologyFilename), Console.WriteLine);
                    if (rootElement == null)
                    {
                        Console.WriteLine("Null or empty XmlStructure in file: '" + ontologyFilename + "'");
                        break;
                    }
                    ontology = OwlParser.CreateOntologyFromXml(rootElement, Console.WriteLine); // .CreateOntologyFromXml(rootElement, Console.WriteLine);

                    #region add Thing manually! - with 3 attributes (Name, ShortAbstract, LongAbstract)
                    OClass thing = new OClass("http://www.w3.org/2002/07/owl#Thing");

                    OProperty oPropertyName = new OProperty();
                    oPropertyName.ID = "Name";
                    oPropertyName.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyName);

                    OProperty oPropertyLongAbstract = new OProperty();
                    oPropertyLongAbstract.ID = "LongAbstract";
                    oPropertyLongAbstract.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyLongAbstract);

                    OProperty oPropertyShortAbstract = new OProperty();
                    oPropertyShortAbstract.ID = "ShortAbstract";
                    oPropertyShortAbstract.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyShortAbstract);

                    ontology.AddOntologyClass(thing);
                    #endregion
                }
                catch (Exception a)
                {
                    Console.WriteLine(a.Message);
                    Console.WriteLine(a.StackTrace);

                    break;
                }
                #endregion

                #region ask for languages to add
                List<String> lLangs = new List<string>();
                Console.WriteLine("Which languages should be added to the Ontology?");
                Console.WriteLine("Enter language id and press <Enter>");
                Console.WriteLine("An empty line quits the iteration process.");
                String currentLine = null;
                bool bFirst = true;
                while ((currentLine = Console.ReadLine().Trim()) != "")
                {
                    lLangs.Add("_" + currentLine);

                    bFirst = true;
                    Console.Write("CurrentlySelected: ");
                    foreach (String lang in lLangs)
                    {
                        if (bFirst)
                            bFirst = false;
                        else Console.Write(", ");
                        Console.Write(lang);
                    }
                    Console.WriteLine();
                }
                #endregion

                #region create gql file
                try
                {
                    StreamWriter swGql = new StreamWriter(gqlFilename);

                    swGql.WriteLine("CREATE VERTEX TYPE httpwwww3org200301geowgs84_posSpatialThing");

                    ontology.ClassTree.GetAllOClassesGql(swGql.WriteLine, lLangs.ToArray());

                    swGql.WriteLine("CREATE VERTEX TYPE Label ATTRIBUTES (String Name)");
                    foreach (String lang in lLangs)
                    {
                        swGql.WriteLine("CREATE INDEX IDX_Thing_Name" + lang + " ON VERTEX TYPE httpwwww3org200207owlThing (Name" + lang + ") INDEXTYPE SingleValuePersistent");
                    }
                    // swGql.WriteLine("ALTER VERTEX TYPE httpwwww3org200207owlThing ADD ATTRIBUTES (String ShortAbstract_de, String ShortAbstract_en)");
                    StringBuilder sbCreateInstance = new StringBuilder();
                    sbCreateInstance.Append("CREATE VERTEX TYPE Instance ATTRIBUTES (String Name, ");
                    foreach (String lang in lLangs)
                    {
                        sbCreateInstance.Append("SET<Label> Labels");
                        sbCreateInstance.Append(lang);
                        sbCreateInstance.Append(", ");
                    }
                    sbCreateInstance.Append("SET<httpwwww3org200207owlThing> RefersToEntry)");
                    swGql.WriteLine(sbCreateInstance.ToString());

                    swGql.WriteLine("CREATE INDEX IDX_Instance_Name ON VERTEX TYPE Instance (Name) INDEXTYPE SingleValuePersistent");

                    swGql.Flush();
                    swGql.Close();
                }
                catch (Exception a)
                {
                    Console.WriteLine(a.Message);
                    Console.WriteLine(a.StackTrace);

                    break;
                }
                #endregion

                break;
            }

            Console.WriteLine("Press <Enter> to quit program.");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            while (true)
            {
                #region create Logging Actions
                Action<string> LogMessage = new Action<string>((msg) =>
                {
                    Console.WriteLine(DateTime.Now + " " + msg);
                });
                Action<string> LogError = new Action<string>((msg) =>
                {
                    Console.WriteLine(DateTime.Now + " " + msg);
                });

                Action<string> LogNull = new Action<string>((msg) => { });
                #endregion

                #region check file existence and load ontology
                Ontology thisOntology = null;
                try
                {
                    string ontologyFilename = "dbpedia_3.6.owl";
                    XmlElement rootElement = XmlParser.loadXml(new StreamReader(ontologyFilename), LogNull);
                    if (rootElement == null)
                    {
                        LogError("Null or empty XmlStructure in file: '" + ontologyFilename + "'");
                        break;
                    }
                    thisOntology = OwlParser.CreateOntologyFromXml(rootElement, LogNull);

                    #region add Thing manually!
                    OClass thing = new OClass("http://www.w3.org/2002/07/owl#Thing");
                    OProperty oPropertyName = new OProperty();
                    oPropertyName.ID = "Name";
                    oPropertyName.Range = "http://www.w3.org/2001/XMLSchema#string";
                    thing.AddDatatype(oPropertyName);
                    thisOntology.AddOntologyClass(thing);
                    #endregion
                }
                catch (Exception a)
                {
                    LogError("Error parsing ontology!");
                    LogError(a.Message);
                    LogError(a.StackTrace);

                    break;
                }
                #endregion

                #region ask for langs to import --> de, en
                List<String> lLangs = new List<string>();
                Console.WriteLine("Which language data should be added to the database?");
                Console.WriteLine("Enter language id and press <Enter>");
                Console.WriteLine("An empty line quits the iteration process.");
                String currentLine = null;
                bool bFirst = true;
                while ((currentLine = Console.ReadLine().Trim()) != "")
                {
                    lLangs.Add(currentLine);

                    bFirst = true;
                    Console.Write("CurrentlySelected: ");
                    foreach (String lang in lLangs)
                    {
                        if (bFirst)
                            bFirst = false;
                        else Console.Write(", ");
                        Console.Write(lang);
                    }
                    Console.WriteLine();
                }
                #endregion

                #region verify work environment
                if (!Directory.Exists(Properties.Settings.Default.WorkDir))
                {
                    LogError("WorkDir could not be loaded! " + Properties.Settings.Default.WorkDir);
                    break;
                }
                if (!Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir))
                {
                    Directory.CreateDirectory(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir);
                }
                else
                {
                    LogMessage("Data directory is already existing.!");
                    Console.WriteLine("All data will be deleted (Y), or program aborted.");
                    ConsoleKeyInfo a = Console.ReadKey();
                    if (a.KeyChar == 'y' || a.KeyChar == 'Y')
                    {
                        Console.WriteLine(" - Deleting existing directory content.");
                        Directory.Delete(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir, true);
                        Directory.CreateDirectory(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir);
                        LogMessage("Finished deleting data directory");
                    }
                    else
                    {
                        Console.WriteLine("Aborting program execution.");
                        break;
                    }
                }
                #endregion

                #region init helper dictionaries
                Dictionary<string, List<string>> dictExistingNodes = new Dictionary<string, List<string>>();
                Dictionary<string, uint> dictDirectoryEntries = new Dictionary<string, uint>();
                Dictionary<string, long> dictVertexIds = new Dictionary<string, long>();
                foreach (OClass oclass in thisOntology.GetAllClasses())
                {
                    dictVertexIds.Add(oclass.ID, long.MinValue);
                }

                #endregion

                #region create nodes from instance_types
                foreach (String lang in lLangs)
                {
                    String filename = "instance_types_" + lang + ".nt";

                    if (!File.Exists(Properties.Settings.Default.WorkDir
                        + Path.DirectorySeparatorChar
                        + lang
                        + Path.DirectorySeparatorChar
                        + filename))
                        continue;

                    CreateNodes(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar
                        + lang + Path.DirectorySeparatorChar + filename, thisOntology, dictExistingNodes, dictDirectoryEntries, dictVertexIds, LogMessage, LogError);
                }
                #endregion

                #region parse other NTripleFiles with specific implementation
                String filenameSub = null;

                foreach (String lang in lLangs)
                {
                    if (Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang))
                        foreach (String filename in Directory.EnumerateFiles(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + lang))
                        {
                            // foreach filename.endsWith(lang+".nt")   geo_coordinates_de.nt
                            FileInfo fi = new FileInfo(filename);
                            if (!fi.Extension.Equals(".nt"))
                            {
                                continue;
                            }

                            filenameSub = fi.Name.Substring(0, (fi.Name.Length - fi.Extension.Length));
                            if (filenameSub.EndsWith("_" + lang))
                            {
                                filenameSub = filenameSub.Substring(0, (filenameSub.Length - lang.Length - 1));
                            }

                            switch (filenameSub)
                            {
                                case "mappingbased_properties":
                                case "specific_mappingbased_properties":
                                    {
                                        string tmp_filename = filename;
                                        string tmp_lang = lang;
                                        //actions.Add(() => 
                                        EvaluateMappingbasedProperties(tmp_filename, tmp_lang, dictExistingNodes, dictDirectoryEntries, thisOntology, LogMessage, LogError);  //);
                                        // EvaluateMappingbasedProperties(filename, lang, thisOntology);
                                        // call impl.
                                        break;
                                    }
                                case "short_abstracts":
                                    {
                                        string tmp_filename = filename;
                                        string tmp_lang = lang;
                                        string tmp_KindOfText = "ShortAbstract";
                                        // actions.Add(() => 
                                        EvaluateAttributes(tmp_filename, tmp_KindOfText, tmp_lang, dictExistingNodes, dictDirectoryEntries, LogMessage, LogError); // );
                                        // EvaluateAttributes(filename, "ShortAbstract", lang);
                                        break;
                                    }
                                case "long_abstracts":
                                    {
                                        string tmp_filename = filename;
                                        string tmp_lang = lang;
                                        string tmp_KindOfText = "LongAbstract";
                                        // actions.Add(() => 
                                        EvaluateAttributes(tmp_filename, tmp_KindOfText, tmp_lang, dictExistingNodes, dictDirectoryEntries, LogMessage, LogError); // );
                                        // EvaluateAttributes(filename, "LongAbstract", lang);
                                        break;
                                    }
                                // other impl.
                                default:
                                    {
                                        LogMessage("ignore " + filename);
                                        // do nothing
                                        break;
                                    }
                            }
                        }
                }
                // Parallel.Invoke(actions.ToArray());
                #endregion

                #region save dictionary dictExistingNodes to file - needed for step 3 for performant evaluation of data within data dir
                try
                {
                    StreamWriter tw = new StreamWriter("ExistingNodes.dict");
                    foreach (string key in dictExistingNodes.Keys)
                    {
                        List<string> values = dictExistingNodes[key];
                        if (values.Count != 3)
                        {
                            LogError("Error savind data entry: '" + key + "' in ExistingNodes.dict");
                            continue;
                        }
                        else
                        {
                            tw.WriteLine(key + ";" + values[0] + ";" + values[1] + ";" + values[2]);
                            Console.Write(".");
                        }
                    }
                    tw.Flush();
                    tw.Close();
                }
                catch (Exception a)
                {
                    LogError("Error saving dictionary dictExistingNodes to file");
                    LogError(a.Message);
                    LogError(a.StackTrace);
                }
                #endregion
                break;   // default behaviour
            }

            Console.WriteLine("Press <Enter> to quit program.");
            Console.ReadLine();
        }
Exemple #15
0
        public static Ontology CreateOntologyFromXml(XmlElement eXmlRoot, Action<string> errorAction)
        {
            Ontology onti = null;
            XmlElement eRootElement = null;

            #region <rdf:RDF>
            try
            {
                eRootElement = eXmlRoot.getChildElements("rdf:RDF").First();                        // <rdf:RDF
                onti = new Ontology();
                onti.Xmlns = eRootElement.getAttribute("xmlns", onti.Xmlns).strValue;               //   xmlns = "http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#"
                onti.XmlBase = eRootElement.getAttribute("xml:base", onti.XmlBase).strValue;        //   xml:base = "http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl"
                onti.XmlnsRdf = eRootElement.getAttribute("xmlns:rdf", onti.XmlnsRdf).strValue;     //   xmlns:rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                onti.XmlnsRdfs = eRootElement.getAttribute("xmlns:rdfs", onti.XmlnsRdfs).strValue;  //   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
                onti.XmlnsOwl = eRootElement.getAttribute("xmlns:owl", onti.XmlnsOwl).strValue;     //   xmlns:owl="http://www.w3.org/2002/07/owl#"
            }
            catch (Exception a) { }
            if (eRootElement == null)
            {
                if (errorAction != null)
                    errorAction("invalid structure: Missing root:element <rdf:RDF ..>");
                return onti;
            }
            #endregion

            #region <owl:ontology>
            try
            {
                XmlElement eOntology = eRootElement.getChildElements("owl:Ontology").First();  // <owl:Ontology
                onti.About = eOntology.getAttribute("rdf:about", onti.About).strValue;         //    rdf:about="">

                // not mandatory
                try
                {  // <rdfs:comment>An university ontology for benchmark tests</rdfs:comment>
                    onti.Comment = eOntology.getChildElements("rdfs:comment").First().getText(onti.Comment);
                }
                catch (Exception aa) { }

                // not mandatory
                try
                {  // <rdfs:label>Univ-bench Ontology</rdfs:label>
                    onti.Label = eOntology.getChildElements("rdfs:label").First().getText(onti.Label);
                }
                catch (Exception aa) { }

                // <owl:versionInfo>univ-bench-ontology-owl, ver April 1, 2004</owl:versionInfo>
                onti.VersionInfo = eOntology.getChildElements("owl:versionInfo").First().getText(onti.VersionInfo);
            }
            catch (Exception a)
            {
                if (errorAction != null)
                    errorAction("invalid structure: Missing param in <owl:ontology>." + a.Message + "\r\n" + a.StackTrace);
                return onti;
            }
            #endregion

            #region <owl:class>
            List<XmlElement> eClasses = eRootElement.getChildElements("owl:Class");
            OClass currentClass;
            String id;
            foreach (XmlElement xmlCurrentClass in eClasses)
            {
                try
                {
                    currentClass = new OClass();
                    try
                    {
                        currentClass.ID = xmlCurrentClass.getAttribute("rdf:ID", currentClass.ID).strValue;  // <owl:Class rdf:ID="AdministrativeStaff">
                    }
                    catch (Exception aa) { }

                    try
                    {
                        if (currentClass.ID == null)
                        {
                            currentClass.ID = xmlCurrentClass.getAttribute("rdf:about", currentClass.ID /*About*/).strValue; // <owl:Class rdf:about="http://dbpedia.org/ontology/Cycad">
                        }
                    }
                    catch (Exception aa) { }

                    if (currentClass.ID == null)
                    {
                        if (errorAction != null)
                            errorAction("Error adding <owl:class>: Missing rdf:ID or rdf:about. " + xmlCurrentClass);
                        continue;
                    }

                    try  // not mandatory: <rdfs:label>administrative staff worker</rdfs:label>
                    {
                        currentClass.Label = xmlCurrentClass.getChildElements("rdfs:label").First().getText(currentClass.Label);
                    }
                    catch (Exception aa) { }

                    try  // not mandatory: <rdfs:comment xml:lang="en">a group of sports teams that compete against each other in Cricket</rdfs:comment>
                    {
                        XmlElement eClassComment = xmlCurrentClass.getChildElements("rdfs:comment").First();
                        currentClass.Comment = eClassComment.getText(currentClass.Comment);
                        currentClass.CommentLang = eClassComment.getAttribute("xml:lang", currentClass.CommentLang).strValue;
                    }
                    catch (Exception aa) { }

                    try  // not mandatory : // <rdfs:subClassOf rdf:resource="#Employee" />
                    {
                        id = xmlCurrentClass
                            .getChildElements("rdfs:subClassOf").First()
                            .getAttribute("rdf:resource", "").strValue;

                        if (id.StartsWith("#"))   // ? todo clarify
                            id = id.Substring(1);

                        currentClass.IsSubClassOf = new OClass(id /*, id*/);
                    }
                    catch (Exception aa) { }

                    try
                    {
                        XmlElement eIntersection = xmlCurrentClass.getChildElements("owl:intersectionOf").First();  // <owl:intersectionOf rdf:parseType="Collection">
                        XmlElement eISectClass = eIntersection.getChildElements("owl:Class").First();               //   <owl:Class rdf:about="#Person" />
                        string strIsectAbout = eISectClass.getAttribute("rdf:about", "").strValue;
                        if (strIsectAbout.StartsWith("#"))
                            strIsectAbout = strIsectAbout.Substring(1);  // todo clarify
                        currentClass.IsSubClassOf = new OClass(/*null,*/ strIsectAbout);

                        OProperty odProp = new OProperty();
                        XmlElement eRestriction = eIntersection.getChildElements("owl:Restriction").First();        // <owl:Restriction>
                        XmlElement eOnProperty = eRestriction.getChildElements("owl:onProperty").First();           // <owl:onProperty rdf:resource="#headOf" />
                        odProp.ID = eOnProperty.getAttribute("rdf:resource", "").strValue;

                        XmlElement eSomeValuesFrom = eRestriction.getChildElements("owl:someValuesFrom").First();    // <owl:someValuesFrom>
                        XmlElement eValuesClass = eSomeValuesFrom.getChildElements("owl:Class").First();            // <owl:Class rdf:about="#Department" />
                        odProp.Domain = eValuesClass.getAttribute("rdf:about", "").strValue;
                        currentClass.AddDatatype(odProp);
                    }
                    catch (Exception aa) { }

                    if (!onti.AddOntologyClass(currentClass))
                    {
                        if (errorAction != null)
                            errorAction("Didn't add <owl:class> " + currentClass + " xml=" + xmlCurrentClass);
                        continue;
                    }
                    // onti.ValidateClasses();
                }
                catch (Exception a)
                {
                    Console.WriteLine("the following OntologyClass had not been imported due to errors.");
                    Console.WriteLine(xmlCurrentClass.ToString());
                    Console.WriteLine(a.Message);
                    Console.WriteLine(a.StackTrace);
                    Console.WriteLine();
                }
            }
            #endregion

            #region <owl:DatatypeProperty>
            List<XmlElement> lDatatypes = eRootElement.getChildElements("owl:DatatypeProperty");
            OProperty currentDatatype;
            foreach (XmlElement xmlCurrentElement in lDatatypes)
            {
                try
                {
                    currentDatatype = new OProperty();

                    try
                    {
                        currentDatatype.ID = xmlCurrentElement.getAttribute("rdf:ID", currentDatatype.ID).strValue;  // <owl:DatatypeProperty rdf:ID="age">
                    }
                    catch (Exception aa) { }

                    try
                    {
                        if (currentDatatype.ID == null)
                            currentDatatype.ID = xmlCurrentElement.getAttribute("rdf:about", currentDatatype.ID).strValue; // <owl:DatatypeProperty rdf:about="http://dbpedia.org/ontology/dateOfBurial">
                    }
                    catch (Exception aa) { }

                    if (currentDatatype.ID == null)
                    {
                        if (errorAction != null)
                            errorAction("Error adding <owl:DatatypeProperty>: Missing rdf:ID or rdf:about. " + xmlCurrentElement);
                        continue;
                    }

                    try
                    {
                        currentDatatype.Domain = xmlCurrentElement
                            .getChildElements("rdfs:domain").First()
                            .getAttribute("rdf:resource", currentDatatype.Domain).strValue;
                    }
                    catch (Exception a) { }

                    try  // not mandatory: <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#date"></rdfs:range>
                    {
                        // if (currentDatatype.Domain == null)
                        // {
                        currentDatatype.Range = xmlCurrentElement
                            .getChildElements("rdfs:range").First()
                            .getAttribute("rdf:resource", currentDatatype.Domain).strValue;
                        // }
                    }
                    catch (Exception aa) { }

                    /*
                    if (currentDatatype.Domain == null)
                    {
                        errorAction("Error adding <owl:DatatypeProperty>: Neither Range or Domain is set in xml:" + xmlCurrentElement);
                        continue;
                    }*/

                    try  // not mandatory: <rdfs:label>is age</rdfs:label>
                    {
                        currentDatatype.Label = xmlCurrentElement.getChildElements("rdfs:label").First().getText(currentDatatype.Label);
                    }
                    catch (Exception aa) { }

                    if (!onti.AddDatatype(currentDatatype))
                    {
                        if (errorAction != null)
                            errorAction("Didn't add <owl:DatatypeProperty> " + currentDatatype);
                        continue;
                    }
                    /*
                    else
                    {
                        Console.WriteLine("added datatype: " + currentDatatype.ToString());
                    }*/
                }
                catch (Exception a)
                {
                    Console.WriteLine("the following datatype had not been imported due to errors.");
                    Console.WriteLine(xmlCurrentElement.ToString());
                    Console.WriteLine(a.Message);
                    Console.WriteLine(a.StackTrace);
                    Console.WriteLine();
                }
            }
            #endregion

            #region <owl:ObjectProperty rdf:about="http://dbpedia.org/ontology/similar">
            List<XmlElement> lObjectProperties = eRootElement.getChildElements("owl:ObjectProperty");
            OProperty currentObjectProperty;
            foreach (XmlElement xmlCurrentObjectProperty in lObjectProperties)
            {
                try
                {
                    currentObjectProperty = new OProperty();

                    try
                    {
                        currentObjectProperty.ID = xmlCurrentObjectProperty.getAttribute("rdf:ID", currentObjectProperty.ID).strValue;  // <owl:ObjectProperty rdf:ID="degreeFrom">
                    }
                    catch (Exception aa) { }

                    try
                    {
                        if (currentObjectProperty.ID == null)
                        {
                            currentObjectProperty.ID = xmlCurrentObjectProperty.getAttribute("rdf:about", currentObjectProperty.ID).strValue; // <owl:ObjectProperty rdf:about="http://dbpedia.org/ontology/similar">
                        }
                    }
                    catch (Exception aa) { }

                    if (currentObjectProperty.ID == null)
                    {
                        if (errorAction != null)
                            errorAction("Error adding <owl:ObjectProperty>: Missing rdf:ID or rdf:about. " + xmlCurrentObjectProperty);
                        continue;
                    }

                    try
                    {
                        currentObjectProperty.Domain = xmlCurrentObjectProperty
                            .getChildElements("rdfs:domain").First()
                            .getAttribute("rdf:resource", currentObjectProperty.Domain).strValue;
                    }
                    catch (Exception aa)
                    {
                        if (errorAction != null)
                            errorAction("Error adding ObjectProperty: <rdfs:domain> is not set in xml:" + xmlCurrentObjectProperty);
                        continue;
                    }

                    try
                    {
                        currentObjectProperty.Range = xmlCurrentObjectProperty
                            .getChildElements("rdfs:range").First()
                            .getAttribute("rdf:resource", currentObjectProperty.Range).strValue;
                    }
                    catch (Exception aa)
                    {
                        if (errorAction != null)
                            errorAction("Error adding ObjectProperty: <rdfs:range> is not set in xml:" + xmlCurrentObjectProperty);
                        continue;
                    }

                    try  // not mandatory: <rdfs:label>is age</rdfs:label>
                    {
                        currentObjectProperty.Label = xmlCurrentObjectProperty.getChildElements("rdfs:label").First().getText(currentObjectProperty.Label);
                    }
                    catch (Exception aa) { }

                    try  // not mandatory: <owl:inverseOf rdf:resource="#hasAlumnus"/>
                    {
                        currentObjectProperty.InverseOf = xmlCurrentObjectProperty.getChildElements("rdfs:label").First().getText(currentObjectProperty.InverseOf);
                    }
                    catch (Exception aa) { }

                    try  // not mandatory: <rdfs:subPropertyOf rdf:resource="#memberOf" />
                    {
                        currentObjectProperty.SubPropertyOf = xmlCurrentObjectProperty
                            .getChildElements("rdfs:subPropertyOf").First()
                            .getAttribute("", currentObjectProperty.SubPropertyOf).strValue;
                    }
                    catch (Exception aa) { }

                    if (!onti.AddObjectProperty(currentObjectProperty))
                    {
                        if (errorAction != null)
                            errorAction("Didn't add <owl:ObjectProperty> " + currentObjectProperty);
                    }
                }
                catch (Exception a)
                {
                    Console.WriteLine("the following ObjectProperty had not been imported due to errors.");
                    Console.WriteLine(xmlCurrentObjectProperty.ToString());
                    Console.WriteLine(a.Message);
                    Console.WriteLine(a.StackTrace);
                    Console.WriteLine();
                }
            }
            #endregion

            return onti;
        }