用 OWL 编写公理
听起来您正在尝试添加一些表单规则:
如果 x 的温度小于或等于 32.0 F,则 x 的天气条件为寒冷。
如果 x 的温度高于 32.0 F 且小于或等于 70.0 F,则 x 具有温暖的天气条件。
如果 x 的温度高于 70.0 F,则 x 的天气条件为 Hot。
您可以在 OWL 中执行这些操作,而不会遇到诸如公理之类的问题
hasTemperature some double[> 32.0, <= 70.0] SubClassOf hasWeatherCondition value暖
这些被称为通用类公理,因为它们在左侧有类表达式而不是原子类名。我上面描述的全套将按如下方式输入 Protégé:

使用 Jena 和 Pellet 查看结果
要对数字进行推理,您需要一个推理器。我不确定 Jena 的规则推理者是否可以进行这种推理,但我知道 Pellet 可以。以下代码使用 Pellet 支持的推理模型。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mindswap.pellet.jena.PelletReasonerFactory;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
public class WeatherExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
final String NS = "http://stackoverflow.com/q/20489574/1281433/weather#";
// Create an OntModel and read in the content from the ontology. We're creating an model
// that has Pellet doing inference behind the scenes. Pellet can handle the types of datatype
// reasoning that we need for this particular problem.
final OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );
try ( final FileInputStream in = new FileInputStream( "/home/taylorj/tmp/ontologies/weather/weather.owl" )) {
model.read( in, null, "RDF/XML" );
}
// create an individual and list the things that the model knows about it.
final Individual todaysWeather = model.createIndividual( NS+"weatherOfToday", OWL.Thing );
System.out.println( "== Initial Knowledge ==" );
for ( final StmtIterator it = model.listStatements( todaysWeather, null, (RDFNode) null ); it.hasNext(); ) {
System.out.println( it.next() );
}
// Add the information that todaysWeather had temperature 28.0.
final OntProperty hasTemperature = model.createOntProperty( NS+"hasTemperature" );
todaysWeather.addLiteral( hasTemperature, model.createTypedLiteral( "28.0", XSDDatatype.XSDdouble ));
// Show the new knowledge about todaysWeather.
System.out.println( "== Later Knowledge ==" );
for ( final StmtIterator it = model.listStatements( todaysWeather, null, (RDFNode) null ); it.hasNext(); ) {
System.out.println( it.next() );
}
}
}
输出如下。请注意,在第二个块todaysWeather hasWeatherCondition Cold
中。
== Initial Knowledge ==
[http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Thing]
[http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday, http://www.w3.org/2002/07/owl#sameAs, http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday]
== Later Knowledge ==
[http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Thing]
[http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday, http://www.w3.org/2002/07/owl#sameAs, http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday]
[http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday, http://stackoverflow.com/q/20489574/1281433/weather#hasWeatherCondition, http://stackoverflow.com/q/20489574/1281433/weather#Cold]
[http://stackoverflow.com/q/20489574/1281433/weather#weatherOfToday, http://stackoverflow.com/q/20489574/1281433/weather#hasTemperature, "28.0"^^http://www.w3.org/2001/XMLSchema#double]
本体论
您可以复制并粘贴我从以下截屏的本体的内容。
<rdf:RDF
xmlns="http://stackoverflow.com/q/20489574/1281433/weather#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
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:Ontology rdf:about="http://stackoverflow.com/q/20489574/1281433/weather"/>
<owl:ObjectProperty rdf:about="http://stackoverflow.com/q/20489574/1281433/weather#hasWeatherCondition"/>
<owl:DatatypeProperty rdf:about="http://stackoverflow.com/q/20489574/1281433/weather#hasTemperature">
<rdfs:comment>temperature in degrees Fahrenheit </rdfs:comment>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#double"/>
</owl:DatatypeProperty>
<owl:Restriction>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://stackoverflow.com/q/20489574/1281433/weather#hasWeatherCondition"/>
<owl:hasValue>
<owl:NamedIndividual rdf:about="http://stackoverflow.com/q/20489574/1281433/weather#Hot"/>
</owl:hasValue>
</owl:Restriction>
</rdfs:subClassOf>
<owl:onProperty rdf:resource="http://stackoverflow.com/q/20489574/1281433/weather#hasTemperature"/>
<owl:someValuesFrom>
<rdfs:Datatype>
<owl:onDatatype rdf:resource="http://www.w3.org/2001/XMLSchema#double"/>
<owl:withRestrictions rdf:parseType="Collection">
<rdf:Description>
<xsd:minExclusive rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>70.0</xsd:minExclusive>
</rdf:Description>
</owl:withRestrictions>
</rdfs:Datatype>
</owl:someValuesFrom>
</owl:Restriction>
<owl:Restriction>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://stackoverflow.com/q/20489574/1281433/weather#hasWeatherCondition"/>
<owl:hasValue>
<owl:NamedIndividual rdf:about="http://stackoverflow.com/q/20489574/1281433/weather#Warm"/>
</owl:hasValue>
</owl:Restriction>
</rdfs:subClassOf>
<owl:onProperty rdf:resource="http://stackoverflow.com/q/20489574/1281433/weather#hasTemperature"/>
<owl:someValuesFrom>
<rdfs:Datatype>
<owl:onDatatype rdf:resource="http://www.w3.org/2001/XMLSchema#double"/>
<owl:withRestrictions rdf:parseType="Collection">
<rdf:Description>
<xsd:maxInclusive rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>70.0</xsd:maxInclusive>
</rdf:Description>
<rdf:Description>
<xsd:minExclusive rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>32.0</xsd:minExclusive>
</rdf:Description>
</owl:withRestrictions>
</rdfs:Datatype>
</owl:someValuesFrom>
</owl:Restriction>
<owl:Restriction>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://stackoverflow.com/q/20489574/1281433/weather#hasWeatherCondition"/>
<owl:hasValue>
<owl:NamedIndividual rdf:about="http://stackoverflow.com/q/20489574/1281433/weather#Cold"/>
</owl:hasValue>
</owl:Restriction>
</rdfs:subClassOf>
<owl:onProperty rdf:resource="http://stackoverflow.com/q/20489574/1281433/weather#hasTemperature"/>
<owl:someValuesFrom>
<rdfs:Datatype>
<owl:onDatatype rdf:resource="http://www.w3.org/2001/XMLSchema#double"/>
<owl:withRestrictions rdf:parseType="Collection">
<rdf:Description>
<xsd:maxInclusive rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>32.0</xsd:maxInclusive>
</rdf:Description>
</owl:withRestrictions>
</rdfs:Datatype>
</owl:someValuesFrom>
</owl:Restriction>
</rdf:RDF>