001    /***************************************************************************/
002    /*  Copyright (C) 2010-2011, Sebastian Hellmann                            */
003    /*  Note: If you need parts of NLP2RDF in another licence due to licence   */
004    /*  incompatibility, please mail hellmann@informatik.uni-leipzig.de        */
005    /*                                                                         */
006    /*  This file is part of NLP2RDF.                                          */
007    /*                                                                         */
008    /*  NLP2RDF is free software; you can redistribute it and/or modify        */
009    /*  it under the terms of the GNU General Public License as published by   */
010    /*  the Free Software Foundation; either version 3 of the License, or      */
011    /*  (at your option) any later version.                                    */
012    /*                                                                         */
013    /*  NLP2RDF is distributed in the hope that it will be useful,             */
014    /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
015    /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           */
016    /*  GNU General Public License for more details.                           */
017    /*                                                                         */
018    /*  You should have received a copy of the GNU General Public License      */
019    /*  along with this program. If not, see <http://www.gnu.org/licenses/>.   */
020    /***************************************************************************/
021    
022    package org.nlp2rdf;
023    
024    import com.hp.hpl.jena.ontology.OntClass;
025    import com.hp.hpl.jena.ontology.OntModel;
026    import com.hp.hpl.jena.ontology.OntModelSpec;
027    import com.hp.hpl.jena.rdf.model.ModelFactory;
028    import com.hp.hpl.jena.vocabulary.OWL;
029    import org.nlp2rdf.ontology.olia.OLiAManager;
030    import org.nlp2rdf.ontology.olia.OLiAOntology;
031    import org.slf4j.Logger;
032    import org.slf4j.LoggerFactory;
033    
034    import java.io.File;
035    import java.io.FileWriter;
036    import java.io.IOException;
037    import java.io.UnsupportedEncodingException;
038    import java.net.URLEncoder;
039    import java.util.ArrayList;
040    import java.util.Iterator;
041    import java.util.List;
042    import java.util.Set;
043    
044    /**
045     * this class should actually be in utilities, but this would cause a cyclic dependency to generated classes
046     */
047    public class MappingGenerator {
048        private static Logger log = LoggerFactory.getLogger(MappingGenerator.class);
049    
050        public static String hasTagURI = "http://purl.org/olia/system.owl#hasTag";
051    
052        public static void main(String[] args) throws UnsupportedEncodingException, IOException {
053            OLiAManager om = new OLiAManager();
054            List<OLiAOntology> l = new ArrayList<OLiAOntology>();
055            l.add(om.getOLiAOntology("http://purl.org/olia/brown-link.rdf"));
056            l.add(om.getOLiAOntology("http://purl.org/olia/penn-link.rdf"));
057            l.add(om.getOLiAOntology("http://purl.org/olia/penn-syntax-link.rdf"));
058            l.add(om.getOLiAOntology("http://purl.org/olia/stanford.owl"));
059    
060    
061            String path = "oliaOntologies/";
062    
063            for (OLiAOntology o : l) {
064                String currentPath = path + URLEncoder.encode(o.getOntologyUrl(), "UTF-8");
065                new File(currentPath).mkdirs();
066                Set<String> s = o.getTagToIndividualMap().keySet();
067                for (String tag : s) {
068    
069                    OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
070                    String ind = o.getIndividualURIForTag(tag);
071                    if (ind == null) {
072                        log.info("tag not found (SKIPPING): " + tag);
073                        continue;
074                    }
075                    m.createIndividual(ind, OWL.Thing);
076    
077                    Set<String> classUris = o.getClassURIsForTag(tag);
078                    for (String classUri : classUris) {
079                        m.add(o.getHierarchy(classUri));
080                        for (Iterator<OntClass> it = m.listClasses(); it.hasNext(); ) {
081                            OntClass c = it.next();
082                            c.addRDFType(OWL.Class);
083                        }
084    
085                    }
086    
087                    String filepart = URLEncoder.encode(tag, "UTF-8");
088                    /*
089                    * Special Handling
090                    * */
091                    filepart = filepart.replaceAll("\\.", "_");
092                    String filename = currentPath + "/" + filepart;
093    
094    
095                    // rdf/xml
096                    FileWriter fstream = new FileWriter(filename+".rdf");
097                    m.write(fstream, "RDF/XML");
098                    fstream.close();
099                    //turtle
100                    fstream = new FileWriter(filename+".ttl");
101                    m.write(fstream, "N3");
102                    fstream.close();
103                }
104            }
105    
106    
107        }
108    
109    }