Is it possible to add customized rules to infer new relations in AllegroGraph? - semantic-web

In my data there are two triples:
entity1 doA entity2 .
entity2 doB entity3 .
I am looking for a way to infer the following triple and have it back in the outcome of my SPARQL query (e.g., select ?a ?c {?a doC ?c)) :
entity1 doC entity3 .
Basically, I want to say:
IF (?a doA ?b) and (?b doB ?c) THEN (?a doC ?c)
Note, I am looking for a solution that can be completely implemented using the AGWebView interface.

If AllegroGraph supports SPARQL 1.1m then you could try:
INSERT {?a <doC> ?c}
WHERE {
?a <doA> ?b .
?b <doB> ?c .
}
That inserts into the default graph, however that is defined. To direct to a specific graph, then add a GRAPH statement to the insert:
INSERT { GRAPH <graph-uri> {
?a <doC> ?c}
}
...

Related

SPARQL "Different Than"

Background
I have a graph database where nodes represent individual people and the edges are predicates hasMother and hasFather. I'd like to identify all of the nodes that share a mother, but don't share the same father.
Attempt
There are a few SPARQL terms that immediately pop into my mind when thinking about this problem. Namely Filter, != and NOT EXISTS.
I first set out and defined some sets...
Let M be the set of all winiks that have mother id, mother_id.
Let F be the set of all winiks that have father id, father_id.
The following query returns a qraph of M ∩ F.
PREFIX ex: <https://ex.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
CONSTRUCT WHERE {
?winik_id ex:hasMother ?mother_id.
?winik_id ex:hasFather ?father_id.
}
My hiccup is writing the exclusion part (its actually hard for me to verbalize the exact part I'm stuck on)-specifically how to deal with how general the query is (across all identifiers).
This should return pairs of people that have the same mother but different father.
SELECT ?p1 ?p2 WHERE {
?p1 ex:hasMother ?m .
?p1 ex:hasFather ?f .
?p2 ex:hasMother ?m .
?p2 ex:hasFather ?f2 .
FILTER (?f != ?f2)

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
}

how to search for strings with different language code

I need to search items in two graphs with the same string, but different language codes ("xx"#en and "xx"#eng - from wordnet). obviously
"xx"#en is not equal to "xx"#eng.
it can be done with (prefix nlp suitably defined):
select * where {
?a nlp:lemma ?as .
?b rdfs:label ?bs .
filter (str(?as)=str(?bs)) .
# more code using ?a and ?b
}
However, this query takes too long and is wasteful. It should be possible to do something like:
?a nlp:lemma ?s .
?b rdfs:label ?s .
but i cannot see how - short of manually change all #eng in the wordnet triples to #en - which i would rather not do.
any solution?
thank you!
You could cut you search space down by filtering only for en and eng, but the only way to compare the string portion of a language-labeled string is to convert them to a string.
I.e. the following could be more efficient if there are language-labeled strings other than en and eng:
select * where {
?a nlp:lemma ?as .
?b rdfs:label ?bs .
filter (lang(?as) = "en" || lang(?as) = "eng")
filter (str(?as)=str(?bs)) .
# more code using ?a and ?b
}

Finding linked topics in DBPedia using SPARQL

Consider we have these two topics:
http://dbpedia.org/page/Jason_Furman
and
http://dbpedia.org/page/London
For the first topic, Jason Furman, we can see on the property almaMater that he went to:
http://dbpedia.org/page/London_School_of_Economics
And looking at London School of Economics we see that it has London as value of the city property.
So my question is:
If we have two topics, A and B, how can we find property values of A that has topic B in any of it's properties?
select distinct ?a ?p ?y ?q ?b where {
values ?a { dbpedia:Jason_Furman }
values ?b { dbpedia:London }
?a ?p ?y .
?y ?q ?b .
}
SPARQL results
(Note that there are two results because there are two such properties: dbpprop:almaMater and dbpedia-owl:almaMater.)

retrieving most specific classes of instances

Is it possible to have a definition of an resource (from DBpedia) with a SPARQL query? I want to have something like the TBox and ABox that are shown in (Conceptual) Clustering methods for
the Semantic Web: issues and applications (slides 10–11). For example, for DBpedia resource Stephen King, I would like to have:
Stephen_King : Person &sqcap; Writer &sqcap; Male &sqcap; … (most specific classes)
You can use a query like the following to ask for the classes of which Stephen King is an instance which have no subclasses of which Stephen King is also an instance. This seems to align well with the idea of “most specific classes.” However, since (as far as I know) there's no reasoner attached to the DBpedia SPARQL endpoint, there may be subclass relationships that could be inferred but which aren't explicitly present in the data.
select distinct ?type where {
dbr:Stephen_King a ?type .
filter not exists {
?subtype ^a dbr:Stephen_King ;
rdfs:subClassOf ?type .
}
}
SPARQL results
Actually, since every class is an rdfs:subClassOf itself, you might want to add another line to that query to exclude the case where ?subtype and ?type are the same:
select distinct ?type where {
dbr:Stephen_King a ?type .
filter not exists {
?subtype ^a dbr:Stephen_King ;
rdfs:subClassOf ?type .
filter ( ?subtype != ?type )
}
}
SPARQL results
If you actually want a result string like the one shown in those slides, you could use values to bind a variable to dbr:Stephen_King, and then use some grouping and string concatenation to get something nicer looking (sort of):
select
(concat( ?person, " =\n", group_concat(?type; separator=" AND\n")) as ?sentence)
where {
values ?person { dbr:Stephen_King }
?type ^a ?person .
filter not exists {
?subtype ^a ?person ;
rdfs:subClassOf ?type .
filter ( ?subtype != ?type )
}
}
group by ?person
SPARQL results
http://dbpedia.org/resource/Stephen_King =
http://dbpedia.org/class/yago/AuthorsOfBooksAboutWritingFiction AND
http://dbpedia.org/ontology/Writer AND
http://schema.org/Person AND
http://xmlns.com/foaf/0.1/Person AND
http://dbpedia.org/class/yago/AmericanSchoolteachers AND
http://dbpedia.org/class/yago/LivingPeople AND
http://dbpedia.org/class/yago/PeopleFromBangor,Maine AND
http://dbpedia.org/class/yago/PeopleFromPortland,Maine AND
http://dbpedia.org/class/yago/PeopleFromSarasota,Florida AND
http://dbpedia.org/class/yago/PeopleSelf-identifyingAsAlcoholics AND
http://umbel.org/umbel/rc/Artist AND
http://umbel.org/umbel/rc/Writer AND
http://dbpedia.org/class/yago/20th-centuryNovelists AND
http://dbpedia.org/class/yago/21st-centuryNovelists AND
http://dbpedia.org/class/yago/AmericanHorrorWriters AND
http://dbpedia.org/class/yago/AmericanNovelists AND
http://dbpedia.org/class/yago/AmericanShortStoryWriters AND
http://dbpedia.org/class/yago/CthulhuMythosWriters AND
http://dbpedia.org/class/yago/HorrorWriters AND
http://dbpedia.org/class/yago/WritersFromMaine AND
http://dbpedia.org/class/yago/PeopleFromDurham,Maine AND
http://dbpedia.org/class/yago/PeopleFromLisbon,Maine AND
http://dbpedia.org/class/yago/PostmodernWriters