Model.listSubjectsWithProperty(Property p, String o) does not list resources having Typed literal with same string value

StackOverflow https://stackoverflow.com/questions/23161586

  •  05-07-2023
  •  | 
  •  

I have a model whcih prints like this -

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://www.graphsearch.org/property/"
    xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:j.1="http://www.graphsearch.org/class/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" > 
  <rdf:Description rdf:about="http://www.graphsearch.org/property/hasPassword">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/class/Nothing">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/property/hasEmail">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/class/User">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <rdfs:subClassOf rdf:resource="http://www.graphsearch.org/class/Thing"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/class/Thing">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
    <owl:disjointWith rdf:resource="http://www.graphsearch.org/class/Nothing"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/property/bornOn">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/property/hasName">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/property/livesInCity">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/resource/User/2">
    <rdf:type rdf:resource="http://www.graphsearch.org/class/User"/>
    <j.0:hasPassword>password</j.0:hasPassword>
    <vcard:Given rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Ravi</vcard:Given>
    <vcard:N rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Kumar</vcard:N>
    <vcard:Family rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Singh</vcard:Family>
    <vcard:EMAIL rdf:datatype="http://www.w3.org/2001/XMLSchema#string">visittoravi@gmail.com</vcard:EMAIL>
    <vcard:BDAY rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">1989-06-14T18:30:00Z</vcard:BDAY>
    <vcard:GROUP rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Male</vcard:GROUP>
    <vcard:UID rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">2</vcard:UID>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.graphsearch.org/property/hasGender">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
  </rdf:Description>
</rdf:RDF>

Now if i say -

model.listSubjectsWithProperty(VCARD.EMAIL,"visittoravi@gmail.com");

it doesn't gives me any resource, the problem what i understood is probably it is because VCARD.EMAIL property has a typed literal which, I am adding like this -

resource.addLiteral(VCARD.EMAIL, model.createTypedLiteral("visittoravi@gmail.com"));

Is this problem is really because of it's a typed literal or something else. How i can list resources with a property having a typed literal.

有帮助吗?

解决方案

If the value of the property is a typed literal, e.g., because you added it with:

resource.addLiteral(
  VCARD.EMAIL,
  model.createTypedLiteral("visittoravi@gmail.com"));

then you need to retrieve it using a typed literal with:

model.listSubjectsWithProperty(
  VCARD.EMAIL,
  model.createTypedLiteral("visittoravi@gmail.com"));

Here's a complete working example with some minimal data:

import java.io.ByteArrayInputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.vocabulary.VCARD;

public class PropertySubjectsExample {
    static String CONTENT = "\n" +
            "@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .\n" +
            "@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0#> .\n" +
            "@prefix : <http://stackoverflow.com/q/23161586/1281433/>\n" +
            "\n" +
            ":x vcard:EMAIL \"visittoravi@gmail.com\"^^xsd:string .\n" +
            "";

    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        model.read( new ByteArrayInputStream( CONTENT.getBytes()), null, "TTL" );
        ResIterator subjects = model.listSubjectsWithProperty(
                                    VCARD.EMAIL,
                                    model.createTypedLiteral( "visittoravi@gmail.com" ));
        while ( subjects.hasNext() ) {
            System.out.println( subjects.next() );
        }
    }
}

Output:

http://stackoverflow.com/q/23161586/1281433/x
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top