private static ICollection <KeyEventRelationship> FindFeedbackRelationshipsRecursive( KeyEventRelationship keyEventRelationship, ILookup <KeyEvent, KeyEventRelationship> fromNodesLookup, HashSet <KeyEventRelationship> visited ) { if (visited.Contains(keyEventRelationship)) { return(new List <KeyEventRelationship>() { keyEventRelationship }); } else { visited = new HashSet <KeyEventRelationship>(visited); visited.Add(keyEventRelationship); var linkedKeyEventRelationships = fromNodesLookup.Contains(keyEventRelationship.ToNode) ? fromNodesLookup[keyEventRelationship.ToNode].ToList() : new List <KeyEventRelationship>(); return(linkedKeyEventRelationships .SelectMany(r => FindFeedbackRelationshipsRecursive(r, fromNodesLookup, visited)) .Distinct() .ToList()); } }
private List <KeyEventRelationship> GetKeyEventRelationShips( string idAopNetwork, IDictionary <string, KeyEvent> keyEvents ) { var endpoint = new Uri("http://aopwiki-rdf.prod.openrisknet.org/sparql/"); var queryString = new SparqlParameterizedString(); queryString.Namespaces.AddNamespace("dc", new Uri("http://purl.org/dc/elements/1.1/")); queryString.Namespaces.AddNamespace("aopo", new Uri("http://aopkb.org/aop_ontology#")); queryString.Namespaces.AddNamespace("rdfs", new Uri("http://www.w3.org/2000/01/rdf-schema#")); queryString.SetUri("value", new Uri("http://aopwiki-rdf.prod.openrisknet.org/sparql/")); queryString.SetUri("id", new Uri($@"http://identifiers.org/aop/{idAopNetwork}")); queryString.CommandText = (@" SELECT ?KeUpstreamName ?KeDownstreamName WHERE { ?KeDownstream a aopo:KeyEvent ; rdfs:label ?KeDownstreamName . ?KeUpstream a aopo:KeyEvent ; rdfs:label ?KeUpstreamName . ?Ker a aopo:KeyEventRelationship ; aopo:has_upstream_key_event ?KeUpstream ; aopo:has_downstream_key_event ?KeDownstream . ?Aop a aopo:AdverseOutcomePathway ; dc:identifier ?AopId ; rdfs:label ?AopName ; aopo:has_key_event ?KeUpstream ; aopo:has_key_event ?KeDownstream ; aopo:has_key_event_relationship ?Ker . FILTER (?AopId = @id) }"); var parser = new SparqlQueryParser(); var processor = new RemoteQueryProcessor(new SparqlRemoteEndpoint(endpoint)); var query = parser.ParseFromString(queryString); var resultSet = processor.ProcessQuery(query); if (resultSet is SparqlResultSet) { var result = new List <KeyEventRelationship>(); foreach (var resultRow in (resultSet as SparqlResultSet)) { var idDownstreamKeyEvent = ((ILiteralNode)resultRow["KeDownstreamName"]).Value.Replace("KE ", ""); var idUpstreamKeyEvent = ((ILiteralNode)resultRow["KeUpstreamName"]).Value.Replace("KE ", ""); keyEvents.TryGetValue(idDownstreamKeyEvent, out var downstreamKeyEvent); keyEvents.TryGetValue(idUpstreamKeyEvent, out var upstreamKeyEvent); var record = new KeyEventRelationship() { FromNode = upstreamKeyEvent, ToNode = downstreamKeyEvent, }; result.Add(record); } return(result); } else { return(null); } }