From ba444bc144e81d3aa752e4b0c21329ecbbb48525 Mon Sep 17 00:00:00 2001 From: Marco De Salvo Date: Thu, 14 Mar 2024 17:50:08 +0100 Subject: [PATCH] Slight code quality and performance improvements --- RDFSharp/Model/RDFGraph.cs | 21 ++++++++----------- RDFSharp/Model/RDFModelUtilities.cs | 5 +++-- .../Constraints/RDFMaxExclusiveConstraint.cs | 18 ++++------------ .../Constraints/RDFMaxInclusiveConstraint.cs | 18 ++++------------ .../Constraints/RDFMinExclusiveConstraint.cs | 18 ++++------------ .../Constraints/RDFMinInclusiveConstraint.cs | 18 ++++------------ .../Model/Validation/RDFValidationResult.cs | 8 +++---- .../Algebra/Queries/RDFConstructQuery.cs | 16 +++++++------- 8 files changed, 40 insertions(+), 82 deletions(-) diff --git a/RDFSharp/Model/RDFGraph.cs b/RDFSharp/Model/RDFGraph.cs index 899a0d3b..cd9941c2 100644 --- a/RDFSharp/Model/RDFGraph.cs +++ b/RDFSharp/Model/RDFGraph.cs @@ -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)); } } } @@ -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 diff --git a/RDFSharp/Model/RDFModelUtilities.cs b/RDFSharp/Model/RDFModelUtilities.cs index 6aab4e76..4ca4ae3e 100644 --- a/RDFSharp/Model/RDFModelUtilities.cs +++ b/RDFSharp/Model/RDFModelUtilities.cs @@ -438,8 +438,9 @@ internal static List 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()))); diff --git a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxExclusiveConstraint.cs b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxExclusiveConstraint.cs index fce789db..bd4a0c54 100644 --- a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxExclusiveConstraint.cs +++ b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxExclusiveConstraint.cs @@ -36,23 +36,13 @@ public class RDFMaxExclusiveConstraint : RDFConstraint /// Default-ctor to build a maxExclusive constraint with the given resource value /// 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."); /// /// Default-ctor to build a maxExclusive constraint with the given literal value /// 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 @@ -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)); } diff --git a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxInclusiveConstraint.cs b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxInclusiveConstraint.cs index 52119797..9a67bfaa 100644 --- a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxInclusiveConstraint.cs +++ b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMaxInclusiveConstraint.cs @@ -36,23 +36,13 @@ public class RDFMaxInclusiveConstraint : RDFConstraint /// Default-ctor to build a maxInclusive constraint with the given resource value /// 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."); /// /// Default-ctor to build a maxInclusive constraint with the given literal value /// 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 @@ -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)); } diff --git a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinExclusiveConstraint.cs b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinExclusiveConstraint.cs index 532b6942..017e6ef2 100644 --- a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinExclusiveConstraint.cs +++ b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinExclusiveConstraint.cs @@ -36,23 +36,13 @@ public class RDFMinExclusiveConstraint : RDFConstraint /// Default-ctor to build a minExclusive constraint with the given resource value /// 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."); /// /// Default-ctor to build a minExclusive constraint with the given literal value /// 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 @@ -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)); } diff --git a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinInclusiveConstraint.cs b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinInclusiveConstraint.cs index c05f3082..36013c1a 100644 --- a/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinInclusiveConstraint.cs +++ b/RDFSharp/Model/Validation/Abstractions/Constraints/RDFMinInclusiveConstraint.cs @@ -36,23 +36,13 @@ public class RDFMinInclusiveConstraint : RDFConstraint /// Default-ctor to build a minInclusive constraint with the given resource value /// 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."); /// /// Default-ctor to build a minInclusive constraint with the given literal value /// 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 @@ -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)); } diff --git a/RDFSharp/Model/Validation/RDFValidationResult.cs b/RDFSharp/Model/Validation/RDFValidationResult.cs index 2b50ed37..372e30e8 100644 --- a/RDFSharp/Model/Validation/RDFValidationResult.cs +++ b/RDFSharp/Model/Validation/RDFValidationResult.cs @@ -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) @@ -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)); } diff --git a/RDFSharp/Query/Mirella/Algebra/Queries/RDFConstructQuery.cs b/RDFSharp/Query/Mirella/Algebra/Queries/RDFConstructQuery.cs index 15b2df81..d53b5624 100644 --- a/RDFSharp/Query/Mirella/Algebra/Queries/RDFConstructQuery.cs +++ b/RDFSharp/Query/Mirella/Algebra/Queries/RDFConstructQuery.cs @@ -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;