Skip to content

Commit

Permalink
Merge pull request #209 from dotnetrdf/feature/issue-207
Browse files Browse the repository at this point in the history
Fixed GetUriNode to return nodes that are used as predicate in the graph
  • Loading branch information
kal committed Oct 8, 2018
2 parents 50750c7 + ee4bbc4 commit 858007e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
15 changes: 6 additions & 9 deletions Libraries/dotNetRDF/Core/Graph.cs
Expand Up @@ -219,17 +219,14 @@ public override IBlankNode GetBlankNode(String nodeId)

private T GetNode<T>(T node) where T: INode
{
var withSubject = Triples.WithSubject(node);
var withObject = Triples.WithObject(node);
var ret = (T) Triples.WithSubject(node).FirstOrDefault()?.Subject;
if (ret != null) return ret;

var firstWithSubject = withSubject.FirstOrDefault();
ret = (T) Triples.WithObject(node).FirstOrDefault()?.Object;
if (ret != null) return ret;

if (firstWithSubject != null)
{
return (T)firstWithSubject.Subject;
}

return (T)withObject.FirstOrDefault()?.Object;
ret = (T) Triples.WithPredicate(node).FirstOrDefault()?.Predicate;
return ret;
}

#endregion
Expand Down
19 changes: 19 additions & 0 deletions Testing/unittest/Core/AbstractGraphTests.cs
Expand Up @@ -142,6 +142,25 @@ public void GetUriNode_ShouldReturnIfUsedAsSubject()
Assert.Same(node, uriNode);
}

[Fact]
public void GetUriNode_ShouldReturnIfUsedAsPredicate()
{
// given
var graph = GetInstance();
var uri = new Uri("http://example.com/pred");
var node = graph.CreateUriNode(uri);
graph.Assert(
graph.CreateBlankNode(),
node,
graph.CreateBlankNode());

// subj
var uriNode = graph.GetUriNode(uri);

// then
Assert.Same(node, uriNode);
}

[Fact]
public void GetUriNode_ShouldReturnNullWhenNotFound()
{
Expand Down

0 comments on commit 858007e

Please sign in to comment.