Commit 8b800489 authored by R.W.Majeed's avatar R.W.Majeed

abbreviate namespace names in concept ids with prefix mappings.

parent 2c3f461d
......@@ -57,6 +57,9 @@ public class OntologyMojo extends AbstractMojo {
@Parameter(defaultValue="${project}", readonly=true, required=true)
MavenProject project;
@Parameter
Map<String, String> prefixes;
private void logWarning(String message){
getLog().warn(message);
}
......@@ -117,6 +120,17 @@ public class OntologyMojo extends AbstractMojo {
} catch (RepositoryException | IOException e) {
throw new MojoExecutionException("Unable to load ontology", e);
}
if( prefixes != null ){
String[] ns = new String[prefixes.size()];
String[] pr = new String[ns.length];
int i=0;
for( String key : prefixes.keySet() ){
pr[i] = key;
ns[i] = prefixes.get(key);
i++;
}
store.setNamespacePrefixes(ns, pr);
}
try {
SQLGenerator gen = new SQLGenerator(metaSQL, dataSQL, map, overwrite);
......
......@@ -3,6 +3,7 @@ package de.sekmi.histream.maven;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Properties;
import org.apache.maven.shared.model.fileset.FileSet;
......@@ -23,8 +24,9 @@ public class TestOntologyMojo {
mojo.destination = new File("target/i2b2-aktin");
mojo.properties = new Properties();
mojo.overwrite = true;
mojo.prefixes = Collections.singletonMap("a", "http://aktin.org/skos-cda#");
try( InputStream in = getClass().getResourceAsStream("/ontology.properties") ){
mojo.properties.load(in);
mojo.properties.load(in);
}
mojo.execute();
......
......@@ -49,7 +49,7 @@ public class ConceptImpl implements Concept {
}
public String toString(){
return res.toString();
return getID();
}
@Override
......@@ -135,8 +135,13 @@ public class ConceptImpl implements Concept {
@Override
public String getID() {
URI me = (URI)res;
// TODO: use better namespace prefix instead of hashcode
return Integer.toHexString(me.getNamespace().hashCode())+":"+me.getLocalName();
String prefix = store.getNamespacePrefix(me.getNamespace());
if( prefix == null ){
// backward compatibility. this breaks the Ontology.getConceptById call.
// TODO: use better namespace prefix instead of hashcode
prefix = Integer.toHexString(me.getNamespace().hashCode());
}
return prefix + ":" +me.getLocalName();
}
......
......@@ -32,6 +32,7 @@ public class HIStreamOntology {
DWH_ECMASCRIPT = f.createURI(DWH_NAMESPACE, "ECMAScript");
DWH_HAS_PART = f.createURI(DWH_NAMESPACE, "hasPart"); // TODO use correct names/URIs
DWH_IS_PART_OF = f.createURI(DWH_NAMESPACE, "isPartOf"); // XXX see above
// dwh:hasPart rdfs:subPropertyOf http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#hasPart
}
}
......@@ -49,7 +49,8 @@ public class Store implements Ontology, Plugin {
private Resource inferredContext;
// SKOS scheme to enforce unique notations
private Resource scheme;
private Map<String, String> namespaceShortener;
private String[] registeredPrefixes;
private String[] registeredNamespaces;
/// XXX namespace resolver
/**
......@@ -142,7 +143,6 @@ public class Store implements Ontology, Plugin {
return count;
}
private void initializeRepo(Iterable<File> files, Iterable<URL> urls, String baseURI) throws RepositoryException, IOException{
namespaceShortener = new HashMap<>();
repo = new SailRepository(new MemoryStore());
repo.initialize();
rc = repo.getConnection();
......@@ -260,7 +260,50 @@ public class Store implements Ontology, Plugin {
}
}
public void setNamespacePrefixes(String[] namespaces, String prefixes[]){
// TODO order namespaces by length
this.registeredNamespaces = namespaces;
this.registeredPrefixes = prefixes;
}
private String expandIdPrefixes(String id){
if( registeredPrefixes == null ){
// no prefixes to expand
return id;
}
for( int i=0; i<registeredPrefixes.length; i++ ){
String prefix = registeredPrefixes[i] + ":";
if( id.length() > prefix.length() && id.startsWith(prefix) ){
return registeredNamespaces[i] + id.substring(prefix.length());
}
}
return id;
}
String getNamespacePrefix(String namespace){
if( registeredNamespaces == null ){
return null;
}
for( int i=0; i<registeredNamespaces.length; i++ ){
if( namespace.equals(registeredNamespaces[i]) ){
return registeredPrefixes[i];
}
}
return null;
}
//
// private String shortenIdNamespaces(String id){
// if( registeredNamespaces == null ){
// // no namespaces to shorten
// return id;
// }
// for( int i=0; i<registeredNamespaces.length; i++ ){
// String ns = registeredNamespaces[i];
// if( id.length() > ns.length() && id.startsWith(ns) ){
// return registeredPrefixes[i] + ":" + id.substring(ns.length());
// }
// }
// return id;
// }
@Override
public ConceptImpl getConceptByNotation(String id) throws OntologyException {
......@@ -494,7 +537,10 @@ public class Store implements Ontology, Plugin {
}
}
@Override
public Concept getConceptById(String id) throws OntologyException {
public Concept getConceptById(String conceptId) throws OntologyException {
// lookup namespaces
String id = expandIdPrefixes(conceptId);
URI uri = repo.getValueFactory().createURI(id);
RepositoryResult<Statement> s = null;
ConceptImpl ci = null;
......
......@@ -23,6 +23,7 @@ public class OntologyTest {
public void setupOntology()throws Exception{
store = new Store(new File("examples/test-ontology.ttl"));
store.setConceptScheme(NS_PREFIX+"TestScheme");
store.setNamespacePrefixes(new String[]{"http://sekmi.de/histream/skos/tests#"}, new String[]{"test"});
}
@Test
......@@ -135,6 +136,16 @@ public class OntologyTest {
Assert.assertEquals(1,n.length);
Assert.assertEquals("other",n[0]);
}
@Test
public void verifyGetByShortIRI() throws OntologyException{
Concept c = store.getConceptById("test:OtherSub");
Assert.assertNotNull(c);
String[] n = c.getNotations();
Assert.assertNotNull(n);
Assert.assertEquals(1,n.length);
Assert.assertEquals("other",n[0]);
}
@After
public void closeOntology() throws IOException{
store.close();
......@@ -142,6 +153,7 @@ public class OntologyTest {
public static void main(String[] args) throws Exception{
try( Store store = new Store(new File("examples/test-ontology.ttl")) ){
store.setNamespacePrefixes(new String[]{"http://sekmi.de/histream/skos/tests#"}, new String[]{"test"});
store.printConceptHierarchy();
Concept c = store.getConceptByNotation("Type");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment