001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 * <p>
004 * This file is part of DL-Learner.
005 * <p>
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 * <p>
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 * <p>
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.algorithms.qtl.operations.lgg;
020
021import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree;
022
023import java.util.List;
024
025/**
026 * A generator of the Least General Generalization (LGG) for RDF query trees.
027 *
028 * @author Lorenz Bühmann
029 */
030public interface LGGGenerator {
031
032        /**
033         * Returns the Least General Generalization of two RDF resource trees.
034         *
035         * @param tree1 the first tree
036         * @param tree2 the second tree
037         * @return the Least General Generalization
038         */
039        default RDFResourceTree getLGG(RDFResourceTree tree1, RDFResourceTree tree2) {
040                return getLGG(tree1, tree2, false);
041        }
042
043        /**
044         * Returns the Least General Generalization of two RDF resource trees. It can be forced to learn filters
045         * on literal values.
046         *
047         * @param tree1        the first tree
048         * @param tree2        the second tree
049         * @param learnFilters whether to learn filters on literal values
050         * @return the Least General Generalization
051         */
052        RDFResourceTree getLGG(RDFResourceTree tree1, RDFResourceTree tree2, boolean learnFilters);
053
054        /**
055         * Returns the Least General Generalization of a list of RDF resource trees.
056         *
057         * @param trees the trees
058         * @return the Least General Generalization
059         */
060        default RDFResourceTree getLGG(List<RDFResourceTree> trees) {
061                return getLGG(trees, false);
062        }
063
064        /**
065         * Returns the Least General Generalization of a list of RDF resource trees. It can be forced to learn filters
066         * on literal values.
067         *
068         * @param trees        the trees
069         * @param learnFilters whether to learn filters on literal values
070         * @return the Least General Generalization
071         */
072        default RDFResourceTree getLGG(List<RDFResourceTree> trees, boolean learnFilters) {
073                if(trees.isEmpty()) {
074                        throw new RuntimeException("LGG computation for empty set of trees.");
075                }
076                // if there is only 1 tree return it
077                if (trees.size() == 1) {
078                        return trees.get(0);
079                }
080
081                RDFResourceTree lgg = trees.get(0);
082                for (int i = 1; i < trees.size(); i++) {
083                        lgg = getLGG(lgg, trees.get(i), learnFilters);
084                }
085                return lgg;
086        }
087}