Skip to content

Commit

Permalink
tools: working prototype
Browse files Browse the repository at this point in the history
- reads the files
- creates class declarations
  • Loading branch information
jdvorak001 committed Oct 10, 2023
1 parent 4fe1307 commit f560d19
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 56 deletions.
17 changes: 11 additions & 6 deletions tools/owl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@
</properties>

<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-distribution</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-all</artifactId>
<version>0.64.8</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
49 changes: 47 additions & 2 deletions tools/owl/src/main/java/org/eurocris/cerif2/owl/Model.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
package org.eurocris.cerif2.owl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.text.CaseUtils;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Model {

protected final Logger log = LoggerFactory.getLogger( getClass().getName() );

private final List<Section> datatypes = new ArrayList<>();

private final List<Section> entities = new ArrayList<>();

public static final IRI IRI_BASE = IRI.create( "https://w3id.org/cerif2/" );

private final IRI baseIRI;

private final OWLOntology ont;

private final OWLOntologyManager man = OWLManager.createOWLOntologyManager();

private final OWLDataFactory dataFactory = man.getOWLDataFactory();

public Model( final String moduleName ) throws OWLOntologyCreationException {
super();
this.baseIRI = IRI_BASE.resolve( moduleName );
log.info( "Starting model for module '" + moduleName + "', IRI " + baseIRI );
this.ont = man.createOntology( baseIRI );
}

public void readInDatatypeFile( final StructuredFile file ) {
log.info( "Parsing {1} as a datatype", file );
final Section mainSection = file.getMainSection();
datatypes.add( mainSection );
}

public void readInEntityFile( final StructuredFile file ) {
log.info( "Parsing {1} as an entity", file );
final Section mainSection = file.getMainSection();
entities.add( mainSection );

final String owlClassName = CaseUtils.toCamelCase( mainSection.getTitle(), true, ' ' );
final IRI classIRI = IRI.create( baseIRI.toString() + "/", owlClassName );
log.info( "Declaring class '" + owlClassName + "', IRI " + classIRI );
final OWLClass owlClass = dataFactory.getOWLClass( classIRI );
ont.add( dataFactory.getOWLDeclarationAxiom( owlClass ) );
}

public void save() throws OWLOntologyStorageException {
ont.saveOntology( System.out );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.eurocris.cerif2.owl;

import com.vladsch.flexmark.util.ast.Node;

public class ParseException extends Exception {

private static final long serialVersionUID = 2761622547753887110L;

private final Node node;

public ParseException( final String message, final Node node ) {
super( message );
this.node = node;
}

@Override
public String getMessage() {
final StringBuilder sb = new StringBuilder( super.getMessage() );
sb.append( " on " );
node.getAstExtra( sb );
return sb.toString();
}

}
85 changes: 85 additions & 0 deletions tools/owl/src/main/java/org/eurocris/cerif2/owl/Section.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,90 @@
package org.eurocris.cerif2.owl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.vladsch.flexmark.ast.Heading;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.collection.iteration.ReversiblePeekingIterator;

public class Section {

private final Heading heading;

private final String title;

private final List<Node> contents;

private final Map<String, Section> subsectionsMap = new HashMap<>();

private final List<Section> subsectionsList = new LinkedList<>();

public static Section create( final ReversiblePeekingIterator<Node> iterator ) throws ParseException {
if ( iterator.hasNext() ) {
final Node firstNode = iterator.next();
if ( firstNode instanceof Heading ) {
final List<Node> contents = new ArrayList<Node>();
while ( iterator.hasNext() ) {
final Node nextNode = iterator.peek();
if ( nextNode instanceof Heading ) break;
contents.add( iterator.next() );
}
final Section section = new Section( (Heading) firstNode, contents );

while ( iterator.hasNext() ) {
final Node nextNode = iterator.peek();
final Heading nextHeading = (Heading) nextNode;
if ( nextHeading.getLevel() <= section.getLevel() ) break;
final Section subsection = create( iterator );
section.addSubsection( subsection );
}

return section;
} else {
throw new ParseException( "Heading expected", firstNode );
}
} else {
return null;
}
}

private Section( final Heading heading, final List<Node> contents ) {
this.heading = heading;
this.title = heading.getText().toString();
this.contents = new ArrayList<>( contents );
}

private void addSubsection( final Section subsection ) {
subsectionsList.add( subsection );
subsectionsMap.put( subsection.getTitle(), subsection );
}

public Heading getHeading() {
return heading;
}

public String getTitle() {
return title;
}

public int getLevel() {
return heading.getLevel();
}

public List<Node> getContents() {
return Collections.unmodifiableList( contents );
}

public List<Section> getSubsections() {
return Collections.unmodifiableList( subsectionsList );
}

public Section getSubsectionByTitle( final String title ) {
return subsectionsMap.get( title );
}

}
64 changes: 38 additions & 26 deletions tools/owl/src/main/java/org/eurocris/cerif2/owl/StructuredFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,58 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Arrays;

public class StructuredFile implements Iterable<Section> {
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private final Path path;

private final String title;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.data.MutableDataSet;

public class StructuredFile {

private final Map<String, Section> sectionsMap = new HashMap<>();;
private static final MutableDataSet options = new MutableDataSet();
static {
// uncomment to set optional extensions
options.set( Parser.EXTENSIONS, Arrays.asList( TablesExtension.create(), StrikethroughExtension.create() ) );

public StructuredFile( final Path path ) throws IOException {
this.path = path;
final Iterator<String> i = Files.lines( path ).iterator();
if (! i.hasNext() ) throw new IllegalArgumentException( "Empty file" );
final String line1 = i.next();
if (! line1.startsWith( "# " ) ) throw new IllegalArgumentException( "First line does not start with # and a space" );
this.title = line1.substring( 2 );
if (! i.hasNext() ) throw new IllegalArgumentException( "File contained just one line" );
final String line2 = i.next();
if (! line2.isEmpty() ) throw new IllegalArgumentException( "The second line is not empty" );
readInSections( i, sectionsMap );
// uncomment to convert soft-breaks to hard breaks
//options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");
}

private static final Parser parser = Parser.builder(options).build();

protected final Logger log = LoggerFactory.getLogger( getClass().getName() );

private static void readInSections( final Iterator<String> i, final Map<String, Section> sectionsMap2 ) {
// TODO
private final Path path;

private final Section section;

public StructuredFile( final Path path ) throws IOException, ParseException {
this.path = path;
final Document document = parser.parseReader( Files.newBufferedReader( path ) );
this.section = Section.create( document.getChildIterator() );
if ( section.getLevel() != 1 ) {
throw new ParseException( "File not starting with heading of level 1", document.getFirstChild() );
}

log.debug( "Reading " + path.toString() + " with title \"" + section.getTitle() + "\"" );
}

public Path getPath() {
return path;
}

public String getTitle() {
return title;
public Section getMainSection() {
return section;
}

@Override
public Iterator<Section> iterator() {
return sectionsMap.values().iterator();
public String toString() {
return path.toString() + ", title " + section.getTitle();
}

}
41 changes: 21 additions & 20 deletions tools/owl/src/main/java/org/eurocris/cerif2/owl/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,47 @@
import java.nio.file.Files;
import java.nio.file.Path;

import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Tools {

protected final static Logger log = LoggerFactory.getLogger( Tools.class.getName() );
protected final static Logger log = LoggerFactory.getLogger( Tools.class );

public Tools() {
}

public static void main( final String[] args ) {
final Tools tools = new Tools();
try {
final Model model = tools.readIn( args );
} catch ( final IOException e ) {
for ( final String dir : args ) {
tools.readInAndProcess( dir );
}
} catch ( final IOException | ParseException | OWLOntologyStorageException | OWLOntologyCreationException e ) {
log.error( "When processing", e );
}
}

private Model readIn( final String[] dirs ) throws IOException {
final Model model = new Model();
for ( final String dir : dirs ) {
final File moduleBaseDir = new File( dir );
if ( moduleBaseDir.isDirectory() ) {
try ( final DirectoryStream<Path> datatypes = Files.newDirectoryStream( moduleBaseDir.toPath().resolve( "datatypes" ), "*.md" ) ) {
for ( final Path datatypeFilePath : datatypes ) {
model.readInDatatypeFile( new StructuredFile( datatypeFilePath ) );
}
private void readInAndProcess( final String dir ) throws IOException, ParseException, OWLOntologyCreationException, OWLOntologyStorageException {
final File moduleBaseDir = new File( dir );
final Model model = new Model( moduleBaseDir.getName() );
if ( moduleBaseDir.isDirectory() ) {
try ( final DirectoryStream<Path> datatypes = Files.newDirectoryStream( moduleBaseDir.toPath().resolve( "datatypes" ), "*.md" ) ) {
for ( final Path datatypeFilePath : datatypes ) {
model.readInDatatypeFile( new StructuredFile( datatypeFilePath ) );
}
try ( final DirectoryStream<Path> entities = Files.newDirectoryStream( moduleBaseDir.toPath().resolve( "entities" ), "*.md" ) ) {
for ( final Path entityFilePath : entities ) {
model.readInEntityFile( new StructuredFile( entityFilePath ) );
}
}
try ( final DirectoryStream<Path> entities = Files.newDirectoryStream( moduleBaseDir.toPath().resolve( "entities" ), "*.md" ) ) {
for ( final Path entityFilePath : entities ) {
model.readInEntityFile( new StructuredFile( entityFilePath ) );
}
} else {
throw new IllegalArgumentException( dir + " does not resolve to a directory" );
}
} else {
throw new IllegalArgumentException( dir + " does not resolve to a directory" );
}
log.info( "Model: {1}", model );
return model;
model.save();
}

}
Empty file.
4 changes: 2 additions & 2 deletions tools/owl/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder name="enc" class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<encoder name="enc">
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
Expand All @@ -12,6 +12,6 @@
<appender-ref ref="STDOUT" />
</root>

<logger name="org.eurocris" level="debug" />
<logger name="org.eurocris" level="debug"/>

</configuration>

0 comments on commit f560d19

Please sign in to comment.