-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ranko Orlic <[email protected]>
- Loading branch information
Showing
17 changed files
with
420 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
ldi-core/ldes-client/tree-node-fetcher/src/main/java/org/apache/jena/graph/GraphExtract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.graph; | ||
|
||
import org.apache.jena.util.CollectionFactory; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
/** | ||
GraphExtract offers a very simple recursive extraction of a subgraph with a | ||
specified root in some supergraph. The recursion is terminated by triples | ||
that satisfy some supplied boundary condition. | ||
*/ | ||
public class GraphExtract { | ||
protected final TripleBoundary b; | ||
|
||
public GraphExtract(TripleBoundary b) { | ||
this.b = b; | ||
} | ||
|
||
/** | ||
Answer a new graph which is the reachable subgraph from <code>node</code> | ||
in <code>graph</code> with the terminating condition given by the | ||
TripleBoundary passed to the constructor. | ||
*/ | ||
public Graph extract(Node node, Graph graph) { | ||
return extractInto(GraphMemFactory.createGraphMem(), node, graph); | ||
} | ||
|
||
/** | ||
Answer the graph <code>toUpdate</code> augmented with the sub-graph of | ||
<code>extractFrom</code> reachable from <code>root</code> bounded | ||
by this instance's TripleBoundary. | ||
*/ | ||
public Graph extractInto(Graph toUpdate, Node root, Graph extractFrom) { | ||
new Extraction(b, toUpdate, extractFrom).extractInto(root); | ||
return toUpdate; | ||
} | ||
|
||
/** | ||
This is the class that does all the work, in the established context of the | ||
source and destination graphs, the TripleBoundary that determines the | ||
limits of the extraction, and a local set <code>active</code> of nodes | ||
already seen and hence not to be re-processed. | ||
*/ | ||
protected static class Extraction { | ||
protected Graph toUpdate; | ||
protected Graph extractFrom; | ||
protected Set<Node> active; | ||
protected TripleBoundary b; | ||
|
||
Extraction(TripleBoundary b, Graph toUpdate, Graph extractFrom) { | ||
this.toUpdate = toUpdate; | ||
this.extractFrom = extractFrom; | ||
this.active = CollectionFactory.createHashedSet(); | ||
this.b = b; | ||
} | ||
|
||
public void extractInto(Node root) { | ||
active.add(root); | ||
Iterator<Triple> it = extractFrom.find(root, Node.ANY, Node.ANY); | ||
while (it.hasNext()) { | ||
Triple t = it.next(); | ||
Node subRoot = t.getObject(); | ||
toUpdate.add(t); | ||
if (!(active.contains(subRoot) || b.stopAt(t))) extractInto(subRoot); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...ore/ldes-client/tree-node-fetcher/src/main/java/org/apache/jena/graph/TripleBoundary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.graph; | ||
|
||
/** | ||
An interface for expressing a stopping condition on triples, such as in | ||
sub-graph extraction. | ||
*/ | ||
public interface TripleBoundary { | ||
/** | ||
Answer true if this triple is a stopping triple, and whatever search is using | ||
this interface should proceed no further. | ||
*/ | ||
boolean stopAt(Triple t); | ||
|
||
/** | ||
A TripleBoundary without limits - stopAt always returns false. | ||
*/ | ||
TripleBoundary stopNowhere = t -> false; | ||
|
||
/** | ||
A TripleBoundary that stops at triples with anonymous objects. | ||
*/ | ||
TripleBoundary stopAtAnonObject = t -> t.getObject().isBlank(); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...e/ldes-client/tree-node-fetcher/src/main/java/org/apache/jena/rdf/model/ModelExtract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.rdf.model; | ||
|
||
import org.apache.jena.graph.Graph; | ||
import org.apache.jena.graph.GraphExtract; | ||
import org.apache.jena.graph.TripleBoundary; | ||
|
||
/** | ||
ModelExtract - a wrapper for GraphExtract, allowing rooted sub-models to be | ||
extracted from other models with some boundary condition. | ||
*/ | ||
public class ModelExtract { | ||
/** | ||
The statement boundary used to bound the extraction. | ||
*/ | ||
protected StatementBoundary boundary; | ||
|
||
/** | ||
Initialise this ModelExtract with a boundary condition. | ||
*/ | ||
public ModelExtract(StatementBoundary b) { | ||
boundary = b; | ||
} | ||
|
||
/** | ||
Answer the rooted sub-model. | ||
*/ | ||
public Model extract(Resource r, Model s) { | ||
return extractInto(ModelFactory.createDefaultModel(), r, s); | ||
} | ||
|
||
/** | ||
Answer <code>model</code> after updating it with the sub-graph of | ||
<code>s</code> rooted at <code>r</code>, bounded by this instances | ||
<code>boundary</code>. | ||
*/ | ||
public Model extractInto(Model model, Resource r, Model s) { | ||
TripleBoundary tb = boundary.asTripleBoundary(s); | ||
Graph g = getGraphExtract(tb).extractInto(model.getGraph(), r.asNode(), s.getGraph()); | ||
return ModelFactory.createModelForGraph(g); | ||
} | ||
|
||
/** | ||
Answer a GraphExtract initialised with <code>tb</code>; extension point | ||
for sub-classes (specifically TestModelExtract's mocks). | ||
*/ | ||
protected GraphExtract getGraphExtract(TripleBoundary tb) { | ||
return new GraphExtract(tb); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...s-client/tree-node-fetcher/src/main/java/org/apache/jena/rdf/model/StatementBoundary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.rdf.model; | ||
|
||
import org.apache.jena.graph.TripleBoundary; | ||
|
||
/** | ||
An interface for expressing search boundaries in terms of bounding statements. | ||
*/ | ||
|
||
public interface StatementBoundary { | ||
/** | ||
Answer true if this statement is a boundary of the search. | ||
*/ | ||
boolean stopAt(Statement s); | ||
|
||
/** | ||
Answer a TripleBoundary corresponding to this StatementBoundary, | ||
where Triples may be converted to Statements using <code>m</code>. | ||
*/ | ||
TripleBoundary asTripleBoundary(Model m); | ||
} |
61 changes: 61 additions & 0 deletions
61
...ient/tree-node-fetcher/src/main/java/org/apache/jena/rdf/model/StatementBoundaryBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.rdf.model; | ||
|
||
import org.apache.jena.graph.TripleBoundary; | ||
|
||
/** | ||
StatementBoundaryBase - a base class for StatementBoundarys, with | ||
built-in conversation to triples and a continueWith as well as a stopAt. | ||
*/ | ||
public abstract class StatementBoundaryBase implements StatementBoundary { | ||
/** | ||
Method to over-ride to define what stops the boundary search; default | ||
definition is !continueWith(s). <i>exactly one</i> of these two methods | ||
must be defined. | ||
*/ | ||
@Override | ||
public boolean stopAt(Statement s) { | ||
return !continueWith(s); | ||
} | ||
|
||
/** | ||
Method to over-ride to define what continues the boundary search; default | ||
definition is !stopAt(s). <i>exactly one</i> of these two methods | ||
must be defined. | ||
*/ | ||
public boolean continueWith(Statement s) { | ||
return !stopAt(s); | ||
} | ||
|
||
/** | ||
Expresses this StatementBoundary as a TripleBoundary. | ||
*/ | ||
@Override | ||
public final TripleBoundary asTripleBoundary(Model m) { | ||
return convert(m, this); | ||
} | ||
|
||
/** | ||
Answer a TripleBoundary that is implemented in terms of a StatementBoundary. | ||
*/ | ||
public static TripleBoundary convert(final Model s, final StatementBoundary b) { | ||
return t -> b.stopAt(s.asStatement(t)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...nt/tree-node-fetcher/src/main/java/org/apache/jena/rdf/model/StatementTripleBoundary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.jena.rdf.model; | ||
|
||
import org.apache.jena.graph.TripleBoundary; | ||
|
||
/** | ||
StatementTripleBoundary - a StatementBoundary that just wraps a | ||
TripleBoundary. | ||
*/ | ||
public class StatementTripleBoundary implements StatementBoundary { | ||
protected TripleBoundary tb; | ||
|
||
/** | ||
Initialise this StatementTripleBoundary with the TripleBoundary <code>tb</code>. | ||
* @param tb | ||
*/ | ||
public StatementTripleBoundary(TripleBoundary tb) { | ||
this.tb = tb; | ||
} | ||
|
||
/** | ||
Answer whatever the triple-boundary answers for the triple of <code>s</code>. | ||
*/ | ||
@Override | ||
public boolean stopAt(Statement s) { | ||
return tb.stopAt(s.asTriple()); | ||
} | ||
|
||
/** | ||
Answer the supplied-to-constructor TripleBoundary. | ||
*/ | ||
@Override | ||
public TripleBoundary asTripleBoundary(Model ignored) { | ||
return tb; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.