1

我有一个简单的本体,它包含一个 Weather 类和一些子类(虽然我不确定是否应该将它们设为个体),例如“Cold”、“Rainy”、“Sunny”等。问题是,我在我在运行时获得天气数据之前,不知道这些条件中的哪一个。例如,如果当前温度低于 70 度,则只有“冷”(我住在德克萨斯州;p)。有没有办法构建本体,以便可以在运行时完成这种推理?(我正在使用 Protege 和 Jena。)

基本上,我想根据当前有效的天气条件做不同的事情。为简单起见,假设我只想打印出“现在很冷,下雨……”,根据温度和降水量等数据列出当前的天气状况。

4

1 回答 1

1

用 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é:

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>
于 2013-12-10T19:33:38.797 回答