Java Code Examples for org.eclipse.rdf4j.model.IRI#stringValue()

The following examples show how to use org.eclipse.rdf4j.model.IRI#stringValue() . 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: ValueTypeSupportRegistryImpl.java    From inception with Apache License 2.0 6 votes vote down vote up
private String getDataType(KBStatement aStatement, KBProperty aProperty)
{
    String datatype = null;
    
    if (aStatement.getValue() != null) {
        Class<?> clazz = aStatement.getValue().getClass();
        IRI type = DefaultDatatypeMapper.getDatatypeURI(clazz);

        // Mapping fails for NaiveIRI class, so check manually
        // if the value is an instance of IRI
        if (type == null && aStatement.getValue() instanceof IRI) {
            type = XMLSchema.ANYURI;
        }
        datatype = type != null ? type.stringValue() : null;
    }
    
    if (datatype == null && aProperty != null && aProperty.getRange() != null) {
        return aProperty.getRange();
    }
    
    if (datatype == null) {
        datatype = XMLSchema.STRING.stringValue();
    }
    
    return datatype;
}
 
Example 2
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testSPOSubjRange() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    IRI cpu2 = VF.createIRI(litdupsNS, "cpu2");
    IRI cpu3 = VF.createIRI(litdupsNS, "cpu3");
    IRI loadPerc = VF.createIRI(litdupsNS, "loadPerc");
    Literal six = VF.createLiteral("6");
    Literal sev = VF.createLiteral("7");
    Literal ten = VF.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu2, loadPerc, sev);
    conn.add(cpu3, loadPerc, ten);
    conn.commit();

    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" +
            "select * where {" +
            "?s ?p ?o.\n" +
            "FILTER(org.apache:range(?s, <" + cpu.stringValue() + ">, <" + cpu2.stringValue() + ">))." +
            "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(cth.getCount(), 2);
}
 
Example 3
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testSPOPredRange() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    IRI loadPerc = VF.createIRI(litdupsNS, "loadPerc1");
    IRI loadPerc2 = VF.createIRI(litdupsNS, "loadPerc2");
    IRI loadPerc3 = VF.createIRI(litdupsNS, "loadPerc3");
    IRI loadPerc4 = VF.createIRI(litdupsNS, "loadPerc4");
    Literal six = VF.createLiteral("6");
    Literal sev = VF.createLiteral("7");
    Literal ten = VF.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu, loadPerc2, sev);
    conn.add(cpu, loadPerc4, ten);
    conn.commit();

    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" +
            "select * where {" +
            "<" + cpu.stringValue() + "> ?p ?o.\n" +
            "FILTER(org.apache:range(?p, <" + loadPerc.stringValue() + ">, <" + loadPerc3.stringValue() + ">))." +
            "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(2, cth.getCount());
}
 
Example 4
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testPOPredRange() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    IRI loadPerc = VF.createIRI(litdupsNS, "loadPerc1");
    IRI loadPerc2 = VF.createIRI(litdupsNS, "loadPerc2");
    IRI loadPerc3 = VF.createIRI(litdupsNS, "loadPerc3");
    IRI loadPerc4 = VF.createIRI(litdupsNS, "loadPerc4");
    Literal six = VF.createLiteral("6");
    Literal sev = VF.createLiteral("7");
    Literal ten = VF.createLiteral("10");
    conn.add(cpu, loadPerc, six);
    conn.add(cpu, loadPerc2, sev);
    conn.add(cpu, loadPerc4, ten);
    conn.commit();

    String query = "PREFIX org.apache: <" + NAMESPACE + ">\n" +
            "select * where {" +
            "?x ?p ?o.\n" +
            "FILTER(org.apache:range(?p, <" + loadPerc.stringValue() + ">, <" + loadPerc3.stringValue() + ">))." +
            "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(cth.getCount(), 2);
}
 
Example 5
Source File: SmartUriAdapter.java    From rya with Apache License 2.0 6 votes vote down vote up
private static Entity convertMapToEntity(final RyaIRI subject, final Map<RyaIRI, Map<IRI, Value>> map) {
    final Entity.Builder entityBuilder = Entity.builder();
    entityBuilder.setSubject(subject);

    for (final Entry<RyaIRI, Map<IRI, Value>> typeEntry : map.entrySet()) {
        final RyaIRI type = typeEntry.getKey();
        final Map<IRI, Value> subMap = typeEntry.getValue();
        entityBuilder.setExplicitType(type);
        for (final Entry<IRI, Value> entry : subMap.entrySet()) {
            final IRI uri = entry.getKey();
            final Value value = entry.getValue();
            final RyaIRI ryaIri = new RyaIRI(uri.stringValue());
            final RyaIRI ryaName = new RyaIRI(uri.stringValue());
            final RyaType ryaType = new RyaType(value.stringValue());
            final Property property = new Property(ryaName, ryaType);
            entityBuilder.setProperty(ryaIri, property);
        }
    }
    final Entity entity = entityBuilder.build();
    return entity;
}
 
Example 6
Source File: RdfCloudTripleStoreConnectionTest.java    From rya with Apache License 2.0 6 votes vote down vote up
public void testEvaluate() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    IRI loadPerc = VF.createIRI(litdupsNS, "loadPerc");
    IRI uri1 = VF.createIRI(litdupsNS, "uri1");
    conn.add(cpu, loadPerc, uri1);
    conn.commit();

    String query = "select * where {" +
            "?x <" + loadPerc.stringValue() + "> ?o1." +
            "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    assertEquals(cth.getCount(), 1);
    conn.close();
}
 
Example 7
Source File: IriConverter.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public String convertToDatabaseColumn(IRI aIri)
{
    if (aIri == null) {
        return null;
    }
    
    return aIri.stringValue();
}
 
Example 8
Source File: PipelineQueryIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery() throws Exception {
    // Insert data
    final IRI alice = VF.createIRI("urn:Alice");
    final IRI bob = VF.createIRI("urn:Bob");
    final IRI eve = VF.createIRI("urn:Eve");
    final IRI relatedTo = VF.createIRI("urn:relatedTo");
    insert(alice, FOAF.KNOWS, bob);
    insert(alice, FOAF.KNOWS, alice);
    insert(alice, FOAF.KNOWS, eve);
    insert(alice, relatedTo, bob);
    insert(bob, FOAF.KNOWS, eve);
    insert(bob, relatedTo, bob);
    dao.flush();
    // Define query 1 and expected results
    final String query1 = "SELECT * WHERE {\n"
            + "  ?x <" + FOAF.KNOWS.stringValue() + "> ?y1 .\n"
            + "  ?x <" + relatedTo.stringValue() + "> ?y2 .\n"
            + "  FILTER (?y1 != ?y2) .\n"
            + "}";
    final List<String> varNames = Arrays.asList("x", "y1", "y2");
    final Multiset<BindingSet> expected1 = HashMultiset.create();
    expected1.add(new ListBindingSet(varNames, alice, alice, bob));
    expected1.add(new ListBindingSet(varNames, alice, eve, bob));
    expected1.add(new ListBindingSet(varNames, bob, eve, bob));
    // Define query 2 and expected results
    final String query2 = "SELECT * WHERE {\n"
            + "  ?x <" + FOAF.KNOWS.stringValue() + "> ?y1 .\n"
            + "  ?x <" + relatedTo.stringValue() + "> ?y2 .\n"
            + "  FILTER (?y1 = ?y2) .\n"
            + "}";
    final Multiset<BindingSet> expected2 = HashMultiset.create();
    expected2.add(new ListBindingSet(varNames, alice, bob, bob));
    // Execute and verify results
    testPipelineQuery(query1, expected1);
    testPipelineQuery(query2, expected2);
}
 
Example 9
Source File: RdfToRyaConversions.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link IRI} into a {@link RyaIRI} representation of the
 * {@code iri}.
 * @param iri the {@link IRI} to convert.
 * @return the {@link RyaIRI} representation of the {@code iri}.
 */
public static RyaIRI convertIRI(final IRI iri) {
    if (iri == null) {
        return null;
    }
    if (iri instanceof RangeIRI) {
        final RangeIRI riri = (RangeIRI) iri;
        return new RyaIRIRange(convertIRI(riri.getStart()), convertIRI(riri.getEnd()));
    }
    return new RyaIRI(iri.stringValue());
}
 
Example 10
Source File: SmartUriAdapter.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link Map} of {@link IRI}/{@link Value}s to a {@link Set} of
 * {@link Property}s.
 * @param map the {@link Map} of {@link IRI}/{@link Value}.
 * @return the {@link Set} of {@link Property}s.
 */
public static Set<Property> mapToProperties(final Map<IRI, Value> map) {
    final Set<Property> properties = new LinkedHashSet<>();
    for (final Entry<IRI, Value> entry : map.entrySet()) {
        final IRI iri = entry.getKey();
        final Value value = entry.getValue();

        final RyaIRI ryaIri = new RyaIRI(iri.stringValue());
        final RyaType ryaType = RdfToRyaConversions.convertValue(value);

        final Property property = new Property(ryaIri, ryaType);
        properties.add(property);
    }
    return properties;
}
 
Example 11
Source File: KnownFunctionParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public org.eclipse.rdf4j.query.algebra.evaluation.function.Function parse(IRI funcUri, TripleSource store)
		throws RDF4JException {
	{
		String name = null;
		if (wellKnownFunctions != null) {
			name = wellKnownFunctions.apply(funcUri);
		}
		if (name == null) {
			name = funcUri.stringValue();
		}
		return functionRegistry.get(name).orElse(null);
	}
}
 
Example 12
Source File: SmartUriAdapter.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes an {@link Entity} into a Smart {@link IRI}.
 * @param entity the {@link Entity} to serialize into a Smart URI.
 * @return the Smart {@link IRI}.
 * @throws SmartUriException
 */
public static IRI serializeUriEntity(final Entity entity) throws SmartUriException {
    final Map<IRI, Value> objectMap = new LinkedHashMap<>();

    // Adds the entity's types to the Smart URI
    final List<RyaIRI> typeIds = entity.getExplicitTypeIds();
    final Map<RyaIRI, String> ryaTypeMap = createTypeMap(typeIds);
    final IRI ryaTypeMapUri = createTypeMapUri(typeIds);
    final RyaType valueRyaType = new RyaType(XMLSchema.ANYURI, ryaTypeMapUri.stringValue());
    final Value typeValue = RyaToRdfConversions.convertValue(valueRyaType);
    objectMap.put(RYA_TYPES_URI, typeValue);

    final RyaIRI subject = entity.getSubject();
    final Map<RyaIRI, ImmutableMap<RyaIRI, Property>> typeMap = entity.getProperties();
    for (final Entry<RyaIRI, ImmutableMap<RyaIRI, Property>> typeEntry : typeMap.entrySet()) {
        final RyaIRI type = typeEntry.getKey();
        String typeShortName = ryaTypeMap.get(type);
        typeShortName = typeShortName != null ? typeShortName + "." : "";
        final ImmutableMap<RyaIRI, Property> typeProperties = typeEntry.getValue();
        for (final Entry<RyaIRI, Property> properties : typeProperties.entrySet()) {
            final RyaIRI key = properties.getKey();
            final Property property = properties.getValue();
            final String valueString = property.getValue().getData();
            final RyaType ryaType = property.getValue();

            //final RyaType ryaType = new RyaType(VF.createIRI(key.getData()), valueString);

            final Value value = RyaToRdfConversions.convertValue(ryaType);

            String formattedKey = key.getData();
            if (StringUtils.isNotBlank(typeShortName)) {
                formattedKey = addTypePrefixToUri(formattedKey, typeShortName);
            }
            final IRI iri = VF.createIRI(formattedKey);
            objectMap.put(iri, value);
        }
    }

    return serializeUri(subject, objectMap);
}
 
Example 13
Source File: SearchFunctionFactory.java    From rya with Apache License 2.0 5 votes vote down vote up
private SearchFunction getSearchFunctionInternal(final IRI searchFunction) throws QueryEvaluationException {
    SearchFunction sf = SEARCH_FUNCTION_MAP.get(searchFunction);

    if (sf != null) {
        return sf;
    } else {
        throw new QueryEvaluationException("Unknown Search Function: " + searchFunction.stringValue());
    }
        
   
}
 
Example 14
Source File: SmartUriAdapter.java    From rya with Apache License 2.0 4 votes vote down vote up
public static RyaIRI findSubject(final IRI uri) throws SmartUriException {
    final String uriString = uri.stringValue();
    return findSubject(uriString);
}
 
Example 15
Source File: FileManager.java    From semagrow with Apache License 2.0 4 votes vote down vote up
public static java.net.URI convertbackURI(IRI uri) throws URISyntaxException {
    return new java.net.URI(uri.stringValue());
}
 
Example 16
Source File: WikiDataReification.java    From inception with Apache License 2.0 4 votes vote down vote up
@Override
public List<KBQualifier> listQualifiers(RepositoryConnection aConnection, KnowledgeBase kb,
        KBStatement aStatement)
{
    SelectQuery query = SELECT(VAR_STATEMENT, VAR_PRED2, VAR_VALUE);
    query.where(
        new ValuesPattern(VAR_STATEMENT, iri(aStatement.getStatementId())),
        GraphPatterns.and(VAR_STATEMENT.has(VAR_PRED2, VAR_VALUE).filter(
            function(STRSTARTS, function(STR, VAR_PRED2), literalOf(PREFIX_PROP_QUALIFIER)))));
    query.limit(kb.getMaxResults());
    
    String queryId = toHexString(query.getQueryString().hashCode());
    
    log.trace("[{}] Query: {}", queryId, query.getQueryString());
    
    TupleQuery tupleQuery = aConnection.prepareTupleQuery(query.getQueryString());

    ValueFactory vf = SimpleValueFactory.getInstance();
    List<KBQualifier> qualifiers = new ArrayList<>();
    try (TupleQueryResult result = tupleQuery.evaluate()) {
        while (result.hasNext()) {
            BindingSet bindings = result.next();
            
            log.trace("[{}] Bindings: {}", queryId, bindings);
            
            Resource stmt = (Resource) bindings.getBinding("st").getValue();
            IRI pred2 = (IRI) bindings.getBinding("p2").getValue();
            Value value = bindings.getBinding("v").getValue();

            // Statement representing the secondary triple for the property value and/or
            // qualifiers with the value in the object position
            Statement secStatement = vf.createStatement(stmt, pred2, value);

            // Qualifier
            KBQualifier qualifier = new KBQualifier(aStatement,
                    new KBProperty(pred2.stringValue()), value);

            // Store the secondary original triples in the qualifier
            qualifier.getOriginalTriples().add(secStatement);
            aStatement.addQualifier(qualifier);
            
            qualifiers.add(qualifier);
        }
        
        return qualifiers;
    }        
}
 
Example 17
Source File: AggregationPipelineQueryNodeTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Var constant(IRI value) {
    return new Var(value.stringValue(), value);
}
 
Example 18
Source File: SparqlToPipelineTransformVisitorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
private static Var constant(IRI value) {
    return new Var(value.stringValue(), value);
}
 
Example 19
Source File: RDFXMLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws Exception {
	URL zipfileUrl = this.getClass().getResource("/org/eclipse/rdf4j/rio/rdfxml/sample-with-rdfxml-data.zip");

	assertNotNull("The sample-data.zip file must be present for this test", zipfileUrl);

	String url = "jar:" + zipfileUrl + "!/index.rdf";

	try (final InputStream in = new URL(url).openStream();) {
		parser.parse(in, url);
	}

	Collection<Statement> stmts = sc.getStatements();

	assertThat(stmts).hasSize(3);

	Iterator<Statement> iter = stmts.iterator();

	Statement stmt1 = iter.next();
	Statement stmt2 = iter.next();

	assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject());
	assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate());
	assertTrue(stmt1.getObject() instanceof IRI);

	IRI res = (IRI) stmt1.getObject();

	String resourceUrl = res.stringValue();

	assertThat(resourceUrl).startsWith("jar:" + zipfileUrl + "!");

	URL javaUrl = new URL(resourceUrl);
	assertEquals("jar", javaUrl.getProtocol());

	try (InputStream uc = javaUrl.openStream();) {
		assertEquals("The resource stream should be empty", -1, uc.read());
	}

	assertEquals(res, stmt2.getSubject());
	assertEquals(DC.TITLE, stmt2.getPredicate());
	assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}
 
Example 20
Source File: RyaTypeUtils.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 *
 * Creates a IRI {@link RyaType} object.
 * @param value the {@link IRI} object.
 * @return the {@link RyaType} with the data type set to
 * {@link XMLSchema#ANYURI} and the data set to the specified {@code value}.
 */
public static RyaType iriRyaType(final IRI value) {
    return new RyaType(XMLSchema.ANYURI, value.stringValue());
}