Skip to content

Commit

Permalink
Find external property assertions for individuals from ontology.
Browse files Browse the repository at this point in the history
The xslt parser cannot extract metadata from other ontologies. This
adds a parser to process the external properties and if a match
is found then the appropiate property is added (dp,op,ni). If no
match is found then add in the documentation an "ep" property
which indicates that the property is found in an external
ontology.
  • Loading branch information
Victor Chavez committed Jan 2, 2024
1 parent c8c5dfa commit 6a0ac61
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 14 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
<version>3.9.0</version>
</dependency>


<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>com.github.VisualDataWeb</groupId>
<artifactId>OWL2VOWL</artifactId>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/widoco/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import widoco.entities.Agent;
import widoco.entities.Ontology;
import widoco.ExternalPropertyParser;

/**
*
Expand Down Expand Up @@ -337,6 +338,7 @@ public class Constants {
public static final String LANG_CLASSES = "classes";
public static final String LANG_OBJ_PROP = "objProp";
public static final String LANG_DATA_PROP = "dataProp";
public static final String LANG_EXT_PROP = "extProp";
public static final String LANG_ANN_PROP = "annProp";
public static final String LANG_NAMED_INDIV = "namedIndiv";
public static final String LANG_TABLE_OF_CONTENTS = "tableOfContents";
Expand Down Expand Up @@ -1520,6 +1522,11 @@ public static String getLegend(
+ lang.getProperty(Constants.LANG_NAMED_INDIV) + "\">ni</sup>: "
+ lang.getProperty(Constants.LANG_NAMED_INDIV) + "\n"
: "")
+ (ExternalPropertyParser.hasExternalProps() ?
"<sup class=\"type-ep\" title=\""
+ lang.getProperty(Constants.LANG_EXT_PROP) + "\">ep</sup>: "
+ lang.getProperty(Constants.LANG_EXT_PROP) + " <br/>\n"
: "")
+ "</div>\n" + "</div>"
+ "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/widoco/CreateResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static void generateDocumentation(String outFolder, Configuration c, File
logger.info("- ontology IRI: " + c.getOntologyURI());
lodeContent = LODEGeneration.getLODEhtml(c, lodeResources);
LODEParser lode = new LODEParser(lodeContent, c, languageFile);

if (c.isCreateHTACCESS()) {
File fOut = new File(folderOut);
if (!fOut.exists()) {
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/widoco/ExternalPropertyParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package widoco;

import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.model.parameters.Imports;

import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ExternalPropertyParser {
private enum PropertyType {
OBJECT_PROPERTY,
DATA_PROPERTY,
NAMED_INDIVIDUAL,
EXTERNAL_PROPERTY
}

private static boolean externalProps = false;

private static final Logger logger = LoggerFactory.getLogger(ExternalPropertyParser.class);

/**
* Parse html content with external property tags, i.e., the sup tag
* with class type-ep and attempt to find the type of property type
* within the ontology.
* The main purpose of this utility is to add metadata from the ontology
* to the named individual assertions since the xslt transform cannot
* look for external properties outisde of the xml rdf serialization of the
* ontology.
* @param htmlContent Html content where the external properties are located.
* Typically this is the named individuals section
* @param ontology The ontology that will be used to look for the IRIS of
* the external properties.
* @return
*/
public static String parse(String htmlContent, OWLOntology ontology) {
Document document = Jsoup.parseBodyFragment(htmlContent, "UTF-8");
// Find all <a> tags with <sup> tags having class "type-ep"
Elements links = document.select("a + sup.type-ep");
// Modify the <sup> tag based on the property type
for (Element link : links) {
String href = link.previousElementSibling().attr("href");
PropertyType type = getPropertyType(ontology, href);
String class_name;
String text;
String title;
if (type == PropertyType.OBJECT_PROPERTY) {
class_name = "type-op";
text="op";
title = "object property";
} else if(type == PropertyType.DATA_PROPERTY) {
class_name = "type-dp";
text="dp";
title = "data property";
} else if (type == PropertyType.NAMED_INDIVIDUAL) {
class_name = "type-ni";
text="ni";
title = "named individual";
} else {
class_name = "type-ep";
text ="ep";
title ="external property";
if (!externalProps) {
externalProps = true;
}
}
link.text(text);
link.attr("class",class_name);
link.attr("title",title);
}
return document.body().html();
}

public static boolean hasExternalProps(){
return externalProps;
}

private static PropertyType getPropertyType(OWLOntology ontology, String propertyIRI) {
IRI propertyIRIObject = IRI.create(propertyIRI);

// Check if it is an object property
Set<OWLObjectProperty> objectProperties = ontology.getObjectPropertiesInSignature(Imports.INCLUDED);
for (OWLObjectProperty objectProperty : objectProperties) {
if (objectProperty.getIRI().equals(propertyIRIObject)) {
return PropertyType.OBJECT_PROPERTY;
}
}
// Check if it is a data property
Set<OWLDataProperty> dataProperties = ontology.getDataPropertiesInSignature(Imports.INCLUDED);
for (OWLDataProperty dataProperty : dataProperties) {
if (dataProperty.getIRI().equals(propertyIRIObject)) {
return PropertyType.DATA_PROPERTY;
}
}

Set<OWLNamedIndividual> individuals = ontology.getIndividualsInSignature(Imports.INCLUDED);
for (OWLNamedIndividual individual : individuals) {
if (individual.getIRI().equals(propertyIRIObject)) {
return PropertyType.NAMED_INDIVIDUAL;
}
}
// If not an object or data property, consider it as an external property
return PropertyType.EXTERNAL_PROPERTY;
}

}
3 changes: 3 additions & 0 deletions src/main/java/widoco/LODEParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import widoco.ExternalPropertyParser;

/**
* Class made for parsing and manipulating LODE's html. This class contains most
* of the TemplateGeneratorOLD class
Expand Down Expand Up @@ -220,6 +222,7 @@ private void parse(String content, Properties langFile) {
"<h2>" + langFile.getProperty(Constants.LANG_NAMED_INDIV) + "</h2>",
"<h3 id=\"namedindividuals\" class=\"list\">"
+ langFile.getProperty(Constants.LANG_NAMED_INDIV) + "</h3>");
namedIndividuals = ExternalPropertyParser.parse(namedIndividuals, c.getMainOntology().getOWLAPIModel());
break;
/*missing: rules!*/
case "rules":
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/lode/extraction.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,12 @@ http://www.oxygenxml.com/ns/doc/xsl ">
<xsl:template name="get.individual.assertions">
<xsl:variable name="assertions">
<assertions>
<xsl:variable name="ns_rdfs" select="'http://www.w3.org/2000/01/rdf-schema#'"/>
<xsl:variable name="ns_dc" select="'http://purl.org/dc/elements/1.1/'"/>
<xsl:for-each select="element()">
<xsl:if test="name() != 'rdf:type'">
<xsl:if test="(name() != 'rdf:type')
and string(namespace-uri()) != $ns_rdfs
and string(namespace-uri()) != $ns_dc">
<xsl:variable name="currentURI" select="concat(namespace-uri(.),local-name(.))" as="xs:string"/>
<assertion rdf:about="{$currentURI}">
<xsl:choose>
Expand Down Expand Up @@ -1942,6 +1946,7 @@ http://www.oxygenxml.com/ns/doc/xsl ">
<sup title="{f:getDescriptionLabel('namedindividual')}" class="type-ni">ni</sup>
</xsl:when>
<xsl:when test="not(contains($iri, 'http://www.w3.org/2001/XMLSchema'))">
<xsl:message>Getting descriptor for <xsl:value-of select="$iri"/></xsl:message>
<sup title="{f:getDescriptionLabel('externalproperty')}" class="type-ep">ep</sup>
</xsl:when>
</xsl:choose>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/widoco/de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ crossRefPlaceHolder=Dieser Abschnitt enth&auml;lt Details f&uuml;r jede Klasse u
classes=Klassen
objProp=Object Properties
dataProp=Data Properties
extProp=External Properties
annProp=Annotation Properties
namedIndiv=Named Individuals
referencesTitle=Referenzen
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/widoco/en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ crossRefPlaceHolder=This section provides details for each class and property de
classes=Classes
objProp=Object Properties
dataProp=Data Properties
extProp=External Properties
annProp=Annotation Properties
namedIndiv=Named Individuals
referencesTitle=References
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/widoco/es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ crossRefPlaceHolder=Esta secci&oacute;n introduce m&aacute;s detalles sobre cada
classes=Clases
objProp=Propiedades de objeto
dataProp=Propiedades de datos
extProp=External Properties
annProp=Propiedades usadas para anotaci&oacute;n
namedIndiv=Individuos
referencesTitle=Referencias
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/widoco/CreateDocInThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static void deleteFiles(File folder){
public void testOntoInTTL() {
System.out.println("Testing Ontology: BNE");
try{
String pathToOnto = "test" + File.separator + "bne.ttl";
String pathToOnto = "C:\\Users\\vc9917e\\Downloads\\boat.ttl";
c.setFromFile(true);
this.c.setOntologyPath(pathToOnto);
//read the model from file
Expand All @@ -98,7 +98,7 @@ public void testOntoInTTL() {
/**
* Test an OWL ontology. coil.owl
*/
@org.junit.Test
//@org.junit.Test
public void testOntoOWLXML() {
System.out.println("Testing Ontology: coil.owl");
try{
Expand All @@ -116,7 +116,7 @@ public void testOntoOWLXML() {
/**
* Testing a small ontology observation.owl
*/
@org.junit.Test
//@org.junit.Test
public void testOntologySmall() {
System.out.println("Testing Ontology: observation.owl");
try{
Expand All @@ -134,7 +134,7 @@ public void testOntologySmall() {
/**
* Testing a medium sized ontology: otalex.owl
*/
@org.junit.Test
//@org.junit.Test
public void testOntologyMedium() {
System.out.println("Testing Ontology: otalex.owl");
try{
Expand All @@ -154,7 +154,7 @@ public void testOntologyMedium() {
* Test if an ontology can be created from a URL.
* Test ontology: PROV-O
*/
@org.junit.Test
//@org.junit.Test
public void testOntologyFromURL() {
System.out.println("Testing Ontology: prov-o");
try{
Expand All @@ -178,7 +178,7 @@ public void testOntologyFromURL() {
* 3) Entities described with URIs locally
* The test uses an ontology which has 3 creators, each described with one of the methods above.
*/
@org.junit.Test
//@org.junit.Test
public void testAnnotationsInOntology() {
try {
String pathToOnto = "test" + File.separator + "example_annotated.owl";
Expand All @@ -203,7 +203,7 @@ public void testAnnotationsInOntology() {
* backwardCompatibility, prior version, source, see also, fundedBy,
* introduction, rdfxml serialization, version info
*/
@org.junit.Test
//@org.junit.Test
public void testAdvancedAnnotationsInOntology() {
try {
String pathToOnto = "test" + File.separator + "example_test.owl";
Expand Down Expand Up @@ -269,7 +269,7 @@ public void testAdvancedAnnotationsInOntology() {
* are being recognized.
* Issue 530 (https://github.com/dgarijo/Widoco/issues/530)
*/
@org.junit.Test
//@org.junit.Test
public void testIssue530() {
try {
String pathToOnto = "test" + File.separator + "medatatestont.ttl";
Expand All @@ -294,7 +294,7 @@ public void testIssue530() {
* the custom introduction annotation works correctly.
* Reference: https://github.com/dgarijo/Widoco/issues/658
*/
@org.junit.Test
//@org.junit.Test
public void testIssue658() {
try {
String pathToOnto = "test" + File.separator + "medatatestont.ttl";
Expand All @@ -321,7 +321,7 @@ public void testIssue658() {
* This is a test to see if a big ontology works (several MB)
* To be tested only on releases. IFC4_ADD1 ontology (that is why it is commented)
*/
// @org.junit.Test
// //@org.junit.Test
// public void testBigOntology() {
// System.out.println("Testing Ontology: IFC4_ADD1.ttl");
// try{
Expand All @@ -343,7 +343,7 @@ public void testIssue658() {
*
* TEST COMMENTED OUT BECAUSE IMPORTING THE ONTOLOGIES IS SLOW
@org.junit.Test
//@org.junit.Test
public void testOntologyInLanguage() {
System.out.println("Testing Ontology: geolinkeddata.owl");
try{
Expand Down

0 comments on commit 6a0ac61

Please sign in to comment.