Enhanced toString() methods for Models, OntModels, Datasets, Graphs, RDFService

This commit is contained in:
Jim Blake 2014-08-29 17:43:30 -04:00
parent f1c9ac23de
commit 2b9a3a5a01
14 changed files with 208 additions and 5 deletions

View file

@ -20,6 +20,8 @@ import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.WrappedIterator;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class BlankNodeFilteringGraph implements Graph {
private Graph graph;
@ -141,4 +143,10 @@ public class BlankNodeFilteringGraph implements Graph {
public void remove(Node arg0, Node arg1, Node arg2) {
graph.remove(arg0, arg1, arg2);
}
@Override
public String toString() {
return "BlankNodeFilteringGraph[" + ToString.hashHex(this) + ", inner="
+ ToString.graphToString(graph) + "]";
}
}

View file

@ -19,6 +19,8 @@ import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.WrappedIterator;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class DifferenceGraph implements Graph {
private Graph g;
@ -134,4 +136,11 @@ public class DifferenceGraph implements Graph {
g.clear();
}
@Override
public String toString() {
return "DifferenceGraph[" + ToString.hashHex(this) + ", g="
+ ToString.graphToString(g) + ", subtract="
+ ToString.graphToString(subtract) + "]";
}
}

View file

@ -16,6 +16,7 @@ import com.hp.hpl.jena.sparql.core.DatasetGraph;
import com.hp.hpl.jena.sparql.util.Context;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class RDFServiceDataset implements Dataset {
@ -139,4 +140,9 @@ public class RDFServiceDataset implements Dataset {
+ " does not support transactions.");
}
@Override
public String toString() {
return "RDFServiceDataset[" + ToString.hashHex(this) + ", " + g + "]";
}
}

View file

@ -39,6 +39,7 @@ import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
import edu.cornell.mannlib.vitro.webapp.rdfservice.adapters.VitroModelFactory;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class RDFServiceGraph implements GraphWithPerform {
@ -460,5 +461,11 @@ public class RDFServiceGraph implements GraphWithPerform {
});
return m;
}
@Override
public String toString() {
return "RDFServiceGraph[" + ToString.hashHex(this) + ", " + rdfService
+ ", graphURI=" + ToString.modelName(graphURI) + "]";
}
}

View file

@ -21,6 +21,8 @@ import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.DCTerms;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class RegeneratingGraph implements Graph, Regenerable {
private final static Log log = LogFactory.getLog(RegeneratingGraph.class);
@ -296,4 +298,9 @@ public class RegeneratingGraph implements Graph, Regenerable {
}
}
@Override
public String toString() {
return "RegeneratingGraph[" + ToString.hashHex(this) + ", "
+ ToString.graphToString(g) + "]";
}
}

View file

@ -43,6 +43,8 @@ import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.SingletonIterator;
import com.hp.hpl.jena.util.iterator.WrappedIterator;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class SparqlGraph implements GraphWithPerform {
private String endpointURI;
@ -497,4 +499,11 @@ public class SparqlGraph implements GraphWithPerform {
// sbuff.append(hexstr);
}
}
@Override
public String toString() {
return "SparqlGraph[" + ToString.hashHex(this) + ", endpoint="
+ endpointURI + ", name=" + ToString.modelName(graphURI) + "]";
}
}

View file

@ -21,6 +21,8 @@ import com.hp.hpl.jena.shared.AddDeniedException;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.WrappedIterator;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class SparqlGraphMultilingual extends SparqlGraph implements GraphWithPerform {
private static final Log log = LogFactory.getLog(SparqlGraphMultilingual.class);
@ -145,5 +147,11 @@ public class SparqlGraphMultilingual extends SparqlGraph implements GraphWithPer
}
@Override
public String toString() {
return "SparqlGraphMultilingual[" + ToString.hashHex(this)
+ ", endpoint=" + getEndpointURI() + ", name="
+ ToString.modelName(getGraphURI()) + "]";
}
}

View file

@ -79,6 +79,8 @@ import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
/**
* The base class for a delegating ontology model decorator.
*
@ -94,6 +96,12 @@ public abstract class AbstractOntModelDecorator implements OntModel {
}
this.inner = m;
}
@Override
public String toString() {
return ToString.simpleName(this) + "[" + ToString.hashHex(this) + ", "
+ ToString.ontModelToString(inner) + "]";
}
@Override
@Deprecated

View file

@ -207,11 +207,6 @@ public class BulkUpdatingOntModel extends AbstractOntModelDecorator {
return this;
}
@Override
public String toString() {
return "<" + this.getClass().getSimpleName() + " " + getGraph() + ">";
}
private Iterator<Triple> asTriples(StmtIterator it) {
return it.mapWith(mapAsTriple);
}

View file

@ -17,6 +17,8 @@ import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.impl.ModelCom;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
/**
* Make models that will do proper bulk updates.
*/
@ -74,6 +76,14 @@ public class VitroModelFactory {
L.getBulkUpdateHandler());
}
@Override
public String toString() {
return "BulkUpdatingUnion[" + ToString.hashHex(this) + ", L="
+ ToString.graphToString(L) + ", R="
+ ToString.graphToString(R) + "]";
}
}
@SuppressWarnings("deprecation")

View file

@ -29,6 +29,7 @@ import com.hp.hpl.jena.util.iterator.WrappedIterator;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ModelChange;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceImpl;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class ListeningGraph implements GraphWithPerform {
@ -225,4 +226,11 @@ public class ListeningGraph implements GraphWithPerform {
}
};
@Override
public String toString() {
return "ListeningGraph["+ToString.hashHex(this)
+ ", " + rdfServiceImpl
+ ", " + ToString.modelName(graphURI) + "]";
}
}

View file

@ -27,6 +27,7 @@ import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeSet;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.RDFServiceJena;
import edu.cornell.mannlib.vitro.webapp.utils.ToString;
public class RDFServiceSDB extends RDFServiceJena implements RDFService {
@ -170,5 +171,10 @@ public class RDFServiceSDB extends RDFServiceJena implements RDFService {
}
}
}
@Override
public String toString() {
return "RDFServiceSDB[" + ToString.hashHex(this) + "]";
}
}

View file

@ -55,6 +55,9 @@ public class SimpleReasonerSetup implements ServletContextListener {
OntModel tboxAssertionsModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_ASSERTIONS);
OntModel tboxInferencesModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_INFERENCES);
OntModel tboxUnionModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_UNION);
log.debug("tboxAssertionsModel=" + tboxAssertionsModel);
log.debug("tboxInferencesModel=" + tboxInferencesModel);
log.debug("tboxUnionModel=" + tboxUnionModel);
WebappDaoFactory wadf = ModelAccess.on(ctx).getWebappDaoFactory();

View file

@ -0,0 +1,119 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.utils;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.compose.Polyadic;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
/**
* Some useful methods for printing out the contents of data structures:
* OntModels, Models, Datasets, etc.
*/
public class ToString {
/**
* Local implementations of OntModel can display themselves. Built-in Jena
* OntModels will show their hashcodes, base models, and sub-models.
*/
public static String ontModelToString(OntModel ontModel) {
if (ontModel == null || isVitroClass(ontModel)) {
return String.valueOf(ontModel);
} else {
Model base = ontModel.getBaseModel();
Graph baseGraph = base.getGraph();
List<Graph> subGraphs = ontModel.getSubGraphs();
return simpleName(ontModel) + "[" + hashHex(ontModel) + ", base="
+ modelToString(base) + ", subgraphs="
+ subGraphsToString(subGraphs, baseGraph) + "]";
}
}
/** Show the sub-graphs, except for the base graph. */
private static String subGraphsToString(Collection<Graph> subGraphs,
Graph baseGraph) {
Set<Graph> set = new HashSet<>(subGraphs);
set.remove(baseGraph);
return setOfGraphsToString(set);
}
private static String setOfGraphsToString(Set<Graph> set) {
Set<String> strings = new HashSet<>();
for (Graph g : set) {
strings.add(graphToString(g));
}
return "[" + StringUtils.join(strings, ", ") + "]";
}
/**
* Local implementations of Model can display themselves. Built-in Jena
* Graphs will show their hashcodes and graphs.
*/
public static String modelToString(Model model) {
if (model == null || isVitroClass(model)) {
return String.valueOf(model);
} else {
return simpleName(model) + "[" + hashHex(model) + ", base="
+ graphToString(model.getGraph()) + "]";
}
}
/**
* Local implementations of Graph can display themselves. Built-in Jena
* Graphs will show their hashcodes.
*/
public static String graphToString(Graph graph) {
if (graph == null || isVitroClass(graph)) {
return String.valueOf(graph);
} else if (graph instanceof Polyadic) {
return polyadicGraphToString((Polyadic) graph);
} else {
return simpleName(graph) + "[" + hashHex(graph) + "]";
}
}
private static String polyadicGraphToString(Polyadic poly) {
Graph baseGraph = poly.getBaseGraph();
List<Graph> subGraphs = poly.getSubGraphs();
return simpleName(poly) + "[" + hashHex(poly) + ", base="
+ graphToString(baseGraph) + ", subgraphs="
+ subGraphsToString(subGraphs, baseGraph) + "]";
}
/**
* If the string is found in ModelNames, return the name of the constant. If
* not, use the string itself.
*
* TODO: Make it work.
*/
public static String modelName(String name) {
return name;
}
public static boolean isVitroClass(Object o) {
return (o == null) ? false : o.getClass().getName()
.startsWith("edu.cornell");
}
public static String simpleName(Object o) {
return (o == null) ? "null" : o.getClass().getSimpleName();
}
public static String hashHex(Object o) {
return (o == null) ? "00000000" : Integer.toString(o.hashCode(), 16);
}
/**
* This class contains only static methods. No need for an instance.
*/
private ToString() {
// Nothing to initialize.
}
}