差分発生行の前後
無視リスト:
更新日時:
2015/03/24 18:18:00 (10 年 前)
更新者:
atsuko
ログメッセージ:

枝刈り,クラス順序変更(数字は下に)

ファイル:
1 変更

凡例:

変更なし
追加
削除
  • SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/OWL/OWLClassGraph.java

    r228 r248  
    1919    List<String> nodeType; 
    2020    ArrayList<HashSet<Integer>> connectionTable; 
    21  
     21    String sparqlEndpoint; 
     22    Set<Integer> visited; 
     23    boolean askcheck; 
     24    List<Map<Integer, Integer>> edgeweight; 
     25    List<Integer> nodeweight; 
     26     
    2227    public class LinkAndPath{ 
    2328        String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL 
     
    4651    } 
    4752 
    48 /* 
    49     public OWLClassGraph(String startClass, String endClass){ 
    50         super(); 
    51          
    52         // start & end 
    53         this.startClass = startClass; 
    54         this.endClass = endClass; 
    55          
    56         // parameters 
    57         nsteps = 3; 
    58         limit = 1000; 
    59         th = 5; 
    60     } 
    61 */ 
    62  
    63 /*     
    64     public OWLClassGraph(String startClass, String endClass, int th){ 
    65         super(); 
    66          
    67         // start & end 
    68         this.startClass = startClass; 
    69         this.endClass = endClass; 
    70         // th of instances 
    71         this.th = th; 
    72          
    73         // parameters 
    74         nsteps = 3; 
    75         limit = 1000; 
    76     } 
    77 */     
    78  
    79     public OWLClassGraph(){ 
     53    public OWLClassGraph(){ // not used 
    8054        super(); 
    8155        nodeType = new LinkedList<String>(); 
    82         //setClassGraph(rdfsa); 
    83         //connectionTable = createConnectionTable(); 
    84     } 
    85      
    86      
    87     public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ 
     56    } 
     57         
     58    public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ // not used 
    8859        super(); 
    8960        nodeType = new LinkedList<String>(); 
    90         //setClassGraph(rdfsa); 
    91         //connectionTable = createConnectionTable(); 
    92     } 
    93      
     61    } 
     62     
     63    public OWLClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass, boolean askcheck){ // used 
     64        super(); 
     65        nodeType = new LinkedList<String>(); 
     66        this.askcheck = askcheck; 
     67        setPartClassGraph(rdfsa, sparqlEndpoint, startClass); 
     68    } 
     69 
    9470    public int getNumberOfEdge(String url){ 
    9571        Integer node = labelednodes.get(url); 
     
    9874        } 
    9975        return adjlist.get(node).size(); 
     76    } 
     77     
     78    public boolean visitedNode(String classURI){ 
     79        if ( visited.contains(labelednodes.get(classURI)) ){ 
     80            return true; 
     81        } 
     82        return false; 
    10083    } 
    10184     
     
    138121        return connectionTable.get(node); 
    139122    } 
    140      
    141     public Path[] getPaths_old(RDFSchemaAnalyzer rdfsa, boolean countLink, String startClass, String endClass){ 
    142         List<List<ClassLink>> paths = null; 
    143         paths = searchPathsbyVisitingNodes(rdfsa, countLink, startClass, endClass); 
    144         NavigableSet<Path> sortedpath = new TreeSet<Path>(); 
    145         ListIterator<List<ClassLink>> pit = paths.listIterator(); 
    146         int j = 0; 
    147         while ( pit.hasNext() ){ 
    148             Path path = new Path(); 
    149             path.setStartClass(startClass); 
    150             List<ClassLink> crrpath = pit.next(); 
    151             path.setClassLinks(crrpath); 
    152             ListIterator<ClassLink> cit = crrpath.listIterator(); 
    153             int min = Integer.MAX_VALUE; 
    154             while ( cit.hasNext() ){ 
    155                 ClassLink cl = cit.next(); 
    156                 if ( cl.getNumOfLinks() < min ){ 
    157                     min = cl.getNumOfLinks(); 
    158                 } 
    159             } 
    160             path.setWidth(min); 
    161             sortedpath.add(path); 
    162             j++; 
    163         } 
    164         Path[] patharray = new Path[paths.size()]; 
    165         Iterator<Path> pait = sortedpath.descendingIterator(); 
    166         int i = 0; 
    167         while ( pait.hasNext() ){ 
    168             patharray[i] = pait.next(); 
    169             i++; 
    170         } 
    171         return patharray; 
    172     } 
    173123 
    174124    private List<List<ClassLink>> searchPaths(String startClass, String endClass){ 
    175  
     125        //int asked = 0; 
     126        Map<String,Boolean> checkedpaths = new HashMap<String, Boolean>(); 
    176127        List<List<ClassLink>> paths = new ArrayList<>(); 
    177128        List<List<Integer>> simplePaths = new LinkedList<>(); 
     
    195146                    List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy 
    196147                    nextpath.add(nextnode); 
     148                    // tmp 
     149                    if ( i >= 1 ){ 
     150                        int wn = nodeweight.get(crrnode); 
     151                        int in = edgeweight.get(crrpath.get(crrpath.size()-2)).get(crrnode); 
     152                        int out = edgeweight.get(nextnode).get(crrnode); 
     153                        if ( wn > in + out ){ 
     154                            /* 
     155                            String key1 = nextnode.toString().concat("-").concat(crrpath.get(crrpath.size()-1).toString()) 
     156                                .concat("-").concat(crrpath.get(crrpath.size()-2).toString()); 
     157                            String key2 = crrpath.get(crrpath.size()-2).toString().concat("-").concat(crrpath.get(crrpath.size()-1).toString()) 
     158                                .concat("-").concat(nextnode.toString()); 
     159                            if ( checkedpaths.containsKey(key1) ){ 
     160                                if ( checkedpaths.get(key1) == false ){ 
     161                                   continue; 
     162                                } 
     163                            }else if ( checkedpaths.containsKey(key2) ){ 
     164                                if ( checkedpaths.get(key2) == false ){ 
     165                                   continue; 
     166                                } 
     167                            }else{                       
     168                                boolean chk = EndpointAccess.check3SimplePathwithJoin(nextnode, crrpath.get(crrpath.size()-1), 
     169                                    crrpath.get(crrpath.size()-2), this, sparqlEndpoint); 
     170                                checkedpaths.put(key1, chk); 
     171                                if ( chk == false ){ 
     172                                    continue; 
     173                                } 
     174                            } 
     175                            */ 
     176                            continue; 
     177                        } 
     178                    } 
    197179                    if ( nextnode.equals(enode) ){ 
    198180                        simplePaths.add(nextpath); 
     
    206188         
    207189        ListIterator<List<Integer>> pit = simplePaths.listIterator(); 
     190        //int i = 0; 
     191        //int j = 0; 
     192        System.out.println("SPATH:"); 
     193        System.out.println(simplePaths.size()); 
    208194        while( pit.hasNext()){ 
    209195            List<Integer> spath = pit.next(); 
     
    211197            paths.addAll(convertedPaths); 
    212198        } 
     199        System.out.println("PATH:"); 
     200        System.out.println(paths.size()); 
    213201        return paths; 
    214202    } 
     203 
    215204     
    216205    private List<List<ClassLink>> convertSimplePathToPaths(List<Integer> simplePath){ 
    217206        List<List<ClassLink>> paths = new LinkedList<List<ClassLink>>(); 
    218         //List<List<LabeledEdge>> multiedges = new LinkedList<List<LabeledEdge>>(); 
    219207        ListIterator<Integer> spit = simplePath.listIterator(); 
    220208        Integer start = spit.next(); 
     209        String startClass = this.labels.get(start); 
    221210        Integer end = spit.next(); 
    222211        List<LabeledEdge> edges = gadjlist.get(start).get(end); 
     
    242231                    List<ClassLink> addedpath = new LinkedList<ClassLink>(basepath); 
    243232                    addedpath.add(cl); 
    244                     tmppaths.add(addedpath); 
     233                    // check 
     234                    //if ( EndpointAccess.checkPath(startClass, addedpath, sparqlEndpoint) ){ 
     235                        tmppaths.add(addedpath); 
     236                    //} 
    245237                } 
    246238            } 
     
    252244     
    253245    /* 
    254     private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ 
    255         List<List<ClassLink>> paths = new ArrayList<>(); 
    256         List<LinkAndPath> lp = new LinkedList<>(); 
    257         lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "")); 
    258         try{ 
    259           for ( int i = 0; i < nsteps; i++ ){ 
    260               ListIterator<LinkAndPath> lit = lp.listIterator(); 
    261               List<LinkAndPath> nextlp = new LinkedList<>(); 
    262               while ( lit.hasNext() ){ 
    263                   LinkAndPath crrlp = lit.next(); 
    264                   ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); 
    265                   for ( int j = 0 ; j < classLinks.length; j++ ){ 
    266                       List<ClassLink> crrpath = new LinkedList<>(crrlp.path); 
    267                       crrpath.add(classLinks[j]); 
    268                       if ( classLinks[j].getLinkedClassURI() == null ){ continue; } 
    269                       if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ 
    270                           paths.add(new LinkedList<>(crrpath)); 
    271                           continue; 
    272                       } 
    273                       if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ 
    274                           continue; 
    275                       } 
    276                       if ( i >= 2 ){ 
    277                           if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && 
    278                            crrlp.classLink.getDirection() != classLinks[j].getDirection() && 
    279                            crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ 
    280                               continue; 
    281                           } 
    282                       } 
    283                       nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); 
    284                   } 
    285               } 
    286               lp = nextlp; 
    287           } 
    288         }catch(Exception e){  
    289             System.err.println(e); 
    290         } 
    291         return paths;   
    292     } 
    293     */ 
    294      
    295246    private List<List<ClassLink>> searchPathsbyVisitingNodes(RDFSchemaAnalyzer rdfsa, boolean countLinks, 
    296247            String startClass, String endClass){ 
     
    365316                       } 
    366317                       addEdge(i, n, classLinks[j]); 
    367                        /* 
    368                        ClassLink rev = new ClassLink( classLinks[j].getPropertyURI(), 
    369                                //classLinks[j].getLinkedClassURI(), 
    370                                classes[i].getClassURI(), 
    371                                classLinks[j].getLinkedLiteralDatatypeURI(), 
    372                                classLinks[j].getDirection(), classLinks[j].getNumOfLinks(),  
    373                                classLinks[j].getNumOfOriginInstances(), classLinks[j].getNumOfLinkedInstances(), 
    374                                classLinks[j].getNumOfOriginClassInstances(), classLinks[j].getNumOfLinkedClassInstances(), 
    375                                classLinks[j].isDomainClassLimitedQ(), classLinks[j].isRangeClassLimitedQ() ); 
    376                        rev.setDirection(Direction.reverse); 
    377                        addEdge(n, i, rev); 
    378                                */ 
    379318                   } 
    380319               } 
     
    383322           } 
    384323       }        
    385    } 
    386  
    387     public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String startClass){ 
    388         // setNodes 
     324   }*/ 
     325 
     326    public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){ 
     327        // set endpoint 
     328        this.sparqlEndpoint = sparqlEndpoint; 
     329         
     330        visited = new HashSet<Integer>(); 
     331        edgeweight = new LinkedList<Map<Integer,Integer>>(); 
     332        nodeweight = new LinkedList<Integer>(); 
     333        // setNodes for all classes 
    389334        SClass[] classes = null; 
    390335        try{ 
     
    396341           addNode(classes[i].getClassURI()); 
    397342           nodeType.add("class"); 
    398         } 
    399         int nedge = 0; 
     343           edgeweight.add(new HashMap<Integer,Integer>()); 
     344           nodeweight.add(classes[i].getNumOfInstances()); 
     345        } 
     346        // setEdges 
     347        //int nedge = 0; 
    400348        Integer snode = labelednodes.get(startClass); 
    401349        Set<Integer> nodes = new HashSet<Integer>(); 
    402350        nodes.add(snode); 
    403         Set<Integer> visited = new HashSet<Integer>(); 
     351        visited.add(snode); 
    404352        for (int i = 0 ; i < nsteps; i++ ){ 
    405353            Iterator<Integer> nit = nodes.iterator(); 
     
    407355            while ( nit.hasNext() ){ 
    408356                Integer crr = nit.next(); 
    409                 visited.add(crr); 
    410357                try{ 
    411358                    ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true); 
    412359                    for (int j = 0 ; j < classLinks.length; j++){ 
    413360                        Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI()); 
     361                        if ( nn == null ){ 
     362                            continue; 
     363                        } 
    414364                        if ( !visited.contains(nn) ){ 
    415365                            nextnodes.add(nn); 
    416366                        } 
    417                         if ( nn != null ){ 
    418                             addEdge(crr, nn, classLinks[j]); 
    419                         /* 
    420                         }else{ 
    421                             nn = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI()); 
    422                             if ( nn == null ){ 
    423                                 addNode(classLinks[j].getLinkedLiteralDatatypeURI()); 
    424                                 n = nodeType.size(); 
    425                                 nodeType.add("literal"); 
    426                             } 
    427                             addEdge(i, n, classLinks[j]); 
    428                             /* 
    429                             ClassLink rev = new ClassLink( classLinks[j].getPropertyURI(), 
    430                                //classLinks[j].getLinkedClassURI(), 
    431                                classes[i].getClassURI(), 
    432                                classLinks[j].getLinkedLiteralDatatypeURI(), 
    433                                classLinks[j].getDirection(), classLinks[j].getNumOfLinks(),  
    434                                classLinks[j].getNumOfOriginInstances(), classLinks[j].getNumOfLinkedInstances(), 
    435                                classLinks[j].getNumOfOriginClassInstances(), classLinks[j].getNumOfLinkedClassInstances(), 
    436                                classLinks[j].isDomainClassLimitedQ(), classLinks[j].isRangeClassLimitedQ() ); 
    437                             rev.setDirection(Direction.reverse); 
    438                             addEdge(n, i, rev); 
    439                         */      
    440                         } 
     367                        addEdge(crr, nn, classLinks[j]); 
     368                        updateWeight(crr, nn, classLinks[j]); 
    441369                    } 
    442370                }catch(Exception e){ 
     
    445373            } 
    446374            nodes = nextnodes; 
     375            visited.addAll(nodes); 
    447376        } 
    448377    } 
    449378    
    450    private ArrayList<HashSet<Integer>> createConnectionTable(){ 
     379   private ArrayList<HashSet<Integer>> createConnectionTable(){ // not used 
    451380       ArrayList<HashSet<Integer>> ct = new ArrayList<HashSet<Integer>>(); 
    452381       for (int i = 0; i < labels.size(); i++ ){ // each node 
     
    457386   } 
    458387    
    459    private HashSet<Integer> createConnectionList(Integer node){ 
     388   private HashSet<Integer> createConnectionList(Integer node){ // not used  
    460389       HashSet<Integer> cl = new HashSet<Integer>(); 
    461390       HashSet<Integer> crrnodes = new HashSet<Integer>(); 
     
    482411       return cl; 
    483412   } 
     413    
     414    
     415   private void updateWeight(Integer node1, Integer node2, ClassLink edge){ 
     416       Map<Integer, Integer> weight = edgeweight.get(node1); 
     417       Integer crr = weight.get(node2); 
     418       if (crr == null ){ 
     419           crr = edge.getNumOfLinkedClassInstances(); 
     420           weight.put(node2, crr);            
     421       } 
     422       if ( crr < edge.getNumOfLinkedClassInstances() ){ 
     423           crr = edge.getNumOfLinkedClassInstances(); 
     424           weight.put(node2, crr); 
     425       } 
     426       weight = edgeweight.get(node2); 
     427       crr = weight.get(node1); 
     428       if (crr == null ){ 
     429           crr = edge.getNumOfOriginClassInstances(); 
     430           weight.put(node2, crr);            
     431       } 
     432       if ( crr < edge.getNumOfOriginClassInstances() ){ 
     433           crr = edge.getNumOfOriginInstances(); 
     434           weight.put(node1, crr); 
     435       } 
     436   } 
     437    
     438   // old codes 
     439   /* 
     440    public Path[] getPaths_old(RDFSchemaAnalyzer rdfsa, boolean countLink, String startClass, String endClass){ 
     441        List<List<ClassLink>> paths = null; 
     442        paths = searchPathsbyVisitingNodes(rdfsa, countLink, startClass, endClass); 
     443        NavigableSet<Path> sortedpath = new TreeSet<Path>(); 
     444        ListIterator<List<ClassLink>> pit = paths.listIterator(); 
     445        int j = 0; 
     446        while ( pit.hasNext() ){ 
     447            Path path = new Path(); 
     448            path.setStartClass(startClass); 
     449            List<ClassLink> crrpath = pit.next(); 
     450            path.setClassLinks(crrpath); 
     451            ListIterator<ClassLink> cit = crrpath.listIterator(); 
     452            int min = Integer.MAX_VALUE; 
     453            while ( cit.hasNext() ){ 
     454                ClassLink cl = cit.next(); 
     455                if ( cl.getNumOfLinks() < min ){ 
     456                    min = cl.getNumOfLinks(); 
     457                } 
     458            } 
     459            path.setWidth(min); 
     460            sortedpath.add(path); 
     461            j++; 
     462        } 
     463        Path[] patharray = new Path[paths.size()]; 
     464        Iterator<Path> pait = sortedpath.descendingIterator(); 
     465        int i = 0; 
     466        while ( pait.hasNext() ){ 
     467            patharray[i] = pait.next(); 
     468            i++; 
     469        } 
     470        return patharray; 
     471    } 
     472 
     473    private List<List<ClassLink>> searchPaths_old(String startClass, String endClass){ 
     474 
     475        List<List<ClassLink>> paths = new ArrayList<>(); 
     476        List<List<Integer>> simplePaths = new LinkedList<>(); 
     477        Integer snode = labelednodes.get(startClass); 
     478        Integer enode = labelednodes.get(endClass); 
     479        List<List<Integer>> lp = new LinkedList<>(); 
     480        List<Integer> ini = new LinkedList<Integer>(); // initial path 
     481        ini.add(snode); 
     482        lp.add(ini); 
     483        for (int i = 0; i < nsteps; i++ ){ 
     484            ListIterator<List<Integer>> lit = lp.listIterator(); 
     485            List<List<Integer>> nextlp = new LinkedList<>(); 
     486            while ( lit.hasNext() ){  
     487                List<Integer> crrpath = lit.next(); 
     488                Integer crrnode = crrpath.get(crrpath.size()-1); 
     489                Set<Integer> nexts = gadjlist.get(crrnode).keySet(); 
     490                Iterator<Integer> nit = nexts.iterator(); 
     491                while( nit.hasNext() ){ 
     492                    Integer nextnode = nit.next(); 
     493                    if ( crrpath.contains(nextnode) ){ continue; } 
     494                    List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy 
     495                    nextpath.add(nextnode); 
     496                    if ( nextnode.equals(enode) ){ 
     497                        simplePaths.add(nextpath); 
     498                        continue; 
     499                    } 
     500                    nextlp.add(nextpath); 
     501                } 
     502            } 
     503            lp = nextlp; 
     504        } 
     505         
     506        ListIterator<List<Integer>> pit = simplePaths.listIterator(); 
     507        while( pit.hasNext()){ 
     508            List<Integer> spath = pit.next(); 
     509            List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath); 
     510            paths.addAll(convertedPaths); 
     511        } 
     512        return paths; 
     513    }    
     514    */ 
     515       /* 
     516    private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ 
     517        List<List<ClassLink>> paths = new ArrayList<>(); 
     518        List<LinkAndPath> lp = new LinkedList<>(); 
     519        lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "")); 
     520        try{ 
     521          for ( int i = 0; i < nsteps; i++ ){ 
     522              ListIterator<LinkAndPath> lit = lp.listIterator(); 
     523              List<LinkAndPath> nextlp = new LinkedList<>(); 
     524              while ( lit.hasNext() ){ 
     525                  LinkAndPath crrlp = lit.next(); 
     526                  ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); 
     527                  for ( int j = 0 ; j < classLinks.length; j++ ){ 
     528                      List<ClassLink> crrpath = new LinkedList<>(crrlp.path); 
     529                      crrpath.add(classLinks[j]); 
     530                      if ( classLinks[j].getLinkedClassURI() == null ){ continue; } 
     531                      if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ 
     532                          paths.add(new LinkedList<>(crrpath)); 
     533                          continue; 
     534                      } 
     535                      if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ 
     536                          continue; 
     537                      } 
     538                      if ( i >= 2 ){ 
     539                          if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && 
     540                           crrlp.classLink.getDirection() != classLinks[j].getDirection() && 
     541                           crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ 
     542                              continue; 
     543                          } 
     544                      } 
     545                      nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); 
     546                  } 
     547              } 
     548              lp = nextlp; 
     549          } 
     550        }catch(Exception e){  
     551            System.err.println(e); 
     552        } 
     553        return paths;   
     554    } 
     555    */ 
     556 
    484557}