Skip to content

Commit

Permalink
Slight code quality and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco De Salvo committed Mar 14, 2024
1 parent 6b56c3d commit ba444bc
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 82 deletions.
21 changes: 9 additions & 12 deletions RDFSharp/Model/RDFGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,18 @@ public void UnreifyTriples()
RDFPatternMember tObject = RDFQueryUtilities.ParseRDFPatternMember(((DataRow)reifiedTriples.Current)["?O"].ToString());

//Cleanup graph from detected reifications
if (tObject is RDFResource)
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
if (tObject is RDFResource tObjRes)
{
AddTriple(new RDFTriple((RDFResource)tSubject, (RDFResource)tPredicate, (RDFResource)tObject));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, (RDFResource)tObject));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, tObjRes));
AddTriple(new RDFTriple((RDFResource)tSubject, (RDFResource)tPredicate, tObjRes));
}
else
{
AddTriple(new RDFTriple((RDFResource)tSubject, (RDFResource)tPredicate, (RDFLiteral)tObject));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.STATEMENT));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.SUBJECT, (RDFResource)tSubject));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.PREDICATE, (RDFResource)tPredicate));
RemoveTriple(new RDFTriple((RDFResource)tRepresent, RDFVocabulary.RDF.OBJECT, (RDFLiteral)tObject));
AddTriple(new RDFTriple((RDFResource)tSubject, (RDFResource)tPredicate, (RDFLiteral)tObject));
}
}
}
Expand Down Expand Up @@ -720,8 +717,8 @@ public static RDFGraph FromDataTable(DataTable table)
throw new RDFModelException("Cannot read RDF graph from datatable because given \"table\" parameter contains a row having null value in the \"?OBJECT\" column.");

RDFPatternMember rowObj = RDFQueryUtilities.ParseRDFPatternMember(tableRow["?OBJECT"].ToString());
if (rowObj is RDFResource)
result.AddTriple(new RDFTriple((RDFResource)rowSubj, (RDFResource)rowPred, (RDFResource)rowObj));
if (rowObj is RDFResource rowObjRes)
result.AddTriple(new RDFTriple((RDFResource)rowSubj, (RDFResource)rowPred, rowObjRes));
else
result.AddTriple(new RDFTriple((RDFResource)rowSubj, (RDFResource)rowPred, (RDFLiteral)rowObj));
#endregion
Expand Down
5 changes: 3 additions & 2 deletions RDFSharp/Model/RDFModelUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,9 @@ internal static List<RDFNamespace> GetGraphNamespaces(RDFGraph graph)
{
string subj = triple.Subject.ToString();
string pred = triple.Predicate.ToString();
string obj = triple.Object is RDFResource ? triple.Object.ToString() :
(triple.Object is RDFTypedLiteral ? GetDatatypeFromEnum(((RDFTypedLiteral)triple.Object).Datatype) : string.Empty);
string obj = triple.Object is RDFResource ? triple.Object.ToString()
: triple.Object is RDFTypedLiteral tlitObj ? GetDatatypeFromEnum(tlitObj.Datatype)
: string.Empty;

//Resolve subject Uri
result.AddRange(RDFNamespaceRegister.Instance.Register.Where(ns => subj.StartsWith(ns.ToString())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,13 @@ public class RDFMaxExclusiveConstraint : RDFConstraint
/// Default-ctor to build a maxExclusive constraint with the given resource value
/// </summary>
public RDFMaxExclusiveConstraint(RDFResource value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMaxExclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMaxExclusiveConstraint because given \"value\" parameter is null.");

/// <summary>
/// Default-ctor to build a maxExclusive constraint with the given literal value
/// </summary>
public RDFMaxExclusiveConstraint(RDFLiteral value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMaxExclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMaxExclusiveConstraint because given \"value\" parameter is null.");
#endregion

#region Methods
Expand Down Expand Up @@ -96,8 +86,8 @@ internal override RDFGraph ToRDFGraph(RDFShape shape)
if (shape != null)
{
//sh:maxExclusive
if (Value is RDFResource)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MAX_EXCLUSIVE, (RDFResource)Value));
if (Value is RDFResource valRes)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MAX_EXCLUSIVE, valRes));
else
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MAX_EXCLUSIVE, (RDFLiteral)Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,13 @@ public class RDFMaxInclusiveConstraint : RDFConstraint
/// Default-ctor to build a maxInclusive constraint with the given resource value
/// </summary>
public RDFMaxInclusiveConstraint(RDFResource value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMaxInclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMaxInclusiveConstraint because given \"value\" parameter is null.");

/// <summary>
/// Default-ctor to build a maxInclusive constraint with the given literal value
/// </summary>
public RDFMaxInclusiveConstraint(RDFLiteral value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMaxInclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMaxInclusiveConstraint because given \"value\" parameter is null.");
#endregion

#region Methods
Expand Down Expand Up @@ -96,8 +86,8 @@ internal override RDFGraph ToRDFGraph(RDFShape shape)
if (shape != null)
{
//sh:maxInclusive
if (Value is RDFResource)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MAX_INCLUSIVE, (RDFResource)Value));
if (Value is RDFResource valRes)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MAX_INCLUSIVE, valRes));
else
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MAX_INCLUSIVE, (RDFLiteral)Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,13 @@ public class RDFMinExclusiveConstraint : RDFConstraint
/// Default-ctor to build a minExclusive constraint with the given resource value
/// </summary>
public RDFMinExclusiveConstraint(RDFResource value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMinExclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMinExclusiveConstraint because given \"value\" parameter is null.");

/// <summary>
/// Default-ctor to build a minExclusive constraint with the given literal value
/// </summary>
public RDFMinExclusiveConstraint(RDFLiteral value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMinExclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMinExclusiveConstraint because given \"value\" parameter is null.");
#endregion

#region Methods
Expand Down Expand Up @@ -96,8 +86,8 @@ internal override RDFGraph ToRDFGraph(RDFShape shape)
if (shape != null)
{
//sh:minExclusive
if (Value is RDFResource)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MIN_EXCLUSIVE, (RDFResource)Value));
if (Value is RDFResource valRes)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MIN_EXCLUSIVE, valRes));
else
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MIN_EXCLUSIVE, (RDFLiteral)Value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,13 @@ public class RDFMinInclusiveConstraint : RDFConstraint
/// Default-ctor to build a minInclusive constraint with the given resource value
/// </summary>
public RDFMinInclusiveConstraint(RDFResource value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMinInclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMinInclusiveConstraint because given \"value\" parameter is null.");

/// <summary>
/// Default-ctor to build a minInclusive constraint with the given literal value
/// </summary>
public RDFMinInclusiveConstraint(RDFLiteral value)
{
if (value == null)
throw new RDFModelException("Cannot create RDFMinInclusiveConstraint because given \"value\" parameter is null.");

Value = value;
}
=> Value = value ?? throw new RDFModelException("Cannot create RDFMinInclusiveConstraint because given \"value\" parameter is null.");
#endregion

#region Methods
Expand Down Expand Up @@ -96,8 +86,8 @@ internal override RDFGraph ToRDFGraph(RDFShape shape)
if (shape != null)
{
//sh:minInclusive
if (Value is RDFResource)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MIN_INCLUSIVE, (RDFResource)Value));
if (Value is RDFResource valRes)
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MIN_INCLUSIVE, valRes));
else
result.AddTriple(new RDFTriple(shape, RDFVocabulary.SHACL.MIN_INCLUSIVE, (RDFLiteral)Value));
}
Expand Down
8 changes: 4 additions & 4 deletions RDFSharp/Model/Validation/RDFValidationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public RDFGraph ToRDFGraph()
result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.SOURCE_CONSTRAINT_COMPONENT, SourceConstraintComponent));

//FocusNode
if (FocusNode != null && FocusNode is RDFResource)
result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.FOCUS_NODE, (RDFResource)FocusNode));
if (FocusNode != null && FocusNode is RDFResource focusNodeResource)
result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.FOCUS_NODE, focusNodeResource));

//ResultPath
if (ResultPath != null)
Expand All @@ -127,8 +127,8 @@ public RDFGraph ToRDFGraph()
//Value
if (ResultValue != null)
{
if (ResultValue is RDFLiteral)
result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.VALUE, (RDFLiteral)ResultValue));
if (ResultValue is RDFLiteral resvalLit)
result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.VALUE, resvalLit));
else
result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.VALUE, (RDFResource)ResultValue));
}
Expand Down
16 changes: 8 additions & 8 deletions RDFSharp/Query/Mirella/Algebra/Queries/RDFConstructQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,31 @@ public RDFConstructQuery AddTemplate(RDFPattern template)
Templates.Add(template);

//Context
if (template.Context != null && template.Context is RDFVariable)
if (template.Context != null && template.Context is RDFVariable ctxVar)
{
if (!Variables.Any(v => v.Equals(template.Context)))
Variables.Add((RDFVariable)template.Context);
Variables.Add(ctxVar);
}

//Subject
if (template.Subject is RDFVariable)
if (template.Subject is RDFVariable subjVar)
{
if (!Variables.Any(v => v.Equals(template.Subject)))
Variables.Add((RDFVariable)template.Subject);
Variables.Add(subjVar);
}

//Predicate
if (template.Predicate is RDFVariable)
if (template.Predicate is RDFVariable predVar)
{
if (!Variables.Any(v => v.Equals(template.Predicate)))
Variables.Add((RDFVariable)template.Predicate);
Variables.Add(predVar);
}

//Object
if (template.Object is RDFVariable)
if (template.Object is RDFVariable objVar)
{
if (!Variables.Any(v => v.Equals(template.Object)))
Variables.Add((RDFVariable)template.Object);
Variables.Add(objVar);
}
}
return this;
Expand Down

0 comments on commit ba444bc

Please sign in to comment.