Skip to content

Commit

Permalink
Code cleanup during review
Browse files Browse the repository at this point in the history
  • Loading branch information
agarciadom committed Jan 16, 2025
1 parent 7034d1e commit 323a8dc
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 485 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
********************************************************************************/
package org.eclipse.epsilon.emc.rdf;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -21,7 +20,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;

import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
Expand Down Expand Up @@ -52,7 +50,7 @@ public class RDFModel extends CachedModel<RDFModelElement> {
public static final String PROPERTY_LANGUAGE_PREFERENCE = "languagePreference";

public static final String PROPERTY_SCHEMA_URIS = "schemaUris";
public static final String PROPERTY_DATA_URIS = "uris"; // TODO Update the uris to dataUris, breaks existing saved .launch files
public static final String PROPERTY_DATA_URIS = "uris";

/**
* One of the keys used to construct the first argument to
Expand All @@ -67,37 +65,25 @@ public class RDFModel extends CachedModel<RDFModelElement> {
protected final List<String> languagePreference = new ArrayList<>();
protected final Map<String, String> customPrefixesMap = new HashMap<>();

public enum RDFReasonerType {
// TODO add to this list to cover reasoner types in the ReasonerRegistry Class
public enum ReasonerType {
NONE,
OWL_FULL;
} // TODO add to this list to cover reasoner types in the ReasonerRegistry Class
protected RDFReasonerType rdfsReasonerType = RDFReasonerType.NONE;

public RDFReasonerType getRdfsReasonerType() {
return rdfsReasonerType;
}

public void setRdfsReasonerType(RDFReasonerType rdfsReasonerType) {
this.rdfsReasonerType = rdfsReasonerType;
protected ReasonerType reasonerType = ReasonerType.NONE;

public ReasonerType getReasonerType() {
return reasonerType;
}

public void setReasonerType(ReasonerType rdfsReasonerType) {
this.reasonerType = rdfsReasonerType;
}


protected final List<String> schemaURIs = new ArrayList<>();
protected final List<String> dataURIs = new ArrayList<>();
protected OntModel model;

// Write the OntModel the driver is using a file, this includes data model (inferred schemas) and additional Ont information
// DO NOT USE THESE TO PERSIST A USER'S DATA MODEL!
public void writeOntModel(OutputStream outputStream, String language) {
model.write(outputStream, language);
}

// As writeOntModel() but with All option, which adds yet more model information into the file
public void writeAllOntModel(OutputStream outputStream, String language) {
model.writeAll(outputStream, language);
}


public RDFModel() {
this.propertyGetter = new RDFPropertyGetter(this);
Expand Down Expand Up @@ -169,7 +155,7 @@ public Collection<RDFModelElement> listRestrictionInModel(){
restrictionIt.forEach(r -> restrictionList.add(new RDFResource(r, this)));
return restrictionList;
}

@Override
public String getElementId(Object instance) {
if (instance instanceof RDFResource) {
Expand Down Expand Up @@ -212,25 +198,8 @@ public void load(StringProperties properties, IRelativePathResolver resolver) th
* configuration of this RDFModel. This is the same as in other popular
* EMC drivers (e.g. the EmfModel class).
*/
this.dataURIs.clear();
{
String sUris = properties.getProperty(PROPERTY_DATA_URIS, "").strip();
if (!sUris.isEmpty()) {
for (String uri : sUris.split(",")) {
this.dataURIs.add(uri.strip());
}
}
}

this.schemaURIs.clear();
{
String sUris = properties.getProperty(PROPERTY_SCHEMA_URIS, "").strip();
if (!sUris.isEmpty()) {
for (String uri : sUris.split(",")) {
this.schemaURIs.add(uri.strip());
}
}
}
loadCommaSeparatedProperty(properties, PROPERTY_DATA_URIS, this.dataURIs);
loadCommaSeparatedProperty(properties, PROPERTY_SCHEMA_URIS, this.schemaURIs);

this.customPrefixesMap.clear();
String sPrefixes = properties.getProperty(PROPERTY_PREFIXES, "").strip();
Expand Down Expand Up @@ -266,6 +235,18 @@ public void load(StringProperties properties, IRelativePathResolver resolver) th
load();
}

protected void loadCommaSeparatedProperty(StringProperties properties, String propertyName, List<String> targetList) {
targetList.clear();
{
String sUris = properties.getProperty(propertyName, "").strip();
if (!sUris.isEmpty()) {
for (String uri : sUris.split(",")) {
targetList.add(uri.strip());
}
}
}
}

@Override
public boolean store(String location) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -361,34 +342,35 @@ protected void loadModel() throws EolModelLoadingException {
for (Iterator<String> itUri = schemaURIs.iterator(); itUri.hasNext(); ) {
schemaModel.read(itUri.next());
}

// If a schema model has been loaded assume need for a reasoner using Jena's default OWL
if( (schemaModel.size() >= 0) && (rdfsReasonerType == RDFReasonerType.NONE) )
{
this.setRdfsReasonerType(RDFReasonerType.OWL_FULL);
if (schemaModel.size() >= 0 && reasonerType == ReasonerType.NONE) {
System.err.println("Schema URIs have been defined: overriding NONE ReasonerType with OWL_FULL ReasonerType");
this.setReasonerType(ReasonerType.OWL_FULL);
}

Model dataModel = ModelFactory.createDefaultModel();
for (Iterator<String> itUri = dataURIs.iterator(); itUri.hasNext(); ) {
dataModel.read(itUri.next());
}

//Create an OntModel to handle the data model being loaded or inferred from data and schema
this.model = ModelFactory.createOntologyModel();

if (rdfsReasonerType == RDFReasonerType.NONE) { // Just OntModel bits are added to the dataModel being loaded.
if (reasonerType == ReasonerType.NONE) {
// Only the OntModel bits are added to the dataModel being loaded.
this.model.add(dataModel);
} else { // OntModel bits are added and the reasoner will add schema bits to the dataModel being loaded.
} else {
// OntModel bits are added and the reasoner will add schema bits to the dataModel being loaded.
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
InfModel infmodel = ModelFactory.createInfModel(reasoner, dataModel, schemaModel);
this.model.add(infmodel);
}
// Copy the Name prefixmaps from the loaded Model dataModel to the new OntModel dataModel representation

// Copy the Name prefix maps from the loaded Model dataModel to the new OntModel dataModel representation
for (Entry<String, String> e : dataModel.getNsPrefixMap().entrySet()) {
this.model.setNsPrefix(e.getKey(), e.getValue());
}

} catch (Exception ex) {
throw new EolModelLoadingException(ex, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

public class RDFPropertyProcesses {

public static ExtendedIterator<Statement> getPropertyStatementIterator (RDFQualifiedName propertyName, Resource resource) {
private RDFPropertyProcesses() {
// This class is not meant to be instantiated
}

public static ExtendedIterator<Statement> getPropertyStatementIterator(RDFQualifiedName propertyName, Resource resource) {
ExtendedIterator<Statement> propertyStatementIt = null;
// Filter all Property (Predicate) statements by prefix and local name
if (propertyName.prefix == null) {
Expand All @@ -37,8 +41,8 @@ public static ExtendedIterator<Statement> getPropertyStatementIterator (RDFQuali
}
return propertyStatementIt;
}
public static ExtendedIterator<Statement> filterPropertyStatementsIteratorWithLanguageTag (RDFQualifiedName propertyName, ExtendedIterator<Statement> propertyStatements) {

public static ExtendedIterator<Statement> filterPropertyStatementsIteratorWithLanguageTag(RDFQualifiedName propertyName, ExtendedIterator<Statement> propertyStatements) {
// If a language tag is used, only keep literals with that tag
if (propertyName.languageTag != null) {
propertyStatements = propertyStatements.filterKeep(stmt -> {
Expand All @@ -51,8 +55,8 @@ public static ExtendedIterator<Statement> filterPropertyStatementsIteratorWithLa
}
return propertyStatements;
}
public static MaxCardinalityRestriction getPropertyStatementMaxCardinalityRestriction (RDFQualifiedName propertyName, Resource resource) {

public static MaxCardinalityRestriction getPropertyStatementMaxCardinalityRestriction(RDFQualifiedName propertyName, Resource resource) {
// Gets all the propertyStatements and finds all the MaxCardinality restrictions, keeps the most restrictive (lowest maxCardinality)
MaxCardinalityRestriction mostRestrictiveMaxCardinality = null;
ExtendedIterator<Statement> propertyStatementIt = getPropertyStatementIterator(propertyName, resource);
Expand All @@ -64,7 +68,7 @@ public static MaxCardinalityRestriction getPropertyStatementMaxCardinalityRestri
.filterKeep(restriction -> restriction.isMaxCardinalityRestriction());

while (restrictionMaxCardinalityIt.hasNext()) {
MaxCardinalityRestriction currentMaxCardinalityRestriction = restrictionMaxCardinalityIt.next().asMaxCardinalityRestriction();
MaxCardinalityRestriction currentMaxCardinalityRestriction = restrictionMaxCardinalityIt.next().asMaxCardinalityRestriction();
if (mostRestrictiveMaxCardinality == null) {
mostRestrictiveMaxCardinality = currentMaxCardinalityRestriction;
} else {
Expand All @@ -76,32 +80,5 @@ public static MaxCardinalityRestriction getPropertyStatementMaxCardinalityRestri
}
return mostRestrictiveMaxCardinality;
}

// Probably delete this later...
private static void checkPropertyStmtForCardinalityRestrictionsOnPredicate(Statement propertyStmt) {
OntProperty predicateOntProperty = propertyStmt.getPredicate().as(OntProperty.class);
ExtendedIterator<Restriction> propertyRestrictionIt = predicateOntProperty.listReferringRestrictions();
propertyRestrictionIt.forEach(refferingRestriction -> {
System.out.println(
" property - " + propertyStmt + " predicate has restrictions -" + refferingRestriction);

// Play guess who with the Cardinality restrictions

if (refferingRestriction.isCardinalityRestriction()) {
System.out.println(" " + refferingRestriction + " asCardinalityRestriction : "
+ refferingRestriction.asCardinalityRestriction().getCardinality());
}

if (refferingRestriction.isMinCardinalityRestriction()) {
System.out.println(" " + refferingRestriction + " asMinCardinalityRestriction : "
+ refferingRestriction.asMinCardinalityRestriction().getMinCardinality());
}

if (refferingRestriction.isMaxCardinalityRestriction()) {
System.out.println(" " + refferingRestriction + " asMaxCardinalityRestriction : "
+ refferingRestriction.asMaxCardinalityRestriction().getMaxCardinality());
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
import java.util.stream.Collectors;

import org.apache.jena.ontology.MaxCardinalityRestriction;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.rdf.model.Literal;
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.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.util.PrintUtil;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.vocabulary.RDF;
import org.eclipse.epsilon.eol.execute.context.IEolContext;
Expand Down Expand Up @@ -56,7 +53,6 @@ public Resource getResource() {

public Collection<Object> listPropertyValues(String property, IEolContext context) {
final RDFQualifiedName pName = RDFQualifiedName.from(property, this.owningModel::getNamespaceURI);

Collection<Object> value = listPropertyValues(pName, context, LiteralMode.VALUES_ONLY);

if (value.isEmpty() && pName.localName.endsWith(LITERAL_SUFFIX)) {
Expand Down Expand Up @@ -110,12 +106,11 @@ protected Collection<Object> filterByPreferredLanguage(Collection<Object> value)
public Collection<Object> listPropertyValues(RDFQualifiedName propertyName, IEolContext context, LiteralMode literalMode) {
// Disable this check to remove the maxCardinality limit on returned properties i.e. maxCardinality == null.
MaxCardinalityRestriction maxCardinality = RDFPropertyProcesses.getPropertyStatementMaxCardinalityRestriction(propertyName, resource);

ExtendedIterator<Statement> itStatements;
itStatements = RDFPropertyProcesses.getPropertyStatementIterator(propertyName, resource);
itStatements = RDFPropertyProcesses.filterPropertyStatementsIteratorWithLanguageTag(propertyName, itStatements);



// Build a collection Objects for the rawValues of the Objects for the Properties remaining
Collection<Object> rawPropertyValues;
if (propertyName.prefix == null) {
Expand Down Expand Up @@ -200,27 +195,5 @@ protected Object convertToModelObject(RDFNode node) {
public String toString() {
return "RDFResource [resource=" + resource + "]";
}

public String getStatementsString() {
String statements = "Statements for RDFResource [" + resource + "]";

boolean resourceIsClass = false;
try {
resourceIsClass = resource.as(OntClass.class).isClass();
} catch (Exception e) {

}
statements = statements.concat(" is Class " + resourceIsClass);

for (StmtIterator i = owningModel.model.listStatements(resource, (Property) null,(Resource) null); i.hasNext();) {
Statement stmt = i.nextStatement();
statements = statements.concat("\n - ").concat(PrintUtil.print(stmt).toString());
}
return statements;
}

public void printStatements() {
System.out.println(getStatementsString());
}

}
Loading

0 comments on commit 323a8dc

Please sign in to comment.