?? classhierarchy.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 顧客數(shù)
//////////////////////////////////
protected OntModel m_model;
private Map m_anonIDs = new HashMap();
private int m_anonCount = 0;
//所在離根的遠(yuǎn)近
private HashMap weight = new HashMap();
//所含兄弟節(jié)點的個數(shù)。
private HashMap brotherNode = new HashMap();
//所含的子節(jié)點的個數(shù)。
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();
//權(quán)重計算模塊-》根據(jù)圖的結(jié)構(gòu)計算。
//基本原理:
//1父節(jié)點擁有子節(jié)點數(shù)越多, 則子節(jié)點與父節(jié)點的關(guān)系越弱
//2兄弟節(jié)點越多,與父節(jié)點關(guān)系越弱
//3子節(jié)點越少,那么該節(jié)點的作用就越大。
//1.一種方法根據(jù)深度計算某個參數(shù)的權(quán)重
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)))
// 注意節(jié)點 跟節(jié)點的距離計算不同,有的計算 兩個節(jié)點在樹中的距離。
//定義3(連接路徑).根據(jù)Vc∈C,H(c,Root)以及 關(guān)系的無環(huán)性,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.
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -