1

I don't know why after setting setNsPrefix of Model it doesn't work according to my expectation.
This is my Java Code

public class TestClass
{
   public static void main(String[] args){
     Model model = ModelFactory.createDefaultModel();
     model.setNsPrefix("vocab","http://myweb.in/vocab#");         
     model.createResource().addLiteral(ResourceFactory.createProperty("vocab:name"),"Dhannan");
     String query
                = " SELECT * "
                + " WHERE { "
                + " ?Subject ?Predicate ?Object . "
                + " } ";
     Services.executeQuery(model, query);
    }
}

Output :

--------------------------------------
| Subject | Predicate    | Object    |
======================================
| _:b0    | <vocab:name> | "Dhannan" |
--------------------------------------

Expected :

------------------------------------------------------
| Subject | Predicate                    | Object    |
======================================================
| _:b0    | <http://myweb.in/vocab#name> | "Dhannan" |
------------------------------------------------------

Where did I make mistake? From my thought setNsPrefix can do that .

Badman
  • 407
  • 5
  • 17

1 Answers1

1

Setting prefixes does not turn it on in the API. That isn't provided, especially for ResourceFactory.createProperty as there is no model at that point.

Expand the prefixed name with model.expandPrefix to get the full URI, the use that URI.

AndyS
  • 16,345
  • 17
  • 21
  • Sorry but it is not working . I added model.expandPrefix("vocab"); System.out.println(model.expandPrefix("vocab")); But it is still showing "vocab:name" and even after printing it is showing "vocab". – Badman Feb 03 '17 at 06:37
  • while this System.out.println(model.shortForm("http://myweb.in/vocab#")); gives me shortForm "vocab:" – Badman Feb 03 '17 at 06:53
  • Sorry I have to do this System.out.println(model.expandPrefix("vocab:")); It works fine and give me "http://myweb.in/vocab#". But still I have question , can we avoid again and again writing this resource.addLiteral(ResourceFactory.createProperty(model.expandPrefix("vocab:")+"name"),"Dhannan"); – Badman Feb 03 '17 at 07:02
  • (1) `model.expandPrefix("vocab:name")` (2) create constants for your vocabulary (see the classes in the "vocabaulary" package) and don't make a property every time. – AndyS Feb 04 '17 at 10:05
  • You can always create a small static helper function to make things the way you want to. – AndyS Feb 04 '17 at 10:06