Switch to unified view

a b/src/main/java/org/sba_research/timbus/kb/App.java
1
/**
2
 * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
3
 * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
4
 * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
5
 * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
8
 * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
11
 * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
12
 * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
13
 * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
14
 * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
15
 * License or out of the use or inability to use the Work.
16
 * See the License for the specific language governing permissions and limitation under the License.
17
 */
18
package org.sba_research.timbus.kb;
19
20
import org.json.simple.parser.ParseException;
21
import org.sba_research.timbus.kb.importer.DataImporter;
22
import org.sba_research.timbus.kb.importer.DataImporterException;
23
import org.sba_research.timbus.kb.importer.FreebaseImporter;
24
import org.sba_research.timbus.kb.importer.PronomImporter;
25
import org.sbaresearch.owl.OwlApiFacade;
26
import org.sbaresearch.owl.OwlElementNotFoundException;
27
import org.semanticweb.owlapi.apibinding.OWLManager;
28
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
29
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
30
31
import java.io.File;
32
import java.io.IOException;
33
import java.util.ArrayList;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.logging.Logger;
38
39
public class App {
40
41
    private static final Logger LOG = Logger.getLogger(App.class.getName());
42
    private static final String kb = "http://www.semanticweb.org/tmp/ontologies/2013/6/untitled-ontology-20";
43
    private static final String owlPath = "ontologies/";
44
    private static final String defaultInputOwlFileName = owlPath + "kb.in.owl";
45
    private static final String defaultOutputOwlFileName = "kb.out.owl";
46
    private static final String defaultSchemaFileName = owlPath + "kb_tool_strategies.owl";
47
48
    public static void main(String[] args) throws IOException, ParseException, OWLOntologyCreationException, OWLOntologyStorageException, OwlElementNotFoundException, DataImporterException {
49
        OwlApiFacade owl = new OwlApiFacade(OWLManager.createOWLOntologyManager(), OWLManager.getOWLDataFactory());
50
        LOG.info("Loading OWL: " + defaultInputOwlFileName);
51
        loadOwl(owl, defaultInputOwlFileName);
52
        LOG.info("Importing data:");
53
        for (DataImporter importer : createImportChain()) {
54
            LOG.info("... " + importer.getClass().getSimpleName());
55
            importer.populate(owl, kb);
56
        }
57
        LOG.info("Saving OWL: " + defaultOutputOwlFileName);
58
        owl.save(new File(defaultOutputOwlFileName).getAbsolutePath());
59
        LOG.info("Validating OWL.");
60
        verifyValidity();
61
    }
62
63
    private static List<DataImporter> createImportChain() {
64
        return new ArrayList<DataImporter>() {{
65
                add(new FreebaseImporter());
66
                add(new PronomImporter());
67
            }};
68
    }
69
70
    private static void verifyValidity() throws OWLOntologyCreationException {
71
        OwlApiFacade owl = new OwlApiFacade(OWLManager.createOWLOntologyManager(), OWLManager.getOWLDataFactory());
72
        loadOwl(owl, defaultOutputOwlFileName);
73
    }
74
75
    private static void loadOwl(OwlApiFacade owl, String owlFileName) throws OWLOntologyCreationException {
76
        Map<String, String> mapping = new HashMap<String, String>() {{
77
            put(kb, new File(defaultSchemaFileName).toURI().toString());
78
        }};
79
        owl.load(new File(owlFileName).toURI().toString(), mapping);
80
    }
81
82
}
83