Skip to content

Commit

Permalink
chore/bump-to-jena-5 (#729)
Browse files Browse the repository at this point in the history
Co-authored-by: Ranko Orlic <[email protected]>
  • Loading branch information
jobulcke and rorlic authored Dec 9, 2024
1 parent 6a1c2b2 commit a25adf8
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 29 deletions.
4 changes: 4 additions & 0 deletions ldi-core/json-to-ld-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
<groupId>be.vlaanderen.informatievlaanderen.ldes.ldi</groupId>
<artifactId>ldi-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import be.vlaanderen.informatievlaanderen.ldes.ldi.exceptions.ParseToJsonException;
import be.vlaanderen.informatievlaanderen.ldes.ldi.exceptions.UnsupportedMimeTypeException;
import be.vlaanderen.informatievlaanderen.ldes.ldi.types.LdiAdapter;
import org.apache.http.entity.ContentType;
import org.apache.hc.core5.http.ContentType;
import org.apache.jena.atlas.json.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
Expand Down Expand Up @@ -84,8 +84,7 @@ private Model mapJsonObjectToModel(JsonValue json) {
final var jsonObject = json.getAsObject();
addContexts(jsonObject);
Model model = ModelFactory.createDefaultModel();
RDFParser.fromString(jsonObject.toString())
.lang(Lang.JSONLD)
RDFParser.fromString(jsonObject.toString(), Lang.JSONLD)
.context(jenaContext)
.parse(model);
return model;
Expand Down
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);
}
}
}


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

}
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);
}
}
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);
}
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));
}
}
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;
}
}
8 changes: 8 additions & 0 deletions ldi-core/ngsiv2-to-ld-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@
<groupId>be.vlaanderen.informatievlaanderen.ldes.ldi</groupId>
<artifactId>ldi-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit a25adf8

Please sign in to comment.