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 */ 019/** 020 * 021 */ 022package org.dllearner.algorithms.qtl.heuristics; 023 024import org.aksw.jena_sparql_api.core.QueryExecutionFactory; 025import org.dllearner.algorithms.qtl.QueryTreeUtils; 026import org.dllearner.algorithms.qtl.datastructures.impl.EvaluatedRDFResourceTree; 027import org.dllearner.core.ComponentAnn; 028import org.dllearner.core.ComponentInitException; 029import org.dllearner.learningproblems.QueryTreeScore; 030 031import org.apache.jena.query.QueryExecution; 032import org.apache.jena.query.ResultSet; 033 034/** 035 * @author Lorenz Buehmann 036 * 037 */ 038@ComponentAnn(name = "QueryTreeHeuristicC", shortName = "qtree_heuristic_complex", version = 0.1) 039public class QueryTreeHeuristicComplex extends QueryTreeHeuristic { 040 041 042 private double resultSetSizePenalty = 0.0001; 043 044 private QueryExecutionFactory qef; 045 046 public QueryTreeHeuristicComplex(QueryExecutionFactory qef) { 047 this.qef = qef; 048 } 049 050 /* (non-Javadoc) 051 * @see org.dllearner.core.Component#init() 052 */ 053 @Override 054 public void init() throws ComponentInitException { 055 initialized = true; 056 } 057 058 @Override 059 public double getScore(EvaluatedRDFResourceTree tree) { 060 QueryTreeScore treeScore = tree.getTreeScore(); 061 062 // accuracy as baseline 063 double score = getAccuracy(tree); 064 065 // distance penalty 066 score -= treeScore.getDistancePenalty(); 067 068 // result set weight 069 int resultCount = getResultCount(tree); 070 071 return score; 072 } 073 074 private int getResultCount(EvaluatedRDFResourceTree evaluatedQueryTree) { 075 int cnt = 0; 076 String query = QueryTreeUtils.toSPARQLQueryString(evaluatedQueryTree.getTree()); 077 QueryExecution qe = qef.createQueryExecution(query); 078 ResultSet rs = qe.execSelect(); 079 080 while (rs.hasNext()) { 081 rs.next(); 082 cnt++; 083 } 084 qe.close(); 085 return cnt; 086 } 087 088}