Example #1
0
        /// <summary>
        /// all links of sweeped
        /// are linked to sink
        /// </summary>
        /// <param name="thingGraph"></param>
        /// <param name="sweeped"></param>
        /// <param name="sink"></param>
        /// <returns></returns>
        public static IEnumerable <IThing> MergeThing(this IThingGraph thingGraph, IThing sweeped, IThing sink)
        {
            foreach (var link in thingGraph.Edges(sweeped).ToArray())
            {
                if (link.Root == sweeped)
                {
                    link.Root = sink;
                }
                if (link.Leaf == sweeped)
                {
                    link.Leaf = sink;
                }
                if (link.Marker == sweeped)
                {
                    link.Marker = sink;
                }
                if (link.Root == link.Leaf)
                {
                    thingGraph.Remove(link);
                }
                else
                {
                    thingGraph.Add(link);
                }
                yield return(link);
            }

            foreach (var link in thingGraph.WhereQ <ILink> (l => l.Marker != null && l.Marker.Id == sweeped.Id))
            {
                link.Marker = sink;
                thingGraph.Add(link);
                yield return(link);
            }
        }
Example #2
0
        public static void AddRange(this IThingGraph thingGraph, IEnumerable <IThing> things)
        {
            var links = new Set <ILink>();

            foreach (var thing in things)
            {
                if (thing is ILink)
                {
                    links.Add((ILink)thing);
                }
                else
                {
                    thingGraph.Add(thing);
                }
            }
            int linkDepth = 100;
            int depth     = 0;

            while (links.Count > 0 && depth < linkDepth)
            {
                depth++;
                foreach (var link in links.ToArray())
                {
                    var idLink = (ILink <Id>)link;
                    if (link.Leaf == null)
                    {
                        var thing = thingGraph.GetById(idLink.Leaf);
                        if (thing != null)
                        {
                            link.Leaf = thing;
                        }
                    }
                    if (link.Root == null)
                    {
                        var thing = thingGraph.GetById(idLink.Root);
                        if (thing != null)
                        {
                            link.Root = thing;
                        }
                    }
                    if (link.Marker == null)
                    {
                        var thing = thingGraph.GetById(idLink.Marker);
                        if (thing != null)
                        {
                            link.Marker = thing;
                        }
                    }
                    if (link.Root != null && link.Leaf != null && link.Marker != null)
                    {
                        thingGraph.Add(link);
                        links.Remove(link);
                    }
                }
            }
        }
Example #3
0
 public virtual void EnsureDefaultThings(IThingGraph graph)
 {
     if (graph.GetById(ChannelRoot.Id) == null)
     {
         graph.Add(ChannelRoot);
     }
     if (graph.GetById(Articles.Id) == null)
     {
         graph.Add(Articles);
     }
 }
Example #4
0
        void TestReadWriteClient(IThingGraph thingGraph)
        {
            var GetByIdCount = ClientService.GetByIdCount;


            var thing1 = Factory.CreateItem("hello service");

            thingGraph.Add(thing1);

            var thing2 = Factory.CreateItem("a child");

            thingGraph.Add(thing2);

            thingGraph.Add(Factory.CreateEdge(thing1, thing2, Factory.CreateItem("marker")));

            var graph = ModelHelper.GetSourceGraph <ProgrammingLanguageFactory>().Two;

            foreach (var thing in graph)
            {
                thingGraph.Add(thing);
            }
            foreach (var thing in graph.Edges())
            {
                thingGraph.Add(thing);
            }
            thingGraph.Clear();

            Debug.WriteLine("**** all items of Service ****");
            try {
                int thingCount = 0;
                foreach (IThing clThing in thingGraph)
                {
                    ReportThing(clThing);
                    thingCount++;
                }
                Debug.WriteLine("** GetByIdCount = " + (ClientService.GetByIdCount - GetByIdCount));
                foreach (IThing clThing in thingGraph)
                {
                    foreach (ILink link in thingGraph.Edges(clThing))
                    {
                        ReportThing(link);
                    }
                }
                Debug.WriteLine("** GetByIdCount = " + (ClientService.GetByIdCount - GetByIdCount));
            } catch (Exception e) {
                Debug.WriteLine(e.Message);
            }
        }
Example #5
0
        public static bool EnsureChangeData(this IThingGraph thingGraph, IThing item, object data)
        {
            if (item == null)
            {
                return(false);
            }
            var result = false;

            var link = item as ILink;

            if (link != null)
            {
                var marker = data as IThing;
                if (marker != null && link.Marker != marker)
                {
                    link.Marker = marker;
                    result      = true;
                }
                return(false);
            }
            else if (!object.Equals(item.Data, data))
            {
                item.Data = data;
                result    = true;
            }
            if (result)
            {
                thingGraph.Add(item);
            }
            return(result);
        }
Example #6
0
        public IThing CreateDocument(IThingGraph graph, string title)
        {
            if (graph == null)
            {
                return(null);
            }

            var document = Factory.CreateItem();

            graph.Add(document);
            if (title == null)
            {
                title = "Document " + document.Id.ToString("X");
            }
            graph.Add(Factory.CreateEdge(document, Factory.CreateItem(title), DigidocSchema.DocumentTitle));
            return(document);
        }
Example #7
0
        public void CreateDocuments(IThingGraph graph, IThing root, int count)
        {
            var digidoc = new DigidocSchema();

            for (int i = 0; i < count; i++)
            {
                var document = digidoc.CreateDocument(graph, null);
                digidoc.CreatePage(graph, document, null, 1);
                graph.Add(Factory.CreateEdge(root, document, DigidocSchema.Document));
            }
        }
Example #8
0
        protected virtual IThing TryAddSomeThing(IThingGraph thingGraph, IThing something)
        {
            var thing = thingGraph.GetById(something.Id);

            if (thing == null)
            {
                thing = something;
                thingGraph.Add(thing);
            }
            return(thing);
        }
Example #9
0
        /// <summary>
        /// creates a StreamThing with content assigned
        /// and adds it to the graph
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public IStreamThing CreateAndAdd(IThingGraph graph, Content <Stream> content)
        {
            var thing = Factory.CreateItem(graph, content.Data) as IStreamThing;

            thing.Compression = content.Compression;
            thing.StreamType  = content.ContentType;

            thing.Compress();
            graph.Add(thing);

            thing.Flush();
            thing.ClearRealSubject();
            return(thing);
        }
Example #10
0
        public static void DeepCopy(this IThingGraph source, IEnumerable <IThing> items, IThingGraph target)
        {
            if (source != null && target != null)
            {
                var walk  = source.Walk();
                var queue = new Queue <IThing>(items);
                while (queue.Count != 0)
                {
                    var item = queue.Dequeue();
                    if (!walk.Visited.Contains(item))
                    {
                        if (item is ILink)
                        {
                            target.Add((ILink)item);
                        }
                        else
                        {
                            target.Add(item);
                        }

                        foreach (var levelItem in walk.DeepWalk(item, 0))
                        {
                            var link = levelItem.Node as ILink;
                            if (link != null)
                            {
                                target.Add(link);
                                queue.Enqueue(link.Marker);
                            }
                            else
                            {
                                target.Add(levelItem.Node);
                            }
                        }
                    }
                }
            }
        }
Example #11
0
        public IThing CreatePage(IThingGraph graph, IThing document, Content <Stream> stream, object pageNr)
        {
            if (graph == null || document == null)
            {
                return(null);
            }
            var page = Factory.CreateItem <Stream>(null) as IStreamThing;

            page.ContentContainer = graph.ContentContainer;
            if (stream != null)
            {
                new ThingContentFacade(Factory).AssignContent(page, stream);
            }
            var pageEdge = Factory.CreateEdge(document, page, DigidocSchema.DocumentPage);

            graph.Add(pageEdge);

            var number     = Factory.CreateItem(pageNr);
            var numberEdge = Factory.CreateEdge(pageEdge, number, DigidocSchema.PageNumber);

            graph.Add(number);
            graph.Add(numberEdge);
            return(page);
        }
Example #12
0
        protected virtual bool AddToSheets(IThingGraph graph, Int64 sheetId)
        {
            var thing = graph.GetById(sheetId) as IStreamThing;

            if (thing != null && thing.StreamType == ContentTypes.LimadaSheet)
            {
                var add = graph.Edges(thing).Where(l => l.Marker.Id != CommonSchema.DescriptionMarker.Id).Count() == 0;
                if (add)
                {
                    var sheets      = TryAddSomeThing(graph, TopicSchema.Sheets);
                    var sheetMarker = TryAddSomeThing(graph, TopicSchema.SheetMarker);
                    TryAddSomeThing(graph, TopicSchema.TopicToSheetsLink);
                    var factory = Registry.Factory.Create <IThingFactory> ();
                    var link    = factory.CreateEdge(sheets, thing, sheetMarker);
                    graph.Add(link);
                }
                return(add);
            }
            return(false);
        }
Example #13
0
        public void ProveEdge(IThingGraph thingGraph, ILink link)
        {
            bool delete = false;
            var  idLink = ((ILink <Id>)link);

            delete = idLink.Root == 0 || idLink.Leaf == 0;
            if (!delete)
            {
                var root   = link.Root;
                var leaf   = link.Leaf;
                var marker = link.Marker;
                delete = root == null || leaf == null || root.Id == 0 || leaf.Id == 0;
            }
            if (!delete)
            {
                thingGraph.Add(link);
            }
            else
            {
                thingGraph.Remove(link);
            }
        }
Example #14
0
        public IThingGraph RawImport(Iori source, IThingGraph sink, bool repair)
        {
            this.Log = new StringWriter();

            var links = new List <ILink>();

            if (repair)
            {
                var db4oGraph = sink as ThingGraph;
                if (db4oGraph != null)
                {
                    ReportClazzes(db4oGraph.Gateway as Gateway);
                }
            }

            var gateway = new Gateway();

            if (!repair)
            {
                gateway.Configuration.AllowVersionUpdates = true;
                gateway.Configuration.DetectSchemaChanges = true;
                //gateway.Configuration.RecoveryMode(true);
            }

            ConfigureAliases(gateway.Configuration);

            gateway.Open(source);
            var session = gateway.Session;

            ReportClazzes(gateway);
            SchemaFacade.InitSchemata();

            var cache = new Dictionary <IReflectClass, Tuple <IReflectClass, List <IReflectField>, Type> >();

            foreach (var item in session.Query <object>())
            {
                var thing = item as IThing;
                if (thing != null)
                {
                    ReportDetail(thing.Id.ToString());
                }
                var go = item as GenericObject;
                if (go != null)
                {
                    IReflectClass clazz = go.GetGenericClass();
                    Tuple <IReflectClass, List <IReflectField>, Type> defs = null;
                    if (!cache.TryGetValue(clazz, out defs))
                    {
                        var name = clazz.GetName();
                        name = name.Substring(0, name.LastIndexOf(','));

                        var newType = typeof(IThing).Assembly.GetType(name);

                        var fields = new List <IReflectField>();
                        var iClazz = clazz;
                        while (iClazz != null)
                        {
                            fields.AddRange(iClazz.GetDeclaredFields());
                            iClazz = iClazz.GetSuperclass();
                        }
                        defs = Tuple.Create(clazz, fields, newType);
                        cache.Add(clazz, defs);
                    }

                    var newThing = Activator.CreateInstance(defs.Item3, new object[] { null });

                    foreach (var field in defs.Item2)
                    {
                        var       val  = field.Get(go);
                        FieldInfo info = null;
                        var       type = defs.Item3;
                        while (info == null && type != null)
                        {
                            info = type.GetField(field.GetName(), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                            if (info == null)
                            {
                                info = type.GetField(field.GetName(), BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                            }
                            if (info == null && field.GetName() == "_writeDate")
                            {
                                info = type.GetField("_changeDate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                            }
                            type = type.BaseType;
                        }
                        if (info != null && val != null && val.GetType() == info.FieldType)
                        {
                            info.SetValue(newThing, val);
                        }
                    }
                    ReportDetail(newThing.ToString());
                    if (repair)
                    {
                        var link = newThing as Link;
                        if (link != null)
                        {
                            link.GetByID = (id) => sink.GetById(id);
                            links.Add(newThing as ILink);
                        }
                        else if (newThing is IThing)
                        {
                            sink.Add(newThing as IThing);
                        }
                        else if (newThing is IIdContent <long> )
                        {
                            sink.ContentContainer.Add(newThing as IIdContent <long>);
                        }
                    }
                }
            }
            if (repair)
            {
                ReportDetail("write links...");

                foreach (var link in links)
                {
                    var idLink = link as ILink <Id>;
                    if (link.Marker == null && idLink.Marker != 0)
                    {
                        IThing marker = null;
                        if (Schema.IdentityMap.TryGetValue(idLink.Marker, out marker))
                        {
                            link.Marker = marker;
                        }
                    }
                    if (link.Marker == null)
                    {
                        link.Marker = CommonSchema.EmptyMarker;
                    }
                    if (link.Root != null && link.Leaf != null)
                    {
                        sink.Add(link);
                    }
                }
            }
            gateway.Close();
            ReportDetail("done:\t");
            if (this.Log != null)
            {
                var logfilename = source.ToFileName() + ".log";
                if (File.Exists(logfilename))
                {
                    File.Delete(logfilename);
                }
                var logfile = new StreamWriter(logfilename);
                logfile.Write(this.Log.ToString());
                logfile.Close();
            }
            return(sink);
        }