001    package org.dllearner.kb.aquisitors;
002    
003    import java.util.ArrayList;
004    import java.util.HashMap;
005    import java.util.List;
006    import java.util.Map;
007    import java.util.SortedSet;
008    import java.util.TreeSet;
009    
010    import org.dllearner.utilities.datastructures.RDFNodeTuple;
011    
012    import com.hp.hpl.jena.query.QuerySolution;
013    import com.hp.hpl.jena.query.ResultSetRewindable;
014    
015    public class BlankNodeCollector {
016    
017            private static int globalBNodeId = 0;
018            public static synchronized int getNextGlobalBNodeId(){
019                    int ret = globalBNodeId;
020                    globalBNodeId++;
021                    return ret;
022            }
023            
024            private static Map<Integer, SortedSet<RDFNodeTuple>> blankNodes = new HashMap<Integer, SortedSet<RDFNodeTuple>>();
025            
026            public static void addBlankNode(int id, RDFNodeTuple t){
027                    if(blankNodes.get(id)==null){
028                            blankNodes.put(id, new TreeSet<RDFNodeTuple>());
029                            }
030                    blankNodes.get(id).add(t);
031                    //System.out.println("added: "+id+" "+t);
032                    //System.out.println();
033            }
034            
035            public static  SortedSet<RDFNodeTuple> getBlankNode(int id){
036                    return blankNodes.get(id);
037            }
038            
039            public static  Map<Integer, SortedSet<RDFNodeTuple>> getBlankNodeMap(){
040                    return blankNodes;
041            }
042            
043            
044            public static boolean testResultSet(ResultSetRewindable rsw, int depth){
045                    List<String> vars = new ArrayList<String>();
046                    vars.add("o0");
047                    for (int i = 1; i <= depth; i++) {
048                            vars.add("o"+i);
049                    }
050                    
051                    while (rsw.hasNext()){
052                            QuerySolution q = rsw.nextSolution();
053                            if(!q.get("o0").isAnon()){
054                                    continue;
055                            }else{
056                                    if(!testOneQuerySolution(vars, q)){
057                                            rsw.reset();
058                                            return false;
059                                    }
060                    
061                            }
062                    }
063                    rsw.reset();
064                    return true;
065            }
066            private static boolean testOneQuerySolution(List<String> vars, QuerySolution q){
067                    for (String v : vars) {
068                            if(!q.get(v).isAnon()){
069                                    return true;
070                            }
071                    }
072                    return false;
073            }
074            
075            public static String makeQuery(String uri, String predicate, int maxDepth){
076                    int currentDepth = 0;
077                    StringBuffer sq = new StringBuffer();
078                    String init = "SELECT * WHERE { "+
079                                    "<"+uri+"> <"+predicate+"> ?o"+currentDepth+". ";
080                    
081                    sq.append(init);
082                    
083                    for (; currentDepth < maxDepth; currentDepth++) {
084                            String currentO = "?o"+currentDepth;
085                            String nextP = "?p"+(currentDepth+1);
086                            String nextO = "?o"+(currentDepth+1);
087                            sq.append(" { OPTIONAL { "+currentO+" "+nextP+" "+nextO+". }}");
088                    }
089                    
090                    
091                    sq.append(" } ");
092                    return sq.toString();
093            }
094            
095    }