Skip to content

Commit

Permalink
Optimize triple/quadruple ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Nov 26, 2023
1 parent 096a0e2 commit 62f4273
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
23 changes: 14 additions & 9 deletions RDFSharp/Model/RDFTriple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public class RDFTriple : IEquatable<RDFTriple>
/// <summary>
/// SPO-flavor ctor
/// </summary>
public RDFTriple(RDFResource subj, RDFResource pred, RDFResource obj)
public RDFTriple(RDFResource subj, RDFResource pred, RDFResource obj)
: this(subj, pred)
{
#region Guards
if (pred == null)
Expand All @@ -78,29 +79,33 @@ public RDFTriple(RDFResource subj, RDFResource pred, RDFResource obj)
#endregion

TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPO;
Subject = subj ?? new RDFResource();
Predicate = pred;
Object = obj ?? new RDFResource();
LazyTripleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", TripleID.ToString())));
}

/// <summary>
/// SPL-flavor ctor
/// </summary>
public RDFTriple(RDFResource subj, RDFResource pred, RDFLiteral lit)
: this(subj, pred)
{
TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPL;
Object = lit ?? new RDFPlainLiteral(string.Empty);
}

/// <summary>
/// Initializer-ctor for common triple properties
/// </summary>
private RDFTriple(RDFResource subj, RDFResource pred)
{
#region Guards
if (pred == null)
throw new RDFModelException("Cannot create RDFTriple because \"pred\" parameter is null");
throw new RDFModelException("Cannot create RDFTriple because given \"pred\" parameter is null");
if (pred.IsBlank)
throw new RDFModelException("Cannot create RDFTriple because \"pred\" parameter is a blank resource");
throw new RDFModelException("Cannot create RDFTriple because given \"pred\" parameter is a blank resource");
#endregion

TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPL;
Subject = subj ?? new RDFResource();
Predicate = pred;
Object = lit ?? new RDFPlainLiteral(string.Empty);
LazyTripleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", TripleID.ToString())));
}
Expand Down
27 changes: 14 additions & 13 deletions RDFSharp/Store/RDFQuadruple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,37 @@ public class RDFQuadruple : IEquatable<RDFQuadruple>
/// SPO-flavor ctor
/// </summary>
public RDFQuadruple(RDFContext context, RDFResource subj, RDFResource pred, RDFResource obj)
: this(context, subj, pred)
{
if (pred == null)
throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is null");
if (pred.IsBlank)
throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is a blank resource");

TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPO;
Context = context ?? new RDFContext();
Subject = subj ?? new RDFResource();
Predicate = pred;
TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPO;
Object = obj ?? new RDFResource();
LazyQuadrupleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", QuadrupleID.ToString())));
}

/// <summary>
/// SPL-flavor ctor
/// </summary>
public RDFQuadruple(RDFContext context, RDFResource subj, RDFResource pred, RDFLiteral lit)
: this(context, subj, pred)
{
TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPL;
Object = lit ?? new RDFPlainLiteral(string.Empty);
}

/// <summary>
/// Initializer-ctor for common quadruple properties
/// </summary>
private RDFQuadruple(RDFContext context, RDFResource subj, RDFResource pred)
{
#region Guards
if (pred == null)
throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is null");
if (pred.IsBlank)
throw new RDFStoreException("Cannot create RDFQuadruple because given \"pred\" parameter is a blank resource");
#endregion

TripleFlavor = RDFModelEnums.RDFTripleFlavors.SPL;
Context = context ?? new RDFContext();
Subject = subj ?? new RDFResource();
Predicate = pred;
Object = lit ?? new RDFPlainLiteral(string.Empty);
LazyQuadrupleID = new Lazy<long>(() => RDFModelUtilities.CreateHash(ToString()));
LazyReificationSubject = new Lazy<RDFResource>(() => new RDFResource(string.Concat("bnode:", QuadrupleID.ToString())));
}
Expand Down

0 comments on commit 62f4273

Please sign in to comment.