From e43e325498013a4b7503c9fc8c7b18794cf6c6ef Mon Sep 17 00:00:00 2001 From: Heinrichs <Heinrichs@itc.rwth-aachen.de> Date: Wed, 4 Jan 2023 11:21:46 +0100 Subject: [PATCH] Fix: Do not remove catalog assignment --- .../Implementations/ResourceStructuralData.cs | 13 ++---- .../ResourceTypeStructuralData.cs | 2 +- src/SQL2Linked/StructuralData.cs | 43 +++++++++++++++---- src/SQL2Linked/WrapperGraph.cs | 40 +++++++++++++++++ 4 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 src/SQL2Linked/WrapperGraph.cs diff --git a/src/SQL2Linked/Implementations/ResourceStructuralData.cs b/src/SQL2Linked/Implementations/ResourceStructuralData.cs index cccc6af..6d3abb1 100644 --- a/src/SQL2Linked/Implementations/ResourceStructuralData.cs +++ b/src/SQL2Linked/Implementations/ResourceStructuralData.cs @@ -136,19 +136,12 @@ namespace SQL2Linked.Implementations AssertToGraphLiteralNode(graph, resourceGraphName, cosc + "terms/resource#deleted", entry.Deleted.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean")); Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#deleted {entry.Deleted}'. "); - var targetClass = GetTargetClass(entry); - - SparqlParameterizedString cmdString = new SparqlParameterizedString + // Reinstate the catalog assignments + var cmdString = new SparqlParameterizedString { - CommandText = "SELECT DISTINCT ?g " + - "WHERE {" + - "GRAPH ?g { ?s a <" + targetClass + "> } . " + - "FILTER(contains(STR(?g), \"" + resourceHandleName + "@\"))" + - "}" + CommandText = "SELECT DISTINCT ?o WHERE { <" + resourceGraphName + "> <http://www.w3.org/ns/dcat#catalog> ?o }" }; - var resultSet = WrapRequest(() => RdfStoreConnector.QueryEndpoint.QueryWithResultSet(cmdString.ToString())); - foreach (var result in resultSet) { AssertToGraphUriNode(graph, resourceGraphName, dcat + "catalog", result[0].ToString()); diff --git a/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs b/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs index be7ca6e..d2536f6 100644 --- a/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs +++ b/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs @@ -34,7 +34,7 @@ namespace SQL2Linked.Implementations } // check if a triple with dcterms:title '{entry.DisplayName}' already exists in the role graph - var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(new Uri(dcterms + "Title")); + var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(new Uri(dcterms + "title")); if (!getTriplesDctermsTitle.Any()) { diff --git a/src/SQL2Linked/StructuralData.cs b/src/SQL2Linked/StructuralData.cs index 0e077a6..2788821 100644 --- a/src/SQL2Linked/StructuralData.cs +++ b/src/SQL2Linked/StructuralData.cs @@ -3,6 +3,7 @@ using Coscine.Database.Models; using Coscine.Metadata; using Polly; using VDS.RDF; +using VDS.RDF.Query.Algebra; namespace SQL2Linked { @@ -44,24 +45,48 @@ namespace SQL2Linked } } - private void StoreGraphs(IEnumerable<IGraph> graphs) + public void StoreGraphs(IEnumerable<IGraph> graphs) { foreach (var graph in graphs) { try { Console.WriteLine($" ({graph.BaseUri})"); - var exists = WrapRequest(() => RdfStoreConnector.HasGraph(graph.BaseUri)); - if (exists) + + if (graph is WrapperGraph) + { + var wrapperGraph = (WrapperGraph)graph; + // Chunking since the size otherwise can be too large + foreach (var triples in wrapperGraph.AssertList.Chunk(100)) + { + WrapRequest(() => RdfStoreConnector.ReadWriteSparqlConnector.UpdateGraph(graph.BaseUri, triples, new List<Triple>())); + } + // Chunking since the size otherwise can be too large + foreach (var triples in wrapperGraph.RetractList.Chunk(100)) + { + WrapRequest(() => RdfStoreConnector.ReadWriteSparqlConnector.UpdateGraph(graph.BaseUri, new List<Triple>(), triples)); + } + } + else { - Console.WriteLine($" - Graph {graph.BaseUri} exists"); + var exists = WrapRequest(() => RdfStoreConnector.HasGraph(graph.BaseUri)); + if (exists) + { + Console.WriteLine($" - Graph {graph.BaseUri} exists"); - // Clear the existing graph from the store - WrapRequest(() => RdfStoreConnector.ClearGraph(graph.BaseUri)); - Console.WriteLine($" - Cleared Graph {graph.BaseUri}"); + // Clear the existing graph from the store + WrapRequest(() => RdfStoreConnector.ClearGraph(graph.BaseUri)); + Console.WriteLine($" - Cleared Graph {graph.BaseUri}"); + } + + // Chunking since the size otherwise can be too large + // Don't change to only addition of triples, otherwise this could break things + foreach (var triples in graph.Triples.Chunk(100)) + { + WrapRequest(() => RdfStoreConnector.ReadWriteSparqlConnector.UpdateGraph(graph.BaseUri, triples, new List<Triple>())); + } } - // Add the new graph to the store - WrapRequest(() => RdfStoreConnector.AddGraph(graph)); + Console.WriteLine($" - Graph {graph.BaseUri} added successfully"); Console.WriteLine(); } diff --git a/src/SQL2Linked/WrapperGraph.cs b/src/SQL2Linked/WrapperGraph.cs new file mode 100644 index 0000000..ce318c3 --- /dev/null +++ b/src/SQL2Linked/WrapperGraph.cs @@ -0,0 +1,40 @@ +using VDS.RDF; + +namespace SQL2Linked +{ + public class WrapperGraph : Graph + { + public List<Triple> AssertList { get; set; } + public List<Triple> RetractList { get; set; } + + public WrapperGraph() : base() + { + AssertList = new List<Triple>(); + RetractList = new List<Triple>(); + } + + public override bool Assert(Triple t) + { + AssertList.Add(t); + return base.Assert(t); + } + + public override bool Assert(IEnumerable<Triple> triples) + { + AssertList.AddRange(triples); + return base.Assert(triples); + } + + public override bool Retract(Triple t) + { + RetractList.Add(t); + return base.Retract(t); + } + + public override bool Retract(IEnumerable<Triple> triples) + { + RetractList.AddRange(triples); + return base.Retract(triples); + } + } +} -- GitLab