-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- reads the files - creates class declarations
- Loading branch information
1 parent
4fe1307
commit f560d19
Showing
8 changed files
with
228 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 47 additions & 2 deletions
49
tools/owl/src/main/java/org/eurocris/cerif2/owl/Model.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
tools/owl/src/main/java/org/eurocris/cerif2/owl/ParseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
85
tools/owl/src/main/java/org/eurocris/cerif2/owl/Section.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters