jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chrisdol...@apache.org
Subject svn commit: r1414809 - in /jena/Scratch/Eyeball/trunk/src: main/java/com/hp/hpl/jena/eyeball/inspectors/SparqlDrivenInspector.java test/java/com/hp/hpl/jena/eyeball/inspectors/test/TestSparqlDrivenInspector.java
Date Wed, 28 Nov 2012 16:56:47 GMT
Author: chrisdollin
Date: Wed Nov 28 16:56:46 2012
New Revision: 1414809

URL: http://svn.apache.org/viewvc?rev=1414809&view=rev
Log:
Add the set of results from a prohibit that fails to the report model.

Modified:
    jena/Scratch/Eyeball/trunk/src/main/java/com/hp/hpl/jena/eyeball/inspectors/SparqlDrivenInspector.java
    jena/Scratch/Eyeball/trunk/src/test/java/com/hp/hpl/jena/eyeball/inspectors/test/TestSparqlDrivenInspector.java

Modified: jena/Scratch/Eyeball/trunk/src/main/java/com/hp/hpl/jena/eyeball/inspectors/SparqlDrivenInspector.java
URL: http://svn.apache.org/viewvc/jena/Scratch/Eyeball/trunk/src/main/java/com/hp/hpl/jena/eyeball/inspectors/SparqlDrivenInspector.java?rev=1414809&r1=1414808&r2=1414809&view=diff
==============================================================================
--- jena/Scratch/Eyeball/trunk/src/main/java/com/hp/hpl/jena/eyeball/inspectors/SparqlDrivenInspector.java
(original)
+++ jena/Scratch/Eyeball/trunk/src/main/java/com/hp/hpl/jena/eyeball/inspectors/SparqlDrivenInspector.java
Wed Nov 28 16:56:46 2012
@@ -21,8 +21,10 @@ package com.hp.hpl.jena.eyeball.inspecto
 import java.util.*;
 
 import com.hp.hpl.jena.eyeball.Report;
+import com.hp.hpl.jena.eyeball.ReportItem;
 import com.hp.hpl.jena.eyeball.util.FileInsertion;
 import com.hp.hpl.jena.eyeball.vocabulary.*;
+import com.hp.hpl.jena.graph.Node;
 import com.hp.hpl.jena.ontology.OntModel;
 import com.hp.hpl.jena.query.*;
 import com.hp.hpl.jena.rdf.model.*;
@@ -85,11 +87,13 @@ public class SparqlDrivenInspector exten
     protected final List<KV> allRequired = new ArrayList<KV>();
     
     protected final List<KV> allProhibited = new ArrayList<KV>();
-    
+
+	static final Property selected = ResourceFactory.createProperty( EYE.getURI() + "selected"
);
+	
     public void begin( Report r, OntModel assume )
         {
-        r.declareProperty( EYE.sparqlRequireFailed );
-        r.declareProperty( EYE.sparqlProhibitFailed );
+        r.declareOrder( EYE.sparqlRequireFailed, selected );
+        r.declareOrder( EYE.sparqlProhibitFailed, selected );
         }
 
     public void inspectModel( Report r, OntModel m )
@@ -103,19 +107,57 @@ public class SparqlDrivenInspector exten
         for (int i = 0; i < demands.size(); i += 1)
             {
             KV demand = demands.get( i );
-            if (hasSolutions( demand.query, m ) != present)
-                r.createItem().addMainProperty( problem, demand.message );
+            ResultSet results = solutions( demand.query, m );
+            if (results.hasNext() != present) reportSparqlReport( r, m, problem, demand,
results );
             }
         }
 
-    private boolean hasSolutions( String queryTemplate, OntModel m )
+	public void reportSparqlReport(Report r, OntModel m, Property problem, KV demand, ResultSet
results) {
+		List<String> varNames = results.getResultVars();
+		ReportItem item = r.createItem();
+		item.addMainProperty( problem, demand.message );
+		while (results.hasNext()) 
+			item.addProperty( selected, stringForSelection( varNames, m, results.next() ) );
+	}
+
+    private String stringForSelection( List<String> varNames, OntModel m, QuerySolution
s ) {
+    	StringBuilder sb = new StringBuilder();
+    	String comma = "";
+		for (String name: varNames) {
+			sb.append( comma ).append( name ).append( " = " ).append( nicely( m, s.get( name ) ) );
+			comma = ", ";
+		}
+		return sb.toString();
+	}
+
+    // TODO copied from ExperimentalTextRenderer, should be some useful sharing
+    private String nicely( OntModel m, RDFNode p )
+        {
+        Node node = p.asNode();
+        if (node.isLiteral() && integer( node.getLiteralDatatypeURI() )) return node.getLiteralLexicalForm();
+        if (node.isBlank()) return niceBnode( node );
+        return node.toString( m, true);
+        }
+    
+    private boolean integer( String URI )
+    	{ return URI != null && URI.matches( ".*int(|eger)" ); }
+    
+    private Map<Node, String> bnodes = new HashMap<Node, String>();
+    
+    private int bnodeCounter = 1000;
+    
+    private String niceBnode( Node node )
+        {
+        String label = bnodes.get( node );
+        if (label == null) bnodes.put( node, label = "_:b" + ++bnodeCounter );
+        return label;
+        }
+
+	private ResultSet solutions( String queryTemplate, OntModel m )
         {
         Query q = QueryFactory.create( getFileInsertion().insert( queryTemplate ) );
         QueryExecution qe = QueryExecutionFactory.create( q, m.getBaseModel() );
-        ResultSet results = qe.execSelect();
-        boolean result = results.hasNext();
-        qe.close();
-        return result;
+        return qe.execSelect();
         }
     
     /**

Modified: jena/Scratch/Eyeball/trunk/src/test/java/com/hp/hpl/jena/eyeball/inspectors/test/TestSparqlDrivenInspector.java
URL: http://svn.apache.org/viewvc/jena/Scratch/Eyeball/trunk/src/test/java/com/hp/hpl/jena/eyeball/inspectors/test/TestSparqlDrivenInspector.java?rev=1414809&r1=1414808&r2=1414809&view=diff
==============================================================================
--- jena/Scratch/Eyeball/trunk/src/test/java/com/hp/hpl/jena/eyeball/inspectors/test/TestSparqlDrivenInspector.java
(original)
+++ jena/Scratch/Eyeball/trunk/src/test/java/com/hp/hpl/jena/eyeball/inspectors/test/TestSparqlDrivenInspector.java
Wed Nov 28 16:56:46 2012
@@ -46,7 +46,7 @@ import com.hp.hpl.jena.util.FileManager;
         SparqlDrivenInspector s = new SparqlDrivenInspector( root );
         s.begin( r, ontModel() );
         List<Property> predicates = r.getPredicateRegister().getRegisteredPredicates();
-        assertEquals( eyeResourceSet( "sparqlRequireFailed sparqlProhibitFailed" ), new HashSet<Property>(
predicates ) );
+        assertEquals( eyeResourceSet( "selected sparqlRequireFailed sparqlProhibitFailed"
), new HashSet<Property>( predicates ) );
         }
     
     @Test public void testReportsSPOViolation()
@@ -92,7 +92,7 @@ import com.hp.hpl.jena.util.FileManager;
         SparqlDrivenInspector s = new SparqlDrivenInspector( root );
         s.inspectModel( r, ontModel( "my count 17" ) );
         String expect =
-            "[eye:mainProperty eye:sparqlProhibitFailed & eye:sparqlProhibitFailed '<S>']"
+            "[eye:mainProperty eye:sparqlProhibitFailed & eye:selected 's = eh:/my, p
= eh:/count, o = 17' & eye:sparqlProhibitFailed '<S>']"
             .replaceAll(  "<S>", sparql )
             ;
         assertIsoModels( "should report prohibition violation", itemModel( expect ), r.model()
);
@@ -106,7 +106,7 @@ import com.hp.hpl.jena.util.FileManager;
         SparqlDrivenInspector s = new SparqlDrivenInspector( root );
         s.inspectModel( r, ontModel( "my count 17" ) );
         String expect =
-            "[eye:mainProperty eye:sparqlProhibitFailed & eye:sparqlProhibitFailed 'SPOO']"
+            "[eye:mainProperty eye:sparqlProhibitFailed & eye:sparqlProhibitFailed 'SPOO'
& eye:selected 's = eh:/my, p = eh:/count, o = 17']"
             .replaceAll(  "<S>", sparql )
             ;
         assertIsoModels( "should report prohibition violation", itemModel( expect ), r.model()
);
@@ -120,7 +120,7 @@ import com.hp.hpl.jena.util.FileManager;
         SparqlDrivenInspector s = new InsertingInspector( root, "NAME", sparql );
         s.inspectModel( r, ontModel( "my count 17" ) );
         String expect =
-            "[eye:mainProperty eye:sparqlProhibitFailed & eye:sparqlProhibitFailed '@\"NAME\"']";
+            "[eye:mainProperty eye:sparqlProhibitFailed & eye:selected 's = eh:/my, p
= eh:/count, o = 17' & eye:sparqlProhibitFailed '@\"NAME\"']";
         assertIsoModels( "should report prohibition violation", itemModel( expect ), r.model()
);        
         }
     



Mime
View raw message