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.aquisitors;
021    
022    import java.util.ArrayList;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.SortedSet;
027    import java.util.TreeSet;
028    
029    import org.dllearner.utilities.datastructures.RDFNodeTuple;
030    
031    import com.hp.hpl.jena.query.QuerySolution;
032    import com.hp.hpl.jena.query.ResultSetRewindable;
033    import com.hp.hpl.jena.rdf.model.RDFNode;
034    
035    public class BlankNodeCollector {
036    
037            private static int globalBNodeId = 0;
038            public static synchronized int getNextGlobalBNodeId(){
039                    int ret = globalBNodeId;
040                    globalBNodeId++;
041                    return ret;
042            }
043            
044            private static Map<Integer, SortedSet<RDFNodeTuple>> blankNodes = new HashMap<Integer, SortedSet<RDFNodeTuple>>();
045            
046            public static void addBlankNode(int id, RDFNodeTuple t){
047                    if(blankNodes.get(id)==null){
048                            blankNodes.put(id, new TreeSet<RDFNodeTuple>());
049                            }
050                    blankNodes.get(id).add(t);
051                    //System.out.println("added: "+id+" "+t);
052                    //System.out.println();
053            }
054            
055            public static  SortedSet<RDFNodeTuple> getBlankNode(int id){
056                    return blankNodes.get(id);
057            }
058            
059            public static  Map<Integer, SortedSet<RDFNodeTuple>> getBlankNodeMap(){
060                    return blankNodes;
061            }
062            
063            
064            /**
065             * @param rsw
066             * @param depth
067             * @return true if there are more blanknodes
068             */
069            public static boolean testResultSet(ResultSetRewindable rsw, int depth){
070                    List<String> vars = new ArrayList<String>();
071                    vars.add("o0");
072                    for (int i = 1; i <= depth; i++) {
073                            vars.add("o"+i);
074                    }
075                    
076                    while (rsw.hasNext()){
077                            QuerySolution q = rsw.nextSolution();
078                            if(!q.get("o0").isAnon()){
079                                    continue;
080                            }else{
081                                    if(!testOneQuerySolution(vars, q)){
082                                            rsw.reset();
083                                            return false;
084                                    }
085                    
086                            }
087                    }
088                    rsw.reset();
089                    return true;
090            }
091            
092            //true to stop expansion
093            private static boolean testOneQuerySolution(List<String> vars, QuerySolution q){
094                    
095    //              System.out.println(q);
096    //              System.out.println(vars);
097    //              for (String v : vars) {
098    //                      RDFNode n = q.get(v);
099    //                      if(n==null){
100    //                              System.out.println("returning true");
101    //                              return true;
102    //                      }
103    //                      
104    //              }
105                    
106                    for (String v : vars) {
107                            RDFNode n = q.get(v);
108                            if(!n.isAnon()){
109    //                              System.out.println(n);
110                                    return true;
111                            }
112                    }
113                    return false;
114            }
115            
116            public static String makeQuery(String uri, String predicate, int maxDepth){
117                    int currentDepth = 0;
118                    StringBuffer sq = new StringBuffer();
119                    String init = "SELECT * WHERE { "+
120                                    "<"+uri+"> <"+predicate+"> ?o"+currentDepth+". ";
121                    
122                    sq.append(init);
123                    
124                    for (; currentDepth < maxDepth; currentDepth++) {
125                            String currentO = "?o"+currentDepth;
126                            String nextP = "?p"+(currentDepth+1);
127                            String nextO = "?o"+(currentDepth+1);
128                            sq.append("  OPTIONAL { "+currentO+" "+nextP+" "+nextO+". }");
129                    }
130                    
131                    
132                    sq.append("  }");
133                    return sq.toString();
134            }
135            
136    }