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.algorithms.qtl.operations; 020 021import java.util.ArrayList; 022 023import org.dllearner.algorithms.qtl.QueryTreeUtils; 024import org.dllearner.algorithms.qtl.datastructures.impl.RDFResourceTree; 025 026import org.apache.jena.graph.Node; 027import org.apache.jena.vocabulary.RDF; 028 029public class Generalisation { 030 031 public RDFResourceTree generalise(RDFResourceTree queryTree){ 032 RDFResourceTree copy = new RDFResourceTree(queryTree); 033 034 copy.setData(RDFResourceTree.DEFAULT_VAR_NODE); 035 036 pruneTree(copy, 0.5); 037 retainTypeEdges(copy); 038 039 return copy; 040 } 041 042 private void replaceAllLeafs(RDFResourceTree queryTree){ 043 for(RDFResourceTree leaf : QueryTreeUtils.getLeafs(queryTree)){ 044 leaf.setData(RDFResourceTree.DEFAULT_VAR_NODE); 045 } 046 } 047 048 private void pruneTree(RDFResourceTree tree, double limit){ 049 int childCountBefore = tree.getNumberOfChildren(); 050 for(RDFResourceTree child : tree.getChildren()){ 051 tree.removeChild(child); 052 if((double)tree.getNumberOfChildren()/childCountBefore <= 0.5){ 053 break; 054 } 055 } 056 } 057 058 private void retainTypeEdges(RDFResourceTree tree){ 059 for(Node edge : tree.getEdges()) { 060 if(!edge.equals(RDF.type.asNode())) { 061 for(RDFResourceTree child : new ArrayList<>(tree.getChildren(edge))){ 062 tree.removeChild(child, edge); 063 } 064 } 065 } 066 } 067}