How to handle nulls in SNARL


I am pretty new to Stardog. I am using SNARL to create and load triples into Stardog. The triples are generated from a Java data structure which is the end result of a UIMA pipeline. I am having two specific issues: handling nulls that crop up in the data, and logging.

First of all, how do you enable logging when inserting data from SNARL? My stardog.log file only has records of inserts through the admin interface, but has nothing for inserts that happen from SNARL. That has meant that it took much longer to find the source of the following error.

What was happening was that Stardog was throwing a null pointer exception intermittently. I traced it down to a place in the underlying data that contained a null value. It is hard to do anything about that because there is a lot of data. So I am wondering if there is a standard SNARL way of handling null values before they get into the Statement structure. Here is the code - the null pointer exception is thrown at the line “Model ctgraph = Models2.newModel(stmts);”

private void writeToStardog(ClinicalTrialInfo trial)
	System.err.println("about to write to stardog");
	System.err.println("Trial is " + trial.getNctId());
	// establish a connection to the ctkr database		
	Connection aConn = ConnectionConfiguration
			.credentials("admin", "admin")             
	//put in data

    ArrayList<Statement> stmts = new ArrayList<Statement>();
    stmts.add(Values.statement(Values.iri(CT, trial.getNctId()), 
			 Values.iri(RDF, "type"), 
			 Values.iri(CT, "Trial")));
    stmts.add(Values.statement(Values.iri(CT, trial.getNctId()), 
    		Values.iri(CT, "hasNCT"),
    ArrayList<Statement> interventionStmts = makeInterventionRep(trial);
    ArrayList<Statement> criteriaStmts = makeCriteriaRep(trial);
    System.err.println("dumping stmts");
    for (Statement curr : stmts)
    Model ctgraph = Models2.newModel(stmts);
	System.err.println("wrote triples for trial " + trial.getNctId() + "to Stardog");

But the root cause is elsewhere - in this code snippet

Literal val = literal(criterion.getCriterionType());
			System.err.println("literal for criterion type is " + val);
			if (val != null)
				System.err.println("criterion type is valid it is " + criterion.getCriterionType());
				System.err.println("criteria type is not valid");

You can see that I have wrapped this with a test to make sure the criterion type is not null, since that happens every so often in the data. But this is ugly, so I am wondering if there is a better way to handle this. And why does Stardog fail so abjectly when this happens? This is the actual exception

org.apache.uima.analysis_engine.AnalysisEngineProcessException: Annotator processing failed.    
	at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.callAnalysisComponentProcess(
	at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.processAndOutputNewCASes(
	at org.apache.uima.analysis_engine.asb.impl.ASB_impl$AggregateCasIterator.processUntilNextOutputCas(
	at org.apache.uima.analysis_engine.asb.impl.ASB_impl$AggregateCasIterator.<init>(
	at org.apache.uima.analysis_engine.asb.impl.ASB_impl.process(
	at org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl.processAndOutputNewCASes(
	at org.apache.uima.analysis_engine.impl.AnalysisEngineImplBase.process(
	at ecproj.krgen.PipelineSystem.<init>(
	at ecproj.krgen.PipelineSystem.main(
Caused by: java.lang.NullPointerException
	at com.complexible.common.rdf.model.AbstractStardogLiteral.hashCode(
	at com.complexible.common.rdf.model.StardogStringLiteral.hashCode(
	at java.util.HashMap.hash(
	at java.util.HashMap.get(
	at org.openrdf.model.impl.LinkedHashModel.asNode(
	at org.openrdf.model.impl.LinkedHashModel.add(
	at org.openrdf.model.impl.AbstractModel.add(
	at org.openrdf.model.impl.AbstractModel.addAll(
	at com.complexible.common.openrdf.model.Models2.newModel(
	at ecproj.krgen.KRWriter.writeToStardog(
	at ecproj.krgen.KRWriter.process(
	at org.apache.uima.analysis_component.CasAnnotator_ImplBase.process(
	at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.callAnalysisComponentProcess(
	... 9 more

It took a couple of hours to find the culprit - with logging, it might have been much quicker, so I would really like to know if it is possible to log inserts that come from SNARL. And again, what is best practice for handling nulls?

Hi and welcome!

You can implement a transaction listener to perform a task (such as logging) on transactions from the SNARL protocol. We have an example of this in our examples repo.

null doesn’t make sense as the object of a triple (or any element of a triple for that matter). We are actively re-working some of our RDF parsing such that this kind of error should be caught at compile time, however until then what you’re doing is my recommendation.

If it’s too “ugly,” perhaps consider tweaking the literal() method to return an empty String when passed a null value? This is assuming you would want that data loaded at all.


Thanks. Your final question “would you want that data loaded at all?” is a good one. Right now, I simply do not create a triple if a null is found, and that may well make the most sense. But if a null ended up in that particular part of the data, the semantics may actually be “unknown” rather than “not there at all”. It is the good old ambiguous null situation. We are discussing what we want to do with that.

But the more serious issue is the logging. I took a look at the link you sent. I am trying to understand where the logging is. Do you mean this snippet in ExampleConnectableConnection.processIndexChange ?

try {
StatementSources.write(aStatements, RDFFormat.NQUADS, System.out);
catch (IOException e) {
LOGGER.warn(“Error while writing transaction data”, e);

If so, I don’t see how it helps - I still wouldn’t be able to identify the specific triple statement that is causing the problem. Am I missing something?

Yes, processIndexChange is where you have access to each Statement being added. In the case of what you pasted, it should output the RDF directly to your stardog.log. You can also do something like aStatements.statements().forEachRemaining(System.out::println); to accomplish a similar result.

I guess I am still not understanding how this would help because the listener just appears to be constructing a bunch of statements and then logging them. What I want is a log message for each individual statement, if an exception is thrown, Maybe I can only get this if I add each statement individually? But would that even work?

for (Statement curr : stmts)
Model ctgraph = Models2.newModel(curr)
catch (Exception e)
// do some logging

The issue is that the exception is coming out of Models2.newModel and I want to know which particular statement is causing it. Right now, as you can see, I dump all the statements before this point, but wading through hundreds and hundreds of statements, trying to guess the one that offended, is not fun.

I think you can safely add each statement one at a time. You won’t trigger a network round trip until the commit and you can avoid building the list of statements. I’m not sure but I’d guess that if you had a lot of data that it will buffer some set amount and trigger a network round trip sometime before the commit but I’m not positive about that.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.