Sparql query to construct a new graph - sparql

I have two graphs with similar data but a slight difference.
my goal is to merge them using SPARQL and perform the integration. I want a final output of two RDF graphs that has a slight difference in a single RDF graph using SPARQL.
example one graph is :
ns0:BaseRoleClassLib
a ns0:ExternalReference ;
ns0:externalReferenceAlias "BaseRoleClassLib" .
ns0:maxTransportationWeight
a ns0:Attribute ;
schema:name "maxTransportationWeight" ;
ns0:hasValue "35" .
second graph is :
ns0:BaseRoleClassLib
a ns0:ExternalReference ;
ns0:maxTransportationWeight
a ns0:Attribute ;
schema:name "maxTransportationWeight" ;
ns0:hasValue "35.0" .
The only difference is one has the transport value in integer and other in the float.
So I write a query to generalise them :
select distinct ?integer
from <graph1>
from <graph2>
where {
?s ns0:hasValue ?y
Bind(xsd:integer(xsd:decimal(?y)) as ?integer)
}
}
This converts the difference in to generalised form of integer.
Now my next goal is I want to integrate these files into a single RDF by using the above result .
I want an RDF file which has the union of these and the solved generalization of float to integer.
S1 , S2 -> generalization -> integration -> s3 RDF
How can I achieve this using SPARQL constructor / insert ?
Thanks so much

This can be done pretty straightforwardly by CONSTRUCT. SPARQL update doesn't seem to support FROM, so you'd need to use a UNION of GRAPH statements. The following should get the merge you are looking for - basically filter out the old ns0:hasValue value and insert the new one:
CONSTRUCT {
?s ?p ?o .
?s ns0:hasValue ?intValue .
}
FROM <graph1>
FROM <graph2>
WHERE {
?s ?p ?o .
OPTIONAL{?s ns0:hasValue ?origValue .}
BIND(IF(datatype(?origValue) = xsd:integer, ?origValue, xsd:integer(STRBEFORE(str(?origValue), ".")) )as ?intValue)
FILTER (?p != ns0:hasValue)
}
Note that conversion of float to integer isn't straightforward. You's have to live with rounding down or have logic to round by decimal values.

Related

How to get a path between IRIs or between two nodes of certain rdf:type using a SPARQL query?

Trying to execute a query using rdf4j console against a sparql endpoint to find the path between 2 nodes using property wildcards but no luck. The first query gives an error as
Malformed query: Not a valid (absolute) IRI:
The second query crashes the console. Should I try to use the query using a different way to query the endpoint as this maybe an rdf4j issue or is the query itself wrong?
PREFIX xy: <http://mainuri/>
select
*
where
{
<http://uriOfInstanceOfData> ((<>|!<>)|^(<>|!<>))* ?x .
?x ?p ?o .
?o ((<>|!<>)|^(<>|!<>))* <http://uriOfInstanceOfData>.
}
AND
PREFIX xy: <http://mainuri/>
select
*
where
{
<http://uriOfInstanceOfData> (xy:|!xy:)* ?x .
?x ?p ?o .
?o (xy:|!xy:)* <http://uriOfInstanceOfData>.
}
The first query is syntactically incorrect: <> is not a valid IRI reference. The SPARQL grammar allows the empty string, but the specification also notes that any IRI reference must be a string that (after escape processing results) in a valid RFC3987 IRI. Since an IRI requires, at a mimimum, a scheme identifier, an empty string can by definition not be a valid IRI.
The second query works when I try it on a small test dataset. However it is likely very expensive to process.
EDIT the query I actually tried:
PREFIX xy: <http://mainuri/>
select
*
where
{
rdfs:domain (xy:|!xy:)* ?x .
?x ?p ?o .
?o (xy:|!xy:)* rdf:Property.
}
On a local in-memory database with basic RDFS inferencing enabled, that gives the following result:
Evaluating SPARQL query...
+------------------------+------------------------+------------------------+
| x | p | o |
+------------------------+------------------------+------------------------+
| rdfs:domain | rdf:type | rdf:Property |
| rdfs:domain | rdfs:domain | rdf:Property |
+------------------------+------------------------+------------------------+
2 result(s) (28 ms)

SPARQL Query to Identify Predicates from One of Many Graphs [duplicate]

This question already has an answer here:
SPARQL - Restricting Result Resource to Certain Namespace(s)
(1 answer)
Closed 4 years ago.
I have created an ontology by grouping together many ontologies.
I want to use SPARQL to identify all middle terms (relationships) from one ontology from the group ontology.
The following approach only produces a pyparsing error.
g = rdflib.Graph()
result = g.parse("without-bfo.owl")
qres = g.query(
""" PREFIX sudo: <http://purl.url/sudo/ontology#>
SELECT ?v
WHERE {
?s sudo:?v ?o.
}""")
If I remove the sudo: prefix, this query returns all triples.
You can check if the relation starts with your namespace with CONTAINS
SELECT ?v
WHERE {
?s ?v ?o.
FILTER CONTAINS(?v, "http://purl.url/sudo/ontology#")
}
You can also try STRSTARTS
see w3 documentation
#Arcturus was close.
The following worked for me. One has to declare ?v as a string using STR. This SO post suggested the syntax.
qres = g.query(
""" SELECT DISTINCT ?v
WHERE {
?s ?v ?o .
FILTER CONTAINS(STR(?v), "sudo")
}""")
for row in qres:
print row

SPARQL list literals of selected OWL Datatype

How to list all literals of only selected OWL datatype (here :VisibilityKind) in SPARQL? The OWL datatype definition of VisibilityKind is:
DatatypeDefinition( :VisibilityKind
DataOneOf( "public"^^xsd:string "private"^^xsd:string "package"^^xsd:string )
)
With SPARQL, you can get all the literals that have a certain datatype IRI. For instance, assuming you have data with literals such as "public"^^:VisibilityKind, you can write:
SELECT ?literal WHERE {
?s ?p ?literal .
FILTER (datatype(?literal) = :VisibilityKind)
}
But this is certainly not what you want because you probably do not have literals with this datatype IRI. What you would probably like is something that could roughly be formulated as:
SELECT ?literal WHERE {
?s ?p ?literal .
FILTER (isInDatatype(?literal,:VisibilityKind))
}
where isInDatatype checks if the value associated with ?o belongs to the value space of :VisibilityKind. However, such function does not exist in standard SPARQL 1.1. Moreover, even if it existed, it would require the query engine to understand the semantics of OWL 2 and do reasoning. So you would need a SPARQL engine that implements the OWL 2 DL entailment regime and you'd have to hack a bit the query to get what you want:
SELECT ?literal WHERE {
?s ?p ?literal .
[] a :VisibilityKind;
:owl:sameAs ?literal;
FILTER (isLiteral(?literal))
}
If you want to find implementations of SPARQL 1.1 Entailment regimes, you could take a look at the implementation reports collected by the SPARQL working group.
To answer directly, you need to search for all values in the owl:oneOf list:
SELECT ?resource ?value
WHERE {
:VisibilityKind owl:equivalentClass / owl:oneOf / ( rdf:rest*/rdf:first ) ?value .
?resource ?prop ?value .
}
However, there is a caveat - other properties could have the value "public" "private" or "package". To ensure that you're getting only the values of :VisibilityKind, define that datatype as a range of properties that use the datatype:
{ :someProp rdfs:domain :VisibilityKind }
Then use the following query to get all properties that define :VisibilityKind in the range:
SELECT ?resource ?value
WHERE {
?prop rdfs:range :VisibilityKind .
?resource ?prop ?value
}

Japanese Virtuoso SPARQL EndPoint times out very quickly

I am trying to use the following query on Japanese dbpedia SPARQL Endpoint
select ?s (group_concat(?album_ja ; separator = "|") AS ?name_album_ja_csv) where{
values ?sType { dbpedia-owl:Song
dbpedia-owl:Single
} .
?s a ?sType .
?s (dbpedia-owl:album|^dbpedia-owl:album)* ?albums;rdfs:label ?album_ja
}group by ?s order by ?s offset 0 limit 10
but I get this error Virtuoso 42000 Error The estimated execution time 1005 (sec) exceeds the limit of 400 (sec). Almost any query involving group by has this problem. Is this a server problem? Is my query inefficient? How can I get around it?
I'm not sure exactly what you're trying to do, but since ?o isn't used in the results, you can get rid of it. Even after you do that, though, you'll still have the same problem. You need to change the property path somehow. I don't think that you actually need arbitrary length paths in both directions. You could use ? instead of * to say "path of length 0 or 1" and thus:
select ?s
(group_concat(?album_ja ; separator = "|") AS ?name_album_ja_csv)
where {
values ?sType { dbpedia-owl:Song dbpedia-owl:Single }
?s a ?sType .
?s (dbpedia-owl:album|^dbpedia-owl:album)? ?album_ja
}
group by ?s
limit 100
SPARQL results
Note that a path of length 0 is going to be a link to itself, so one value of ?album_ja is always going to be the same as ?s. Is this really what you want?

SPARQL queries for Pizza ontology

i have to use ROWLKit
http://www.dis.uniroma1.it/quonto/?q=node/30
(1) can anybody suggest two sparql queries for the Pizza.owl ?
(2) is this query valid ?
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT *
WHERE { ?p rdf:type pizza:Pizza;
pizza:hasTopping ?t.
?t rdf:type pizza:TomatoTopping }
(3) if it is a valid query then: is the response an empty result?
SELECT DISTINCT *
WHERE {
?NombrePizza ?Relacion pizza:MushroomTopping .
?Relacion owl:inverseOf pizza:isToppingOf .
OPTIONAL {
?NombrePizza2 ?Relacion2 pizza:HamTopping .
?Relacion2 owl:inverseOf pizza:isToppingOf .
}
FILTER(?NombrePizza2 = ?NombrePizza)
}
(1) can anybody suggest two sparql queries for the Pizza.owl ?
Here are two examples:
SELECT * WHERE { ?s ?p ?o }
and:
SELECT ?class WHERE { ?class a owl:Class }
(2) is this query valid ?
Yes.
(3) if it is a valid query then: is the response an empty result?
I assume that you mean "if I query the RDF document that serialises the pizza ontology, is the response an empty result?". The answer is yes.
(2) appears to be a valid query
I don't understand part (3) of your question. (2) cannot be compared to a boolean since it returns a Result Set, if you want a boolean result then you need to use an ASK query. If an ASK query returns true then it means that there are solutions to the query in the data you are querying so it would not be an empty result.