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

The following examples show how to use org.openrdf.model.ValueFactory#createStatement() . 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: SesameTransformationRepairPosLength.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void activate(final Collection<SesamePosLengthMatch> matches) throws RepositoryException {
	final RepositoryConnection con = driver.getConnection();
	final ValueFactory vf = driver.getValueFactory();

	final URI lengthProperty = vf.createURI(BASE_PREFIX + LENGTH);

	for (final SesamePosLengthMatch match : matches) {
		final Resource segment = match.getSegment();
		final Value length = match.getLength();

		final RepositoryResult<Statement> statementsToRemove = con.getStatements(segment, lengthProperty, length, true);
		while (statementsToRemove.hasNext()) {
			final Statement oldStatement = statementsToRemove.next();
			con.remove(oldStatement);
		}

		final Integer lengthInteger = new Integer(length.stringValue());
		final Integer newLengthInteger = -lengthInteger + 1;
		final Literal newLength = vf.createLiteral(newLengthInteger);
		final Statement newStatement = vf.createStatement(segment, lengthProperty, newLength);
		con.add(newStatement);
	}
}
 
Example 2
Source File: AbstractRMLProcessor.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Statement> processSubjectTypeMap(SesameDataSet dataset, Resource subject,
		SubjectMap subjectMap, Object node) {
	List<Statement> statements=new LinkedList<>();
	ValueFactory myFactory = ValueFactoryImpl.getInstance();
	
	// Add the type triples
	Set<org.openrdf.model.URI> classIRIs = subjectMap.getClassIRIs();
	if (subject != null)
		for (org.openrdf.model.URI classIRI : classIRIs) {
			Statement st = myFactory.createStatement((Resource) subject, RDF.TYPE,
       				(Value) classIRI);
               //dataset.add(subject, predicate, object);
               dataset.addStatement(st);
               
               statements.add(st);
			
			//dataset.add(subject, RDF.TYPE, classIRI);
		}
	
	return statements;
}
 
Example 3
Source File: JoinRMLPerformer.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
/**
   * Compare expressions from join to complete it
   * 
   * @param node current object in parent iteration
   * @param dataset
   * @param map 
   */
  @Override
  public Collection<Statement> perform(Object node, SesameDataSet dataset, TriplesMap map) {
  	List<Statement> statements=new LinkedList<>();
  	
      Value object = processor.processSubjectMap(dataset, map.getSubjectMap(), node);

      if (object == null){
          return statements;
      }       
      log.debug("[JoinRMLPerformer:addTriples] Subject "
                  + subject + " Predicate " + predicate + "Object " + object.toString());
      
      //add the join triple
      ValueFactory myFactory = ValueFactoryImpl.getInstance();
Statement st = myFactory.createStatement((Resource) subject, predicate,
		(Value) object);
      //dataset.add(subject, predicate, object);
      dataset.addStatement(st);
      
      statements.add(st);
      return statements;
  }
 
Example 4
Source File: WriteStatementsKnowledgeStore.java    From EventCoreference with Apache License 2.0 6 votes vote down vote up
static public org.openrdf.model.Statement castJenaOpenRdf(com.hp.hpl.jena.rdf.model.Statement jenaStatement, String modelName) {
    org.openrdf.model.Statement statement = null;
    try {
        ValueFactory valueFactory = ValueFactoryImpl.getInstance();
        URI modelURI = valueFactory.createURI(modelName);
        URI subject = valueFactory.createURI(jenaStatement.getSubject().getURI());
        URI sem = valueFactory.createURI(jenaStatement.getPredicate().getURI());
        if (jenaStatement.getObject().isLiteral()) {
            Literal objectLiteral = valueFactory.createLiteral(jenaStatement.getObject().toString());
             statement = valueFactory.createStatement(subject, sem, objectLiteral, modelURI);
        }

        else {
            URI objectUri = valueFactory.createURI(jenaStatement.getObject().asResource().getURI());
             statement = valueFactory.createStatement(subject, sem, objectUri, modelURI);
        }
    } catch (Exception e) {
        System.out.println("jenaStatement.toString() = " + jenaStatement.toString());
        e.printStackTrace();
    }
    return statement;
}
 
Example 5
Source File: SesameTransformationInjectRouteSensor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<SesameRouteSensorInjectMatch> matches) throws RepositoryException {
	final RepositoryConnection connection = driver.getConnection();
	final ValueFactory vf = connection.getValueFactory();

	final List<Statement> statementsToRemove = new ArrayList<>(matches.size());
	final URI requires = vf.createURI(BASE_PREFIX + ModelConstants.REQUIRES);
	for (final SesameRouteSensorInjectMatch match : matches) {
		final Statement statement = vf.createStatement(match.getRoute(), requires, match.getSensor());
		statementsToRemove.add(statement);
	}
	connection.remove(statementsToRemove);
}
 
Example 6
Source File: CustomSesameDataset.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public void remove(Resource s, URI p, Value o, Resource... context) {
	try {
		RepositoryConnection con = currentRepository.getConnection();
		try {
			ValueFactory myFactory = con.getValueFactory();
			Statement st = myFactory.createStatement((Resource) s, p,
					(Value) o);
			con.remove(st, context);
		} finally {
			con.close();
		}
	} catch (Exception e) {
		// handle exception
	}
}
 
Example 7
Source File: JoinReferenceRMLPerformer.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
/**
 * Compare expressions from join to complete it
 * 
 * @param node current object in parent iteration
 * @param dataset
 * @param map 
 */
@Override
public Collection<Statement> perform(Object node, SesameDataSet dataset, TriplesMap map) {
	List<Statement> statements=new LinkedList<>();
    //Value object = processor.processSubjectMap(dataset, map.getSubjectMap(), node);
	List<Object> objects = processor.extractValueFromNode(node, expr);
			
    if (objects == null){
        return statements;
    }       
    log.debug("[JoinReferenceRMLPerformer:addTriples] Subject "
                + subject + " Predicate " + predicate + "Object " + objects.toString());
    
    ValueFactory myFactory = ValueFactoryImpl.getInstance();		
    
    //add the join triple
    for(Object obj:objects){
    	Statement st = myFactory.createStatement((Resource) subject, predicate,
				(Value) new LiteralImpl(obj.toString().trim()));
        //dataset.add(subject, predicate, object);
        dataset.addStatement(st);
        
        statements.add(st);
        
    	//dataset.add(subject, predicate, new LiteralImpl(obj.toString().trim()));
    }
    return statements;
}
 
Example 8
Source File: ConversionUtil.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static org.openrdf.model.Statement toOpenRDF(Statement statement, ValueFactory factory) {
	Resource subject = (Resource)toOpenRDF(statement.getSubject(), factory);
	org.openrdf.model.URI predicate = toOpenRDF(statement.getPredicate(), factory);
	Value object = toOpenRDF(statement.getObject(), factory);
	org.openrdf.model.URI context = toOpenRDF(statement.getContext(), factory);
	
	return factory.createStatement(subject, predicate, object, context);
}
 
Example 9
Source File: StressTestConcurrentRestApiRequests.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void doApplyToNamespace(final RemoteRepository repo,
        final UUID uuid) throws Exception {

    /*
     * Setup data to be deleted.
     * 
     * Note: This uses SELECT rather than CONSTRUCT since we need to get
     * the graph as well when addressing a QUADS mode namespace.
     */
    final ValueFactory vf = ValueFactoryImpl.getInstance();
    final Collection<Statement> stmts = new ArrayList<>(batchSize);
    TupleQueryResult result = null;
    try {
        if (quads) {
            result = repo.prepareTupleQuery(
                    "SELECT * WHERE {GRAPH ?g {?s ?p ?o} }")
                    .evaluate();
        } else {
            result = repo.prepareTupleQuery(
                    "SELECT * WHERE {?s ?p ?o}").evaluate();
        }
        while (result.hasNext() && stmts.size() < batchSize) {
            final BindingSet bset = result.next();
            final Resource s = (Resource) bset.getBinding("s")
                    .getValue();
            final URI p = (URI) bset.getBinding("p").getValue();
            final Value o = (Value) bset.getBinding("o").getValue();
            final Resource g = (Resource) (quads ? bset.getBinding("g")
                    .getValue() : null);
            final Statement stmt = quads ? vf.createStatement(s, p, o,
                    g) : vf.createStatement(s, p, o);
            stmts.add(stmt);
        }
    } finally {
        if (result != null)
            result.close();
    }

    // do mutation.
    repo.remove(new RemoteRepository.RemoveOp(stmts), uuid);

}
 
Example 10
Source File: ConditionalJoinRMLPerformer.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
/**
 * Compare expressions from join to complete it
 * 
 * @param node current object in parent iteration
 * @param dataset
 * @param map 
 */
@Override
public Collection<Statement> perform(Object node, SesameDataSet dataset, TriplesMap map) {
	List<Statement> statements=new LinkedList<>();
    ValueFactory myFactory = ValueFactoryImpl.getInstance();
	
    Value object;
    
    //iterate the conditions, execute the expressions and compare both values
    if(conditions != null){
        boolean flag = true;

        iter: for (String expr : conditions.keySet()) {
            String cond = conditions.get(expr);
            List<Object> values = processor.extractValueFromNode(node, expr);
            
            for(Object value : values){
                if(value == null || !value.equals(cond)){
                        flag = false;
                        break iter;
                }
            }
        }
        if(flag){
            object = processor.processSubjectMap(dataset, map.getSubjectMap(), node);
            if (object != null){
            	Statement st = myFactory.createStatement((Resource) subject, predicate,
        				(Value) object);
                //dataset.add(subject, predicate, object);
                dataset.addStatement(st);
                
                statements.add(st);
            	
                //dataset.add(subject, predicate, object);
                log.debug("[ConditionalJoinRMLPerformer:addTriples] Subject "
                            + subject + " Predicate " + predicate + "Object " + object.toString());
            } 
        }
    }
    return statements;
}