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