SPARQL Answer Lab
Unit 5 of 622 min

Change One Fact

Both answers say CountryZ. What could make them disagree?

Start here if you can follow a shared name and read a result column. Review the join or review distinct results.

Sections in this chapter
  1. Start with two identical answers
  2. Follow Pia to her team’s room
  3. Trace the extra match
  4. Change one permission
  5. Plan the university experiment
  6. Record a conclusion someone else can check
  7. Practice and check your reasoning

Start with two identical answers

What both queries report on the starting graph
QueryCountries displayed
ACountryZ
BCountryZ

Suppose you ask which countries have a campus of a university where Alice studies. Two people send you queries. Both run, and both return CountryZ. You can check that CountryZ is a supported answer on this graph by following Alice to her university, a campus city, and then a country.

There is a second question to ask before you trust either query on other data. Which match supplied each answer? A match holds the values used to satisfy all the conditions. It can tell you which university and city contributed a country, even if the answer screen only shows the country.

Think of two people finding the same meeting room from separate calendars. One checks your group’s entry. The other uses an unrelated entry that happens to name the same room. You get to the meeting either way that day. If the other group moves rooms, the second method can send you somewhere else.

That is the distinction we will recreate with a query. First compare the paths that each query can take. Then decide on one change to the facts that would make an unrelated path easier to spot. Check both queries with the same changed data so that the difference tells you something about the queries.

In this chapter, a counterexample is a case that disproves a claim about what always happens. The claim we will test is that the two queries always give the same displayed results. To challenge it, you need one graph on which the results differ.

The starting agreement tells you how the queries behave on that specific graph. It is a useful baseline to keep. A second graph can expose a mismatch by separating values that were previously identical. We will practice this first with rooms, where it is easy to see every relevant fact.

When you compare queries, agree on which part of the output you are comparing. Here the question asks for the set of country values. The order of those values on the screen can change without changing the set. A new country in the set would be a different answer.

Check what the starting answer tells you

It shows that both queries report CountryZ on the starting graph. The displayed answer leaves out which match, campus, and university supplied that country.

Follow Pia to her team’s room

Imagine a workshop that records membership and room access. Pia belongs to Blue. Both Blue and Green can enter a room called Hall. The question is “Which rooms can Pia’s team enter?” You need Pia’s team in order to decide whose access record to use.

  • Piabelongs toBlue
  • Bluecan enterHall
  • Greencan enterHall
Pia’s membership points to Blue. The two access arrows point to the same room.

Read the membership arrow first. Put Blue in an empty slot named ?team. At this point, the slot tells you whose permission record to check. Follow the arrow labelled “can enter” from that team. The recorded access leads you to Hall.

Now consider this pair of patterns. The first line is identical in both versions. In the shared version, the access line starts at the team that the first line found. In the separate version, the access line starts at whatever team fits the access fact. Check the two start positions before looking at the room.

Shared team

ex:Pia ex:belongsTo ?team .
?team ex:canEnter ?room .

Separate teams

ex:Pia ex:belongsTo ?team .
?otherTeam ex:canEnter ?room .

In the shared version, Blue has to fill both occurrences of ?team in each match. The second line succeeds because there is an access fact for Blue. The fact for Green is available in the graph, but cannot use the same value that was found on the membership line.

In the separate version, Blue still fills ?team on the first line. The access line can use Blue or Green in ?otherTeam. Each choice gives a complete match to the two lines as written. The version with separate names therefore allows a combination that the question did not ask for.

A new variable name makes another slot available. It can have the same value as the first slot, as when both use Blue. It can also have a different one, as when the second slot uses Green. The names determine whether the query has to keep those choices tied together.

To check the second version, try reading its two selected facts as a pair: “Pia belongs to Blue. Green can enter Hall.” The graph supports both sentences. A reader still needs the access permission of Pia’s own team, which those two sentences together do not establish.

Trace the extra match

A result table can help you find the error even when the final answers agree. Read each row as one complete assignment of values. Here we show a team value from the membership line, a team value from the access line, and the room at the end of the access arrow.

Complete matches before selecting the room column
VersionMembership teamAccess teamRoom
SharedBlueBlueHall
SeparateBlueBlueHall
SeparateBlueGreenHall

The shared version has one row. Both team positions refer to Blue. The separate version has a row with the same values because its two slots are allowed to agree. It also has a row where the membership team is Blue and the access team is Green. Locate the two different values in that last row.

Check which row uses the wrong team

Look for Blue, Green, Hall in the separate version. Blue comes from Pia’s membership. Green supplies the access to Hall, so the row has used a permission from outside the team named by the question.

Now imagine selecting just the Room column. The shared version gives one copy of Hall. The separate version gives Hall twice because it found two ways to fill the patterns. With SELECT DISTINCT ?room, both versions then display Hall once.

The query keeps the meaning of its match pattern when it selects a column. In this example, selecting the room leaves the team information out of the display. Removing duplicate room values compresses what remains. Neither operation changes which membership and access facts the patterns originally combined.

The last row identifies an opening for a test. Instead of changing a value in every row, look at the particular fact that only the extra row uses. That is Green’s access to Hall. The room title is the part of that fact that later appears in the displayed answer.

When you report the problem, name Green and the access arrow. Saying “the query has duplicates” gives an observation about this starting graph. Pointing to the independent team slot explains why the query pulled in information about a different team even when the resulting room matched Blue’s room.

An extra match can matter even if its visible value is familiar. Looking at the intermediate assignments tells you what the query would still consider eligible if the access records changed. That is the step that makes a deliberate test possible.

Change one permission

Leave Pia in Blue and keep Blue’s permission. Change Green’s permission from Hall to Lab. Pia’s and Blue’s rows stay in the same positions in both drawings. The changed arrow is dashed and its label marks the replacement. Find that row before reading the results.

Before

  • Piabelongs toBlue
  • Bluecan enterHall
  • Greencan enterHall
Green can enter Hall.

After

  • Piabelongs toBlue
  • Bluecan enterHall
  • Greencan enter · changedLab
Green can enter Lab in place of Hall.

Predict what happens to each room list. The shared version still follows Blue to Hall. The separate version continues to use both teams’ access facts. Its two stored room values used to agree. They now differ because Green supplies Lab, so a duplicate removal step keeps both rooms.

Same queries, one replaced fact
GraphShared teamSeparate teams
Green can enter HallHallHall
Green can enter LabHallHall, Lab

Notice that we still have three facts. One describes membership and two describe access. The shared query still builds one row. The separate query still builds two rows. The difference you can see in the final answer comes from the value in one row, rather than from an increase in the number of rows.

To perform the replacement, remove Green’s permission for Hall and put in its permission for Lab. If you kept both permissions, Green would add two possible access facts for the independent variable. You would then be testing a different graph with an extra fact as well as a new room value.

Check what would happen if Green kept Hall

The separate version would have three matches: Blue with Hall, Green with Hall, and Green with Lab. It would still display Hall and Lab after removing duplicate room values. The one-edge replacement keeps the original number of match rows.

You can test other changes too. If you move Blue’s permission to Lab and keep Green in Hall, the shared result itself legitimately changes to Lab. That comparison is harder to explain because it affects the team the question asked for. In our test, Blue’s access stayed fixed.

The Green change gives a useful way to describe the fault. We can point to a room that has no supporting access fact for Pia’s team in the changed graph. The separate version reported it because its access line was allowed to choose Green independently.

Once you have compared both queries on the same changed data, you have a counterexample to the claim of agreement. The changed graph is concrete enough for another reader to reconstruct and check. Put the original and replacement fact next to each other when you describe it.

Plan the university experiment

Return to the country question. It has one more arrow than the room example. Start with Alice, find the university where she studies, find a city with one of its campuses, and read the country of that city. Use the university from the first line when you choose which campus relation to follow.

  • Alicestudies atU1
  • U1has a campus inCityA
  • U1has a campus inCityB
  • U2has a campus inCityC
Before changing a country, identify the university at the start of each campus arrow.

On this starting graph CityA, CityB, and CityC are all in CountryZ. Their country arrows look similar. The campus arrows are what distinguish them for Alice’s question. CityA and CityB have campus edges from U1. CityC has its campus edge from U2.

What each row uses on the starting graph
Campus cityUniversity where Alice studiesUniversity on campus lineCountry
CityAU1U1CountryZ
CityBU1U1CountryZ
CityC (Query B only)U1U2CountryZ

Query A uses the name ?university on both the study and campus lines. Query B uses ?otherUniversity on the campus line. Both versions keep the city tied to its country by reusing ?city. This gives you a precise place to investigate: which university may supply the campus?

The experiment will use those two query versions throughout. Start by locating a match one version can make and the other cannot. Next, find the fact that supplies that match’s displayed value. Choose a replacement value to distinguish it, then run both versions on the same graph.

Before predicting, underline the part of the question that matters for this choice: “of a university where Alice studies.” The study edge is fixed throughout the test. An answer should still follow from a campus of a university that fits this condition.

It is possible to know where the extra path begins and still be unsure which output will change. Keep two notes separate: which queries can reach the changed edge, and which displayed values the original graph already contains. The second note helps when the results are shown with DISTINCT.

Get a hint for making your prediction

Read the starting campus table. Compare the university in the two middle columns for CityC. Then trace which country value would come from that row if its final edge changes.

You can go to the experiment now or read the reporting example first. In the experiment, record your prediction before moving the edge. Include a reason if you can: name the line that uses the city you intend to move.

Record a conclusion someone else can check

Good experiment notes describe the starting data, the one fact you changed, and what both queries reported. This is enough for somebody else to repeat the comparison. Keep any other edits separate so the test describes a single change.

For example, in the room problem you can report, “I replaced Green’s Hall access with Lab access. The shared query still returned Hall. The separate query added Lab. Its extra match used Blue for membership and Green for access.” Every part of that explanation points to a fact or a row.

Report a test with three pieces of evidence
RecordPia example
One changed factGreen can enter Hall → Green can enter Lab
Result from each queryShared: Hall · Separate: Hall, Lab
Row that explains the differenceMembership Blue, access Green, room Lab

In your own experiment, first record which query or queries you expect to change. Open the graph, compare its starting state, and move CityC. Check the changed edge and the corresponding country cell in the match table. Then connect that cell to the answer shown above the table.

Try switching back after you have written a sentence. If CityC moves back to the original country, the starting answer should be reproducible with the same query settings. You can compare that return with the notes you took before the change.

Sometimes a first attempt keeps the displayed results equal. Look at the full assignments and ask whether the new value was already present elsewhere. A changed match can still be hidden when the chosen answer column has another copy of that same value. The last exercise lets you compare a change like that with one that separates the results.

Treat the test as a claim about the graphs you tried. When outputs disagree, you have shown they can disagree. When they agree, record the starting conditions and look for additional evidence before claiming that the patterns always have the same meaning.

Finally, if you can repair the query, tie the changed line back to the variable that the earlier condition found. Then repeat the test on both graphs. The goal is to connect the repair to the original question and check the particular case that revealed the fault.

If you share your notes with somebody who already sees the new answer, ask them to trace it without looking at your conclusion. They should be able to identify the changed edge and the query line that could reach it. That is a practical test of whether your evidence is specific enough.

Review shared variables or distinct results, or continue to the country experiment.

The same answer, two ways

Unsure what to change? Walk through Pia’s one-edge test before making your own prediction.

Ask: Which countries have a campus of a university where Alice studies? On the starting graph, both queries display CountryZ.

Query A

ex:Alice ex:studiedAt ?university .
?university ex:hasCampusIn ?city .
?city ex:inCountry ?country .

Query B

ex:Alice ex:studiedAt ?university .
?otherUniversity ex:hasCampusIn ?city .
?city ex:inCountry ?country .

Alice studies at U1. CityA and CityB belong to U1, while CityC belongs to U2. Both queries display SELECT DISTINCT ?country. If CityC moves to CountryY, which displayed answer do you expect to change?

Make two short notes beside your prediction. For Query A, write which university the campus line is allowed to use. For Query B, write which universities could fill its separate blank. Use those notes to decide whether either query can get as far as CityC.

Make a prediction

Change one fact

CityC belongs to U2 in both versions of the graph. Change just CityC’s country and compare the evidence.

Compare both starting result lists first. After changing the country, look for the CityC row and read its university and country cells together. Switch back to CountryZ when you want to repeat the comparison.

After the test, report the result in one sentence that includes both query names and the extra country, if there is one. In a second sentence, point to the university that supplied the extra campus. Check your explanation against the worked answer when you are ready.

Choose a change that reveals the error

Ren belongs to Oak. The question is “Which rooms does Ren’s group meet in?” Oak meets in Room1 and Room2. Cedar meets in Room1. The shared query reuses the group on its meeting line. The separate query lets any group fill that line.

  • Renbelongs toOak
  • Oakmeets inRoom1
  • Oakmeets inRoom2
  • Cedarmeets inRoom1
Oak supplies both recorded rooms for Ren. Cedar supplies one additional match to the separate query.

Both queries select distinct rooms. Before changing anything, explain why both display Room1 and Room2. Then pick a replacement for Cedar’s meeting place: Room2 or Room3. Try both on paper or aloud. For each replacement, say which rooms each query displays and whether the number of complete matches changes.

Work from the starting graph for each candidate. You are changing Cedar’s meeting place once and keeping Oak’s two room facts. If you need help, look at the row that only the separate query can make before opening the answer.

Get a hint

The extra row comes from Ren belonging to Oak and Cedar supplying its room. Change the room on that row. Does the new room already appear in a row from Oak?

Check the two replacements
Displayed room lists after each replacement
Cedar’s roomShared groupSeparate groups
Room2Room1, Room2Room1, Room2
Room3Room1, Room2Room1, Room2, Room3

The shared version has two rows throughout, one per room for Oak. The separate version has three throughout, with the extra row using Cedar. In the starting graph Cedar’s value, Room1, is already listed for Oak. The duplicate room value shows up only once.

Room2 is also already in Oak’s result. Replacing Cedar’s room with Room2 changes the extra row without creating a different displayed room. If you chose it as the counterexample, look back at Oak’s second meeting arrow and find the duplicated value.

Room3 exposes the error. It creates the row membership Oak, meeting group Cedar, room Room3. Since Oak does not meet in Room3 and the display includes it only for the separate query, it gives you a concrete difference to report.

A complete explanation identifies what changed, explains why Oak’s answer stays put, and points to the exact row that let the other query list an additional room.

Ready to move on?

Name one fact you could change, then trace the new answer back to the specific match that produced it.