Package org.openrdf.model

Examples of org.openrdf.model.Model


        Assert.assertTrue(expected.isEmpty());
    }
   
    @Test
    public void testFloat(){
        Model graph = new TreeModel();
        URI id = vf.createURI("http://www.example.org/test");
        URI floatTestField = vf.createURI("http://www.example.org/field/float");
        graph.add(id, floatTestField, vf.createLiteral(Float.NaN));
        graph.add(id, floatTestField, vf.createLiteral(Float.POSITIVE_INFINITY));
        graph.add(id, floatTestField, vf.createLiteral(Float.NEGATIVE_INFINITY));
       
        RdfValueFactory valueFactory = new RdfValueFactory(graph,vf);
        Representation r = valueFactory.createRepresentation(id.stringValue());
        Set<Float> expected = new HashSet<Float>(Arrays.asList(
            Float.NaN, Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY));
View Full Code Here


        con.commit();
        con.close();
        Yard yard = getYard();
        Representation rep = yard.getRepresentation(subject.stringValue());
        Assert.assertTrue(rep instanceof RdfRepresentation);
        Model model = ((RdfRepresentation)rep).getModel();
        //Assert for the indirect statements to be present in the model
        Assert.assertFalse(model.filter(null, property2, null).isEmpty());
        Assert.assertFalse(model.filter(null, loop1, null).isEmpty());
        Assert.assertFalse(model.filter(null, loop2, null).isEmpty());
    }
View Full Code Here

     * @return the representation with the extracted data.
     * @throws RepositoryException
     */
    protected RdfRepresentation createRepresentationGraph(RepositoryConnection con, RdfValueFactory valueFactory, URI uri) throws RepositoryException{
        RdfRepresentation rep = valueFactory.createRdfRepresentation(uri);
        Model model = rep.getModel();
        extractRepresentation(con, model, uri, new HashSet<BNode>());
        return rep;
    }
View Full Code Here

                getConfig().getMaxQueryResultNumber());
            results = executeSparqlFieldQuery(con,query, limit, false);
            //parse the results and generate the Representations
            //create an own valueFactors so that all the data of the query results
            //are added to the same Sesame Model
            Model model = new TreeModel();
            RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
            List<Representation> representations = limit > 0 ?
                    new ArrayList<Representation>(limit) : new ArrayList<Representation>();
            while(results.hasNext()){
                BindingSet result = results.next();
                Value value = result.getValue(query.getRootVariableName());
                if(value instanceof URI){
                    //copy all data to the model and create the representation
                    RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI)value);
                    model.add(queryRoot, queryResult, value); //link the result with the query result
                    representations.add(rep);
                } //ignore non URI results
            }
            con.commit();
            return new SesameQueryResultList(model, query, representations);
View Full Code Here

                getConfig().getMaxQueryResultNumber());
            results = executeSparqlFieldQuery(con,query, limit, true);
            //parse the results and generate the Representations
            //create an own valueFactors so that all the data of the query results
            //are added to the same Sesame Model
            Model model = new TreeModel();
            RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
            List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit)
                    : new ArrayList<Representation>();
            Map<String,URI> bindings = new HashMap<String,URI>(query.getFieldVariableMappings().size());
            for(Entry<String,String> mapping : query.getFieldVariableMappings().entrySet()){
                bindings.put(mapping.getValue(), sesameFactory.createURI(mapping.getKey()));
            }
            while(results.hasNext()){
                BindingSet result = results.next();
                Value value = result.getValue(query.getRootVariableName());
                if(value instanceof URI){
                    URI subject = (URI) value;
                    //link the result with the query result
                    model.add(queryRoot, queryResult, subject);
                    //now copy over the other selected data
                    for(String binding : result.getBindingNames()){
                        URI property = bindings.get(binding);
                        if(property != null){
                            model.add(subject, property, result.getValue(binding));
                        } //else no mapping for the query.getRootVariableName()
                    }
                    //create a representation and add it to the results
                    representations.add(valueFactory.createRdfRepresentation(subject));
                } //ignore non URI results
View Full Code Here

     * Creates a {@link RdfRepresentation} for the parsed {@link URI}
     * @param subject the URI
     * @return the {@link RdfRepresentation}
     */
    public RdfRepresentation createRdfRepresentation(URI subject) {
        Model model = this.model == null ? new TreeModel() : this.model;
        return new RdfRepresentation(subject, model, this);
    }
View Full Code Here

    return workflow;
  }
 
  private void writePROV() throws IOException {
    ValueFactory factory = ValueFactoryImpl.getInstance();   
    Model stmts = new LinkedHashModel();
    FileInputStream fis = new FileInputStream(workflow.file());
    String workflowMD5sum = DigestUtils.md5Hex(fis);
    long currentTimeMilis = System.currentTimeMillis();
   
    // Define all the URI's that we are going to (re)use
    URI eURI = factory.createURI(PROV_NAMESPACE, "Entity");
    URI acURI = factory.createURI(PROV_NAMESPACE, "Activity");
    URI usedURI = factory.createURI(PROV_NAMESPACE, "used");
    URI wgbURI  = factory.createURI(PROV_NAMESPACE, "wasGeneratedBy");
    URI  genAtURI  = factory.createURI(PROV_NAMESPACE, "generatedAtTime");
    URI  startAtURI  = factory.createURI(PROV_NAMESPACE, "startedAtTime");
    URI  endAtURI  = factory.createURI(PROV_NAMESPACE, "endedAtTime");
   
    URI valueURI = factory.createURI(NAMESPACE, "value");
   
    URI agURI = factory.createURI(PROV_NAMESPACE, "Agent");
    URI watURI  = factory.createURI(PROV_NAMESPACE, "wasAttributedTo");
    URI wawURI  = factory.createURI(PROV_NAMESPACE, "wasAssociatedWith");
   
    URI planURI  = factory.createURI(PROV_NAMESPACE, "Plan");
    URI assoURI  = factory.createURI(PROV_NAMESPACE, "Association")
    URI qualAssoURI  = factory.createURI(PROV_NAMESPACE, "qualifiedAssociation")
   
    URI hadPlanURI  = factory.createURI(PROV_NAMESPACE, "hadPlan");
    URI hadAgentURI  = factory.createURI(PROV_NAMESPACE, "agent");
     
    URI platformURI = factory.createURI(NAMESPACE + "ducktape/", InetAddress.getLocalHost().getHostName() + "/" + Global.getSerialversionuid());
    URI workflowURI = factory.createURI(NAMESPACE + "workflow/", workflow.file().getAbsolutePath() + "/" + workflowMD5sum);
     
    // The software is the agent and the workflow is the plan
    stmts.add(factory.createStatement(platformURI, RDF.TYPE, agURI));
    stmts.add(factory.createStatement(workflowURI, RDF.TYPE, planURI));
   
    stmts.add(factory.createStatement(platformURI, RDFS.LABEL,
        Literals.createLiteral(factory, "ducktape on: " + InetAddress.getLocalHost().getHostName() + ", versionID: " + Global.getSerialversionuid())));
    stmts.add(factory.createStatement(workflowURI, RDFS.LABEL,
        Literals.createLiteral(factory, workflow.name() + ", date: " + new Date(workflow.file().lastModified()))));
   
 
   
    String moduleInstanceSumTimestamp = "module/instance/"+InetAddress.getLocalHost().getHostName()+"/"+workflowMD5sum+"/"+currentTimeMilis+"/";
    for (Module module : workflow.modules()) {
     
      for (ModuleInstance mi : module.instances()) {
        // Create provenance for the module (as an activity)
        URI miURI = factory.createURI(NAMESPACE + moduleInstanceSumTimestamp, module.name() + mi.moduleID());
        stmts.add(factory.createStatement(miURI, RDF.TYPE, acURI)); // Activity
        stmts.add(factory.createStatement(miURI, startAtURI, Literals.createLiteral(factory, new Date(mi.startTime())))); // Start time
        stmts.add(factory.createStatement(miURI, endAtURI, Literals.createLiteral(factory, new Date(mi.endTime())))); // end time     
        stmts.add(factory.createStatement(miURI, wawURI, platformURI)); // wasAssociatedWith
       
        // qualified Association
        BNode bn = factory.createBNode();
        stmts.add(factory.createStatement(bn, RDF.TYPE, assoURI));
        stmts.add(factory.createStatement(bn, hadPlanURI, workflowURI));
        stmts.add(factory.createStatement(bn, hadAgentURI, platformURI));
        stmts.add(factory.createStatement(miURI, qualAssoURI, bn));
       
        // Create provenance for the outputs (as entities)
        for (InstanceOutput io : mi.outputs()) {
          URI ioURI = factory.createURI(NAMESPACE + moduleInstanceSumTimestamp, module.name() + mi.moduleID() + "/output/" + io.name());
          stmts.add(factory.createStatement(ioURI, RDF.TYPE, eURI)); // entity
          stmts.add(factory.createStatement(ioURI, wgbURI, miURI)); // wasGeneratedBy
          stmts.add(factory.createStatement(ioURI, genAtURI, Literals.createLiteral(factory, new Date(io.creationTime())))); // generated at time
          stmts.add(factory.createStatement(ioURI, watURI, platformURI)); // wasAttributedTo
         
          // If we can create a literal of the value, save it and create a rdfs-label
          if (Literals.canCreateLiteral(io.value())) {
            stmts.add(factory.createStatement(ioURI, valueURI, Literals.createLiteral(factory, io.value())));
            stmts.add(factory.createStatement(ioURI, RDFS.LABEL, Literals.createLiteral(factory, io)));
          }
        }
       
        // Create provenance for the inputs (as entities)
        for (InstanceInput ii : mi.inputs()) {
          URI iiURI = null;
         
          if (ii.instanceOutput() != null) {
            iiURI = factory.createURI(NAMESPACE + moduleInstanceSumTimestamp, ii.instanceOutput().module().name()
                + ii.instanceOutput().instance().moduleID() + "/output/" + ii.name());
          } else {
            iiURI = factory.createURI(NAMESPACE + moduleInstanceSumTimestamp, module.name() + mi.moduleID()
                + "/input/" + ii.name());
           
            // If we can create a literal
            if (Literals.canCreateLiteral(ii.value())) {
              stmts.add(factory.createStatement(iiURI, valueURI, Literals.createLiteral(factory, ii.value())));
              stmts.add(factory.createStatement(iiURI, RDFS.LABEL, Literals.createLiteral(factory, ii)));
            }     
          }
             
          stmts.add(factory.createStatement(iiURI, RDF.TYPE, eURI)); // entity
          stmts.add(factory.createStatement(miURI, usedURI, iiURI)); // used         
        }
      }
    }
   
    File file = new File(root, PROV_FILE);
View Full Code Here

                if(ce != null) {
                    SailConnection con = store.getConnection();
                    try {
                        con.begin();

                        Model triples = new TreeModel();
                        ModelCommons.add(triples,con.getStatements(resource,null,null,true,store.getValueFactory().createURI(cacheContext)));
                        ce.setTriples(triples);

                        con.commit();
                    } catch(SailException ex) {
View Full Code Here

    // static only
  }
   
    public static Map<String,Metadata> parseRDFJSON(InputStream data) throws ParseException {
        try {
            Model model = Rio.parse(data, "", RDFFormat.RDFJSON);
           
            // convert Sesame Model into a map to Metadata objects
            Map<String,Metadata> result = new HashMap<String, Metadata>();
           
            for(Resource subject : model.subjects()) {
                Metadata m = new Metadata(subject.stringValue());
                for(org.openrdf.model.URI property : model.filter(subject, null, null).predicates()) {
                    Set<RDFNode> propValue = new HashSet<RDFNode>();
                    for(Value value : model.filter(subject, property, null).objects()) {
                        propValue.add(parseRDFJSONNode(value));
                    }
                    m.put(property.stringValue(),propValue);
                }
                result.put(subject.stringValue(),m);
View Full Code Here

    }
   
  
    public static void serializeRDFJSON(Map<String,Metadata> data, OutputStream out) throws IOException {
        ValueFactory vf = ValueFactoryImpl.getInstance();
        Model results = new LinkedHashModel();
       
        for(Map.Entry<String,Metadata> subject : data.entrySet()) {
            Resource subjectResource = stringToResource(subject.getKey(), vf);
            for(Map.Entry<String,Set<RDFNode>> predicate : subject.getValue().entrySet()) {
                org.openrdf.model.URI predicateURI = vf.createURI(predicate.getKey());
                for(RDFNode objectNode : predicate.getValue()) {
                    org.openrdf.model.Value objectValue;
                    if( objectNode instanceof Literal) {
                        if(((Literal) objectNode).getLanguage() != null )
                            objectValue = vf.createLiteral(((Literal)objectNode).getContent(),
                                                ((Literal)objectNode).getLanguage());
                        else if(((Literal) objectNode).getType() != null)
                            objectValue = vf.createLiteral(((Literal)objectNode).getContent(),
                                                vf.createURI(((Literal)objectNode).getType().getUri()));
                        else
                            objectValue = vf.createLiteral(((Literal)objectNode).getContent());
                    } else {
                        if( objectNode instanceof URI ) {
                            objectValue = vf.createURI(((URI)objectNode).getUri());
                        } else {
                            objectValue = vf.createBNode(((BNode)objectNode).getAnonId());
                        }
                    }
                    results.add(subjectResource, predicateURI, objectValue);
                }
            }
               
        }
       
View Full Code Here

TOP

Related Classes of org.openrdf.model.Model

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.