예제 #1
0
        //public static string DebugTest(int ent)
        //{
        //    string message = "";
        //    string sql = "SELECT * FROM rdf_ostatements WHERE oobj=" + ent;
        //    foreach (var row in RelationalDb.RunQuery(sql))
        //    {
        //        message += string.Format("({0}, {1}, {2})", row[0], row[1], row[2]);
        //    }
        //    message += entities[ent].s_value + " type=" + ontology_entities_inverse[entities[ent].i_value];
        //    return message;
        //}
        public static void Reload(bool rebuild)
        {
            MyTimer.Start();
            string formats_enc_filename = path + "logs/formats_enc.xml";
            if (rebuild)
            {
                logwrite("Rebuild starts");
                RelationalDb.xontology = xontology;
                RelationalDb.LoadDb(getfilelist());
                //XElement format = XElement.Load(path + "ApplicationProfile.xml").Element("formats");
                if (RelationalDb.server != "virtuoso")
                {
                    formats_enc = RelationalEngine.EncodeFormatTree(formats);
                    formats_enc.Save(formats_enc_filename);
                }
                logwrite("Rebuild ok");
            }
            else if (RelationalDb.server != "virtuoso")
            {
                if (System.IO.File.Exists(formats_enc_filename))
                {
                    formats_enc = XElement.Load(formats_enc_filename);
                }
                else
                {
                    formats_enc = RelationalEngine.EncodeFormatTree(formats);
                    formats_enc.Save(formats_enc_filename);
                }
            }

            // Вычисление полезных структур
            if (RelationalDb.server != "virtuoso")
            {
                entities = RelationalEngine.GenerateEntityTable();
                ontology_entities = RelationalEngine.GenerateOnologyEntities(xontology);
                ontology_entities_inverse = ontology_entities.ToDictionary(pair => pair.Value, pair => pair.Key);
            }

            if (rebuild)
            {
                // А еще, создадим в базе данных элемент с типом collection и идентификатором cassetterootcollection
                string idFull = ONames.FOGEntity + "cassetterootcollection";
                var triplets = new List<Triplet>();
                triplets.Add(new OProp(idFull, ONames.rdfnsstring + "type", ONames.FOG + "collection"));
                triplets.Add(new DProp(idFull, ONames.p_name, "Кассеты", "ru"));
                if (RelationalDb.server == "virtuoso")
                {
                    RelationalDb.InsertTripletsVirtuoso(triplets);
                }
                else
                {
                    TripletPortion tp = new TripletPortion(triplets, entities, ontology_entities,
                        ontology_entities_inverse);
                    RelationalDb.FillDatabaseFromTripletFlow(tp);
                }
            }
            MyTimer.Look("инициация движка");
        }
예제 #2
0
 // ===================== Основная процедура редактирования базы данных
 public static void InsertXRecord(XElement xrecord, bool check, factograph.RDFDocumentInfo docinfo)
 {
     if (xrecord.Name == ONames.TagDelete)
     {
         // Идентификатор записи
         string id = xrecord.Attribute(ONames.AttItem_id).Value;
         if (!id.Contains(':')) id = ONames.FOGEntity + id;
         RelationalDb.DeleteRecordFromDatabase(id);
     }
     else if (xrecord.Name == ONames.TagSubstitute)
     {
         // пока не реализован
         throw new NotImplementedException();
     }
     else
     {
         // Превратить запись в набор утверждений
         var id = ONames.FOGEntity + xrecord.Attribute(ONames.rdfabout).Value;
         List<Triplet> triplets = new List<Triplet>(new Triplet[] {
                 new OProp(id,
                     ONames.rdfnsstring + "type", ONames.FOG + xrecord.Name) });
         foreach (var el in xrecord.Elements())
         {
             XAttribute rdfresource = el.Attribute(ONames.rdfresource);
             if (rdfresource == null)
             {
                 XAttribute lang_att = el.Attribute(ONames.xmllang);
                 string lang = lang_att == null ? null : lang_att.Value;
                 triplets.Add(new DProp(id, ONames.FOG + el.Name, el.Value, lang));
             }
             else
             {
                 triplets.Add(new OProp(id, ONames.FOG + el.Name, ONames.FOGEntity + rdfresource.Value));
             }
         }
         if (!check)
         {
         }
         else // if (check)
         {
             string id_full = id.Contains(':') ? id : ONames.FOGEntity + id;
             // Получим триплеты из базы данных
             var db_triplets = RelationalDb.GetTripletsBySubject(id_full,
                 entities, ontology_entities_inverse);
             // теперь можно стереть запись в базе данных
             RelationalDb.DeleteRecordFromDatabase(id_full);
             // Теперь объединяем триплеты в общий набор
             // сначала надо из db_triplets вычесть triplets теоретико-множественно
             //var qu = db_triplets.Except(triplets, ...);
             // не получается теоретико-множественно, сделаю вычитание напрямую
             var restflow = db_triplets.Where(tr => triplets.All(tri => { // проверка на неравенство
                 if (tr.p != tri.p) return true;
                 if (tr is OProp)
                 {
                     if (tri is DProp) return true; // несуществующий в онтологии вариант
                 }
                 else // tr is DProp
                 {
                     if (tri is OProp) return true; // несуществующий в онтологии вариант
                     var dtr = tr as DProp; var dtri = tri as DProp;
                     if (dtr.lang != dtri.lang) return true;
                     // если оба lang == null, оба lang совпадают
                 }
                 return false;
             }));
             // теперь вставим restflow в xrecord
             xrecord.Add(restflow.Select(tr =>
             {
                 string prop_s = tr.p.StartsWith(ONames.FOG) ?
                     tr.p.Substring(ONames.FOG.Length) : tr.p;
                 // Непонятно что делать если будут не фог-овские предикаты
                 XElement prop = new XElement(prop_s);
                 if (tr is OProp)
                 {
                     string obj = ((OProp)tr).o;
                     if (obj.StartsWith(ONames.FOGEntity)) obj = obj.Substring(ONames.FOGEntity.Length);
                     prop.Add(new XAttribute(ONames.rdfresource, obj));
                 }
                 return prop;
             }));
             // А также вставим в triplets
             triplets.AddRange(restflow);
         }
         if (RelationalDb.server == "virtuoso")
         {
             RelationalDb.InsertTripletsVirtuoso(triplets);
         }
         else
         {
             TripletPortion tp = new TripletPortion(triplets, entities, ontology_entities,
                 ontology_entities_inverse);
             RelationalDb.FillDatabaseFromTripletFlow(tp);
         }
     }
     // Внедрить и сохранить
     xrecord.Add(new XAttribute(ONames.AttModificationTime, DateTime.Now.ToString("s")));
     docinfo.GetRoot().Add(xrecord);
     docinfo.isChanged = true;
     docinfo.Save();
     // Добавить служебные поля
     xrecord.Add(new XAttribute("dbid", docinfo.dbId));
     xrecord.Add(new XAttribute("sender", docinfo.owner));
     // Запомнить действие в логе изменений
     changelog(xrecord.ToString());
 }