Skip to content

Commit

Permalink
chore: format jena classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jobulcke committed Dec 9, 2024
1 parent 45432aa commit e99d7f6
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,72 @@

package org.apache.jena.graph;

import org.apache.jena.util.CollectionFactory;

import java.util.Iterator;
import java.util.Set;

import org.apache.jena.util.CollectionFactory ;

/**
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.
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 );
}
}
}

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);
}
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,24 @@
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.
*/
public static final TripleBoundary stopNowhere = new TripleBoundary()
{ @Override
public boolean stopAt( Triple t ) { return false; } };

/**
A TripleBoundary that stops at triples with anonymous objects.
*/
public static final TripleBoundary stopAtAnonObject = new TripleBoundary()
{ @Override
public boolean stopAt( Triple t ) { return t.getObject().isBlank(); } };
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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,50 @@

package org.apache.jena.rdf.model;

import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.GraphExtract ;
import org.apache.jena.graph.TripleBoundary ;
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 ); }
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@

package org.apache.jena.rdf.model;

import org.apache.jena.graph.TripleBoundary ;
import org.apache.jena.graph.TripleBoundary;

/**
An interface for expressing search boundaries in terms of bounding statements.
*/
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);

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 );
}
/**
Answer a TripleBoundary corresponding to this StatementBoundary,
where Triples may be converted to Statements using <code>m</code>.
*/
TripleBoundary asTripleBoundary(Model m);
}
Loading

0 comments on commit e99d7f6

Please sign in to comment.