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.kb.manipulator; 020 021import java.util.ArrayList; 022import java.util.List; 023import java.util.SortedSet; 024 025import org.apache.log4j.Logger; 026import org.dllearner.kb.extraction.Node; 027import org.dllearner.kb.manipulator.Rule.Months; 028import org.dllearner.kb.manipulator.TypeFilterRule.Nodes; 029import org.dllearner.utilities.JamonMonitorLogger; 030import org.dllearner.utilities.datastructures.RDFNodeTuple; 031import org.dllearner.utilities.owl.OWLVocabulary; 032 033import com.jamonapi.Monitor; 034 035/** 036 * Used to manipulate retrieved tupels, identify blanknodes, etc. 037 * 038 * @author Sebastian Hellmann 039 * 040 */ 041public class Manipulator { 042 043 @SuppressWarnings("unused") 044 private static Logger logger = Logger.getLogger(Manipulator.class); 045 private List<Rule> rules = new ArrayList<>(); 046 047 private Manipulator() { 048 } 049 050 public Manipulator(List<Rule> rules) { 051 for (Rule rule : rules) { 052 addRule(rule); 053 } 054 } 055 056 /** 057 * this checks for consistency and manipulates the tuples, before they get 058 * triple 059 */ 060 public SortedSet<RDFNodeTuple> manipulate( Node node, SortedSet<RDFNodeTuple> tuples) { 061 Monitor m = JamonMonitorLogger.getTimeMonitor(Manipulator.class, "Time for Rules").start(); 062 //logger.warn("before: "+tuples.size()); 063 for (Rule rule : rules) { 064 tuples = rule.applyRule(node, tuples); 065 } 066 //logger.warn("after: "+tuples.size()); 067 m.stop(); 068 return tuples; 069 } 070 071 072 073 public static Manipulator getManipulatorByName(String predefinedManipulator) 074 { if (predefinedManipulator == null) { 075 return getDefaultManipulator(); 076 }else if (predefinedManipulator.equalsIgnoreCase("DBPEDIA-NAVIGATOR")) { 077 return getDBpediaNavigatorManipulator(); 078 079 } else if(predefinedManipulator.equalsIgnoreCase("DEFAULT") 080 ||predefinedManipulator.equalsIgnoreCase("STANDARD")){ 081 return getDefaultManipulator(); 082 } 083 else { 084 //QUALITY maybe not the best, 085 return getDefaultManipulator(); 086 } 087 } 088 089 public static Manipulator getDBpediaNavigatorManipulator(){ 090 Manipulator m = new Manipulator(); 091 //m.addRule(new DBPediaNavigatorCityLocatorRule(Months.JANUARY)); 092 //m.addRule(new DBpediaNavigatorOtherRule(Months.DECEMBER)); 093 m.addRule(new DBpediaNavigatorFilterRule(Months.JANUARY)); 094 return m; 095 } 096 097 public static Manipulator getDefaultManipulator(){ 098 Manipulator m = new Manipulator(); 099 m.addDefaultRules(Months.DECEMBER); 100 return m; 101 } 102 103 // 104// if(t.a.equals("http://www.holygoat.co.uk/owl/redwood/0.1/tags/taggedWithTag")) { 105// //hackGetLabel(t.b); 106// 107// } 108 109 // GovTrack hack 110 // => we convert a string literal to a URI 111 // => : introduce an option for converting literals for certain 112 // properties into URIs 113// String sp = "http://purl.org/dc/elements/1.1/subject"; 114// if(t.a.equals(sp)) { 115// System.out.println(t); 116// System.exit(0); 117// } 118 119 120 private void addDefaultRules(Months month){ 121 122 // addRule(new TypeFilterRule(month, OWLVocabulary.RDF_TYPE, OWLVocabulary.OWL_CLASS,ClassNode.class.getCanonicalName() )) ; 123 // addRule(new TypeFilterRule(month, OWLVocabulary.RDF_TYPE, OWLVocabulary.OWL_THING,InstanceNode.class.getCanonicalName() )) ; 124 // addRule(new TypeFilterRule(month, "", OWLVocabulary.OWL_CLASS, ClassNode.class.getCanonicalName()) ) ; 125 126 addRule(new TypeFilterRule(month, OWLVocabulary.RDF_TYPE, OWLVocabulary.OWL_THING, Nodes.INSTANCENODE )) ; 127 128 addRule(new TypeFilterRule(month, OWLVocabulary.RDF_TYPE, OWLVocabulary.OWL_CLASS, Nodes.CLASSNODE)) ; 129 addRule(new TypeFilterRule(month, "", OWLVocabulary.OWL_CLASS, Nodes.CLASSNODE) ) ; 130 addRule(new TypeFilterRule(month, "", OWLVocabulary.RDFS_CLASS, Nodes.CLASSNODE) ) ; 131 132 } 133 134 public synchronized void addRule(Rule newRule){ 135 rules.add(newRule); 136 List<Rule> l = new ArrayList<>(); 137 138 for (Months month : Rule.MONTHS) { 139 for (Rule rule : rules) { 140 if(rule.month.equals(month)) { 141 l.add(rule); 142 } 143 } 144 145 } 146 rules = l; 147 } 148 149 150 /*private String hackGetLabel(String resname){ 151 String query="" + 152 "SELECT ?o \n" + 153 "WHERE { \n" + 154 "<"+resname+"> "+ " <http://www.holygoat.co.uk/owl/redwood/0.1/tags/tagName> ?o " + 155 "}"; 156 157 System.out.println(query); 158 //http://dbtune.org/musicbrainz/sparql?query= 159 //SELECT ?o WHERE { <http://dbtune.org/musicbrainz/resource/tag/1391> <http://www.holygoat.co.uk/owl/redwood/0.1/tags/tagName> ?o } 160 SparqlQuery s=new SparqlQuery(query,SparqlEndpoint.EndpointMusicbrainz()); 161 ResultSet rs=s.send(); 162 while (rs.hasNext()){ 163 rs.nextBinding(); 164 } 165 //System.out.println("AAA"+s.getAsXMLString(s.send()) ); 166 return ""; 167 }*/ 168 169}