001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.reasoning;
020
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.apache.jena.reasoner.ReasonerException;
026import org.apache.jena.reasoner.ReasonerFactory;
027import org.apache.jena.reasoner.rulesys.GenericRuleReasoner;
028import org.apache.jena.reasoner.rulesys.Rule;
029
030/**
031 * @author Lorenz Buehmann
032 *
033 */
034public class SKOSReasoner extends GenericRuleReasoner {
035        
036         /** Constant: used to indicate default SKOS processing level */
037    public static final String DEFAULT_RULES = "default";
038    
039    /** Constant: used to indicate full SKOS processing level */
040    public static final String FULL_RULES = "full";
041    
042    /** Constant: used to indicate minimal SKOS processing level */
043    public static final String SIMPLE_RULES = "simple";
044    
045    /** The location of the default SKOS rule definitions on the class path */
046    protected static final String RULE_FILE = "etc/skos.rules";
047    
048    /** The location of the full SKOS rule definitions on the class path */
049    protected static final String FULL_RULE_FILE = "etc/skos-full.rules";
050    
051    /** The location of the simple SKOS rule definitions on the class path */
052    protected static final String SIMPLE_RULE_FILE = "etc/skos-simple.rules";
053    
054    /** The cached rule sets, indexed by processing level */
055    protected static final Map<String, List<Rule>> ruleSets = new HashMap<>();
056    
057    /** The rule file names, indexed by processing level */
058    protected static final Map<String, String> ruleFiles = new HashMap<>();
059
060        /**
061         * @param factory the Jena reasoner factory
062         */
063        public SKOSReasoner(ReasonerFactory factory) {
064                super(loadRulesLevel(DEFAULT_RULES), factory);
065                setMode(HYBRID);
066                setTransitiveClosureCaching(true);
067        }
068        
069         /**
070     * Return the SKOS rule set, loading it in if necessary.
071     * @param level a string defining the processing level required
072     */
073    public static List<Rule> loadRulesLevel(String level) {
074        List<Rule> ruleSet = ruleSets.get(level);
075        if (ruleSet == null) {
076            String file = ruleFiles.get(level);
077            if (file == null) {
078                throw new ReasonerException("Illegal SKOS conformance level: " + level);
079            }
080            ruleSet = loadRules( file );
081            ruleSets.put(level, ruleSet);
082        }
083        return ruleSet;
084    }
085}