Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added konclude_test.py #231

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions .github/workflows/evaluation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,38 @@ jobs:
path: ./build/files/oops.xml

consistency:
name: HermiT
name: Konclude
runs-on: ubuntu-latest
needs: compile

steps:
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1

- uses: actions/download-artifact@v1
- name: Setup Java
uses: actions/setup-java@v3
with:
name: SOMA OWL
path: ./build/owl/current
distribution: 'temurin'
java-version: '18'
cache: 'maven'

- name: Download DUL
run: wget http://www.ease-crc.org/ont/DUL.owl -O ./build/DUL.owl
- name: Compile
run: |
mvn spring-boot:run -f scripts/java/pom.xml \
-Dspring-boot.run.arguments="--versionInfo=current \
--propertyListPath=$PWD/properties"

- name: HermiT
uses: ./.github/actions/hermit
with:
args: ./build/DUL.owl ./build/SOMA-HOME.owl
- name: Konclude
run: docker run -v $PWD/build:/data --rm konclude/konclude classify -i /data/SOMA-HOME.owl -o /data/konclude.output

- name: Print HermiT output
run: cat ./hermit.output
- name: Print Konclude output
run: cat ./build/konclude.output
if: always()

- name: Evaluate HermiT output
- name: Evaluate Konclude output
if: success()
uses: docker://python:3.7.6-slim
with:
args: python ./scripts/hermit_test.py -o ./owl/hermit.html ./hermit.output
args: python ./scripts/konclude_test.py -p $PWD/properties/SOMA-HOM_objectProperties -k ./.github/actions/Konclude -s ./build/SOMA-HOME.owl -o ./owl/konclude.html ./build/konclude.output

6 changes: 6 additions & 0 deletions scripts/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- JGraphT -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.5.1</version>
</dependency>

</dependencies>

Expand Down
6 changes: 5 additions & 1 deletion scripts/java/src/main/java/main/CIRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public class CIRunner implements CommandLineRunner {
@Autowired
private SubclassNothingRewriter gciRewriter;

@Autowired
private PropertyListExporter propertyListExporter;


@Override
public void run(final String... args) throws Exception {
final CIRunnable[] toRun = {gciRewriter, isDefinedInAdder, versionInfoAdder, collapser, ontologySaver};
final CIRunnable[] toRun = {gciRewriter, isDefinedInAdder, versionInfoAdder, collapser, propertyListExporter,
ontologySaver};
for (final var next : toRun) {
next.run();
}
Expand Down
30 changes: 30 additions & 0 deletions scripts/java/src/main/java/main/OntologyManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main;

import main.config.OntologyConfig;
import org.jgrapht.Graph;
import org.jgrapht.alg.connectivity.GabowStrongConnectivityInspector;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.protege.xmlcatalog.owlapi.XMLCatalogIRIMapper;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
Expand All @@ -16,6 +20,7 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

/**
* Class to manage the resources that are associated with an {@link OWLOntology}.
Expand Down Expand Up @@ -75,4 +80,29 @@ private void loadOntology(final File ontologyFile) throws OWLOntologyCreationExc
public OWLOntologyManager getOntologyManager() {
return ontologyManager;
}

/**
* @return A {@link Stream} of a subset of the managed {@link OWLOntology}s such that their imports include all
* managed ontologies
*/
public Stream<OWLOntology> mainOntologies() {
// condensate import structure to strongly connected components
final var condensation = new GabowStrongConnectivityInspector<>(graphOfImportStructure()).getCondensation();

// get roots of condensation
//noinspection OptionalGetWithoutIsPresent
return condensation.vertexSet().stream().filter(key -> condensation.incomingEdgesOf(key).isEmpty())
.map(next -> next.vertexSet().stream().findAny().get());
}

/**
* @return A {@link Graph} with all managed {@link OWLOntology}s as vertices and edges from o1 to o2 if o1 imports
* o2
*/
public Graph<OWLOntology, DefaultEdge> graphOfImportStructure() {
final DefaultDirectedGraph<OWLOntology, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
ontologyManager.ontologies().forEach(graph::addVertex);
ontologyManager.ontologies().forEach(next -> next.getDirectImports().forEach(imp -> graph.addEdge(next, imp)));
return graph;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main.ci_runners;

import main.OntologyManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.parameters.Imports;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.stream.Collectors;

@Component
@Lazy
public class PropertyListExporter implements CIRunnable {

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

private final Path path;
private final OntologyManager ontologyManager;

@Autowired
public PropertyListExporter(final OntologyManager ontologyManager,
@Value("${propertyListPath:#{null}}") final Path path) {
this.path = path;
this.ontologyManager = ontologyManager;
}

@Override
public void run() throws IOException {
if (path == null) {
logger.info("CL argument 'propertyListPath' not given. Skipping export of properties.");
return;
}
for (final OWLOntology owlOntology : ontologyManager.mainOntologies().collect(Collectors.toSet())) {
exportProperties(owlOntology);
}
}

private void exportProperties(final OWLOntology ontology) throws IOException {
@SuppressWarnings("OptionalGetWithoutIsPresent") String fileName = ontology.getOntologyID().getOntologyIRI()
.get().getShortForm();
fileName = fileName.substring(0, fileName.length() - 5);
if (!path.toFile().exists() && !path.toFile().mkdirs()) {
throw new IOException("Could not create dir " + path.toAbsolutePath());
}
logger.info("Writing object- and dataProperties of {} to {}_[object,data]Properties", fileName,
path.resolve(fileName).toAbsolutePath());
Files.write(path.resolve(fileName + "_objectProperties"),
ontology.objectPropertiesInSignature(Imports.INCLUDED)
.map(next -> (CharSequence) next.toString())::iterator,
StandardOpenOption.CREATE);
Files.write(path.resolve(fileName + "_dataProperties"), ontology.dataPropertiesInSignature(Imports.INCLUDED)
.map(next -> (CharSequence) next.toString())::iterator,
StandardOpenOption.CREATE);
}
}
162 changes: 162 additions & 0 deletions scripts/konclude_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/usr/bin/env python
import argparse
import re
import os
import sys
from pathlib import Path


OWL_NOTHING = 'http://www.w3.org/2002/07/owl#Nothing'
BOTTOM_OBJECT_PROPERTY = 'http://www.w3.org/2002/07/owl#bottomObjectProperty'

## IRI = '<(.*?)>'

## EQUIVALENT_CLASSES = r'^EquivalentClasses\( (.*) \)$'
## OBJECT_INVERSE_OF = r'ObjectInverseOf\( ' + IRI + r' \)'
## EQUIVALENT_OBJECT_PROPERTIES = r'^EquivalentObjectProperties\( (.*) \)$'

def _parseHomebrew(fileText):
inEq = False
nullClasses = []
aux = []
iriPLen = len("<Class IRI=\"")
for l in fileText.splitlines():
l = l.strip()
if inEq:
if "</EquivalentClasses>" == l:
inEq = False
if OWL_NOTHING in aux:
nullClasses = nullClasses + aux
aux = []
else:
if l.startswith("<Class IRI=\""):
aux.append(l[iriPLen:-3])
else:
if "<EquivalentClasses>" == l:
inEq = True
nullClasses = list(set(nullClasses))
if OWL_NOTHING in nullClasses:
nullClasses.remove(OWL_NOTHING)
return nullClasses

def makeObjectPropertyQueryFile(objProps, somaPath):
queryMap = {}
queryOwl = './SOMA_QUERY.owl'
with open(queryOwl, 'w') as outfile:
outfile.write('Prefix(:=<http://www.ease-crc.org/ont/SOMA_QUERY.owl#>)\n')
outfile.write('Prefix(dul:=<http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>)\n')
outfile.write('Prefix(owl:=<http://www.w3.org/2002/07/owl#>)\n')
outfile.write('Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)\n')
outfile.write('Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)\n')
outfile.write('Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)\n')
outfile.write('Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)\n')
outfile.write('Prefix(soma:=<http://www.ease-crc.org/ont/SOMA.owl#>)\n\n\n')
outfile.write('Ontology(<http://www.ease-crc.org/ont/SOMA_QUERY.owl>\n')
outfile.write('Import(<file://%s>)\n' % somaPath)
for k,op in enumerate(objProps):
cName = 'http://www.ease-crc.org/ont/SOMA_QUERY.owl#QUERY%d' % k
queryMap[cName] = op
outfile.write('EquivalentClasses(<%s> ObjectSomeValuesFrom(<%s> owl:Thing))\n' % (cName, op))
outfile.write(')\n')
return queryMap, queryOwl

def runKonclude(queryOwl, koncludePath):
outName = queryOwl[:queryOwl.rfind('.')] + '_OUT.html'
os.system("%s classification -i %s -o %s >/dev/null 2>&1" % (koncludePath, queryOwl, outName))
return _parseHomebrew(open(outName).read())

def main():
parser = argparse.ArgumentParser()
parser.add_argument('konclude_output', help='Konclude output file', type=str)
parser.add_argument('-k', '--koncludePath', help='path to Konclude executable', default='./.github/actions/Konclude', type=str)
parser.add_argument('-s', '--somaPath', help='path to merged SOMA owl', default='./build/SOMA-HOME.owl', type=str)
parser.add_argument('-p', '--propertyListPath', help='object property list file', default='', type=str)
parser.add_argument('-o', '--output', help='output file', default='konclude.html', type=str)
args = parser.parse_args()

nullClasses = _parseHomebrew(open(args.konclude_output).read())
results = {
'EquivalentClasses': [nullClasses],
'SubClassOf': [],
'EquivalentObjectProperties': [],
'SubObjectPropertyOf': [],
'Error': [],
}

objProps = []
if '' != args.propertyListPath:
objProps = [x.strip().strip('<>') for x in open(args.propertyListPath).read().splitlines() if x.strip()]
if objProps:
query2objectProperty, queryOwl = makeObjectPropertyQueryFile(objProps, args.somaPath)
nullQueries = runKonclude(queryOwl, args.koncludePath)
for n in nullQueries:
if n in query2objectProperty:
results['EquivalentObjectProperties'].append(query2objectProperty[n])

output = format_output(results, format=args.output[args.output.rfind('.'):])
with open(args.output, 'w') as outfile:
_ = outfile.write(output)
print(format_output(results))

# check if there are problems, return non-zero exit code if so
if len(nullClasses) != 0:
sys.exit(1)


def as_html(titles, results):
template = '<h2>{title}</h2>\n{issues}'
outer = '<section>\n<p><h3>Set</h3>{}</p>\n</section>\n'
outeritem = '</p><p><h3>Set</h3>'
inner = '<ul>\n<li>{}</li>\n</ul>\n'
inneritem = '</li>\n<li>'
sections = []
for k in sorted(results.keys()):
sets = results[k]
if (not sets) or (not sets[0]):
issues = '<p>No issues detected.</p>'
else:
inners = list(inner.format(inneritem.join(sorted(s))) for s in sets)
issues = outer.format(outeritem.join(inners))
section = template.format(title=titles[k], issues=issues)
sections.append(section)
sections = '\n'.join(sections)
return "<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<title>Konclude results</title>\n\t\t<meta charset='utf-8' />\n\t</head>\n\t<body>\n\t\t<h1>Konclude results</h1>\n\t\t\t" + sections + "\n\t</body>\n</html>"


def as_text(titles, results):
template = '\n## {title}\n\n{issues}'
outer = '* {}'
outeritem = '\n* '
inner = '- {}\n'
inneritem = '\n - '
sections = []
for k in sorted(results.keys()):
sets = results[k]
if (not sets) or (not sets[0]):
issues = 'No issues detected.\n'
else:
inners = list(inner.format(inneritem.join(sorted(s))) for s in sets)
issues = outer.format(outeritem.join(inners))
section = template.format(title=titles[k], issues=issues)
sections.append(section)
sections = '\n'.join(sections)
return '# Konclude results\n\n' + sections


def format_output(results, format='.md'):
titles = {
'EquivalentClasses': 'Classes equivalent to ' + OWL_NOTHING,
'SubClassOf': 'Subclasses of ' + OWL_NOTHING,
'EquivalentObjectProperties': 'Object properties equivalent to ' + BOTTOM_OBJECT_PROPERTY,
'SubObjectPropertyOf': 'Subproperties of ' + BOTTOM_OBJECT_PROPERTY,
'Error': 'Unhandled subsumptions',
}

if format == '.html':
return as_html(titles, results)
return as_text(titles, results)


if __name__ == '__main__':
main()