001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.core;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    /**
021     * This is a partial implementation of an AceWiki engine.
022     * 
023     * @author Tobias Kuhn
024     */
025    public abstract class AbstractAceWikiEngine implements AceWikiEngine {
026            
027            private static final String defaultEngineClassName
028                            = "ch.uzh.ifi.attempto.acewiki.aceowl.ACEOWLEngine";
029            
030            /**
031             * Creates the AceWiki engine for the given ontology.
032             * 
033             * @param ontology The ontology.
034             * @return The AceWiki engine.
035             */
036            static AceWikiEngine createLanguageEngine(Ontology ontology) {
037                    String n = ontology.getParameter("engine_class");
038                    Object loadedObj = null;
039                    ClassLoader classLoader = AbstractAceWikiEngine.class.getClassLoader();
040                    
041                    if (n != null && !n.equals("")) {
042                        try {
043                            loadedObj = classLoader.loadClass(n).newInstance();
044                        } catch (ClassNotFoundException ex) {
045                            ontology.log("Engine class not found: " + n);
046                            System.err.println("Engine class not found: " + n);
047                        } catch (Exception ex) {
048                            ontology.log("Failed to load engine object: " + n);
049                            System.err.println("Failed to load engine object: " + n);
050                            ex.printStackTrace();
051                        }
052                    }
053                    
054                    if (loadedObj == null) {
055                            n = defaultEngineClassName;
056                    ontology.log("Loading default engine: " + n);
057                        try {
058                            loadedObj = classLoader.loadClass(n).newInstance();
059                        } catch (Exception ex) {
060                            throw new RuntimeException("Failed to load default engine.", ex);
061                        }
062                    }
063                
064                    if (!(loadedObj instanceof AceWikiEngine)) {
065                            throw new RuntimeException("Engine object must be an instance of LanguageEngine");
066                    }
067                
068                    AceWikiEngine engine = (AceWikiEngine) loadedObj;
069                    engine.init(ontology);
070                    return engine;
071            }
072            
073            private List<OntologyExporter> exporters = new ArrayList<OntologyExporter>();
074            private String[] lexicalTypes = new String[] {};
075            private WordIndex wordIndex;
076            
077            public void init(Ontology ontology) {
078                    for (String language : getLanguages()) {
079                            getLanguageHandler(language).init(ontology);
080                    }
081                    getReasoner().init(ontology);
082            }
083    
084            /**
085             * Sets the lexical types, as defined by the respective ontology element types.
086             * 
087             * @param lexicalTypes The lexical types.
088             */
089            public void setLexicalTypes(String... lexicalTypes) {
090                    this.lexicalTypes = lexicalTypes;
091            }
092    
093            /**
094             * Adds an exporter to export the wiki content in a certain format.
095             * 
096             * @param exporter An ontology exporter.
097             */
098            public void addExporter(OntologyExporter exporter) {
099                    exporters.add(exporter);
100            }
101            
102            public List<OntologyExporter> getExporters() {
103                    return exporters;
104            }
105            
106            public String[] getLexicalTypes() {
107                    return lexicalTypes;
108            }
109            
110            public WordIndex getWordIndex() {
111                    if (wordIndex == null) {
112                            wordIndex = new DefaultWordIndex();
113                    }
114                    return wordIndex;
115            }
116    
117    }