SPARQL INSERT query in Protege - insert

I'm trying to map my Data Properties in Protege to owl:Class with Sparql but it doesn't work. If any one has an example, please don't hesitate to give me an answer.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX myOnto: <http://imis/SNI/9.4.3#>
INSERT DATA
{
?s rdf:type owl:Class
}
WHERE { ?s ?o owl:DatatypeProperty}
When i was running it i got this message . Please help me to resolve this problem.
Error 61 Logged at Wed Oct 15 23:11:26 CEST 2014
SparqlReasonerException: org.openrdf.query.MalformedQueryException: Encountered " "insert" "INSERT "" at line 7, column 1.
Was expecting one of:
"base" ...
"prefix" ...
"select" ...
"construct" ...
"describe" ...
"ask" ...

Related

SPARQL - Filter all instances that only contain a condition

I have recipes with ingredients that have a category.
#prefix schema: <http://schema.org/> .
#prefix ex: <http://example.org> .
ex:recipe1 a schema:Recipe ;
schema:recipeIngredient ex:ingredient1 , ex:ingredient3 .
ex:recipe2 a schema:Recipe ;
schema:recipeIngredient ex:ingredient1, ex:ingredient2 .
ex:ingredient1 ex:category ex:plant .
ex:ingredient2 ex:category ex:spice .
ex:ingredient3 ex:category ex:meat .
ex:ingredient4 ex:category ex:fish .
I want to filter all recipes that only contain ingredients from the category ex:plants or ex:spice. I tried:
SELECT ?recipes
WHERE {
?recipes a schema:Recipe;
schema:recipeIngredient ?ingredient.
?ingredient ex:category ?category.
FILTER(?category NOT IN (ex:meat && ex:fish))
}
This returns both recipes, I guess because ex:recipe1 also includes a ex:plant. However, my desired output is only ex:recipe2 because it does not contain any ex:meat or ex:fish. If any ex:meat or ex:fish is included in the recipes, I want it to be excluded from the result set.

Stardog raises ConcurrentModificationException upon reasoned query on graph with SWRL Rules

When trying to execute a query with reasoning in a model, I get this error: 000012: com.complexible.stardog.plan.eval.operator.OperatorException: Uncaught error during query evaluation: ConcurrentModificationException
Ontology model has 2 SWRL rules and is consistent, as it runs fine in Protege (with built-in pellet reasoner). Model has been imported into a stardog rdf model and the query
SELECT ?tse
FROM <urn:test_graph>
{
?tse a :TaperedShaftEnd
}
fails with the above error message.
By deleting the SWRL rules the query runs fine, but of course the model does no longer represent the desired domain. By translating the SWRL rules into stardog native rules, the error appears again.
Reasoning level is set to DL, as SL would imply too high processing times for the target application.
Changing the sameAs flag in the reasoner options does not change the behavior.
Setting the reasoning level to SL, as advised generally in Stardog documentation would solve the problem on this simplified model, but as said would result impractical for the use on the final target application.
May you help me in identifying the root cause and the possible solution?
Did anyone experienced the same issue when dealing with SWRL rules?
Here below the concerned model in turtle format, to easily replicate the issue on a test graph.
Heartful thanks to anyone who may help me with this.
#prefix : <http://api.stardog.com/> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix stardog: <tag:stardog:api:> .
#prefix test: <urn:test_graph#> .
<http://swrl.stanford.edu/ontologies/3.3/swrla.owl#isRuleEnabled> a owl:AnnotationProperty .
<urn:test> a owl:Ontology .
:hasTorqueTransmissionConnectionWith a owl:ObjectProperty , owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:SymmetricProperty , owl:IrreflexiveProperty .
:CouplingConnection a owl:Class ;
owl:disjointWith :ShaftEnd .
:ShaftEnd a owl:Class ;
owl:disjointWith :ShaftEnd .
:LeftCouplingConnection a owl:Class ;
rdfs:subClassOf :CouplingConnection ;
owl:disjointWith :RightCouplingConnection .
:RightCouplingConnection a owl:Class ;
rdfs:subClassOf :CouplingConnection .
:LeftShaftEnd a owl:Class ;
rdfs:subClassOf :ShaftEnd ;
owl:disjointWith :RightShaftEnd .
:RightShaftEnd a owl:Class ;
rdfs:subClassOf :ShaftEnd .
:TaperedCouplingConnection a owl:Class , <tag:stardog:api:rule:SPARQLRule> ;
rdfs:subClassOf :CouplingConnection ;
<tag:stardog:api:rule:content> """IF {
?tse a :TaperedShaftEnd .
?tse :hasTorqueTransmissionConnectionWith ?cc .
?cc a :CouplingConnection .
}
THEN {
?cc a :TaperedCouplingConnection .
}
""" .
:TaperedShaftEnd a owl:Class , <tag:stardog:api:rule:SPARQLRule> ;
rdfs:subClassOf :ShaftEnd ;
<tag:stardog:api:rule:content> """IF {
?tcc a :TaperedCouplingConnection .
?tcc :hasTorqueTransmissionConnectionWith ?se .
?se a :ShaftEnd .
}
THEN {
?se a :TaperedShaftEnd .
}
""" .
:lcc1 a :LeftCouplingConnection , owl:NamedIndividual .
:lse2 a :LeftShaftEnd , owl:NamedIndividual .
:rcc2 a :RightCouplingConnection , :TaperedCouplingConnection , owl:NamedIndividual ;
:hasTorqueTransmissionConnectionWith :lse2 .
:rse1 a :RightShaftEnd , owl:NamedIndividual ;
:hasTorqueTransmissionConnectionWith :lcc1 .

Add Individuals to owl file with SPARQL query

i want to insert some individuals into my ontology owl file (OWL/XML) with SPARQL using Protege for example :
person1 is an instance of class person and has these object property assertions : hasname Alex, hasage 26
Here is my code
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX my_onto: <http://www.semanticweb.org/hp/ontologies/2020/5/personOnto.owl#>
INSERT DATA
{
my_onto:person1 rdf:type my_onto:person .
rdf:type owl:NamedIndividual .
my_onto:hasname Alex.
my_onto:hasage 26.
}
But, i had this error :
Caused by: org.openrdf.query.parser.sparql.ast.ParseException: Encountered " "insert" "INSERT "" at line 7, column 1.
Was expecting one of:
"base" ...
"prefix" ...
"select" ...
"construct" ...
"describe" ...
"ask" ...
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.generateParseException(SyntaxTreeBuilder.java:8943) ~[na:na]
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.jj_consume_token(SyntaxTreeBuilder.java:8813) ~[na:na]
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.Query(SyntaxTreeBuilder.java:313) ~[na:na]
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.QueryContainer(SyntaxTreeBuilder.java:188) ~[na:na]
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.parseQuery(SyntaxTreeBuilder.java:31) ~[na:na]
at org.openrdf.query.parser.sparql.SPARQLParser.parseQuery(SPARQLParser.java:153) ~[na:na]
... 42 common frames omitted

can I do this in AllegroGraph prolog?

I have an RDF file and I need to extract some information from it in a single line.
Now, I'm using AllegroGraph with Prolog query engin :
(select (?result)
(q ?a !rdfs:label ?alabel)
(q ?b !rdfs:label ?blabel)
(lisp ?result (string+ ?alabel " AND " ?blabel)))
to get the results in a single line:
"{a1} AND {b1}"
"{a1} AND {b2}"
"{a2} AND {b1}"
"{a2} AND {b2}"
Now, I need to group all the rows of ?result in a single line with the string "OR". so i get:
"{a1} AND {b1} OR {a1} AND {b2} OR {a2} AND {b1} OR {a2} AND {b2}"
Is there any function in prolog to do this?
The fact that you've only got a* on the left and b* on the right means that you've got some other selection condition than just having a label. Given data like this:
#prefix : <http://example.org/>.
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
:a1 a :ClassA ; rdfs:label "a1" .
:a2 a :ClassA ; rdfs:label "a2" .
:b1 a :ClassB ; rdfs:label "b1" .
:b2 a :ClassB ; rdfs:label "b2" .
you can select ?a and ?b by their classes (:ClassA and :ClassB), and then extract their labels as well, with a pattern like:
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
Then you can get the {alabel} AND {blabel} with a bind and a concat:
bind( concat( "{", ?alabel, "} AND {", ?blabel, "}" ) as ?AandB )
Using these, a query like
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?AandB {
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
bind( concat( "{", ?alabel, "} AND {", ?blabel, "}" ) as ?AandB )
}
will get you the kind of results that you can already get:
-------------------
| AandB |
===================
| "{a2} AND {b2}" |
| "{a2} AND {b1}" |
| "{a1} AND {b2}" |
| "{a1} AND {b1}" |
-------------------
The trick now is to use group_concat and an implicit group to combine all these into a string, with a separator of " OR ":
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ( group_concat( ?AandB ; separator=" OR ") as ?string ) where {
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
bind( concat( "{", ?alabel, "} AND {", ?blabel, "}" ) as ?AandB )
}
to get a result:
----------------------------------------------------------------------
| string |
======================================================================
| "{a2} AND {b2} OR {a2} AND {b1} OR {a1} AND {b2} OR {a1} AND {b1}" |
----------------------------------------------------------------------
If you like, you can even get rid of the bind, and just put the concat expression right into the group_concat. You might find that easier to read (less jumping around) or harder to read (big one-liner), but at least it's good to have options:
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ( group_concat( concat( "{",?alabel,"} AND {",?blabel,"}" ) ; separator=" OR ") as ?string ) where {
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
}
There are some other examples of group_concat floating around on StackOverflow that might be useful to you as well:
Aggregating results from SPARQL query
RDF list subjects with their objects in a single line (this is actually another question that you asked, and that I answered, and in that answer, I also pointed out the previous question)
obtain the matrix in protege (this does some pretty fancy string manipulation)
SPARQL query to get all parent of a node

RDflib results printing error on windows7

I use SPARQLWrapper to create SPARQL queries, but I don't know how to debug the following error message:
Warning (from warnings module):
File "D:\Python27\lib\site-packages\sparqlwrapper-1.5.2-py2.7.egg\SPARQLWrapper\Wrapper.py", line 550
RuntimeWarning: unknown response content type, returning raw response...
Traceback (most recent call last):
File "D:\Python27\testwrapper.py", line 31, in <module>
if (len(results["results"]["bindings"]) == 0):
AttributeError: addinfourl instance has no attribute '__getitem__'
This is my code:
from SPARQLWrapper import SPARQLWrapper,JSON
sparql = SPARQLWrapper('http://thedatahub.org/dataset/semanticquran');
queryString = """
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX olia-ar: <http://purl.org/olia/arabic_khoja.owl#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lexvo: <http://lexvo.org/id/iso639-3/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX gold: <http://purl.org/linguistics/gold/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX qvoc: <http://www.nlp2rdf.org/quranvocab#>
SELECT DISTINCT ?wordText ?pos
WHERE
{ ?wordPart rdf:type qvoc:LexicalItem .
?wordPart gold:Root "smw" .
?wordPart dcterms:isPartOf ?word .
?wordPart gold:PartOfSpeechProperty ?pos .
?word rdf:type qvoc:Word .
?word skos:prefLabel ?wordText
}
"""
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
if (len(results["results"]["bindings"]) == 0):
print "No results found."
else:
for result in results["results"]["bindings"]:
print result["wordText"]["value"]
Any Help?
The return format is not JSON, so the setReturnFormat call is not working as expected:
Encode the return value depending on the return format:
in the case of XML, a DOM top element is returned;
in the case of JSON, a simplejson conversion will return a dictionary;
in the case of RDF/XML, the value is converted via RDFLib into a Graph instance.
In all other cases the input simply returned.

Resources