Package org.ontoware.rdf2go.model

Examples of org.ontoware.rdf2go.model.Model


                } catch (MessagingException e) {
                    // happens on unencoded file names! so just ignore it and leave the file name as it is
                }
                URI attachURI = URIGenerator.createNewRandomUniqueURI();
                rdf.add(NMO.hasAttachment, attachURI);
                Model m = rdf.getModel();
                m.addStatement(attachURI, RDF.type, NFO.Attachment);
                m.addStatement(attachURI, NFO.fileName, fileName);
                if (handler != null) {
                    if (encoding != null) {
                        m.addStatement(attachURI, NFO.encoding, encoding);
                    }
                }
                if (contentType != null) {
                    contentType = (new ContentType(contentType)).getBaseType();
                    m.addStatement(attachURI, NIE.mimeType, contentType.trim());
                }
                // TODO: encoding?
            }
           
            // append the content, if any
View Full Code Here


        InputStream stream = new ByteArrayInputStream(string.getBytes());
        RDFContainerFactory containerFactory = new RDFContainerFactoryImpl();
        URI id = rdf.getDescribedUri();
        RDFContainer result = containerFactory.getRDFContainer(id);
        extractor.extract(id, charset, stream, result);
        Model meta = result.getModel();
       
        // append metadata and full-text to a string buffer
        StringBuilder buffer = new StringBuilder(32 * 1024);
        append(buffer, extractor.getTitle(meta), "\n");
        append(buffer, extractor.getAuthor(meta), "\n");
        append(buffer, extractor.getDescription(meta), "\n");
        List<String> keywords = extractor.getKeywords(meta);
        for (String kw : keywords) {
            append(buffer, kw, " ");
        }
        buffer.append("\n");
        append(buffer, extractor.getText(meta), " ");
        logger.debug("text extracted:\n{}", buffer);
        meta.close();
       
        // return the buffer's content
        return buffer.toString();
    }
View Full Code Here

            File file = new File(args[i]);
            InputStream in = new FileInputStream(file);
            URI uri = new URIImpl(file.toURI().toString());
            RDFContainer rdfContainer = rdfFactory.getRDFContainer(uri);
            extractor.extract(uri, in, null, null, rdfContainer);
            Model model = rdfContainer.getModel();
            model.writeTo(System.out, Syntax.RdfXml);
            model.close();
        }
    }
View Full Code Here

    }

    public void computeEnhancements(ContentItem ci) throws EngineException {
        // get model from the extraction
        URIImpl docId;
        Model m = null;
        ci.getLock().readLock().lock();
        try {
            docId = new URIImpl(ci.getUri().getUnicodeString());
            m = this.extractor.extract(ci.getStream(), docId, ci.getMimeType());
        } catch (ExtractorException e) {
            throw new EngineException("Error while processing ContentItem "
                + ci.getUri()+" with Metaxa",e);
        } catch (IOException e) {
            throw new EngineException("Error while processing ContentItem "
                    + ci.getUri()+" with Metaxa",e);
        } finally {
            ci.getLock().readLock().unlock();
        }
        // Convert the RDF2go model to a Clerezza Graph and also extract
        // the extracted plain text from the model
        if (null == m) {
            log.debug("Unable to preocess ContentItem {} (mime type {}) with Metaxa",
                ci.getUri(),ci.getMimeType());
            return;
        }
        ContentSink plainTextSink;
        try {
            plainTextSink = ciFactory.createContentSink("text/plain");
        } catch (IOException e) {
            m.close();
            throw new EngineException("Unable to initialise Blob for storing" +
                "the plain text content",e);
        }
        HashMap<BlankNode, BNode> blankNodeMap = new HashMap<BlankNode, BNode>();
        RDF2GoUtils.urifyBlankNodes(m);
        ClosableIterator<Statement> it = m.iterator();
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
            plainTextSink.getOutputStream(), UTF8));
        boolean textExtracted = false; //used to detect if some text was extracted
        try {
            MGraph g = new SimpleMGraph(); //first add to a temporary graph
            while (it.hasNext()) {
                Statement oneStmt = it.next();
                //we need to treat triples that provide the plain/text
                //version differently. Such Objects need to be added to
                //the plain text Blob!
                if(oneStmt.getSubject().equals(docId) &&
                        oneStmt.getPredicate().equals(NIE_PLAINTEXT_PROPERTY)){
                    String text = oneStmt.getObject().toString();
                    if(text != null && !text.isEmpty()){
                        try {
                            out.write(oneStmt.getObject().toString());
                        } catch (IOException e) {
                            throw new EngineException("Unable to write extracted" +
                                "plain text to Blob (blob impl: "
                                    + plainTextSink.getBlob().getClass()+")",e);
                        }
                        textExtracted = true;
                        if (includeText) {
                            NonLiteral subject = (NonLiteral) asClerezzaResource(oneStmt.getSubject(), blankNodeMap);
                            UriRef predicate = (UriRef) asClerezzaResource(oneStmt.getPredicate(), blankNodeMap);
                            Resource object = asClerezzaResource(oneStmt.getObject(), blankNodeMap);
                            g.add(new TripleImpl(subject, predicate, object));
                        }
                    }
                } else { //add metadata to the metadata of the contentItem
                    NonLiteral subject = (NonLiteral) asClerezzaResource(oneStmt.getSubject(), blankNodeMap);
                    UriRef predicate = (UriRef) asClerezzaResource(oneStmt.getPredicate(), blankNodeMap);
                    Resource object = asClerezzaResource(oneStmt.getObject(), blankNodeMap);

                    if (null != subject && null != predicate && null != object) {
                        Triple t = new TripleImpl(subject, predicate, object);
                        g.add(t);
                        log.debug("added " + t.toString());
                    }
                }
            }
            //add the extracted triples to the metadata of the ContentItem
            ci.getLock().writeLock().lock();
            try {
                ci.getMetadata().addAll(g);
                g = null;
            } finally {
                ci.getLock().writeLock().unlock();
            }
        } finally {
            it.close();
            m.close();
            IOUtils.closeQuietly(out);
        }
        if(textExtracted){
            //add plain text to the content item
            UriRef blobUri = new UriRef("urn:metaxa:plain-text:"+randomUUID());
View Full Code Here

*/
public class RDF2GoUtils {

    public static void urifyBlankNodes(Model model) {
        HashMap<BlankNode, URI> nodeMap = new HashMap<BlankNode, URI>();
        Model add = RDF2Go.getModelFactory().createModel();
        add.open();

        Model remove = RDF2Go.getModelFactory().createModel();
        remove.open();
        for (Statement stmt : model) {
            Resource subj = stmt.getSubject();
            URI pred = stmt.getPredicate();
            Node obj = stmt.getObject();
            boolean match = false;
            if (subj instanceof BlankNode) {
                match = true;
                URI newSubj = nodeMap.get(subj);
                if (newSubj == null) {
                    newSubj = URIGenerator.createNewRandomUniqueURI();
                    nodeMap.put(subj.asBlankNode(), newSubj);
                }
                subj = newSubj;
            }
            if (obj instanceof BlankNode) {
                match = true;
                URI newObj = nodeMap.get(obj);
                if (newObj == null) {
                    newObj = URIGenerator.createNewRandomUniqueURI();
                    nodeMap.put(obj.asBlankNode(), newObj);
                }
                obj = newObj;
            }
            if (match) {
                remove.addStatement(stmt);
                add.addStatement(subj, pred, obj);
            }
        }
        ClosableIterator<Statement> addIt = add.iterator();
        ClosableIterator<Statement> removeIt = remove.iterator();
        model.update(new DiffImpl(addIt, removeIt));
        addIt.close();
        removeIt.close();
        add.close();
        remove.close();
    }
View Full Code Here

            InputStream in, URIImpl docId, String mimeType)
            throws ExtractorException, IOException {

        @SuppressWarnings("rawtypes")
        Set factories = this.extractorRegistry.getExtractorFactories(mimeType);
        Model result = null;
        if (factories != null && !factories.isEmpty()) {
            // get extractor from the first available factory
            ExtractorFactory factory =
                (ExtractorFactory)factories.iterator().next();
            Extractor extractor = factory.get();
View Full Code Here

      catch(NumberFormatException e) {}
    }
  }
 
  protected void addSimpleContact(URI property, String fullname, RDFContainer container) {
    Model model = container.getModel();
    Resource resource = ModelUtil.generateRandomResource(model);
    model.addStatement(resource, RDF.type, NCO.Contact);
    model.addStatement(resource, NCO.fullname, fullname);
    model.addStatement(container.getDescribedUri(), property, resource);
}
View Full Code Here

 
  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  public String toString(Node param) {
    Model m = S3BRepository.getInstance().getModel();
    Object[] aslabels = new String[this.queries.length];
   
    for(int i = 0; i < aslabels.length; i++) {
      StringBuilder sb = new StringBuilder();
     
      QueryResultTable table = m.querySelect(String.format(this.queries[i], param), "SPARQL");
      ClosableIterator<QueryRow> it = table.iterator();
     
      while(it.hasNext()) {
        QueryRow row = it.next();
        sb.append(row.getLiteralValue("value")).append(", ");
View Full Code Here

   */
  @SuppressWarnings("unchecked")
  public void addTerms(List<QueryParameterEntry> searched, Person person){
   
   
    Model md = Repository.S3B_HISTORY.getModel();
    md.open();
    /*ClosableIterator<Statement> rows3 = md.findStatements(null , null, null);
    while(rows3.hasNext()){
      md.removeStatement(rows3.next());
    }*/
    int i=0;
   
    String visitURI = "http://s3b.corrib.org/s3b/0.1/Searches/";;
    String randomID = "";
    String finalURI = "";
    while (true)
    {
    randomID = gerateId();
    finalURI = visitURI+"search#"+randomID;
    ClosableIterator<Statement> rows2 = md.findStatements(md.createURI(finalURI), null, null);
    if(rows2.hasNext())
      {
        i=1;
      }
    else break;
    }
   
   
    String hasSearched = "http://s3b.corrib.org/s3b/0.1/hasSearched";
    String Search = "http://s3b.corrib.org/s3b/0.1/Search";
    String SearchDate = "http://s3b.corrib.org/s3b/0.1/SearchDate";
    String SearchTerm = "http://s3b.corrib.org/s3b/0.1/SearchTerm";
    String userUri = "";
    String date = "";
    String res = "";
   
   
    userUri = person.getURI().toString();
    date = ""+System.currentTimeMillis();
    md.addStatement(md.createURI(userUri.toString()), md.createURI(Search), md.createURI(finalURI));
    md.addStatement(md.createURI(finalURI), md.createURI(SearchDate), date);
    for(QueryParameterEntry ex : searched)
    {
      if(ex.isWordnetType())
      {
        res = ex.getMeaning().getURI().toString();
        md.addStatement(md.createURI(finalURI), md.createURI(SearchTerm), md.createURI(res));
       
      }
      else if(ex.isTaxonomyType())
      {
        res = ex.getTaxonomy().getURI().toString();
        md.addStatement(md.createURI(finalURI), md.createURI(SearchTerm), md.createURI(res));
      }
      else if(ex.isPersonType())
      {
        res = ex.getPerson().getURI().toString();
        md.addStatement(md.createURI(finalURI), md.createURI(SearchTerm), md.createURI(res));
      }
    }
   
   
   
    ClosableIterator<Statement> rows2 = md.findStatements(null, null, null);
    while(rows2.hasNext()){
        System.out.println(rows2.next());
    }
    rows2.close();
    //md.dump();
    md.commit();
    md.close();
  }
View Full Code Here

      ModelSet model = DbFace.getModel();
      //LocalService service = SesameServer.getLocalService();
      //LocalRepository repository = (LocalRepository) service.getRepository("foafrealm-rew");
      //Graph graphNew = SesameDbFace.getDbFace(repository).getGraph();
     
      Model modelNew = DbFace.getTempModel();
     
      //- find all uris
      Iterator<Statement> it =  model.findStatements(null,null,null,null);
     
      while (it.hasNext())
      {
        Statement st = it.next();

        modelNew.addStatement(st);
        statementsChanged++;
       
      }
    }
    catch (Exception e)
View Full Code Here

TOP

Related Classes of org.ontoware.rdf2go.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.