private string toRelation(IRelation obj, string tag) { string type = obj.GetType().Name; string jsonObj = JsonConvert.SerializeObject(obj); string query = $"[{tag}:{type} {Regex.Replace(jsonObj, regexPattern, "$1:")}]"; return(query); }
public void addRelation(IRelation r) { if (_relations.Contains(r)) { throw new System.ArgumentException("Relation is already defined in the current state", r.ToString()); } if (r.GetType() == typeof(UnaryRelation)) { UnaryRelation ur = r as UnaryRelation; // check that the source entitiy in the relation is already part of the state if (_entities.Contains(ur.Source) == false) { throw new System.ArgumentException("Relation source " + ur.Source + " does not exist in this state"); } // check that all the predicate in the relation is defined in the domain if (_domain.Predicates.Contains(ur.Predicate) == false) { throw new System.ArgumentException("Relation predicate " + ur.Predicate + " does not exist in this domain"); } } if (r.GetType() == typeof(BinaryRelation)) { BinaryRelation br = r as BinaryRelation; // check that the source and destination entitiy in the relation is already part of the state if (_entities.Contains(br.Source) == false) { throw new System.ArgumentException("Relation source " + br.Source + " does not exist in this state"); } if (_entities.Contains(br.Destination) == false) { throw new System.ArgumentException("Relation destination " + br.Destination + " does not exist in this state"); } // check that all the predicate in the relation is defined in the domain if (_domain.Predicates.Contains(br.Predicate) == false) { throw new System.ArgumentException("Relation predicate " + br.Predicate + " does not exist in this domain"); } } _relations.Add(r); }
/// <summary> /// Updates a relation between nodeFrom and nodeTo in Neo4j. /// </summary> /// <param name="nodeFrom">Node From</param> /// <param name="relation">Type of the Edge</param> /// <param name="nodeTo">Node To</param> /// <returns></returns> public INode UpdateRelation(INode nodeFrom, IRelation relation, INode nodeTo) { string tagFrom = nodeFrom.GetType().Name.ToLower(); string tagTo = nodeTo.GetType().Name.ToLower(); string jsonObj = JsonConvert.SerializeObject(relation); string relationJson = Regex.Replace(jsonObj, regexPattern, "$1:"); _client.Cypher.Match(toNode(nodeFrom)) .Match(toNode(nodeTo)) .Match($"({tagFrom})-[r:{relation.GetType().Name}]->({tagTo})") .Set($"r = {relationJson}").ExecuteWithoutResultsAsync(); return(nodeTo); }
private async Task AddRelation(DocumentCollection collection, IRelation relation, string relationKey, int?i = null) { var expression = $"g.V('{relation.FromId}').AddE('{relationKey}')"; // TODO: It seems Gremlin accepts properties that we add to the Edge but they are not persisted ... //var properties = relation.GetProperties(); //foreach (var property in properties.Where(p => p.Key != "fromId" && p.Key != "toId")) //{ // expression += $".property('{property.Key}', {property.Value.ToValueString()})"; //} expression += $".to(g.V('{relation.ToId}'))"; await Execute(collection, expression); var edgeCounter = i.HasValue ? $"({i})" : string.Empty; Console.WriteLine($"{relation.GetType().Name} relation created {edgeCounter}"); }
/***************************************************/ /**** Private Methods ****/ /***************************************************/ private static IRelation UniqueEntities(this IRelation relation, Dictionary <Guid, IBHoMObject> replaceMap) { if (replaceMap.ContainsKey(relation.Source)) { relation.Source = replaceMap[relation.Source].BHoM_Guid; } else { Reflection.Compute.RecordError($"The Source reference on IRelation of type {relation.GetType().ToString()} cannot be found in the entities provided. Check all required entities have been included."); } if (replaceMap.ContainsKey(relation.Target)) { relation.Target = replaceMap[relation.Target].BHoM_Guid; } else { Reflection.Compute.RecordError($"The Target reference on IRelation of type {relation.GetType().ToString()} cannot be found in the entities provided. Check all required entities have been included."); } //go deeper into making the subgraph unique is an option for future use //relation.Subgraph.UniqueEntities(replaceMap); return(relation); }
private string toRelation(IRelation obj) { return(this.toRelation(obj, tag: obj.GetType().Name.ToLower())); }