Exemple #1
0
 public void Assert(JsonLdTriple triple)
 {
     lock (this)
     {
         AssertNoLock(triple);
     }
 }
Exemple #2
0
        /// <summary>
        /// Return the entity from the graph
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private JToken GetEntityFromGraph(Uri entity)
        {
            JToken token = null;

            JsonLdTripleCollection triples = null;

            DataTraceSources.Verbose("[EntityCache] GetEntity {0}", entity.AbsoluteUri);

            // make sure everything added at this point has gone into the master graph
            WaitForTasks();

            lock (this)
            {
                // find the best JToken for this subject that we have
                triples = _masterGraph.SelectSubject(entity);

                // update the last access time for these pages
                foreach (var page in triples.Select(t => t.Page).Distinct())
                {
                    page.UpdateLastUsed();
                }
            }

            // find the best jtoken for the subject
            JsonLdTriple triple = triples.Where(n => n.JsonNode != null).OrderByDescending(t => t.HasIdMatchingUrl ? 1 : 0).FirstOrDefault();

            if (triple != null)
            {
                token = triple.JsonNode;
            }

            return(token);
        }
Exemple #3
0
        private void AssertNoLock(JsonLdTriple triple)
        {
            string subject   = triple.Subject.GetValue();
            string predicate = triple.Predicate.GetValue();
            string obj       = triple.Object.GetValue();

            Dictionary <string, List <JsonLdTriple> > inner = null;

            if (_subjectIndex.TryGetValue(subject, out inner))
            {
                List <JsonLdTriple> objs = null;
                if (inner.TryGetValue(predicate, out objs))
                {
                    JsonLdTriple existingTriple = objs.Where(t => t.Object.IsValue(obj)).FirstOrDefault();

                    if (existingTriple != null)
                    {
                        // move the existing one to the alts if we have a better one now
                        if (IsBetter(triple, existingTriple))
                        {
                            objs.Remove(existingTriple);
                            objs.Add(triple);
                        }
                    }
                    else
                    {
                        // normal add
                        objs.Add(triple);
                    }
                }
                else
                {
                    inner.Add(predicate, new List <JsonLdTriple>()
                    {
                        triple
                    });
                }
            }
            else
            {
                // completely new
                inner = new Dictionary <string, List <JsonLdTriple> >();
                _subjectIndex.Add(subject, inner);

                inner.Add(predicate, new List <JsonLdTriple>()
                {
                    triple
                });
            }
        }
Exemple #4
0
        /// <summary>
        /// True if A is better
        /// </summary>
        public static bool IsBetter(JsonLdTriple a, JsonLdTriple b)
        {
            if (a == b)
            {
                return(false);
            }

            if (a.JsonNode != null && b.JsonNode == null)
            {
                return(true);
            }

            if (a.HasIdMatchingUrl && !b.HasIdMatchingUrl)
            {
                return(true);
            }

            if (a.JsonNode != null && b.JsonNode != null)
            {
                return(a.JsonNode.Descendants().Count() > a.JsonNode.Descendants().Count());
            }

            return(false);
        }
Exemple #5
0
 /// <summary>
 /// True if the Page uri and JToken reference are also equal.
 /// </summary>
 public bool IsExactSame(JsonLdTriple other)
 {
     return(JsonNode.Equals(other.JsonNode) && Page.Equals(other.Page) && base.Equals(other));
 }
Exemple #6
0
        /// <summary>
        /// Load a compacted json object into a JsonLdGraph
        /// </summary>
        public static JsonLdGraph Load(JObject compacted, JsonLdPage page)
        {
            Dictionary <int, JObject> nodes = new Dictionary <int, JObject>();
            int marker = 0;

            // Mark each node with a serial number
            Action <JObject> addSerial = (node) =>
            {
                if (!Utility.IsInContext(node))
                {
                    int serial = marker++;
                    node[Constants.CacheNode] = serial;
                    nodes.Add(serial, node);
                }
            };

            // add serials
            Utility.JsonEntityVisitor(compacted, addSerial);

            // create graph without JTokens
            var basicGraph = Utility.GetGraphFromCompacted(compacted);

            // split out the cache triples
            List <Triple> normalTriples = new List <Triple>();
            Dictionary <string, JObject> cacheTriples = new Dictionary <string, JObject>();

            foreach (var triple in basicGraph.Triples)
            {
                // cache node predicates represent the mapping between the subject and token serial
                if (triple.Predicate.IsValue(Constants.CacheNode))
                {
                    string subject = triple.Subject.GetValue();

                    int serial;
                    Int32.TryParse(triple.Object.GetValue(), out serial);

                    // Remove the serial we added
                    JObject jObject = nodes[serial];
                    jObject.Remove(Constants.CacheNode);

                    // there should not be any duplicates here
                    cacheTriples.Add(subject, jObject);
                }
                else
                {
                    // store this to go into the graph
                    normalTriples.Add(triple);
                }
            }

            // create the real graph
            JsonLdGraph jsonGraph = new JsonLdGraph();

            // merge the graph data with the compacted json tokens
            foreach (var triple in normalTriples)
            {
                string subject = triple.Subject.GetValue();

                JObject jObject = null;
                cacheTriples.TryGetValue(subject, out jObject);

                var jsonTriple = new JsonLdTriple(page, jObject, triple.Subject, triple.Predicate, triple.Object);
                jsonGraph.Assert(jsonTriple);
            }

            return(jsonGraph);
        }