Author: bdelacretaz
Date: Mon Apr  4 13:41:22 2011
New Revision: 1088621

URL: http://svn.apache.org/viewvc?rev=1088621&view=rev
Log:
STANBOL-138 - benchmark presentation implemented

Added:
    incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/
    incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/
    
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-input.html
   (with props)
    
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-results.html
   (with props)
    
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark.css
   (with props)
Modified:
    incubator/stanbol/trunk/enhancer/benchmark/pom.xml
    
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/TripleMatcher.java
    
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/BenchmarkServlet.java
    
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/TripleMatcherImpl.java

Modified: incubator/stanbol/trunk/enhancer/benchmark/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/pom.xml?rev=1088621&r1=1088620&r2=1088621&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/benchmark/pom.xml (original)
+++ incubator/stanbol/trunk/enhancer/benchmark/pom.xml Mon Apr  4 13:41:22 2011
@@ -43,6 +43,26 @@
                         <Private-Package>
                             org.apache.stanbol.enhancer.benchmark.impl.*
                         </Private-Package>
+                        
+                        <!-- TODO velocity should be in its own bundle -->
+                        
<Embed-Dependency>velocity,velocity-tools</Embed-Dependency>
+                        <Embed-Transitive>false</Embed-Transitive>
+                        
+                        <!-- Ignore Velocity stuff that we don't need -->
+                        <Import-Package>
+                            !com.werken.xpath.*,
+                            !org.apache.log.*,
+                            !org.apache.oro.*,
+                            !org.apache.tools.ant.*,
+                            !org.jdom.*,
+                            !org.apache.commons.beanutils.*,
+                            !org.apache.commons.validator.*,
+                            !org.apache.commons.digester.*,
+                            !org.apache.struts.*,
+                            !org.dom4j.*,
+                            !javax.servlet.jsp.*,
+                            *
+                        </Import-Package>
                     </instructions>
                 </configuration>
             </plugin>
@@ -88,5 +108,15 @@
             <artifactId>slf4j-simple</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.velocity</groupId>
+            <artifactId>velocity</artifactId>
+            <version>1.7</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.velocity</groupId>
+            <artifactId>velocity-tools</artifactId>
+            <version>2.0</version>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file

Modified: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/TripleMatcher.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/TripleMatcher.java?rev=1088621&r1=1088620&r2=1088621&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/TripleMatcher.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/TripleMatcher.java
 Mon Apr  4 13:41:22 2011
@@ -38,5 +38,9 @@ import org.apache.clerezza.rdf.core.Trip
  *  concern.
  */
 public interface TripleMatcher {
+    /** True if this matches suppplied Triple */
     boolean matches(Triple t);
+    
+    /** Get the expression used to build this matcher */
+    String getExpression();
 }

Modified: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/BenchmarkServlet.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/BenchmarkServlet.java?rev=1088621&r1=1088620&r2=1088621&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/BenchmarkServlet.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/BenchmarkServlet.java
 Mon Apr  4 13:41:22 2011
@@ -17,9 +17,8 @@
 package org.apache.stanbol.enhancer.benchmark.impl;
 
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.io.StringReader;
-import java.util.List;
+import java.util.Properties;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -29,10 +28,12 @@ import javax.servlet.http.HttpServletRes
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
-import org.apache.stanbol.enhancer.benchmark.Benchmark;
 import org.apache.stanbol.enhancer.benchmark.BenchmarkParser;
-import org.apache.stanbol.enhancer.benchmark.BenchmarkResult;
 import org.apache.stanbol.enhancer.servicesapi.EnhancementJobManager;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.tools.generic.EscapeTool;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.http.HttpService;
 import org.osgi.service.http.NamespaceException;
@@ -61,73 +62,63 @@ public class BenchmarkServlet extends Ht
     @Property(value=DEFAULT_MOUNT_PATH)
     public static final String MOUNT_PATH_PROPERTY = "mount.path";
     private String mountPath; 
+    private final VelocityEngine velocity = new VelocityEngine();
 
-    // TODO temp example
-    public static final String EXAMPLE =
-        "= INPUT =\n"
-        + "# Comments such as this one are ignored\n"
-        + "# This is the enhancer input, can be split on several lines\n"
-        + "Bob\n"
-        + "Marley was born in Kingston, Jamaica.\n"
-        + "\n"
-        + "= EXPECT =\n"
-        + "# EXPECT defines groups of predicate/object matchers that we expect 
to find in the output\n"
-        + "# Each group applies to one given enhancement: for the expectation 
to succeed, at least\n"
-        + "# one enhancement must match all lines in the group\n"
-        + "\n"
-        + "Description: Kingston must be found\n"
-        + "http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Kingston%2C_Jamaica\n";
-        + "\n"
-        + "Description: This one should fail\n"
-        + "http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Basel\n";
-        + "\n"
-        + "# The description: line starts a new group\n"
-        + "Description: Bob Marley must be found as a musical artist\n"
-        + "http://fise.iks-project.eu/ontology/entity-type URI 
http://dbpedia.org/ontology/MusicalArtist\n";
-        + "http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Bob_Marley\n";
-        + "\n"
-        + "= COMPLAIN =\n"
-        + "\n"
-        + "Description: Miles Davis must not be found\n"
-        + "http://fise.iks-project.eu/ontology/entity-type URI 
http://dbpedia.org/ontology/MusicalArtist\n";
-        + "http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Miles_Davis\n";
-        + "\n"
-        + "Description: Bob Marley in the COMPLAIN section should fail\n"
-        + "http://fise.iks-project.eu/ontology/entity-type URI 
http://dbpedia.org/ontology/MusicalArtist\n";
-        + "http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Bob_Marley\n";
-    ;
-        
     /** Register with HttpService when activated */
     public void activate(ComponentContext ctx) throws ServletException, 
NamespaceException {
         mountPath = (String)ctx.getProperties().get(MOUNT_PATH_PROPERTY);
         if(mountPath == null) {
             mountPath = DEFAULT_MOUNT_PATH;
         }
+        if(mountPath.endsWith("/")) {
+            mountPath = mountPath.substring(mountPath.length() - 1);
+        }
         
         httpService.registerServlet(mountPath, this, null, null);
         log.info("Servlet mounted at {}", mountPath);
+        
+        final Properties config = new Properties();
+        config.put("class.resource.loader.description", "Velocity Classpath 
Resource Loader");
+        config.put("class.resource.loader.class", 
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+        config.put("resource.loader","class");
+        velocity.init(config);
     }
     
     public void deactivate(ComponentContext ctx) {
         httpService.unregister(mountPath);
     }
+    
+    @Override
+    public String getServletInfo() {
+        return "Apache Stanbol Enhancer Benchmarks";
+    }
 
+    private VelocityContext getVelocityContext(HttpServletRequest request, 
String pageTitle) {
+        final VelocityContext ctx = new VelocityContext();
+        ctx.put("title", getServletInfo() + " - " + pageTitle);
+        ctx.put("contextPath", request.getContextPath());
+        ctx.put("cssPath", request.getContextPath() + mountPath + 
"/benchmark.css");
+        ctx.put("esc", new EscapeTool());
+        return ctx;
+    }
+    
     @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse 
response) 
     throws ServletException, IOException {
         
-        // TODO need a nicer page...
-        final StringBuilder sb = new StringBuilder();
-        sb.append("<html><body>");
-        sb.append("<form method='POST'>");
-        sb.append("<input type='submit'/><br/>");
-        sb.append("<textarea name='content' rows='60' cols='120'>" + EXAMPLE + 
"</textarea>");
-        sb.append("</form>");
-        sb.append("</body></html>");
-        
-        response.setContentType("text/html");
-        response.setCharacterEncoding("UTF-8");
-        response.getWriter().write(sb.toString());
+        if(request.getPathInfo() != null && 
request.getPathInfo().endsWith(".css")) {
+            final Template t = velocity.getTemplate("/velocity/benchmark.css");
+            response.setContentType("text/css");
+            response.setCharacterEncoding("UTF-8");
+            t.merge(getVelocityContext(request, null), response.getWriter());
+        } else {
+            final Template t = 
velocity.getTemplate("/velocity/benchmark-input.html");
+            final VelocityContext ctx = getVelocityContext(request, "Benchmark 
Input");
+            ctx.put("formAction", request.getContextPath() + mountPath);
+            response.setContentType("text/html");
+            response.setCharacterEncoding("UTF-8");
+            t.merge(ctx, response.getWriter());
+        }
     }
     
     @Override
@@ -138,23 +129,13 @@ public class BenchmarkServlet extends Ht
             throw new ServletException("Missing " + PARAM_CONTENT + " 
parameter");
         }
         
-        response.setContentType("text/plain");
-        response.setCharacterEncoding("UTF-8");
-        final PrintWriter pw = response.getWriter();
+        final Template t = 
velocity.getTemplate("/velocity/benchmark-results.html");
+        final VelocityContext ctx = getVelocityContext(request, "Benchmark 
Results");
+        ctx.put("jobManager", jobManager);
+        ctx.put("benchmarks", parser.parse(new StringReader(content)));
         
-        try {
-            final List<? extends Benchmark> benchmarks = parser.parse(new 
StringReader(content));
-            for(Benchmark b : benchmarks) {
-                final List<BenchmarkResult> results = b.execute(jobManager);
-                for(BenchmarkResult r : results) {
-                    pw.println(r.toString());
-                }
-            }
-        } catch(Exception e) {
-            log.error("Exception in runBenchmark", e);
-            e.printStackTrace(pw);
-        } finally {
-            pw.flush();
-        }
-  }
- }
\ No newline at end of file
+        response.setContentType("text/html");
+        response.setCharacterEncoding("UTF-8");
+        t.merge(ctx, response.getWriter());
+    }
+}
\ No newline at end of file

Modified: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/TripleMatcherImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/TripleMatcherImpl.java?rev=1088621&r1=1088620&r2=1088621&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/TripleMatcherImpl.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/benchmark/src/main/java/org/apache/stanbol/enhancer/benchmark/impl/TripleMatcherImpl.java
 Mon Apr  4 13:41:22 2011
@@ -50,7 +50,12 @@ class TripleMatcherImpl implements Tripl
     
     @Override
     public String toString() {
-        return getClass().getSimpleName() + " (" + predicateUri + " " + 
operator + " " + objectUri + ")";
+        return getClass().getSimpleName() + " (" + getExpression() + ")";
+    }
+    
+    /** Get the expression used to build this matcher */
+    public String getExpression() {
+        return predicateUri + " " + operator + " " + objectUri;
     }
     
     @Override

Added: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-input.html
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-input.html?rev=1088621&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-input.html
 (added)
+++ 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-input.html
 Mon Apr  4 13:41:22 2011
@@ -0,0 +1,71 @@
+<html>
+<head>
+       <title>$title</title>
+       <link rel="stylesheet" href="$cssPath"/>
+</head>
+<body>
+       <div class="home">
+               <a href="http://incubator.apache.org/stanbol";>
+                       <img 
src="/static/images/apache_stanbol_logo_cropped.png" alt="Apache Stanbol" />
+               </a>
+       </div>
+        
+       <div class="header">
+               <h1>$title</h1>
+               <div style="clear: both"></div> 
+       </div>
+        
+       <div class="content">
+               <form method='POST' action="$formAction">
+                       <div style="text-align:right"><input 
type='submit'/></div>
+                       <br/>
+               <textarea name='content' rows='80' cols='120' 
style:"width=100%; height=100%;">
+# Here are examples benchmarks, see unit tests for more detailed syntax 
information.           
+= INPUT =
+# Comments such as this one are ignored
+# This is the enhancer input, can be split on several lines
+Bob
+Marley was born in Kingston, Jamaica.
+
+= EXPECT =
+# EXPECT defines groups of predicate/object matchers that we expect to find in 
the output
+# Each group applies to one given enhancement: for the expectation to succeed, 
at least
+# one enhancement must match all lines in the group
+
+Description: Kingston must be found
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Kingston%2C_Jamaica
+
+Description: This one should fail
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Basel
+
+# The description: line starts a new group
+Description: Bob Marley must be found as a musical artist
+http://fise.iks-project.eu/ontology/entity-type URI 
http://dbpedia.org/ontology/MusicalArtist
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Bob_Marley
+
+= COMPLAIN =
+
+Description: Miles Davis must not be found
+http://fise.iks-project.eu/ontology/entity-type URI 
http://dbpedia.org/ontology/MusicalArtist
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Miles_Davis
+
+Description: Bob Marley in the COMPLAIN section should fail
+http://fise.iks-project.eu/ontology/entity-type URI 
http://dbpedia.org/ontology/MusicalArtist
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/resource/Bob_Marley
+
+= INPUT =
+Here's a second benchmark that talks about Paris, Texas
+
+= EXPECT =
+Description: Paris, Texas must be found
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/page/Paris%2C_Texas
+
+= COMPLAIN =
+Description: Paris, France must not be found
+http://fise.iks-project.eu/ontology/entity-reference URI 
http://dbpedia.org/page/Paris
+
+               </textarea>
+               </form>
+       </div>          
+</body>
+</html>
\ No newline at end of file

Propchange: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-input.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-results.html
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-results.html?rev=1088621&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-results.html
 (added)
+++ 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-results.html
 Mon Apr  4 13:41:22 2011
@@ -0,0 +1,54 @@
+<html>
+<head>
+       <title>$title</title>
+       <link rel="stylesheet" href="$cssPath"/>
+</head>
+<body>
+       <div class="home">
+               <a href="http://incubator.apache.org/stanbol";>
+                       <img 
src="/static/images/apache_stanbol_logo_cropped.png" alt="Apache Stanbol" />
+               </a>
+       </div>
+        
+       <div class="header">
+               <h1>$title</h1>
+               <div style="clear: both"></div> 
+       </div>
+        
+       <div class="content">
+               <div class="results">
+               #foreach($benchmark in $benchmarks)
+                       <div class="benchmark">
+                               <div class="input"><span 
class="label">Benchmark input: </span>$benchmark.getInputText()</div>
+                               #foreach($result in 
$benchmark.execute($jobManager))
+                                       <div class="result">
+                                               <div class="description">
+                                               
$result.getTripleMatcherGroup().getDescription()
+                                               </div>
+                                               
+                                               #if($result.successful())
+                                               <div 
class="success">SUCCESSFUL</div> 
+                                               #{else}
+                                               <div 
class="failure">**FAILED**: $result.getInfo()</div> 
+                                               #end
+                                               
+                                               <div class="matchers">
+                                               #foreach($matcher in 
$result.getTripleMatcherGroup().getMatchers())
+                                                       <div class="matcher">
+                                                               
#if($result.getTripleMatcherGroup().isExpectGroup())
+                                                                       (E) 
+                                                               #{else}
+                                                                       (C) 
+                                                               #end
+                                                               
$esc.html($matcher.getExpression())
+                                                       </div>
+                                               #end
+                                               </div>
+                                       </div>
+                               #end
+                       </div>                  
+               #end
+               </div>
+       </div>
+</body>
+</html>
\ No newline at end of file

Propchange: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark-results.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark.css
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark.css?rev=1088621&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark.css
 (added)
+++ 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark.css
 Mon Apr  4 13:41:22 2011
@@ -0,0 +1,39 @@
+@import url("$contextPath/static/style/stanbol.css");
+
+.benchmark {
+       margin-bottom: 2em;             
+}
+
+.benchmark .result {
+       margin-top: 1em;
+       margin-left: 2em;
+}
+
+.benchmark .input {
+       font-weight:bold;
+       font-size: 110%;
+}
+
+.benchmark .label {
+       font-weight:normal;
+       display:block;
+}
+
+.result .description {
+       font-size: 110%;
+       font-style: italic;
+}
+
+.result .success {
+       color:blue;
+}
+
+.result .failure {
+       color:red;
+       font-weight: bold;
+}
+
+.result .matchers {
+       margin-left: 2em;
+       font-size: 90%;
+}
\ No newline at end of file

Propchange: 
incubator/stanbol/trunk/enhancer/benchmark/src/main/resources/velocity/benchmark.css
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to