我尝试创建一个使用Jena API的Ontology,输出将在OWL中粘贴。
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class BandsOntology {
public static void main(String[] args)
{
// Create an empty ontology model
OntModel m = ModelFactory.createOntologyModel();
String baseURI = "http://www.bands.fr";
String NSBand = baseURI + "#";
m.setNsPrefix("Band", NSBand);
OntClass Band = m.createClass(NSBand + "Band");
OntClass Guitarist = m.createClass(NSBand + "Guitarist");
OntClass Bassist = m.createClass(NSBand + "Bassist");
OntClass Keyboardist = m.createClass(NSBand + "Keyboardist");
OntClass Vocalist = m.createClass(NSBand + "Vocalist ");
ObjectProperty bandComposedBy = m.createObjectProperty(NSBand +"composedBy");
bandComposedBy.hasDomain(Band);
bandComposedBy.hasRange(Guitarist);
bandComposedBy.hasRange(Bassist);
bandComposedBy.hasRange(Keyboardist);
bandComposedBy.hasRange(Vocalist);
Individual queen = Band.createIndividual(NSBand + "Queen");
Individual guitarist01 = Guitarist.createIndividual(NSBand + "guita01");
m.add(queen, bandComposedBy, guitarist01);
m.write(System.out, Constants.TYPE_RDF_XML_ABBREV);
}
}
控制台上的输出:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:Band="http://www.bands.fr#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Class rdf:about="http://www.bands.fr#Bassist"/>
<owl:Class rdf:about="http://www.bands.fr#Band"/>
<owl:Class rdf:about="http://www.bands.fr#Vocalist "/>
<owl:Class rdf:about="http://www.bands.fr#Keyboardist"/>
<owl:Class rdf:about="http://www.bands.fr#Guitarist"/>
<owl:ObjectProperty rdf:about="http://www.bands.fr#composedBy"/>
<Band:Band rdf:about="http://www.bands.fr#Queen">
<Band:composedBy>
<Band:Guitarist rdf:about="http://www.bands.fr#guita01"/>
</Band:composedBy>
</Band:Band>
</rdf:RDF>
我问到为什么在宣布财产之后,我没有得到&#34; composy&#34;声明为我编码的属性。
<owl:ObjectProperty rdf:about="http://www.bands.fr#composedBy">
<rdfs:domain rdf:resource="http://www.bands.fr#Band"/>
<rdfs:range rdf:resource="http://www.bands.fr#Bassist"/>
<rdfs:range rdf:resource="http://www.bands.fr#Guitarist"/>
...
</owl:ObjectProperty>
答案 0 :(得分:1)
hasDomain和hasRange不添加域和范围,如果属性具有指定的域和范围,它们只返回true。这在Javadoc中针对这些方法进行了解释,并遵循Java中的典型命名约定,如果对象满足某些条件,则以has-或is-开头的方法返回true。
您应该使用addDomain和addRange代替。也就是说,而不是
bandComposedBy.hasDomain(Band);
bandComposedBy.hasRange(Guitarist);
bandComposedBy.hasRange(Bassist);
bandComposedBy.hasRange(Keyboardist);
bandComposedBy.hasRange(Vocalist);
你应该使用:
bandComposedBy.addDomain(Band);
bandComposedBy.addRange(Guitarist);
bandComposedBy.addRange(Bassist);
bandComposedBy.addRange(Keyboardist);
bandComposedBy.addRange(Vocalist);