last day (15 days later) » 

2:40 PM
2
Q: How to generate uniform bindings for rename operation

Rob HallI have a dataset wherein I'm attempting to replace an individual that matches certain criteria with another individual. In the minimal example provided, I am looking to replace x-data://old with x-data://new. Example input dataset: <x-data://new> <x-dom://betterThan> <x-data://old> . <x-data://o0

 
What's the purpose of matching ?new and ?old in a sub-select? Is it just to limit yourself to one result? E.g., why can't you just use ?new :betterThan ?old . { … } union { … }?
Andy's answer on this answers.semanticweb.com question mentions that you can combine multiple operations. I wonder if something with that technique can be applied here…
 
@JoshuaTaylor If I don't limit it to one result, then I believe that cases where there are chains of ?newest :betterThan ?old. ?old :betterThan ?oldest. will result in inconsistencies because all allowable bindings are generated before the query executes. For example, replacing ?old with ?new, then replacing ?oldest with ?old results in there no longer existing a link from ?new to ?old and not being able to mend the document.
@JoshuaTaylor regarding Andy's Answer, I don't believe that I could propagate my select criteria through all three queries without leaving SPARQL and doing some Jena-Fu. While that is an acceptable workaround (this post is tagged Jena), I'd like to lean away from that, if possible.
 
OK, but if there is more than one :betterThan triple, how do you know that you're getting the one that you want? There's no ordering condition or anything in your subselect at the moment…
 
@JoshuaTaylor at the moment, if any single element that satisfies that clause were picked individually, then the desired rename would not cause any invalidation within the document. For example, regardless of which (valid) pair were picked (in my previous comment), then a chain would exist that would still allow ?newestRemaining :betterThan ?oldestRemaining to bind. I'm fine with running this query until there are no longer candidates for replacement.
 
Are you sure? If you have 3 > 2 . 2 > 1 (where > is :betterThan) and you pick 2 > 1 first, then don't you replace 2 > 1 with 2 > 2, and then you could simply be iterating on 2 > 2 endlessly?
As a quite possibly poorly-performing option, could you do the two other queries as sub-selects as well, and thus join on the ?old and ?new that you choose with the first subselect?
 
2:40 PM
@JoshuaTaylor we restrict the query so that FILTER( !sameTerm( ?nodeReplacing, ?nodeBeingReplaced) ) in practice. This is a good catch in terms of oversight in the example. I'll edit it to match.
 
Now you're missing a paren, since you did filter( !sameTerm( ?x, ?y ) . :(
 
Oh no..
Theoretically, the parenthesis is fixed.
 
hm
not much of an alert that this thing is on
 
Nope. I looked away and missed feedback
 
2:56 PM
but yeah, you could probably make three subselects and join on them
if you don't have a whole lot of :betterThan links, that wouldn't even be too terrible
 
ahhhhh now I'm seeing what you mean
well, the hard part is that my graph is huge and it's 90% :betterThan links. Renaming is part of an overall reduction strategy that hopes to preserve connectivity
 
yeah, like { select ?new ?old ?s ?p ?o { … :betterThan … … } } union { select ?new ?old ?s ?p ?o { … :betterThan … … } }
or whatever it'd be
oh
hm, that does make things harder
 
An existing workaround is to just use a SELECT query to grab the nodes, and them to use Jena statement-level manipulations in order to perform the rename. This is what is currently in place.
 
is the betterThan link a single chain
or is it a tree?
 
For this particular query, we're trying to replace the B in an A B C link. The graph as a whole is a directed cyclic graph where every possible combination of edges seems to exist somewhere
 
3:04 PM
so it's not like some kind of order where you're saying "replace all the elements with the greatest element"?
 
Basically, I'm grabbing node B where there are no inbound or outbound links from B except for the inbound from A and the outbound to C, then deleting it, and placing all of it's properties on A, instead
no. The :betterThan phrasing was mostly towards defining an arbitrary criteria for the example. There is no ordering aspect over the nodes
The SELECT query currently identifies a single pair of nodes that need to have this replacement operation occur and provide valid bindings for "nodes that we can do this to, right now"
 
when you phrase it like that, it almost sounds like you could do these all in parallel
something like
hm
 
The problem with having done it in parallel the first time, is that a chain of A->B->C->D resulted in A->C, B->D, because all valid replacements were generated in the WHERE clause
Without the LIMIT 1 the scope problem disappears, but it introduces that other complication
 
but if you select A and D with something like A p+ D filter not exists { D p+ E }, you could make sure you're just getting the A's
so D is always the end of a chain, and every binding for A is a predecessor
 
In the graph as a whole, we don't really have a chain like that in isolation. A has a large number of links coming into it, and D has a large number of outgoing links. The goal is to eliminate B and C because they aren't providing us with any new information. They could be replaced with A->D instead
It almost feels like the GenericRuleReasoner + com.hp.hpl.jena.reasoner.rulesys.builtins.Drop would be a valid approach to take.
I'd also like to avoid that, if possible
 
3:19 PM
so, you have some resources, and a path between them
and you want to essentially merge the nodes between them with one of the resources?
or you want to merge the two ends of the path?
 
For now, let's just do the nodes between them
 
ok
so the nodes between them
do you want to merge them with the source or the sink of the path?
i.e., should B and C become A, or should they become D?
 
Either would work just fine. I've been merging them towards A.
A is preferable, actually. A,B,C all share the same types, for certain, and D may not. Merging towards A would preserve proper type information.
Ie: Semantically, all statements regarding C would hold for A, but not necessarily for D.
The result should pretty much be just A->A, and A->D, nothing else interesting or not already present in the graph would/should be expressed
 
not sure what you mean by the last bit
 
Meaning, if we took all the statements about B and C, and rewrote them in terms of A, the only new statements would be: A->D and A->A
73 intermediate nodes would distil down to those two statements being introduced
the rest, all the statements about the intermediate nodes, would be dropped
A->B => A->A; B->C => A->A; C->D => A->D
I'm sure we could use that, somehow
 
3:31 PM
ok, so b isn't the subject of anything except triples whose object is c, and c … d?
 
There are other statements about B such as rdfs:label and rdf:type, but they can be ignored. A does not have the same rdfs:label but the information loss would not be a game changer. Additionally, A does have the same rdf:type.
The same holds for C.
I believe one of the most important takeaways for this question would be how to perform a rewrite operation where we are given ?new and ?old from a sub-query. That has a lot of generic applicability. If we can't solve the question in terms of those things, then I understand.
 
OK, I don't have an endpoint for insert and delete up and running, but a construct like this generates the triples you want to insert, doesn't it?
prefix : <urn:ex:>

construct {
  ?begin ?p ?o
}
where {
  ?begin :to* ?s .
  ?s :to+ ?end .
  filter not exists { ?realBegin :to+ ?begin }
  filter not exists { ?end :to+ ?realEnd }

  ?s ?p ?o .
}
The two filters make sure that you're at A and D (begin and end), and you're trying to grab all the triples whose subject is A, B, or C
and replace them with triples whose predicate and object is the same, but whose subject is now begin (A)
and you could delete ?s ?p ?o at the same time
since the intermediate nodes only appear as objects of triples that have other intermediate nodes as subjects (this might not be the case in your actual data, but it sounded like it was), you don't even need to worry about binding as subjects and objects
 
4:44 PM
that might actually work
If that would work, then CONSTRUCT could be replaced with INSERT and an additional DELETE { ?s ?p ?o } could be added.
 
exactly
you might need some more filtering if you want to remove the :to links that are already there, or if you don't want to preserve labels, etc., on intermediate nodes
but if that's the case, then you could make the first pattern ?begin :to+ ?s (so that ?s isn't bound to ?begin) and add appropriate filters on ?p, either filter ( p in … ) or filter ( !(p in … ) ).
that's what I meant about doing it in parallel
 
A problem is that the end/beginning conditions may be a lot harder to specify than the example query suggests (just because there are inbound links to the ?readBegin and outbound links out of ?realEnd)
 
because this handles not just a single source and sink connected by a :to chain, but all of them
right, but that's why I filtered on not exists { ?end :to+ ?realEnd } with the concrete property :to (which actually, in this case, could be just :to; it doesn't need to be :to+)
but if you can specify the beginning and ending conditions with a SPARQL query, then this seems like it would work…
 
So what I would do then, is augment the filter's to reflect my actual begin and end conditions
namely, that there exists more than one inbound link, or some number of outbound links that does not equal 1 (ie: 0, greater than 1)
correction: I'd need to bind ?realBegin and ?realEnd elsewhere in the query
note: I'm fried. Hahah, I'll probably need to revisit this somewhat later and attempt another minimal example using this approach.
@JoshuaTaylor In this query, I still cannot use a sub-query to bind ?realBegin and ?realEnd, can I?
 
5:02 PM
I don't think so, but I'm not whether you'd need to anyway
is there some reason that you want to use a subquery here? If you can identify all the nodes that you want to replace at once, then I don't think you need the subquery anymore…
 
Without some testing, and thinking about the query provided, I have difficulty being certain that we won't knit together the data wrong again. The idea looks solid, and I'm definitely going to try it
It would be nice to construct some data that varies: having multiple chains; having chains with a start node that has no inputs; having a chain with a terminal node that has no output; having a chain that has a terminal node with multiple outputs; having a chain with an input node that has multiple inputs.
That will be my next step in testing this. If it works, what should be the next steps. I want you to have the appropriate credit, and I'd want this question to reflect the actual scenario better
 

last day (15 days later) »