⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classhierarchy.java

📁 RDF的解析例程
💻 JAVA
字号:
/*****************************************************************************
 * Source code information
 * -----------------------
 * Original author    ICE
 * Author email       pearlriver1981@hotmail.com
 * Created            23-Mar-2006
 * Filename           $RCSfile: ClassHierarchy.java,v $
 * Revision           $Revision: 1.1 $
 * Release status     $State: Exp $
 *
 * Last modified on   $Date: 2005/10/06 17:49:07 $
 *               by   $Author: pearlriver $
 *
 * (c)
 *****************************************************************************/

// Package
///////////////
//
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.Restriction;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.util.iterator.Filter;



/**
 * <p>
 * Simple demonstration program to show how to list a hierarchy of classes. This
 * is not a complete solution to the problem (sub-classes of restrictions, for example,
 * are not shown).  It is inteded only to be illustrative of the general approach.
 * </p>
 */
public class ClassHierarchy {
    // Constants
    //////////////////////////////////

    // Static variables
    //////////////////////////////////

    // Instance variables
    //////////////////////////////////

    // OntModel 本体模型
    // Map  映射
    // m_anonCount 顾客数
    //////////////////////////////////
    protected OntModel m_model;
    private Map m_anonIDs = new HashMap();
    private int m_anonCount = 0;

    //所在离根的远近
    private HashMap weight = new HashMap();
    //所含兄弟节点的个数。
    private HashMap brotherNode = new HashMap();
    //所含的子节点的个数。
    private HashMap sonNode = new  HashMap();
    
    private int Scalingfactor = 1;
    



    // Constructors
    //////////////////////////////////

    // External signature methods
    //////////////////////////////////


    /** Show the sub-class hierarchy encoded by the given model */
    public HashMap showHierarchy( PrintStream out, OntModel m ) {		//Filter ,Boolean accept(Object O) 接不接受对象 看是否匿名。匿名的对象给过滤掉了。

        // create an iterator over the root classes that are not anonymous class expressions
        Iterator i = m.listHierarchyRootClasses()
                      .filterDrop( new Filter() {
                                    public boolean accept( Object o ) {
                                        return ((Resource) o).isAnon();
                                    }} );
      
        while (i.hasNext()) {
            showClass( out, (OntClass) i.next(), new ArrayList(), 0 );
           
        }
     
        return weight;
    }

    public HashMap  getBrotherCount()
    {
    	return brotherNode;
    }
    
    public HashMap getsonCount()
    {
    	return sonNode;
    }
    
    public HashMap getlevelCount()
    {
    	return weight;
    }

    //////////////////////////////////

    /** Present a class, then recurse down to the sub-classes.
     *  Use occurs check to prevent getting stuck in a loop
     */
    protected void showClass( PrintStream out, OntClass cls, List occurs, int depth ) {
    	
       
         int m_SonCount = 0;
        
        renderClassDescription( out, cls, depth );
        out.println();

        // recurse to the next level down
        if (cls.canAs( OntClass.class )  &&  !occurs.contains( cls )) {
        	
             int m_brotherCount = 0;
            for (Iterator i = cls.listSubClasses( true );  i.hasNext(); ) {
            	
                OntClass sub = (OntClass) i.next();
               //权重计算模块-》根据图的结构计算。
         //基本原理:
               //1父节点拥有子节点数越多, 则子节点与父节点的关系越弱
               //2兄弟节点越多,与父节点关系越弱
               //3子节点越少,那么该节点的作用就越大。
               
               //1.一种方法根据深度计算某个参数的权重
                float  asa = (float)((float)(1) / (depth + 1)) ;
              
               //2.方法2:公式不同。
                // float asa = (float)((exp(0.6* depth) - exp((-0.6)*depth))/ (exp(0.6* depth) + exp((-0.6)*depth)))
                // 注意节点 跟节点的距离计算不同,有的计算 两个节点在树中的距离。
                //定义3(连接路径).根据Vc∈C,H(c,Root)以及 关系的无环性,Cl和C2在本体0中的连接路径有且只
                //有一条,记做path(cl,c2),而CI到msa(cI,C2)的父子链与C2到msa(cl,c2)的父子链的并集即为这样的连接路径,定义
                //如下:path(cI,C2)=pcc(cl,msa(cl,C2))~pcc(c2,msa(cI,C2)
                
                
                weight.put(cls.getLocalName(),Float.toString(asa));
               
                
                
                
                
                // we push this expression on the occurs list before we recurse
                occurs.add( cls );
                showClass( out, sub, occurs, depth + 1 );
                occurs.remove( cls );
                
                m_brotherCount ++ ;                             
                
            }
            sonNode.put(cls.getLocalName(),  Float.toString(m_brotherCount) );
            for (Iterator i = cls.listSubClasses( true );  i.hasNext(); ) {
                       OntClass sub = (OntClass) i.next();
            
              brotherNode.put(sub.getLocalName(),  Float.toString(m_brotherCount));
                               
                      // we push this expression on the occurs list before we recurse
                 
            }
            
            
            
      
            
            
            
        }
    }


    /**
     * <p>Render a description of the given class to the given output stream.</p>
     * @param out A print stream to write to
     * @param c The class to render
     */
    public void renderClassDescription( PrintStream out, OntClass c, int depth ) {
        indent( out, depth );

        if (c.isRestriction()) {
            renderRestriction( out, (Restriction) c.as( Restriction.class ) );
        }
        else {
            if (!c.isAnon()) {
             //   out.print( "Class " );
             //   renderURI( out, c.getModel(), c.getURI() );
             //   out.print( ' ' );
            }
            else {
                renderAnonymous( out, c, "class" );
            }
        }
    }

    /**
     * <p>Handle the case of rendering a restriction.</p>
     * @param out The print stream to write to
     * @param r The restriction to render
     */
    protected void renderRestriction( PrintStream out, Restriction r ) {
        if (!r.isAnon()) {
            out.print( "Restriction " );
            renderURI( out, r.getModel(), r.getURI() );
        }
        else {
            renderAnonymous( out, r, "restriction" );
        }

        out.print( " on property " );
        renderURI( out, r.getModel(), r.getOnProperty().getURI() );
    }

    /** Render a URI */
    protected void renderURI( PrintStream out, PrefixMapping prefixes, String uri ) {
        out.print( prefixes.shortForm( uri ) );
    }

    /** Render an anonymous class or restriction */
    protected void renderAnonymous( PrintStream out, Resource anon, String name ) {
        String anonID = (String) m_anonIDs.get( anon.getId() );
        if (anonID == null) {
            anonID = "a-" + m_anonCount++;
            m_anonIDs.put( anon.getId(), anonID );
        }

        out.print( "Anonymous ");
        out.print( name );
        out.print( " with ID " );
        out.print( anonID );
    }

    /** Generate the indentation */
    protected void indent( PrintStream out, int depth ) {
        for (int i = 0;  i < depth; i++) {
            out.print( "  " );
        }
    }


    //==============================================================================
    // Inner class definitions
    //==============================================================================

}


/*
    (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -