Skip to content
Snippets Groups Projects

Release: Sprint/2022 24 :robot:

Merged CoscineBot requested to merge dev into master
1 file
+ 163
118
Compare changes
  • Side-by-side
  • Inline
@@ -18,8 +18,8 @@ namespace Coscine.Metadata
public readonly SparqlRemoteEndpoint QueryEndpoint;
public readonly ReadWriteSparqlConnector ReadWriteSparqlConnector;
public readonly string UserUrlPrefix = "https://purl.org/coscine/users";
public readonly Uri OrgPrefixUrl = new Uri("http://www.w3.org/ns/org#");
public readonly Uri FoafPrefixUrl = new Uri("http://xmlns.com/foaf/0.1/");
public readonly Uri OrgPrefixUrl = new("http://www.w3.org/ns/org#");
public readonly Uri FoafPrefixUrl = new("http://xmlns.com/foaf/0.1/");
public RdfStoreConnector(string sparqlEndpoint = "http://localhost:8890/sparql")
{
@@ -42,11 +42,12 @@ namespace Coscine.Metadata
{
var commandString = new SparqlParameterizedString
{
CommandText = @"SELECT DISTINCT ?s
WHERE
{
?s a <http://www.w3.org/ns/shacl#NodeShape>
}"
CommandText = @"
SELECT DISTINCT ?s
WHERE
{
?s a <http://www.w3.org/ns/shacl#NodeShape>
}"
};
var resultSet = QueryEndpoint.QueryWithResultSet(commandString.ToString());
return resultSet.Select(x => new Uri(x.Value("s").ToString()));
@@ -102,9 +103,9 @@ namespace Coscine.Metadata
public void ClearGraph(Uri graphUri)
{
SparqlParameterizedString queryString = new SparqlParameterizedString
var queryString = new SparqlParameterizedString
{
CommandText = "CLEAR GRAPH @graph"
CommandText = @"CLEAR GRAPH @graph"
};
queryString.SetUri("graph", graphUri);
QueryEndpoint.QueryRaw(queryString.ToString());
@@ -117,9 +118,9 @@ namespace Coscine.Metadata
public void CreateNamedGraph(Uri graphUri)
{
SparqlParameterizedString queryString = new SparqlParameterizedString
var queryString = new SparqlParameterizedString
{
CommandText = "CREATE GRAPH @graph"
CommandText = @"CREATE GRAPH @graph"
};
queryString.SetUri("graph", graphUri);
QueryEndpoint.QueryRaw(queryString.ToString());
@@ -160,7 +161,9 @@ namespace Coscine.Metadata
private Triple GetDefinitionTriple(IGraph graph, Uri graphName)
{
var triples = graph.GetTriplesWithSubjectPredicate(graph.CreateUriNode(graphName), graph.CreateUriNode(new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")));
var triples = graph.GetTriplesWithSubjectPredicate(
graph.CreateUriNode(graphName),
graph.CreateUriNode(new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")));
if (triples.Count() == 1)
{
return triples.First();
@@ -179,12 +182,18 @@ namespace Coscine.Metadata
public IEnumerable<Uri> GetVocabularies(IGraph graph)
{
return graph.GetTriplesWithPredicate(new Uri("http://www.w3.org/ns/shacl#class")).Where(x => x.Object.NodeType == NodeType.Uri).Select(x => new Uri(x.Object.ToString())).Distinct();
return graph.GetTriplesWithPredicate(new Uri("http://www.w3.org/ns/shacl#class"))
.Where(x => x.Object.NodeType == NodeType.Uri)
.Select(x => new Uri(x.Object.ToString()))
.Distinct();
}
public IEnumerable<Uri> GetClasses(IGraph graph)
{
return graph.GetTriplesWithPredicate(new Uri("http://www.w3.org/ns/shacl#class")).Where(x => x.Object.NodeType == NodeType.Uri).Select(x => new Uri(x.Object.ToString())).Distinct();
return graph.GetTriplesWithPredicate(new Uri("http://www.w3.org/ns/shacl#class"))
.Where(x => x.Object.NodeType == NodeType.Uri)
.Select(x => new Uri(x.Object.ToString()))
.Distinct();
}
public bool ValidateShacl(IGraph graph, Uri graphName)
@@ -243,9 +252,18 @@ namespace Coscine.Metadata
public IGraph GetClassGraph(Uri classElement)
{
// construct graph with all instances of classElement
SparqlParameterizedString cmdString = new SparqlParameterizedString
{
CommandText = "CONSTRUCT { ?s a @classElement . ?s ?p ?o } WHERE { ?s a ?class . ?class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* @classElement . ?s ?p ?o }"
var cmdString = new SparqlParameterizedString
{
CommandText = @"
CONSTRUCT {
?s a @classElement .
?s ?p ?o
}
WHERE {
?s a ?class .
?class <http://www.w3.org/2000/01/rdf-schema#subClassOf>* @classElement .
?s ?p ?o
}"
};
cmdString.SetUri("classElement", classElement);
@@ -292,12 +310,16 @@ namespace Coscine.Metadata
private IEnumerable<Triple> GetTriplesByObjectWrapper(Uri subject, Uri predicate, Action<SparqlParameterizedString> objWrapper)
{
SparqlParameterizedString cmdString = new SparqlParameterizedString
var cmdString = new SparqlParameterizedString
{
CommandText = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . " +
((subject != null) ? "@s " : "?s ") +
((predicate != null) ? "@p " : "?p ") +
"@o }"
CommandText = @"
SELECT ?s ?p ?o
WHERE {
?s ?p ?o . " +
((subject != null) ? "@s " : "?s ") +
((predicate != null) ? "@p " : "?p ") +
"@o " +
"}"
};
if (subject != null)
{
@@ -321,12 +343,14 @@ namespace Coscine.Metadata
public IEnumerable<Triple> GetLabelForSubject(Uri subject)
{
SparqlParameterizedString cmdString = new SparqlParameterizedString
var cmdString = new SparqlParameterizedString
{
CommandText = "PREFIX rdfs: < http://www.w3.org/2000/01/rdf-schema#> " +
"SELECT DISTINCT (@baseGraph AS ?s) (rdfs:label AS ?p) ?o " +
"WHERE { " +
"@baseGraph rdfs:label ?o . }"
CommandText = @"
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT (@baseGraph AS ?s) (rdfs:label AS ?p) ?o
WHERE {
@baseGraph rdfs:label ?o .
}"
};
cmdString.SetUri("baseGraph", subject);
var resultSet = QueryEndpoint.QueryWithResultSet(cmdString.ToString());
@@ -345,33 +369,36 @@ namespace Coscine.Metadata
var searchFilter = (searchterm != null) ? "filter contains(LCASE(?o), LCASE(@searchterm)) . " : "";
var membersAndExternalIdFilter = (member == 1 && externalIds != null) ?
@"{
SELECT DISTINCT ?s WHERE
{ ?class ?p ?memberUrl
; org:organization ?s .
{ SELECT DISTINCT ?memberUrl WHERE {
?memberUrl ?p ?value . FILTER(?value IN ( " + PrepareIncludeString(externalIds) + @" ))
} }
SELECT DISTINCT ?s
WHERE {
?class ?p ?memberUrl;
org:organization ?s .
{
SELECT DISTINCT ?memberUrl
WHERE {
?memberUrl ?p ?value .
FILTER(?value IN ( " + PrepareIncludeString(externalIds) + @" ))
}
}
}
}"
: "";
SparqlParameterizedString cmdString = new SparqlParameterizedString
{
CommandText =
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> " +
"PREFIX org: <http://www.w3.org/ns/org#> " +
"SELECT DISTINCT ?s (rdfs:label AS ?p) ?o " +
graphFilter +
"WHERE { " +
objectFilter +
"?s rdfs:label ?o . " +
searchFilter +
membersAndExternalIdFilter +
"} " +
"LIMIT @numberOfResults " +
"OFFSET @offset "
var cmdString = new SparqlParameterizedString
{
CommandText = @"
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX org: <http://www.w3.org/ns/org#>
SELECT DISTINCT ?s (rdfs:label AS ?p) ?o " + graphFilter +
@"WHERE { " +
objectFilter +
"?s rdfs:label ?o . " +
searchFilter +
membersAndExternalIdFilter +
@"}
LIMIT @numberOfResults
OFFSET @offset"
};
// bind all the required values
@@ -424,20 +451,19 @@ namespace Coscine.Metadata
{
var cmdString = new SparqlParameterizedString()
{
CommandText = $@"
CommandText = @"
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?ror (rdfs:label AS ?p) ?name
WHERE {{
WHERE {
?ror rdfs:label ?name .
{{
{
SELECT DISTINCT ?ror
WHERE {{
WHERE {
?ror ?p ?value .
FILTER( ?value IN ( @entityId ))
}}
}}
}}
}
}
}
"
};
cmdString.SetLiteral("entityId", entityId);
@@ -474,20 +500,34 @@ namespace Coscine.Metadata
public string GetIkzForOrganization(Uri subject)
{
SparqlParameterizedString cmdString = new SparqlParameterizedString
var cmdString = new SparqlParameterizedString
{
CommandText = "SELECT SUBSTR( ?o, 5 ) AS ?o WHERE {{GRAPH < https://ror.org/04xfq0f34> { @subject <http://www.w3.org/ns/org#identifier> ?o .FILTER strStarts( ?o, \"ikz:\" )}}}}"
CommandText = @"
SELECT SUBSTR( ?o, 5 ) AS ?o
WHERE {
GRAPH <https://ror.org/04xfq0f34> {
@subject <http://www.w3.org/ns/org#identifier> ?o .
FILTER strStarts( ?o, ""ikz:"" )
}
}"
};
cmdString.SetUri("baseGraph", subject);
cmdString.SetUri("subject", subject);
var resultSet = QueryEndpoint.QueryWithResultSet(cmdString.ToString());
return resultSet.First().Value("o").ToString();
}
public Dictionary<string, string> GetOrganizationToIkzMap()
{
SparqlParameterizedString cmdString = new SparqlParameterizedString
var cmdString = new SparqlParameterizedString
{
CommandText = "SELECT ?s SUBSTR( ?o, 5 ) AS ?o WHERE {{ GRAPH <https://ror.org/04xfq0f34> { ?s <http://www.w3.org/ns/org#identifier> ?o .FILTER strStarts( ?o, \"ikz:\" )}}}"
CommandText = @"
SELECT ?s SUBSTR( ?o, 5 ) AS ?o
WHERE {
GRAPH <https://ror.org/04xfq0f34> {
?s <http://www.w3.org/ns/org#identifier> ?o .
FILTER strStarts( ?o, ""ikz:"" )
}
}"
};
var resultSet = QueryEndpoint.QueryWithResultSet(cmdString.ToString());
var dict = new Dictionary<string, string>();
@@ -526,7 +566,7 @@ namespace Coscine.Metadata
{
var commandString = new SparqlParameterizedString
{
CommandText = "CREATE SILENT GRAPH @graphIri"
CommandText = @"CREATE SILENT GRAPH @graphIri"
};
commandString.SetUri("graphIri", new Uri(graphIri));
QueryEndpoint.QueryRaw(commandString.ToString());
@@ -537,11 +577,12 @@ namespace Coscine.Metadata
{
var commandString = new SparqlParameterizedString
{
CommandText = @"SELECT DISTINCT ?organization
WHERE
{
?organization org:identifier @entityId .
}"
CommandText = @"
SELECT DISTINCT ?organization
WHERE
{
?organization org:identifier @entityId .
}"
};
commandString.Namespaces.AddNamespace("org", OrgPrefixUrl);
commandString.SetLiteral("entityId", entityId);
@@ -560,15 +601,16 @@ namespace Coscine.Metadata
{
var commandString = new SparqlParameterizedString
{
CommandText = @"SELECT DISTINCT ?organization
WHERE
{
?organization org:hasUnit ?subOrganization .
?organization a org:FormalOrganization .
?nodeId org:organization ?subOrganization .
?nodeId org:member ?organizationMember .
?organizationMember foaf:openId @identifier .
}"
CommandText = @"
SELECT DISTINCT ?organization
WHERE
{
?organization org:hasUnit ?subOrganization .
?organization a org:FormalOrganization .
?nodeId org:organization ?subOrganization .
?nodeId org:member ?organizationMember .
?organizationMember foaf:openId @identifier .
}"
};
commandString.Namespaces.AddNamespace("foaf", FoafPrefixUrl);
commandString.Namespaces.AddNamespace("org", OrgPrefixUrl);
@@ -589,17 +631,18 @@ namespace Coscine.Metadata
{
var commandString = new SparqlParameterizedString
{
CommandText = @"INSERT
{
GRAPH @userGraph
{
[
a org:Membership ;
org:member @member ;
org:organization @organization ;
]
}
}"
CommandText = @"
INSERT
{
GRAPH @userGraph
{
[
a org:Membership ;
org:member @member ;
org:organization @organization ;
]
}
}"
};
commandString.Namespaces.AddNamespace("org", OrgPrefixUrl);
@@ -614,21 +657,22 @@ namespace Coscine.Metadata
{
var commandString = new SparqlParameterizedString
{
CommandText = @"DELETE
{
GRAPH @userGraph
{
?s ?p0 ?o0 .
}
}
USING @userGraph WHERE
{
?s ?p0 ?o0 .
?s a org:Membership .
?s org:member @member .
?s org:organization @organization .
}"
CommandText = @"
DELETE
{
GRAPH @userGraph
{
?s ?p0 ?o0 .
}
}
USING @userGraph WHERE
{
?s ?p0 ?o0 .
?s a org:Membership .
?s org:member @member .
?s org:organization @organization .
}"
};
commandString.Namespaces.AddNamespace("org", OrgPrefixUrl);
@@ -649,20 +693,21 @@ namespace Coscine.Metadata
var commandString = new SparqlParameterizedString
{
CommandText = @"SELECT ?resourceType MAX(?dQuota) as ?defaultQuota MAX(?dMaxQuota) as ?defaultMaxQuota
WHERE
{
?bNode2 coscineresource:type ?resourceType .
?bNode2 coscineresource:defaultQuota ?dQuota .
?bNode2 coscineresource:defaultMaxQuota ?dMaxQuota .
?organization coscineresource:typeSpecification ?bNode2 .
?bNode1 org:organization ?organization .
GRAPH @userGraph
{
?bNode1 org:member @userGraph .
}
}"
CommandText = @"
SELECT ?resourceType MAX(?dQuota) as ?defaultQuota MAX(?dMaxQuota) as ?defaultMaxQuota
WHERE
{
?bNode2 coscineresource:type ?resourceType .
?bNode2 coscineresource:defaultQuota ?dQuota .
?bNode2 coscineresource:defaultMaxQuota ?dMaxQuota .
?organization coscineresource:typeSpecification ?bNode2 .
?bNode1 org:organization ?organization .
GRAPH @userGraph
{
?bNode1 org:member @userGraph .
}
}"
};
commandString.Namespaces.AddNamespace("org", OrgPrefixUrl);
Loading