如何使用Jena RDF/Ontology API以编程方式计算路径长度? [英] How can I programmatically compute path length using Jena RDF/Ontology API?

查看:76
本文介绍了如何使用Jena RDF/Ontology API以编程方式计算路径长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RDF图,在所有类之间只有一个关系(RDFS.subClassOf或is-a).

I have an RDF Graph with only one relationship(RDFS.subClassOf or is-a) between all the classes.

每个类的大小"等于其子类的总数. 如果RDFS.subClassOf属性是从子类总数较少的类连接的,则其大小"为0.5;如果是从子类总数较大的类连接到子类总数较少的类的,则"size"为1.

The 'size' of each class is equal to the total number of its subclasses. The 'size' of each RDFS.subClassOf property is 0.5 if it is connecting from a class with fewer total number of subclasses and 1 if it is connecting from class with large total number of subclasses to class with less total number of subclasses.

我想使用Jena RDF/Ontology API对路径中每个语义成分(类/关系)的所有大小进行算术求和.

I want to perform an arithmetic sum of all the sizes of each semantic component (class / relationship) in the path using the Jena RDF/Ontology API.

例如,给定摄影机本体( http://protege. cim3.net/file/pub/ontologies/camera/camera.owl )的路径之一是:Thing-PurchaseableItem-Lens:其总和为(5 + 0.5 + 3 + 0.5 + 0)= 9.

For example, given the camera ontology (http://protege.cim3.net/file/pub/ontologies/camera/camera.owl) one of the paths is: Thing - PurchaseableItem - Lens : Its sum would be (5 + 0.5 + 3 + 0.5 + 0) = 9.

推荐答案

假定这是基于您对上一个问题的答案,您可以只使用:

Assuming that this builds on the answer to your previous question about constructing the type of paths that you mention in the question, can you just use:

public static int classSize( final Resource klass ) {
    return klass.getModel().listSubjectsWithProperty( RDFS.subClassOf, klass ).toList().size();
}

public static double pathSize( final List<Resource> path ) {
    int prevSize = classSize( path.get( 0 )); 
    double pathSum = prevSize;
    for ( int i = 1; i < path.size(); i++ ) {
        int currSize = classSize( path.get( i ));
        double linkWeight = currSize < prevSize ? 0.5 : 1.0;
        pathSum += linkWeight + currSize;
        prevSize = currSize;
    }
    return pathSum;
}

然后,我们得到以下路径总和.完整的代码跟随此输出.请注意,这里的owl:Thing大小为4,而不是问题中给出的5.如果要计算一个类在rdfs:subClassOf的三元组中作为对象显示为对象的次数,则链接到的OWL文件中只有something rdfs:subClassOf owl:Thing形式的四三元组,因此看起来像大小应为四个,而不是五个.考虑到这一点,请注意,"Thing-PurchaseableItem-Lens"路径具有预期的权重8(如您在问题中提到的,权重小于9).

Then we get the following sums for the paths. The complete code follows this output. Notice the owl:Thing has a size of four here, not five, as you gave in the question. if the idea was to count the number of times that a class appeared as the object in a triple of rdfs:subClassOf, there are only four triples of the form something rdfs:subClassOf owl:Thing in the OWL file that you linked to, so it seems like its size should be four, not five. Given that consideration, note that the "Thing–PurchaseableItem–Lens" path has weight eight, as expected (one less than nine, as you mentioned in the question).

4.0 [http://www.w3.org/2002/07/owl#Thing]
7.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem]
4.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Window]
4.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Range]
4.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#Money]
10.0 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera]
8.0 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Lens]
8.0 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Body]
10.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera, http://www.xfront.com/owl/ontologies/camera/#Digital]
10.5 [http://www.w3.org/2002/07/owl#Thing, http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem, http://www.xfront.com/owl/ontologies/camera/#Camera, http://www.xfront.com/owl/ontologies/camera/#Large-Format]

完整代码:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDFS;

public class BFSInRDFWithJena {

    public static List<List<Resource>> BFS( final Model model, final Queue<List<Resource>> queue, final int depth ) {
        final List<List<Resource>> results = new ArrayList<>();
        while ( !queue.isEmpty() ) {
            final List<Resource> path = queue.poll();
            results.add( path );
            if ( path.size() < depth ) {
                final Resource last = path.get( path.size() - 1 );
                final StmtIterator stmt = model.listStatements( null, RDFS.subClassOf, last );
                while ( stmt.hasNext() ) {
                    final List<Resource> extPath = new ArrayList<>( path );
                    extPath.add( stmt.next().getSubject().asResource() );
                    queue.offer( extPath );
                }
            }
        }
        return results;
    }

    public static int classSize( final Resource klass ) {
        return klass.getModel().listSubjectsWithProperty( RDFS.subClassOf, klass ).toList().size();
    }

    public static double pathSize( final List<Resource> path ) {
        int prevSize = classSize( path.get( 0 )); 
        double pathSum = prevSize;
        for ( int i = 1; i < path.size(); i++ ) {
            int currSize = classSize( path.get( i ));
            double linkWeight = currSize < prevSize ? 0.5 : 1.0;
            pathSum += linkWeight + currSize;
            prevSize = currSize;
        }
        return pathSum;
    }

    public static void main( final String[] args ) throws IOException {
        final Model model = ModelFactory.createDefaultModel();
        try ( final InputStream in = BFSInRDFWithJena.class.getClassLoader().getResourceAsStream( "camera.owl" ) ) {
            model.read( in, null );
        }

        // setup the initial queue
        final Queue<List<Resource>> queue = new LinkedList<>();
        final List<Resource> thingPath = new ArrayList<>();
        thingPath.add( OWL.Thing.inModel( model ));
        queue.offer( thingPath );

        // Get the paths, and display them
        final List<List<Resource>> paths = BFS( model, queue, 4 );
        for ( List<Resource> path : paths ) {
            System.out.println( pathSize( path ) + " " + path );
        }
    }
}

这篇关于如何使用Jena RDF/Ontology API以编程方式计算路径长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆