I have some data on students' test scores as follows:
#prefix : <http://example.org/#> .
:Bob a :Student;
:tookTest :Test0,:Test1,:Test2 ,:Test3 .
:Test0 :score 90 .
:Test1 :score 81 .
:Test2 :score 62 .
:Test3 :score 32 .
The rules for grading based on scores are as follows:
[90-100] --> A
[75-90) --> B
[60-75) --> C
[0-60) --> D
So I used the following SPARQL query to get the grade of the student's Test:
prefix : <http://example.org/#>
select ?student ?test ?grade
WHERE {
?student :tookTest ?test .
?test :score ?score .
BIND ( IF ( ?score >= 90 , "A", IF ( ?score>=75, "B", IF ( ?score>=60, "C", "D" ) ) ) AS ?grade )
}
And I got the right result as such:
student, test, grade
http://example.org/#Bob, http://example.org/#Test3, D
http://example.org/#Bob, http://example.org/#Test2, C
http://example.org/#Bob, http://example.org/#Test1, B
http://example.org/#Bob, http://example.org/#Test0, A
Now my question is how to store this rule data in RDF (along with the score) rather than in SPARQL, so that the rule data can be defined and modified by the user, The server side reads the rule data to generate the appropriate SPARQL query.
My initial thoughts are as follows:
:range1 a :range ;
:grade "A" ;
:lowerLimit 90 ;
:upperLimit 100.
:range2 a :range ;
:grade "B" ;
:lowerLimit 75 ;
:upperLimit 89 .
:range3 a :range ;
:grade "C" ;
:lowerLimit 60 ;
:upperLimit 74 .
:range4 a :range ;
:grade "D" ;
:lowerLimit 0 ;
:upperLimit 59 .
:rule1 :hasRange :range1,:range2, :range3, :range4 .
But now how do I make the SPARQL work? And how to consider inclusive and exclusive of boundary values?
Related
I want to store some facts in pure RDF data (ttl) like :
:Person :hasGender :Male, :Female ;
:drink :Liquor, :softDrinks .
if (:someone :hasGender :Male) then
:someone :drink :Liquor ;
else
:someone :drink :softDrinks .
:Susan a :Person ;
:hasGender :Female .
and then read this rdf data (ttl) by Sparql or other app (rdf4j or rdflib) , and get :
:Susan :drink :softDrinks .
I want to use pure RDF as much as possible to store data, rather than OWL, N3, RDF-star or SHACL, but I can reconstruct a new N3 or shacl file from these rdf data, and then get infered result.
I wonder if this is possible in RDF, an how can I modify this RDF data? Thanks.
I got It done. here is data.ttl :
#prefix : <http://example.org/#> .
:Person :drink :someLiquid .
:someLiquid :hasRule :rule1, :rule2.
:rule1 :forGender :Male ;
:beverage :liquor .
:rule2 :forGender :Female ;
:beverage :softDrinks .
:Susan a :Person ;
:hasGender :Female .
:John a :Person ;
:hasGender :Male .
here is the sparql query:
PREFIX : <http://example.org/#>
SELECT ?person ?gender ?beverage
WHERE {
:Person :drink :someLiquid .
:someLiquid :hasRule ?rule .
?rule :forGender ?forGender ;
:beverage ?beverage .
?person a :Person;
:hasGender ?gender .
FILTER ( ?gender = ?forGender )
}
and I get the result:
1 person, gender, beverage
2 :Susan, :Female, :softDrinks
3 :John, :Male, :liquor
Given the following schema, "driver-passenger" lineages can be easily seen:
tp:trip a owl:Class ;
rdfs:label "trip"#en ;
rdfs:comment "an 'asymmetric encounter' where someone is driving another person."#en .
tp:driver a owl:ObjectProperty ;
rdfs:label "driver"#en ;
rdfs:comment "has keys."#en ;
rdfs:domain tp:trip ;
rdfs:range tp:person .
tp:passenger a owl:ObjectProperty ;
rdfs:label "passenger"#en ;
rdfs:comment "has drinks."#en ;
rdfs:domain tp:trip ;
rdfs:range tp:person .
Consider the following data:
<alice> a tp:person .
<grace> a tp:person .
<tim> a tp:person .
<ruth> a tp:person .
<trip1> a tp:trip ;
tp:participants <alice> , <grace> ;
tp:driver <alice> ;
tp:passenger <grace> .
<trip2> a tp:trip ;
tp:participants <alice> , <tim> ;
tp:driver <alice> ;
tp:passenger <tim> .
<trip3> a tp:trip ;
tp:participants <tim> , <grace> ;
tp:driver <tim> ;
tp:passenger <grace> .
<trip4> a tp:trip ;
tp:participants <grace> , <ruth> ;
tp:driver <grace> ;
tp:passenger <ruth> .
<trip5> a tp:trip ;
tp:participants <grace> , <tim> ;
tp:driver <grace> ;
tp:passenger <tim> .
Now let a "driver-passenger descendent" be any tp:passenger at the end of a trip sequence where the tp:passenger of one trip is the tp:driver of the next trip
Ex. <ruth> is a descendent of <alice> according to the following sequence of trips:
<trip2> -> <trip3> -> <trip4>.
Question:
How to get the (ancestor,descendent) pairs of all driver-passenger lineages?
Attempt 1:
I initially tried the following CONSTRUCT subquery to define an object property: tp:drove, which can be easily used in a property path. However, this did not work on my actual data:
SELECT ?originalDriver ?passengerDescendent
WHERE {
?originalDriver tp:drove+ ?passengerDescendent .
{
CONSTRUCT { ?d tp:drove ?p . }
WHERE { ?t a tp:trip .
?t tp:driver ?d .
?t tp:passenger ?p .}
}
}
Attempt 2:
I tried to create property path which expresses an ancestor as the driver of a passenger, but I don't think I've properly understood how this is supposed to work:
(tp:driver/^tp:passenger)+
Regarding MWE: Is there some kind of RDF sandbox that would allow me to create an MWE by defining a simple ontology like tp above, along with some sample data? The following "playgrounds" are available but none of them seem to support defining a toy ontology: SPARQL Playground, SPARQL Explorer.
Notes on related content:
This question is directly related to a previous question, but no longer requires saving the paths themselves, a feature not directly supported by SPARQL 1.1.
This answer by Joshua Taylor seems relevant, but doesn't address the identification of specific types of paths, such as the lineages defined above.
This one seems to do the trick:
select ?driver ?passenger where {
?driver (^tp:driver/tp:passenger)+ ?passenger .
filter( ?driver != ?passenger)
}
The filter condition can be removed if you want to also see relationships that lead back to the same person.
I have to write a SPARQL query that returns the length of the path between two nodes (:persA and :persD) that are connected by these relationships:
#prefix : <http://www.example.org/> .
:persA :knows :knowRelation1 .
:knowRelation1 :hasPerson :persB .
:persB :knows :knowRelation2 .
:knowRelation2 :hasPerson :persC .
:persC :knows :knowRelation3 .
:knowRelation3 :hasPerson :persD .
I tried with this query:
PREFIX : <http://www.example.org/>
SELECT (COUNT(?mid) AS ?length)
WHERE
{
:persA (:knows | :hasPerson)* ?mid .
?mid (:knows | :hasPerson)+ :persD .
}
the result seems to be a infinite loop.
Any advice/examples of how this can be done?
After fixing some syntax errors in your initial post, the provided triples and query work for me in GraphDB Free 8.2 and BlazeGraph 2.1.1. I have since applied these edits to your post itself.
added a trailing / to your definition of the empty prefix
added a trailing . to your prefix definition line (required if you want to start with a #)
fixed the spelling of length (OK, that's just a cosmetic fix)
.
#prefix : <http://www.example.org/> .
:persA :knows :knowRelation1 .
:knowRelation1 :hasPerson :persB .
:persB :knows :knowRelation2 .
:knowRelation2 :hasPerson :persC .
:persC :knows :knowRelation3 .
:knowRelation3 :hasPerson :persD .
.
PREFIX : <http://www.example.org/>
SELECT (COUNT(?mid) AS ?length)
WHERE
{ :persA (:knows|:hasPerson)* ?mid .
?mid (:knows|:hasPerson)+ :persD
}
result:
length
"6"^^xsd:integer
I have such a query:
CONSTRUCT {
?p a :IndContainer .
?p :contains ?ind .
} WHERE{
:ClassContainer_1 :contains ?class .
?ind a ?class .
BIND (IRI(...) AS ?p) .
}
An individual ClassContainer_1 relates to some classes. I get this classes and try to find individuals for these classes. Then I try to create an IndContainer that should store found individuals (dots are used only for simplification). So, I want to:
Create individual of IndContainer only when individuals for all bindings of ?class have been found;
Create individuals of IndContainer for all possible sets of individuals from ?ind (i.e. when some of ?class has a nuber of individuals).
Is it possible to create such a SPARQL query? Or it is necessary to use some rule engine?
EDIT (add illustration):
Positive example. Have:
test:ClassContainer_1
rdf:type test:ClassContainer ;
test:contains test:Type1 ;
test:contains test:Type2 ;
.
test:Type1_1
rdf:type test:Type1 ;
.
test:Type1_2
rdf:type test:Type1 ;
.
test:Type2_1
rdf:type test:Type2 ;
.
Want to receive:
test:IndContainer_1
rdf:type test:IndContainer ;
test:contains test:Type1_1 ;
test:contains test:Type2_1 ;
.
test:IndContainer_2
rdf:type test:IndContainer ;
test:contains test:Type1_2 ;
test:contains test:Type2_1 ;
.
Negative example: the same as positive except that there is no individuals of class Type2 and so no individuals of IndContainer should be generated.
EDIT 2 (problem essence):
We may look at this problem from the perspective of combination composing. We have two positions (in my example) in each combination. The number of positions is determined by the number of classes each ClassContainer depends on. Each position must be filled in with one individual of a class that correspond to that position. So in my example first position must be filled with one individual of Type1 class, the second - with Type2 class (but the order does not matter). We have two individuals for the first class and one individual for the second class. To get the number of combinations we may use the rule of product from combinatorics - 2*1 = 2, i.e. {Type1_1,Type2_1} - is the first combination and {Type1_2,Type2_1} - is the second combination. For each combination it is necessary to generate IndContainer individual.
If I understand your question correctly, you want a "container" for each class that is contained in a "class container" that contains the individuals that belong to that class. That's not too hard to do, as long as you can construct the IRI of the container from the IRI of the class. Here's some sample data with two classes, A and B, and a few instances (some of just A, some of just B, and some of A and B):
#prefix : <urn:ex:> .
:container a :ClassContainer ;
:contains :A, :B .
:w a :A . # an :A
:x a :A . # another :A
:y a :B . # a :B
:z a :A, :B . # both an :A and a :B
You query is already pretty close. Here's one that works, along with its result:
prefix : <urn:ex:>
construct {
?indContainer a :IndContainer ;
:contains ?ind .
}
where {
:container a :ClassContainer ;
:contains ?class .
?ind a ?class .
bind(IRI(concat(str(?class),"-container")) as ?indContainer)
}
#prefix : <urn:ex:> .
:B-container a :IndContainer ;
:contains :y , :z .
:A-container a :IndContainer ;
:contains :w , :x , :z .
I have two tables, Table1 has historical transactions and Table2 has a field that stores the balance of those transactions for each account.
I need a SQL query that will return the transactions in Table1 for a specific account, starting from the latest transaction that sum up to the current balance in Table2.
Any help would be greatly appreciated.
Table1
UniqID . AcctNum . TranType . TranDate . TranAmt
1 . . . . . 1001123 . . . . A . . . . . 11/1/13 . . . . 100
2 . . . . . 1010877 . . . . B . . . . . 12/2/13 . . . . . 10
7 . . . . . 1010877 . . . . C . . . . . 12/2/13 . . . . . 22
10. . . .. 1001123 . . . . A . . . . . 12/2/14 . . . .-100
11. . . .. 1001123 . . . . B . . . . . 12/6/13 . . . . 145
12. . . .. 1003699 . . . . A . . . . . 12/8/13 . . . . 250
13. . . .. 1001123 . . . . B . . . . . 1/2/14 . . . . . 145
14. . . . .1003699 . . . . C . . . . . 1/4/14 . . . . . 110
15. . . . .1003699 . . . . C . . . . . 1/4/14 . . . . .-110
19. . . . .1003699 . . . . B . . . . . 1/8/14 . . . . . . 25
21. . . . .1001123 . . . . B . . . . . 1/2/14 . . . . . . 80
22. . . . .1001123 . . . . B . . . . . 1/8/14 . . . . . . 45
26. . . . .1001123 . . . . A . . . . . 1/21/14 . . . .-145
Table2
AcctNum . TranBal
1001123 . . . . 270
1003699 . . . . 275
1010877 . . . . . 32
Expected Result for account 1001123
UniqID . AcctNum . TranType . TranDate . TranAmt
11. . . .. 1001123 . . . . B . . . . . 12/6/13 . . . . 145
13. . . .. 1001123 . . . . B . . . . . 1/2/14 . . . . . 145
21. . . . .1001123 . . . . B . . . . . 1/2/14 . . . . . . 80
22. . . . .1001123 . . . . B . . . . . 1/8/14 . . . . . . 45
26. . . . .1001123 . . . . A . . . . . 1/21/14 . . . .-145
OK I think i got it now. First you will need a running total of TranAmt per AcctNum. In SQL Server 2012 this can be conveniently done by OVER PARTITION:
SELECT t.AcctNum, t.TranAmt,t.TranDate, SUM(t.TranAmt) OVER(PARTITION BY t.AcctNum ORDER BY t.TranDate,t.UniqID)
FROM Transactions t
I used UniqID because TranDate is not unique per AcctNum. In SQL Server 2008 you can't use OVER PARTITION because the ORDER BY clause was only added in SQL Server 2012 so we must use a JOIN here:
SELECT t1.AcctNum, t1.TranDate, t1.TranAmt, SUM(t2.TranAmt) running_total
FROM Transactions t1,
Transactions t2
WHERE t1.TranDate >= t2.TranDate AND t1.AcctNum=t2.AcctNum AND t1.UniqID>=t2.UniqID
GROUP BY t1.AcctNum, t1.TranDate, t1.TranAmt
ORDER BY t1.AcctNum, t1.TranDate
Now we need only the rows where the running_total is 0 because they give us the threshold date before which the transaction data can be discarded:
SELECT t1.AcctNum, t1.TranDate, t1.TranAmt, SUM(t2.TranAmt) running_total
FROM Transactions t1,
Transactions t2
WHERE t1.TranDate >= t2.TranDate AND t1.AcctNum=t2.AcctNum AND t1.UniqID>=t2.UniqID
GROUP BY t1.AcctNum, t1.TranDate, t1.TranAmt
HAVING SUM(t2.TranAmt)=0
ORDER BY t1.AcctNum, t1.TranDate
Finally we have to link those Threshold dates to the Transaction table again. I use a left join and to add the row with the threshold date to any row in Transaction which is equal or before that date (per AcctNum). Then we can discard those and keep only the rows which have date newer than threshold date.
SELECT *
FROM Transactions t
LEFT JOIN (SELECT t1.AcctNum, t1.TranDate ThresholdDate, t1.TranAmt, SUM(t2.TranAmt) running_total
FROM Transactions t1,
Transactions t2
WHERE t1.TranDate >= t2.TranDate AND t1.AcctNum=t2.AcctNum AND t1.UniqID>=t2.UniqID
GROUP BY t1.AcctNum, t1.TranDate, t1.TranAmt
HAVING SUM(t2.TranAmt)=0) td ON t.AcctNum=td.AcctNum AND t.TranDate<=td.ThresholdDate
WHERE td.AcctNum is null
ORDER BY t.AcctNum, t.TranDate