Java Code Examples for org.openrdf.model.ValueFactory#createURI()

The following examples show how to use org.openrdf.model.ValueFactory#createURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphHandler.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
/**
 * get the URI of GRAPH, shoulc be "graph"
 * 
 * @param request the HttpServletRequest object
 * @param vf the ValueFactory object
 * @return the URI of the name of Graph
 * @throws ClientHTTPException throws when no parameters epxected for direct reference request
 */
private URI getGraphName(final HttpServletRequest request, final ValueFactory vf)
		throws ClientHTTPException {
	String requestURL = request.getRequestURL().toString();
	boolean isServiceRequest = requestURL.endsWith("/service");

	String queryString = request.getQueryString();

	if (isServiceRequest) {
		if (!"default".equalsIgnoreCase(queryString)) {
			URI graph = ProtocolUtil.parseGraphParam(request, vf);
			if (graph == null) {
				throw new ClientHTTPException(HttpServletResponse.SC_BAD_REQUEST,
						"Named or default graph expected for indirect reference request.");
			}
			return graph;
		}
		return null;
	} else {
		if (queryString != null) {
			throw new ClientHTTPException(HttpServletResponse.SC_BAD_REQUEST,
					"No parameters epxected for direct reference request.");
		}
		return vf.createURI(requestURL);
	}
}
 
Example 2
Source File: SmokeTest.java    From anno4j with Apache License 2.0 6 votes vote down vote up
public void testDynamicQuery() throws Exception {
	// create a Document
	Document doc = new Document();
	doc.setTitle("Getting Started");

	// add a Document to the repository
	ValueFactory vf = con.getValueFactory();
	URI id = vf
			.createURI("http://meta.leighnet.ca/data/2009/getting-started");
	con.addObject(id, doc);

	// retrieve a Document by id
	doc = con.getObject(Document.class, id);
	assertEquals("Getting Started", doc.getTitle());

	// retrieve a Document by title using a dynamic query
	ObjectQuery query = con
			.prepareObjectQuery("PREFIX gs:<http://meta.leighnet.ca/rdf/2009/gs#>\n"
					+ "SELECT ?doc WHERE {?doc gs:title ?title}");
	query.setObject("title", "Getting Started");
	doc = query.evaluate(Document.class).singleResult();
	assertEquals("Getting Started", doc.getTitle());
}
 
Example 3
Source File: DavidsTestBOps.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testImplementationDefinedDefaultGraph ()
    throws Exception
{
    final BigdataSail sail = getTheSail () ;
    final ValueFactory vf = sail.getValueFactory();
    final RepositoryConnection cxn = getRepositoryConnection ( sail ) ;
    try {
        final String ns = "http://xyz.com/test#" ;
        final String kb = String.format ( "<%ss> <%sp> <%so> .", ns, ns, ns ) ;
        final String qs = String.format ( "select ?p ?o where { <%ss> ?p ?o .}", ns ) ;

        final Resource graphs [] = new Resource [] { vf.createURI ( String.format ( "%sg1", ns ) ), vf.createURI ( String.format ( "%sg2", ns ) ) } ;

        final Collection<BindingSet> expected = getExpected ( createBindingSet ( new BindingImpl ( "p", new URIImpl ( String.format ( "%sp", ns ) ) )
                                                                               , new BindingImpl ( "o", new URIImpl ( String.format ( "%so", ns ) ) )
                                                                               )
                                                      ) ;
        run ( sail, cxn, kb, graphs, qs, expected ) ;
    } finally {
        cxn.close();
    }
}
 
Example 4
Source File: ObjectConstructorMarshall.java    From anno4j with Apache License 2.0 6 votes vote down vote up
public ObjectConstructorMarshall(ValueFactory vf, Class<T> type)
		throws NoSuchMethodException {
	this.vf = vf;
	ValueFactory uf = ValueFactoryImpl.getInstance();
	this.datatype = uf.createURI("java:", type.getName());
	try {
		constructor = type.getConstructor(new Class[] { String.class });
	} catch (NoSuchMethodException e) {
		try {
			constructor = type
					.getConstructor(new Class[] { CharSequence.class });
		} catch (NoSuchMethodException e1) {
			throw e;
		}
	}
}
 
Example 5
Source File: DavidsTestBOps.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testExplicitDefaultAndNamedGraphNoGraphKeyword ()
    throws Exception
{
    final BigdataSail sail = getTheSail () ;
    final ValueFactory vf = sail.getValueFactory();
    final RepositoryConnection cxn = getRepositoryConnection ( sail ) ;
    try {
    String ns = "http://xyz.com/test#" ;
    String kb = String.format ( "<%ss> <%sp> <%so> .", ns, ns, ns ) ;
    String qs = String.format ( "select ?s from <%sg1> from named <%sg2> where { ?s ?p ?o .}", ns, ns ) ;

    Resource graphs [] = new Resource [] { vf.createURI ( String.format ( "%sg1", ns ) ), vf.createURI ( String.format ( "%sg2", ns ) ) } ;

    Collection<BindingSet> expected = getExpected ( createBindingSet ( new BindingImpl ( "s", new URIImpl ( String.format ( "%ss", ns ) ) ) ) ) ;

    run ( sail, cxn, kb, graphs, qs, expected ) ;
    } finally {
        cxn.close();
    }
}
 
Example 6
Source File: TestGPO.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testRemoveValue() {
	doLoadData();
	
	final ValueFactory vf = om.getValueFactory();

    final URI workeruri = vf.createURI("gpo:#123");
    IGPO workergpo = om.getGPO(workeruri);
    final URI worksFor = vf.createURI("attr:/employee#worksFor");
	
    assertFalse(workergpo.getValues(worksFor).isEmpty());
    Value old = workergpo.getValue(worksFor);
    while (old != null) {
    	workergpo.removeValue(worksFor, old);
    	old = workergpo.getValue(worksFor);
    }
    assertTrue(workergpo.getValues(worksFor).isEmpty());
}
 
Example 7
Source File: DavidsTestBOps.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testNamedGraphNoGraphKeyword1 ()
    throws Exception
{
    final BigdataSail sail = getTheSail () ;
    final ValueFactory vf = sail.getValueFactory();
    final RepositoryConnection cxn = getRepositoryConnection ( sail ) ;
    try {
    String ns = "http://xyz.com/test#" ;
    String kb = String.format ( "<%ss> <%sp> <%so> .", ns, ns, ns ) ;
    String qs = String.format ( "select ?s from named <%sg2> where { ?s ?p ?o .}", ns ) ;

    Resource graphs [] = new Resource [] { vf.createURI ( String.format ( "%sg1", ns ) ), vf.createURI ( String.format ( "%sg2", ns ) ) } ;

    Collection<BindingSet> expected = getExpected () ;

    run ( sail, cxn, kb, graphs, qs, expected ) ;
    } finally {
        cxn.close();
    }
}
 
Example 8
Source File: StressTest_ClosedByInterrupt_RW.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void doInsert(final RepositoryConnection conn, final int loop, final int index)
        throws RepositoryException {
    final ValueFactory vf = conn.getValueFactory();
    final URI c = vf.createURI("context:loop:" + loop + ":item:" + index);
    final URI s = vf.createURI("subject:loop:" + loop + ":item:" + index);
    for (int x = 0; x < NUM_STATEMENTS_PER_INSERT; ++x) {
        final URI p = vf.createURI("predicate:" + x);
        final Literal o = vf.createLiteral("SomeValue");
        conn.add(s, p, o, c);
    }
}
 
Example 9
Source File: SesameTransformationInjectSwitchSet.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<SesameSwitchSetInjectMatch> matches) throws RepositoryException {
	final RepositoryConnection con = driver.getConnection();
	final ValueFactory vf = driver.getValueFactory();

	final URI currentPositionProperty = vf.createURI(BASE_PREFIX + CURRENTPOSITION);

	for (final SesameSwitchSetInjectMatch match : matches) {
		final URI sw = match.getSw();
		final RepositoryResult<Statement> statements = con.getStatements(sw, currentPositionProperty, null, true);
		if (!statements.hasNext()) {
			continue;
		}

		final Statement oldStatement = statements.next();

		// delete old statement
		con.remove(oldStatement);

		// get next enum value
		final URI currentPositionURI = (URI) oldStatement.getObject();
		final String currentPositionRDFString = currentPositionURI.getLocalName();
		final String currentPositionString = RdfHelper.removePrefix(Position.class, currentPositionRDFString);
		final Position currentPosition = Position.valueOf(currentPositionString);
		final Position newCurrentPosition = Position.values()[(currentPosition.ordinal() + 1) % Position.values().length];
		final String newCurrentPositionString = RdfHelper.addEnumPrefix(newCurrentPosition);
		final URI newCurrentPositionUri = vf.createURI(BASE_PREFIX + newCurrentPositionString);

		// set new value
		con.add(sw, currentPositionProperty, newCurrentPositionUri);
	}
}
 
Example 10
Source File: SesameTransformationInjectConnectedSegments.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<SesameConnectedSegmentsInjectMatch> matches) throws Exception {
	final RepositoryConnection connection = driver.getConnection();
	final ValueFactory vf = driver.getValueFactory();

	final URI length = vf.createURI(BASE_PREFIX + LENGTH);
	final URI connectsTo = vf.createURI(BASE_PREFIX + CONNECTS_TO);
	final URI monitoredBy = vf.createURI(BASE_PREFIX + MONITORED_BY);
	final URI segmentType = vf.createURI(BASE_PREFIX + SEGMENT);
	final Literal lengthLiteral = vf.createLiteral(TrainBenchmarkConstants.DEFAULT_SEGMENT_LENGTH);

	for (final SesameConnectedSegmentsInjectMatch match : matches) {
		// create (segment2) node
		final Long newVertexId = driver.generateNewVertexId();
		final URI segment2 = vf.createURI(BASE_PREFIX + ID_PREFIX + newVertexId);
		connection.add(segment2, RDF.TYPE, segmentType);
		connection.add(segment2, length, lengthLiteral);

		// (segment1)-[:connectsTo]->(segment2)
		connection.add(match.getSegment1(), connectsTo, segment2);
		// (segment2)-[:connectsTo]->(segment3)
		connection.add(segment2, connectsTo, match.getSegment3());

		// (segment2)-[:monitoredBy]->(sensor)
		connection.add(segment2, monitoredBy, match.getSensor());

		// remove (segment1)-[:connectsTo]->(segment3)
		connection.remove(match.getSegment1(), connectsTo, match.getSegment3());
	}
}
 
Example 11
Source File: SesameTransformationRepairRouteSensor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<SesameRouteSensorMatch> matches) throws RepositoryException {
	final RepositoryConnection con = driver.getConnection();
	final ValueFactory vf = driver.getValueFactory();

	final URI requires = vf.createURI(BASE_PREFIX + REQUIRES);

	for (final SesameRouteSensorMatch match : matches) {
		final Resource route = match.getRoute();
		final Resource sensor = match.getSensor();

		con.add(route, requires, sensor);
	}
}
 
Example 12
Source File: TestGPO.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testHashCode() {
	doLoadData();
	
	final ValueFactory vf = om.getValueFactory();

    final URI clssuri = vf.createURI("gpo:#1");
    IGPO clssgpo = om.getGPO(clssuri);
	
    assertTrue(clssgpo.hashCode() == clssgpo.getId().hashCode());
    assertTrue(clssgpo.hashCode() == clssuri.hashCode());
}
 
Example 13
Source File: AbstractRAMGraphTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The data:
 * 
 * <pre>
 * @prefix : <http://www.bigdata.com/> .
 * @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 * @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 * @prefix foaf: <http://xmlns.com/foaf/0.1/> .
 * 
 * #: {
 *    :Mike rdf:type foaf:Person .
 *    :Bryan rdf:type foaf:Person .
 *    :Martyn rdf:type foaf:Person .
 * 
 *    :Mike rdfs:label "Mike" .
 *    :Bryan rdfs:label "Bryan" .
 *    :DC rdfs:label "DC" .
 * 
 *    :Mike foaf:knows :Bryan .
 *    :Bryan foaf:knows :Mike .
 *    :Bryan foaf:knows :Martyn .
 *    :Martyn foaf:knows :Bryan .
 * #}
 * </pre>
 */
public SmallGraphProblem() throws Exception {

    final RAMGraph g = getGraphFixture().getGraph();

    final ValueFactory vf = g.getValueFactory();

    rdfType = vf.createURI(RDF.TYPE.stringValue());
    rdfsLabel = vf.createURI(RDFS.LABEL.stringValue());
    foafKnows = vf.createURI("http://xmlns.com/foaf/0.1/knows");
    foafPerson = vf.createURI("http://xmlns.com/foaf/0.1/Person");
    mike = vf.createURI("http://www.bigdata.com/Mike");
    bryan = vf.createURI("http://www.bigdata.com/Bryan");
    martyn = vf.createURI("http://www.bigdata.com/Martyn");
    dc = vf.createURI("http://www.bigdata.com/DC");
    
    g.add(vf.createStatement(mike, rdfType, foafPerson));
    g.add(vf.createStatement(bryan, rdfType, foafPerson));
    g.add(vf.createStatement(martyn, rdfType, foafPerson));

    g.add(vf.createStatement(mike, rdfsLabel, vf.createLiteral("Mike")));
    g.add(vf.createStatement(bryan, rdfsLabel, vf.createLiteral("Bryan")));
    g.add(vf.createStatement(dc, rdfsLabel, vf.createLiteral("DC")));

    g.add(vf.createStatement(mike, foafKnows, bryan));
    g.add(vf.createStatement(bryan, foafKnows, mike));
    g.add(vf.createStatement(bryan, foafKnows, martyn));
    g.add(vf.createStatement(martyn, foafKnows, bryan));

}
 
Example 14
Source File: TestGPO.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testBound() {
	doLoadData();
	
	final ValueFactory vf = om.getValueFactory();

    final URI workeruri = vf.createURI("gpo:#123");
    IGPO workergpo = om.getGPO(workeruri);
    final URI worksFor = vf.createURI("attr:/employee#worksFor");
    final URI notWorksFor = vf.createURI("attr:/employee#notWorksFor");
	
    assertTrue(workergpo.isBound(worksFor));
    
    assertFalse(workergpo.isBound(notWorksFor));
}
 
Example 15
Source File: StressTest_ClosedByInterrupt_RW.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void doDelete(final RepositoryConnection conn, final int loop, final int index)
        throws RepositoryException {
    final ValueFactory vf = conn.getValueFactory();
    final URI context = vf.createURI("context:loop:" + loop + ":item:" + index);
    final Collection<Statement> statements = getStatementsForContext(conn, context);
    for (Statement statement : statements) {
        conn.remove(statement, context);
    }
}
 
Example 16
Source File: TestSingleTailRule.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testOptionalFilter()
        throws Exception
    {
        final BigdataSail sail = getSail();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
//        final Sail sail = new MemoryStore();
//        final Repository repo = new SailRepository(sail);
        
        repo.initialize();
        final RepositoryConnection cxn = repo.getConnection();
        cxn.setAutoCommit(false);
        
        try {
    
            final ValueFactory vf = sail.getValueFactory();

            URI s = vf.createURI("urn:test:s");
            URI p1 = vf.createURI("urn:test:p1");
            URI p2 = vf.createURI("urn:test:p2");
            Literal v1 = vf.createLiteral(1);
            Literal v2 = vf.createLiteral(2);
            Literal v3 = vf.createLiteral(3);
            cxn.add(s, p1, v1);
            cxn.add(s, p2, v2);
            cxn.add(s, p1, v3);
            cxn.commit();
            
            String qry = 
                "PREFIX :<urn:test:> " +
                "SELECT ?s ?v1 ?v2 " +
                "WHERE { " +
                "  ?s :p1 ?v1 . " +
                "  OPTIONAL {?s :p2 ?v2 FILTER(?v1 < 3) } " +
                "}";
            
            TupleQuery query = cxn.prepareTupleQuery(QueryLanguage.SPARQL, qry);
            TupleQueryResult result = query.evaluate();
            
//            while (result.hasNext()) {
//                System.err.println(result.next());
//            }
            
            Collection<BindingSet> solution = new LinkedList<BindingSet>();
            solution.add(createBindingSet(new Binding[] {
                new BindingImpl("s", s),
                new BindingImpl("v1", v1),
                new BindingImpl("v2", v2),
            }));
            solution.add(createBindingSet(new Binding[] {
                new BindingImpl("s", s),
                new BindingImpl("v1", v3),
            }));
            
            compare(result, solution);
            
        } finally {
            cxn.close();
            if (sail instanceof BigdataSail)
                ((BigdataSail)sail).__tearDownUnitTest();
        }
            
    }
 
Example 17
Source File: TestEncodeDecodeValue.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test case for <code>foo(s,p,o,x,y,z)</code>. This is case is equivalent to
 * <code>foo(s,p,o,new Resource[]{x,y,z}). It is allowed and refers
 * to the specified named graphs.
 * 
 * @see com.bigdata.rdf.store.BD#NULL_GRAPH
 * @see <a href="http://trac.bigdata.com/ticket/1177"> Resource... contexts
 *      not encoded/decoded according to openrdf semantics (REST API) </a>
 */
public void test_encodeDecodeContexts_quads_context_x_y_z() {

   final ValueFactory vf = new ValueFactoryImpl();
   final URI x = vf.createURI(":x");
   final URI y = vf.createURI(":y");
   final URI z = vf.createURI(":z");

   final String[] encoded = EncodeDecodeValue.encodeContexts(new Resource[] {
         x, y, z });

   // Note: Encoding wraps URI with <...>
   assertEquals(new String[] { "<:x>", "<:y>", "<:z>" }, encoded);

   final Resource[] decoded = EncodeDecodeValue.decodeContexts(encoded);

   assertEquals(new Resource[] { x, y, z }, decoded);

}
 
Example 18
Source File: TestGPO.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Checks for reverse linkset referential integrity when property removed
 */
public void testLinkSetConsistency1() {
	doLoadData();
	
	final ValueFactory vf = om.getValueFactory();

    final URI workeruri = vf.createURI("gpo:#123");
    IGPO workergpo = om.getGPO(workeruri);
    final URI worksFor = vf.createURI("attr:/employee#worksFor");
    final ILinkSet ls = workergpo.getLinksOut(worksFor);
    
    assertTrue(ls.size() > 0);
    
    final IGPO employer = ls.iterator().next();
    
    final ILinkSet employees = employer.getLinksIn(worksFor);
    assertTrue(employees.contains(workergpo));
    
    workergpo.removeValue(worksFor, employer.getId());
    
    assertFalse(employees.contains(workergpo));
}
 
Example 19
Source File: TestEncodeDecodeValue.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Test case for <code>foo(s,p,o,x,null,z)</code>. This is case is equivalent
 * to <code>foo(s,p,o,new Resource[]{x,null,z}). It is allowed and refers
 * to the named graph (x), the null graph, and the named graph (z).
 * <p>
 * Note: When a <code>null</code> is encoded into an HTTP parameter it will
 * be represented as <code>c=</code>. On decode, the value will be a zero
 * length string. Therefore this test verifies that we decode a zero length
 * string into a <code>null</code>.
 * 
 * @see com.bigdata.rdf.store.BD#NULL_GRAPH
 * @see <a href="http://trac.bigdata.com/ticket/1177"> Resource... contexts
 *      not encoded/decoded according to openrdf semantics (REST API) </a>
 */
public void test_encodeDecodeContexts_quads_context_x_null_z_2() {

   final ValueFactory vf = new ValueFactoryImpl();
   final URI x = vf.createURI(":x");
   final URI z = vf.createURI(":z");

   // Note: Encoding wraps URI with <...>
   final String[] encoded = new String[] { "<:x>", "", "<:z>" };

   final Resource[] decoded = EncodeDecodeValue.decodeContexts(encoded);

   assertEquals(new Resource[] { x, null, z }, decoded);

}
 
Example 20
Source File: TestGPO.java    From database with GNU General Public License v2.0 3 votes vote down vote up
public void testValues() {
	doLoadData();
	
	final ValueFactory vf = om.getValueFactory();

    final URI workeruri = vf.createURI("gpo:#123");
    IGPO workergpo = om.getGPO(workeruri);
    final URI worksFor = vf.createURI("attr:/employee#worksFor");
    final URI notWorksFor = vf.createURI("attr:/employee#notWorksFor");
	
    assertTrue(workergpo.getValues(worksFor).size() > 0);
    
    assertTrue(workergpo.getValues(notWorksFor).isEmpty());
	
}