org.apache.jena.shared.JenaException Java Examples

The following examples show how to use org.apache.jena.shared.JenaException. 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: RDFTopicMapReader.java    From ontopia with Apache License 2.0 6 votes vote down vote up
@Override
public void importInto(TopicMapIF topicmap) throws IOException {
  try {
    if (inputStream != null) {
      RDFToTopicMapConverter.convert(inputStream, syntax, mappingurl == null ? null : mappingurl.toString(), 
                                     mappingsyntax, topicmap, lenient);
    } else {
      RDFToTopicMapConverter.convert(infileurl, syntax, mappingurl == null ? null : mappingurl.toString(), 
                                     mappingsyntax, topicmap, lenient);
    }
    if (generate_names)
      RDFToTopicMapConverter.generateNames(topicmap);
  } catch (JenaException e) {
    throw new OntopiaRuntimeException(e);
  }

  if (duplicate_suppression)
    DuplicateSuppressionUtils.removeDuplicates(topicmap);
}
 
Example #2
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private TopicIF getType(RDFNode rdfprop, Model model)
  throws JenaException, MalformedURLException {

  Resource subject = (Resource) rdfprop;
  Property prop = model.getProperty(RTM_TYPE);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  while (it.hasNext()) {
    Resource obj = (Resource) it.next();
    LocatorIF loc = new URILocator(obj.getURI());
    TopicIF topic = topicmap.getTopicBySubjectIdentifier(loc);
    if (topic == null) {
      topic = builder.makeTopic();
      topic.addSubjectIdentifier(loc);
    }
    return topic;
  }
  return null;
}
 
Example #3
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all RTM_IN_SCOPE properties for this property and returns a
 * collection containing the RDF URIs of the values as URILocators.
 */
private Collection getScope(RDFNode rdfprop, Model model)
  throws JenaException, MalformedURLException {

  Resource subject = (Resource) rdfprop;
  Property prop = model.getProperty(RTM_IN_SCOPE);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  ArrayList scope = new ArrayList();

  while (it.hasNext()) {
    Object o = it.next();

    if (!(o instanceof Resource))
      throw new RDFMappingException("Scoping topic must be specified by a resource, not by " + o);

    Resource obj = (Resource) o;
    LocatorIF loc = new URILocator(obj.getURI());
    scope.add(loc);
  }

  return scope;
}
 
Example #4
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void doConversion(URL url, String syntax)
  throws JenaException, IOException {

  if (mappings != null && (syntax == null || syntax.equals("RDF/XML")))
    RDFUtils.parseRDFXML(url, new ToTMStatementHandler());

  else {
    Model model = ModelFactory.createDefaultModel();
    model.read(url.openStream(), url.toString(), syntax);
    if (mappings == null)
      buildMappings(model);

    doConversion(model);
  }
}
 
Example #5
Source File: TarqlTest.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testMultipleInputFiles() throws IOException {
	String query = "SELECT * FROM <x> <y> {}";
	try {
		TarqlQuery tq =  new TarqlParser(new StringReader(query), null).getResult();
		assertSelect(tq);
		fail("Expected exception due to multiple input files in query");
	} catch (JenaException ex) {
		// Expected
	}
}
 
Example #6
Source File: ExTDB3.java    From xcurator with Apache License 2.0 5 votes vote down vote up
public static void main(String... argv)
{
    String assemblerFile = "Store/tdb-assembler.ttl" ;
    
    // Find a particular description in the file where there are several: 
    Model spec = RDFDataMgr.loadModel(assemblerFile) ;

    // Find the right starting point for the description in some way.
    Resource root = null ;

    if ( false )
        // If you know the Resource URI:
        root = spec.createResource("http://example/myChoiceOfURI" );
    else
    {
        // Alternatively, look for the a single resource of the right type. 
        try {
            // Find the required description - the file can contain descriptions of many different types.
            root = GraphUtils.findRootByType(spec, VocabTDB.tDatasetTDB) ;
            if ( root == null )
                throw new JenaException("Failed to find a suitable root") ;
        } catch (TypeNotUniqueException ex)
        { throw new JenaException("Multiple types for: "+DatasetAssemblerVocab.tDataset) ; }
    }

    Dataset ds = (Dataset)Assembler.general.open(root) ;
}
 
Example #7
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private LocatorIF getTopicIndicator(Resource subject, String property,
                                    Model model)
  throws JenaException, MalformedURLException {
  Property prop = model.getProperty(property);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  while (it.hasNext()) {
    Resource obj = (Resource) it.next();
    if (obj.isAnon())
      continue; // FIXME: is this really ok?
    return new URILocator(obj.getURI());
  }
  return null;
}
 
Example #8
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private StatementHandler getMapper(Resource subject, RDFNode node, Model model)
  throws JenaException, MalformedURLException {
  String uri = node.toString();
  if (RTM_BASENAME.equals(uri))
    return new TopicNameMapper(getScope(subject, model));
  else if (RTM_INSTANCE_OF.equals(uri)) {
    Collection scope = getScope(subject, model);
    if (scope.isEmpty())
      return new InstanceMapper();
    else
      return new ScopedInstanceMapper(scope);
  } else if (RTM_SUBJECT_IDENTIFIER.equals(uri))
    return new SubjectIdentifierMapper();
  else if (RTM_SOURCE_LOCATOR.equals(uri))
    return new SourceLocatorMapper();
  else if (RTM_SUBJECT_LOCATOR.equals(uri))
    return new SubjectLocatorMapper();
  else if (RTM_OCCURRENCE.equals(uri))
    return new OccurrenceMapper(getType(subject, model), getScope(subject, model));
  else if (RTM_ASSOCIATION.equals(uri)) {
    LocatorIF srole = getTopicIndicator(subject, RTM_SUBJECT_ROLE, model);
    if (srole == null)
      throw new RDFMappingException("No rtm:subject-role for " + subject);
    LocatorIF orole = getTopicIndicator(subject, RTM_OBJECT_ROLE, model);
    if (orole == null)
      throw new RDFMappingException("No rtm:object-role for " + subject);
    return new AssociationMapper(srole, orole, getType(subject, model),
                                 getScope(subject, model));
  } else
    throw new RDFMappingException("Unknown value for rtm:maps-to: " + uri);
}
 
Example #9
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void doConversion(Model model) throws JenaException {
  StatementHandler totm = new ToTMStatementHandler();
  AResourceWrapper subjw = new AResourceWrapper();
  AResourceWrapper propw = new AResourceWrapper();
  AResourceWrapper objtw = new AResourceWrapper();
  ALiteralWrapper litlw = new ALiteralWrapper();

  ResIterator it = model.listSubjects();
  while (it.hasNext()) {
    Resource subject = (Resource) it.next();

    StmtIterator it2 = subject.listProperties(); // get all statements
    while (it2.hasNext()) {
      Statement stmt = (Statement) it2.next();

      subjw.resource = stmt.getSubject();
      propw.resource = stmt.getPredicate();

      RDFNode obj = stmt.getObject();
      if (obj instanceof Resource) {
        objtw.resource = (Resource) obj;
        totm.statement(subjw, propw, objtw);
      } else {
        litlw.literal = (Literal) obj;
        totm.statement(subjw, propw, litlw);
      }
    }
  }
}
 
Example #10
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void doConversion(InputStream input, String syntax)
  throws JenaException, IOException {

  if (mappings != null && (syntax == null || syntax.equals("RDF/XML")))
    RDFUtils.parseRDFXML(input, new ToTMStatementHandler());

  else {
    Model model = ModelFactory.createDefaultModel();
    model.read(input, input.toString(), syntax);
    if (mappings == null)
      buildMappings(model);

    doConversion(model);
  }
}
 
Example #11
Source File: TestRDFChangesGraph.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_add_abort_1() {
    setup();
    try {
        txn(graph, ()->{
            graph.add(triple1);
            throw new JenaException("Abort!");
        });
    } catch (JenaException ex) {}
    Graph g2 = replay();
    check(g2);
}
 
Example #12
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private RDFToTopicMapConverter(Model model, TopicMapIF topicmap)
  throws JenaException, MalformedURLException {

  this.topicmap = topicmap;
  this.builder = topicmap.getBuilder();

  buildMappings(model);
}
 
Example #13
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private RDFToTopicMapConverter(String mappingurl, String syntax, TopicMapIF topicmap)
  throws JenaException, MalformedURLException {

  this.topicmap = topicmap;
  this.builder = topicmap.getBuilder();

  if (mappingurl != null) {
    Model model = ModelFactory.createDefaultModel();
    model.read(mappingurl, syntax);
    buildMappings(model);
  }
}
 
Example #14
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/**
 * EXPERIMENTAL: Converts an RDF model into the topic map using the
 * mapping found within the RDF model.
 */
public static void convert(Model model, TopicMapIF topicmap)
  throws JenaException, IOException {

  RDFToTopicMapConverter converter = new RDFToTopicMapConverter(model, topicmap);
  converter.doConversion(model);

}
 
Example #15
Source File: ALiteralWrapper.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  try {
    return literal.getString();
  } catch (JenaException e) {
    throw new OntopiaRuntimeException(e);
  }
}
 
Example #16
Source File: IOHelper.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Given a path to an RDF/XML or TTL file and a RDF language, load the file as the default model
 * of a TDB dataset backed by a directory to improve processing time. Return the new dataset.
 *
 * <p>WARNING - this creates a directory at given tdbDir location!
 *
 * @param inputPath input path of RDF/XML or TTL file
 * @param tdbDir location to put TDB mappings
 * @return Dataset instantiated with triples
 * @throws JenaException if TDB directory can't be written to
 */
public static Dataset loadToTDBDataset(String inputPath, String tdbDir) throws JenaException {
  Dataset dataset;
  if (new File(tdbDir).isDirectory()) {
    dataset = TDBFactory.createDataset(tdbDir);
    if (!dataset.isEmpty()) {
      return dataset;
    }
  }
  dataset = TDBFactory.createDataset(tdbDir);
  logger.debug(String.format("Parsing input '%s' to dataset", inputPath));
  // Track parsing time
  long start = System.nanoTime();
  Model m;
  dataset.begin(ReadWrite.WRITE);
  try {
    m = dataset.getDefaultModel();
    FileManager.get().readModel(m, inputPath);
    dataset.commit();
  } catch (JenaException e) {
    dataset.abort();
    dataset.end();
    dataset.close();
    throw new JenaException(String.format(syntaxError, inputPath));
  } finally {
    dataset.end();
  }
  long time = (System.nanoTime() - start) / 1000000000;
  logger.debug(String.format("Parsing complete - took %s seconds", String.valueOf(time)));
  return dataset;
}
 
Example #17
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 3 votes vote down vote up
/**
 * EXPERIMENTAL: Converts an RDF model into the topic map using the
 * given mapping.
 * @param input the InputStream to read the input data from
 * @param syntax the syntax of the input data. Values are "RDF/XML", "N3",
 *               and "N-TRIPLE". Defaults to "RDF/XML" if null.
 * @param mappingurl the URL to read the mapping from. If null the mapping
 *                   is taken from the input data.
 * @param mappingsyntax the syntax of the mapping. Values are "RDF/XML", "N3",
 *                   and "N-TRIPLE". Defaults to "RDF/XML" if null.
 * @param topicmap The topic map to add the converted data to.
 * @param lenient When false, errors are thrown if the RDF data cannot be
 *        correctly mapped (for example, a statement type is mapped to a
 *        topic name, but has a URI value).
 */
public static void convert(InputStream input, String syntax,
                           String mappingurl, String mappingsyntax,
                           TopicMapIF topicmap, boolean lenient)
  throws JenaException, IOException {

  RDFToTopicMapConverter converter =
    new RDFToTopicMapConverter(mappingurl, mappingsyntax, topicmap);
  converter.setLenient(lenient);
  converter.doConversion(input, syntax);

}
 
Example #18
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 3 votes vote down vote up
/**
 * EXPERIMENTAL: Converts an RDF model into the topic map using the
 * given mapping.
 * @param infileurl the URL to read the input data from
 * @param syntax the syntax of the input data. Values are "RDF/XML", "N3",
 *               and "N-TRIPLE". Defaults to "RDF/XML" if null.
 * @param mappingurl the URL to read the mapping from. If null the mapping
 *                   is taken from the input data.
 * @param mappingsyntax the syntax of the mapping. Values are "RDF/XML", "N3",
 *                   and "N-TRIPLE". Defaults to "RDF/XML" if null.
 * @param topicmap The topic map to add the converted data to.
 * @param lenient When false, errors are thrown if the RDF data cannot be
 *        correctly mapped (for example, a statement type is mapped to a
 *        topic name, but has a URI value).
 */
public static void convert(URL infileurl, String syntax,
                           String mappingurl, String mappingsyntax,
                           TopicMapIF topicmap, boolean lenient)
  throws JenaException, IOException {

  RDFToTopicMapConverter converter =
    new RDFToTopicMapConverter(mappingurl, mappingsyntax, topicmap);
  converter.setLenient(lenient);
  converter.doConversion(infileurl, syntax);

}