001    package org.nlp2rdf;
002    
003    import com.hp.hpl.jena.ontology.OntClass;
004    import com.hp.hpl.jena.ontology.OntModel;
005    import com.hp.hpl.jena.ontology.OntModelSpec;
006    import com.hp.hpl.jena.rdf.model.ModelFactory;
007    import com.hp.hpl.jena.vocabulary.OWL;
008    import org.nlp2rdf.ontology.olia.OLiAManager;
009    import org.nlp2rdf.ontology.olia.OLiAOntology;
010    import org.slf4j.Logger;
011    import org.slf4j.LoggerFactory;
012    
013    import java.io.File;
014    import java.io.FileWriter;
015    import java.io.IOException;
016    import java.io.UnsupportedEncodingException;
017    import java.net.URLEncoder;
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Set;
022    
023    /**
024     * this class should actually be in utilities, but this would cause a cyclic dependency to generated classes
025     */
026    public class MappingGenerator {
027        private static Logger log = LoggerFactory.getLogger(MappingGenerator.class);
028    
029        public static String hasTagURI = "http://purl.org/olia/system.owl#hasTag";
030    
031        public static void main(String[] args) throws UnsupportedEncodingException, IOException {
032            OLiAManager om = new OLiAManager();
033            List<OLiAOntology> l = new ArrayList<OLiAOntology>();
034            l.add(om.getOLiAOntology("http://purl.org/olia/brown-link.rdf"));
035            l.add(om.getOLiAOntology("http://purl.org/olia/penn-link.rdf"));
036            l.add(om.getOLiAOntology("http://purl.org/olia/penn-syntax-link.rdf"));
037            l.add(om.getOLiAOntology("http://purl.org/olia/stanford.owl"));
038    
039    
040            String path = "oliaOntologies/";
041    
042            for (OLiAOntology o : l) {
043                String currentPath = path + URLEncoder.encode(o.getOntologyUrl(), "UTF-8");
044                new File(currentPath).mkdirs();
045                Set<String> s = o.getTagToIndividualMap().keySet();
046                for (String tag : s) {
047    
048                    OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
049                    String ind = o.getIndividualURIForTag(tag);
050                    if (ind == null) {
051                        log.info("tag not found (SKIPPING): " + tag);
052                        continue;
053                    }
054                    m.createIndividual(ind, OWL.Thing);
055    
056                    Set<String> classUris = o.getClassURIsForTag(tag);
057                    for (String classUri : classUris) {
058                        m.add(o.getHierarchy(classUri));
059                        for (Iterator<OntClass> it = m.listClasses(); it.hasNext(); ) {
060                            OntClass c = it.next();
061                            c.addRDFType(OWL.Class);
062                        }
063    
064                    }
065    
066                    String filepart = URLEncoder.encode(tag, "UTF-8");
067                    /*
068                    * Special Handling
069                    * */
070                    filepart = filepart.replaceAll("\\.", "_");
071                    String filename = currentPath + "/" + filepart;
072    
073    
074                    // rdf/xml
075                    FileWriter fstream = new FileWriter(filename+".rdf");
076                    m.write(fstream, "RDF/XML");
077                    fstream.close();
078                    //turtle
079                    fstream = new FileWriter(filename+".ttl");
080                    m.write(fstream, "N3");
081                    fstream.close();
082                }
083            }
084    
085    
086        }
087    
088    }