Skip to content

Commit

Permalink
graph querying cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 9, 2014
1 parent 8e713c7 commit 62f5fd0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 50 deletions.
102 changes: 102 additions & 0 deletions src/TCode.r2rml4net/Extensions/GraphExtensions.cs
@@ -0,0 +1,102 @@
#region Licence
// Copyright (C) 2012-2014 Tomasz Pluskiewicz
// http://r2rml.net/
// r2rml@t-code.pl
//
// ------------------------------------------------------------------------
//
// This file is part of r2rml4net.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
//
// ------------------------------------------------------------------------
//
// r2rml4net may alternatively be used under the LGPL licence
//
// http://www.gnu.org/licenses/lgpl.html
//
// If these licenses are not suitable for your intended use please contact
// us at the above stated email address to discuss alternative
// terms.
#endregion

using System;
using System.Linq;
using NullGuard;
using TCode.r2rml4net.Exceptions;
using TCode.r2rml4net.Mapping.Fluent;
using VDS.RDF;

namespace TCode.r2rml4net.Extensions
{
public static class GraphExtensions
{
[return: AllowNull]
public static INode GetObjectNode(this TermMapConfiguration termMap, string predicate)
{
var predicateNode = termMap.R2RMLMappings.CreateUriNode(predicate);
var triples = termMap.R2RMLMappings.GetTriplesWithSubjectPredicate(termMap.Node, predicateNode).ToArray();

if (triples.Length > 1)
throw new InvalidMapException(
string.Format("Term map {1} contains multiple values for predicate {2}:\r\n{0}",
string.Join("\r\n", triples.Select(triple => triple.Object.ToString())),
termMap.Node,
predicateNode.Uri));

return triples.SingleOrDefault().GetObject();
}

[return: AllowNull]
public static string GetLiteral([AllowNull] this INode node)
{
var literalNode = node as ILiteralNode;

if (literalNode != null)
{
return literalNode.Value;
}

return null;
}

[return: AllowNull]
public static Uri GetIri([AllowNull] this INode node)
{
var literalNode = node as IUriNode;

if (literalNode != null)
{
return literalNode.Uri;
}

return null;
}

private static INode GetObject([AllowNull] this Triple node)
{
if (node == null)
{
return null;
}

return node.Object;
}
}
}
3 changes: 2 additions & 1 deletion src/TCode.r2rml4net/Mapping/Fluent/ObjectMapConfiguration.cs
Expand Up @@ -40,6 +40,7 @@
using System.Linq;
using NullGuard;
using TCode.r2rml4net.Exceptions;
using TCode.r2rml4net.Extensions;
using TCode.r2rml4net.Validation;
using VDS.RDF;

Expand Down Expand Up @@ -213,7 +214,7 @@ public Uri URI
public string Literal
{
[return: AllowNull]
get { return GetSingleLiteralValueForPredicate(R2RMLMappings.CreateUriNode(R2RMLUris.RrConstantProperty)); }
get { return this.GetObjectNode(R2RMLUris.RrConstantProperty).GetLiteral(); }
}

#endregion
Expand Down
53 changes: 4 additions & 49 deletions src/TCode.r2rml4net/Mapping/Fluent/TermMapConfiguration.cs
Expand Up @@ -39,6 +39,7 @@
using System.Linq;
using NullGuard;
using TCode.r2rml4net.Exceptions;
using TCode.r2rml4net.Extensions;
using VDS.RDF;

namespace TCode.r2rml4net.Mapping.Fluent
Expand Down Expand Up @@ -221,7 +222,7 @@ protected void AssertTermTypeNotSet()
public string ColumnName
{
[return: AllowNull]
get { return GetSingleLiteralValueForPredicate(R2RMLMappings.CreateUriNode(R2RMLUris.RrColumnProperty)); }
get { return this.GetObjectNode(R2RMLUris.RrColumnProperty).GetLiteral(); }
}

/// <summary>
Expand All @@ -230,7 +231,7 @@ public string ColumnName
public string Template
{
[return: AllowNull]
get { return GetSingleLiteralValueForPredicate(R2RMLMappings.CreateUriNode(R2RMLUris.RrTemplateProperty)); }
get { return this.GetObjectNode(R2RMLUris.RrTemplateProperty).GetLiteral(); }
}

/// <summary>
Expand All @@ -240,7 +241,7 @@ public string Template
protected internal Uri ConstantValue
{
[return: AllowNull]
get { return GetSingleUriValueForPredicate(R2RMLMappings.CreateUriNode(R2RMLUris.RrConstantProperty)); }
get { return this.GetObjectNode(R2RMLUris.RrConstantProperty).GetIri(); }
}

/// <summary>
Expand Down Expand Up @@ -362,51 +363,5 @@ public void IsColumnValued(string columnName)

R2RMLMappings.Assert(Node, R2RMLMappings.CreateUriNode(R2RMLUris.RrColumnProperty), R2RMLMappings.CreateLiteralNode(columnName));
}

/// <summary>
/// Gets a single literal object value for <see cref="BaseConfiguration.Node"/> ans <paramref name="predicate"/> predicate
/// </summary>
/// <exception cref="InvalidMapException">if multiple values found or object is not a literal</exception>
protected string GetSingleLiteralValueForPredicate(IUriNode predicate)
{
var triplesForPredicate = R2RMLMappings.GetTriplesWithSubjectPredicate(Node, predicate).ToArray();

if (triplesForPredicate.Length > 1)
throw new InvalidMapException(
string.Format("Term map {1} contains multiple constant values:\r\n{0}",
string.Join("\r\n", triplesForPredicate.Select(triple => triple.Object.ToString())),
Node));

if (triplesForPredicate.Length == 1)
{
var node = triplesForPredicate[0].Object as ILiteralNode;
if (node != null)
{
return node.Value;
}
}

return null;
}

/// <summary>
/// Gets a single URI object value for <see cref="BaseConfiguration.Node"/> ans <paramref name="predicate"/> predicate
/// </summary>
/// <exception cref="InvalidMapException">if multiple values found or object is not a URI</exception>
protected Uri GetSingleUriValueForPredicate(IUriNode predicate)
{
var triplesForPredicate = R2RMLMappings.GetTriplesWithSubjectPredicate(Node, predicate).ToArray();

if (triplesForPredicate.Length > 1)
throw new InvalidMapException(
string.Format("Term map {1} contains multiple values constant values:\r\n{0}",
string.Join("\r\n", triplesForPredicate.Select(triple => triple.Object.ToString())),
Node));

if (triplesForPredicate.Length == 1 && triplesForPredicate[0].Object is IUriNode)
return ((IUriNode)triplesForPredicate[0].Object).Uri;

return null;
}
}
}
1 change: 1 addition & 0 deletions src/TCode.r2rml4net/TCode.r2rml4net.csproj
Expand Up @@ -73,6 +73,7 @@
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="Exceptions\InvalidSqlVersionException.cs" />
<Compile Include="Extensions\GraphExtensions.cs" />
<Compile Include="Extensions\UriExtensions.cs" />
<Compile Include="IFreezable.cs" />
<Compile Include="Log\LogFacadeBase.cs" />
Expand Down

0 comments on commit 62f5fd0

Please sign in to comment.