Skip to content

Commit

Permalink
Merge pull request #20 from nickft/migrattion-from-rdf4j-to-apache-jena
Browse files Browse the repository at this point in the history
Migrattion from rdf4j to apache jena
  • Loading branch information
nickft authored Feb 13, 2025
2 parents 744ad6d + d074e2a commit 75fba0b
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 225 deletions.
15 changes: 3 additions & 12 deletions jlink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@
</dependency>

<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-client</artifactId>
<type>pom</type>
<version>5.1.0</version>

</dependency>

<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-rdfxml</artifactId>
<type>pom</type>
<version>5.1.0</version>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.List;

import org.eclipse.rdf4j.model.Value;
import org.apache.jena.rdf.model.Statement;

public interface ElementValidator<T> {
public T readFromMetadata(List<Value> descriptorContents) throws Exception;
public T readFromMetadata(List<Statement> descriptorContents) throws Exception;

public void validateSchema(List<Value> descriptorContents) throws Exception;
public void validateSchema(List<Statement> descriptorContents) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;
import java.util.Map;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.mipams.jlink.entities.JlinkImage;

public class ImageValidator extends JlinkAbstractValidator<JlinkImage> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.List;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;

import java.util.List;

import static org.eclipse.rdf4j.model.util.Values.iri;

public abstract class JlinkAbstractValidator<T> implements ElementValidator<T> {

Expand All @@ -31,23 +31,23 @@ protected Map<String, Resource> getSubjectNameToResourceMap() {
}

@Override
public T readFromMetadata(List<Value> contents) throws Exception {
public T readFromMetadata(List<Statement> contents) throws Exception {
return validateElement(contents, true);
}

@Override
public void validateSchema(List<Value> contents) throws Exception {
public void validateSchema(List<Statement> contents) throws Exception {
validateElement(contents, false);
}

private T validateElement(List<Value> sceneContents, boolean isMetadataStructure) throws Exception {
private T validateElement(List<Statement> sceneContents, boolean isMetadataStructure) throws Exception {
Map<String, String> metadataProperties = new HashMap<>();
List<String> allowedPropertyKeys = getAllowedProperties();

T jlinkElement = initializeJlinkElement();

for (Value i : sceneContents) {
Resource resource = getSubjectNameToResourceMap().get(i.stringValue());
for (Statement i : sceneContents) {
Resource resource = getSubjectNameToResourceMap().get(i.getObject().toString());

if (supportsSubschemata() && ValidatorUtils.isSchemaStatement(jlinkModel, resource)) {
Optional<Statement> schemaStatement = ValidatorUtils.getSchemaStatement(jlinkModel, resource);
Expand Down Expand Up @@ -106,17 +106,17 @@ protected void removePropertyIfExistsOrElseThrowException(String propertyName, L
}
}

protected List<Value> getSchemaContents(Statement schemaStatement, boolean isMetadataStructure) throws Exception {
protected List<Statement> getSchemaContents(Statement schemaStatement, boolean isMetadataStructure) throws Exception {
Statement descriptor = getChildStatementBasedOnElementType(schemaStatement, isMetadataStructure);
Resource bag = getSubjectNameToResourceMap().get(descriptor.getObject().stringValue());
Resource bag = getSubjectNameToResourceMap().get(descriptor.getObject().toString());

return ValidatorUtils.getRdfBagContents(bag, jlinkModel);
}

protected Statement getChildStatementBasedOnElementType(Statement parentStatement, boolean isSchemaElement) {
return (!isSchemaElement) ? ValidatorUtils.getOptionalValue(jlinkModel,
parentStatement.getSubject(), iri("http://ns.intel.com/umf/2.0descriptors")).get()
parentStatement.getSubject(), ResourceFactory.createProperty("http://ns.intel.com/umf/2.0descriptors")).get()
: ValidatorUtils.getOptionalValue(jlinkModel,
parentStatement.getSubject(), iri("http://ns.intel.com/umf/2.0set")).get();
parentStatement.getSubject(), ResourceFactory.createProperty("http://ns.intel.com/umf/2.0set")).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.ArrayList;
import java.util.Map;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.mipams.jlink.entities.JlinkElement;

public class JlinkValidator extends JlinkAbstractValidator<JlinkElement> {
Expand Down Expand Up @@ -51,9 +50,9 @@ protected void populateObjectFromMap(JlinkElement jlinkElement, Map<String, Stri
protected void handleSubschema(JlinkElement jlinkElement, Statement schemaStatement,
List<String> allowedPropertyNames,
boolean isMetadataStructure) throws Exception {
String schemaName = schemaStatement.getObject().stringValue();
String schemaName = schemaStatement.getObject().toString();

List<Value> schemaContents = getSchemaContents(schemaStatement, isMetadataStructure);
List<Statement> schemaContents = getSchemaContents(schemaStatement, isMetadataStructure);
JlinkProperty schema = JlinkProperty.getSchemaPropertyFromString(schemaName);

if (isMetadataStructure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.List;
import java.util.ArrayList;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.mipams.jlink.entities.JlinkLink;

public class LinkValidator extends JlinkAbstractValidator<JlinkLink> {
Expand Down Expand Up @@ -37,8 +36,8 @@ protected boolean supportsSubschemata() {
@Override
protected void handleSubschema(JlinkLink link, Statement schemaStatement, List<String> allowedPropertyNames,
boolean isMetadataStructure) throws Exception {
String schemaName = schemaStatement.getObject().stringValue();
List<Value> schemaContents = getSchemaContents(schemaStatement, isMetadataStructure);
String schemaName = schemaStatement.getObject().toString();
List<Statement> schemaContents = getSchemaContents(schemaStatement, isMetadataStructure);

removePropertyIfExistsOrElseThrowException(schemaName, allowedPropertyNames);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;
import java.util.Map;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.mipams.jlink.entities.JlinkRegion;

public class RegionValidator extends JlinkAbstractValidator<JlinkRegion> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.util.Map;
import java.util.ArrayList;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.mipams.jlink.entities.JlinkScene;

public class SceneValidator extends JlinkAbstractValidator<JlinkScene> {
Expand Down Expand Up @@ -44,9 +43,9 @@ protected PropertyType getExpectedTypeFromPropertyName(String propertyName) thro
@Override
protected void handleSubschema(JlinkScene scene, Statement schemaStatement, List<String> allowedPropertyNames,
boolean isMetadataStructure) throws Exception {
String schemaName = schemaStatement.getObject().stringValue();
String schemaName = schemaStatement.getObject().toString();

List<Value> schemaContents = getSchemaContents(schemaStatement, isMetadataStructure);
List<Statement> schemaContents = getSchemaContents(schemaStatement, isMetadataStructure);
SceneProperty schema = SceneProperty.getSchemaPropertyFromString(schemaName);

if (SceneProperty.IMAGE.equals(schema)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
package org.mipams.jlink.entities.validator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.util.RDFContainers;
import org.eclipse.rdf4j.model.vocabulary.RDF;

import static org.eclipse.rdf4j.model.util.Values.iri;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;

public class ValidatorUtils {

public static ArrayList<Value> getRdfBagContents(Resource rdfBagParentResource, Model jlinkModel) {
return RDFContainers.toValues(RDF.BAG, jlinkModel, rdfBagParentResource, new ArrayList<>());
public static List<Statement> getRdfBagContents(Resource rdfBagParentResource, Model jlinkModel) {
return jlinkModel
.getBag(rdfBagParentResource)
.listProperties()
.toList()
.stream()
.filter(st ->
!st.getObject()
.toString()
.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag"))
.collect(Collectors.toList()
);
}

public static Optional<Statement> getOptionalValue(Model model, Resource sub, IRI predicate) {
Iterator<Statement> schemaDescriptors = model
.getStatements(sub, predicate, null).iterator();
public static Optional<Statement> getOptionalValue(Model model, Resource sub, Property predicate) {
StmtIterator schemaDescriptorIterator = model.listStatements(sub, predicate, (RDFNode) null);

if (!schemaDescriptors.hasNext()) {
if (!schemaDescriptorIterator.hasNext()) {
return Optional.empty();
}

return Optional.of(schemaDescriptors.next());
return Optional.of(schemaDescriptorIterator.next());
}

public static void validatePropertyName(String propertyName, Set<String> occuredProperties,
Expand Down Expand Up @@ -62,24 +69,28 @@ public static boolean isSchemaStatement(Model model, Resource resource) {
}

public static Optional<Statement> getSchemaStatement(Model model, Resource resource) {
return ValidatorUtils.getOptionalValue(model, resource, iri("http://ns.intel.com/umf/2.0schema"));
Property property = ResourceFactory.createProperty("http://ns.intel.com/umf/2.0schema");
return ValidatorUtils.getOptionalValue(model, resource, property);
}

public static String getPropertyName(Model model, Resource resource) {
Property property = ResourceFactory.createProperty("http://ns.intel.com/umf/2.0name");
return ValidatorUtils
.getOptionalValue(model, resource, iri("http://ns.intel.com/umf/2.0name"))
.get().getObject().stringValue();
.getOptionalValue(model, resource, property)
.get().getObject().toString();
}

public static String getPropertyType(Model model, Resource resource) {
Property property = ResourceFactory.createProperty("http://ns.intel.com/umf/2.0type");
return ValidatorUtils
.getOptionalValue(model, resource, iri("http://ns.intel.com/umf/2.0type"))
.get().getObject().stringValue();
.getOptionalValue(model, resource, property)
.get().getObject().toString();
}

public static String getPropertyValue(Model model, Resource resource) {
Property property = ResourceFactory.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#value");
return ValidatorUtils
.getOptionalValue(model, resource, iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#value"))
.get().getObject().stringValue();
.getOptionalValue(model, resource, property)
.get().getObject().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;
import java.util.Map;

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.mipams.jlink.entities.JlinkViewport;

public class ViewportValidator extends JlinkAbstractValidator<JlinkViewport> {
Expand Down
Loading

0 comments on commit 75fba0b

Please sign in to comment.