use of org.apache.jena.shared.JenaException in project jena by apache.
the class TestXMLFeatures method checkPropURI.
public void checkPropURI(String s, String p, Object val, int behaviour) throws IOException {
// create triple and graph.
// BaseXMLWriter.dbg = true;
// SystemOutAndErr.block();
// TestLogger tl = new TestLogger(BaseXMLWriter.class);
blockLogger();
Node blank = NodeFactory.createBlankNode();
Node prop = NodeFactory.createURI(s);
Graph g = Factory.createGraphMem();
g.add(Triple.create(blank, prop, blank));
// create Model
Model m = ModelFactory.createModelForGraph(g);
// serialize
RDFWriter rw = m.getWriter(lang);
if (p != null)
rw.setProperty(p, val);
try (StringWriter w = new StringWriter()) {
rw.write(m, w, "http://example.org/");
String f = w.toString();
switch(behaviour) {
case BadPropURI:
fail("Bad property URI <" + s + "> was not detected.");
return;
case BadURI:
fail("Bad URI <" + s + "> was not detected.");
return;
}
// read back in
Model m2 = createMemModel();
RDFReader rdr = m2.getReader("RDF/XML");
rdr.setProperty("error-mode", "lax");
try (StringReader sr = new StringReader(f)) {
rdr.read(m2, sr, "http://example.org/");
}
// check
switch(behaviour) {
case ExtraTriples:
assertTrue("Expecting Brickley behaviour.", m2.size() == 3);
break;
case NoError:
assertTrue("Comparing Model written out and read in.", m.isIsomorphicWith(m2));
break;
}
} catch (BadURIException e) {
if (behaviour == BadURI)
return;
throw e;
} catch (InvalidPropertyURIException je) {
if (behaviour == BadPropURI)
return;
throw je;
} catch (JenaException e) {
throw e;
} finally {
// BaseXMLWriter.dbg = false;
// tl.end();
unblockLogger();
// SystemOutAndErr.unblock();
}
}
use of org.apache.jena.shared.JenaException in project jena by apache.
the class OWLWGTester method runTest.
/**
* Run a single designated test.
* @param test the root node descibing the test
* @param log set to true to enable derivation logging
* @param stats set to true to log performance statistics
* @return true if the test passes
* @throws IOException if one of the test files can't be found
*/
public boolean runTest(Resource test, boolean log, boolean stats) throws IOException {
// Find the specification for the named test
RDFNode testType = test.getRequiredProperty(RDF.type).getObject();
if (!(testType.equals(NegativeEntailmentTest) || testType.equals(PositiveEntailmentTest))) {
throw new JenaException("Can't find test: " + test);
}
String description = test.getRequiredProperty(descriptionP).getObject().toString();
String status = test.getRequiredProperty(statusP).getObject().toString();
logger.debug("WG test " + test.getURI() + " - " + status);
// Load up the premise documents
Model premises = ModelFactory.createDefaultModel();
for (StmtIterator premisesI = test.listProperties(premiseDocumentP); premisesI.hasNext(); ) {
premises.add(loadFile(premisesI.nextStatement().getObject().toString() + ".rdf"));
}
// Load up the conclusions document
Resource conclusionsRes = (Resource) test.getRequiredProperty(conclusionDocumentP).getObject();
Model conclusions = loadFile(conclusionsRes.toString() + ".rdf");
// Optional logging
if (log) {
if (configuration == null) {
Model m = ModelFactory.createDefaultModel();
configuration = m.createResource();
}
configuration.addProperty(ReasonerVocabulary.PROPtraceOn, "true").addProperty(ReasonerVocabulary.PROPderivationLogging, "true");
}
Reasoner reasoner = reasonerF.create(configuration);
long t1 = System.currentTimeMillis();
InfGraph graph = reasoner.bind(premises.getGraph());
Model result = ModelFactory.createModelForGraph(graph);
if (stats && graph instanceof FBRuleInfGraph) {
// ((FBRuleInfGraph)graph).resetLPProfile(true);
}
// Check the results against the official conclusions
boolean correct = true;
if (testType.equals(PositiveEntailmentTest)) {
correct = WGReasonerTester.testConclusions(conclusions.getGraph(), result.getGraph());
} else {
// A negative entailment check
correct = !WGReasonerTester.testConclusions(conclusions.getGraph(), result.getGraph());
}
long t2 = System.currentTimeMillis();
timeCost += (t2 - t1);
numTests++;
if (stats) {
logger.info("Time=" + (t2 - t1) + "ms for " + test.getURI());
printStats();
if (graph instanceof FBRuleInfGraph) {
((FBRuleInfGraph) graph).printLPProfile();
}
}
if (!correct) {
// List all the forward deductions for debugging
// if (graph instanceof FBRuleInfGraph) {
// System.out.println("Error: deductions graph was ...");
// FBRuleInfGraph fbGraph = (FBRuleInfGraph)graph;
// Graph deductions = fbGraph.getDeductionsGraph();
// org.apache.jena.PrintUtil.printOut(deductions.find(null,null,null));
// }
}
// Signal the results
if (testcase != null) {
Assert.assertTrue("Test: " + test + "\n" + reasonerF.getURI() + "\n" + description, correct);
}
return correct;
}
use of org.apache.jena.shared.JenaException in project jena by apache.
the class ExTDB3 method main.
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);
}
use of org.apache.jena.shared.JenaException in project jena by apache.
the class JenaReader method read.
/**
* Reads from url, using url as base, adding triples to model.
* Uses content negotiation to ask for application/rdf+xml, if available.
*
* @param m
* A model to add triples to.
* @param url
* The URL of the RDF/XML document.
*/
@Override
public void read(Model m, String url) throws JenaException {
try {
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("accept", "application/rdf+xml, application/xml; q=0.8, text/xml; q=0.7, application/rss+xml; q=0.3, */*; q=0.2");
String encoding = conn.getContentEncoding();
if (encoding == null)
read(m, conn.getInputStream(), url);
else
read(m, new InputStreamReader(conn.getInputStream(), encoding), url);
} catch (FileNotFoundException e) {
throw new DoesNotExistException(url);
} catch (IOException e) {
throw new JenaException(e);
}
}
use of org.apache.jena.shared.JenaException in project jena by apache.
the class RDFReaderFImpl method setBaseReaderClassName.
/**
* Use RIOT to add custom RDF parsers. See
* {@code RDFParserRegistry.registerLang}
*
* @deprecated Register with RIOT.
*/
@Deprecated
public static String setBaseReaderClassName(String lang, String className) {
if (rewiredAlternative != null)
Log.error(RDFReaderFImpl.class, "Rewired RDFReaderFImpl - configuration changes have no effect on reading");
String oldClassName = currentEntry(lang);
try {
@SuppressWarnings("unchecked") Class<? extends RDFReader> newClass = (Class<? extends RDFReader>) Class.forName(className, false, Thread.currentThread().getContextClassLoader());
custom.put(lang, newClass);
return oldClassName;
} catch (ClassNotFoundException e) {
throw new ConfigException("Reader not found on classpath", e);
} catch (Exception e) {
throw new JenaException(e);
}
}
Aggregations