Add a parameter to reduce training when adding tags
authorymh <ymh.work@gmail.com>
Wed, 28 Apr 2010 22:32:31 +0200
changeset 15 4f60d2511e12
parent 14 3467142cfc25
child 16 36a9450b88ba
Add a parameter to reduce training when adding tags
WebContent/WEB-INF/templates/status.ftl
script/context.xml
src/fr/iri/thd/sonyengine/core/DataAccessor.java
src/fr/iri/thd/sonyengine/core/EngineAccessor.java
src/fr/iri/thd/sonyengine/core/MovieFragment.java
src/fr/iri/thd/sonyengine/core/Tag.java
src/fr/iri/thd/sonyengine/web/EngineResource.java
src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java
src/fr/iri/thd/sonyengine/web/ServletContainer.java
src/fr/iri/thd/sonyengine/web/StatusResource.java
src/fr/iri/thd/sonyengine/web/TagResource.java
test_src/fr/iri/thd/sonyengine/test/AllTests.java
test_src/fr/iri/thd/sonyengine/test/TestEngineAccessor.java
test_src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java
--- a/WebContent/WEB-INF/templates/status.ftl	Wed Apr 14 17:34:44 2010 +0200
+++ b/WebContent/WEB-INF/templates/status.ftl	Wed Apr 28 22:32:31 2010 +0200
@@ -16,6 +16,14 @@
       <td>${dbpath}</td>
     </tr>
     <tr>
+      <td>UNTRAINED LIMIT</td>
+      <td>${untrained_limit}</td>
+    </tr>
+    <tr>
+      <td>UNTRAINED COUNT</td>
+      <td>${untrained_count}</td>
+    </tr>
+    <tr>
       <td>Segments</td>
       <td>${segments_nb}</td>
     </tr>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/script/context.xml	Wed Apr 28 22:32:31 2010 +0200
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+--><!-- The contents of this file will be loaded for each web application -->
+<Context>
+
+    <!-- Default set of monitored resources -->
+    <WatchedResource>WEB-INF/web.xml</WatchedResource>
+	
+    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
+    <!--
+    <Manager pathname="" />
+    -->
+
+    <!-- Uncomment this to enable Comet connection tacking (provides events
+         on session expiration as well as webapp lifecycle) -->
+    <!--
+    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
+    -->
+    <!--Parameter name="initDatabasePath" value="c:/tmp/db" /-->
+	<!--  -->
+	<Parameter name="initDatabasePath" value="c:/tmp/db" />
+	<Parameter name="untrainedLimit" value="5" />
+
+</Context>
\ No newline at end of file
--- a/src/fr/iri/thd/sonyengine/core/DataAccessor.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/core/DataAccessor.java	Wed Apr 28 22:32:31 2010 +0200
@@ -13,6 +13,7 @@
 	public PrimaryIndex<String, Tag> tagById;
 	public SecondaryIndex<String, String, Tag> tagByName;
 	public SecondaryIndex<String, String, Tag> tagBySegment;
+	public SecondaryIndex<Boolean, String, Tag> tagByTrained;
 	
 	public DataAccessor(EntityStore store) throws DatabaseException {
 		
@@ -20,7 +21,8 @@
 		
 		tagById = store.getPrimaryIndex(String.class, Tag.class);
 		tagByName = store.getSecondaryIndex(tagById, String.class, "name");
-		tagBySegment = store.getSecondaryIndex(tagById, String.class, "segment");	
+		tagBySegment = store.getSecondaryIndex(tagById, String.class, "segment");
+		tagByTrained = store.getSecondaryIndex(tagById, Boolean.class, "trained");
 		
 	}
 	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/core/EngineAccessor.java	Wed Apr 28 22:32:31 2010 +0200
@@ -0,0 +1,165 @@
+package fr.iri.thd.sonyengine.core;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import thd.Neighbor;
+import thd.ThdEngine;
+import thd.ThdMovieFragment;
+
+import com.sleepycat.je.Transaction;
+import com.sleepycat.persist.EntityCursor;
+import com.sleepycat.persist.EntityIndex;
+
+import core.Tag;
+import core.TransformableItem;
+
+public class EngineAccessor {
+
+    private ThdEngine engine = null;
+    private DbEnv dbenv = null;
+    private int untrainedLimit = 0;
+    
+    private EngineAccessor() {
+        this.engine = ThdEngine.getEngine();
+    }
+    
+    private static class SingletonHolder {
+        private final static EngineAccessor instance = new EngineAccessor();
+    }
+    
+    public static EngineAccessor getInstance() {
+        return SingletonHolder.instance;
+    }
+
+    public Object clone()
+	    throws CloneNotSupportedException {
+        throw new CloneNotSupportedException(); 
+    }
+    
+    public ThdEngine getEngine() {
+        return this.engine;
+    }
+    
+    public void setDbEnv(DbEnv dbenv) {
+        if(this.dbenv == null)
+            this.dbenv = dbenv;
+    }
+    
+    public void setUntrainedLimit(int untrainedLimit) {
+        this.untrainedLimit = untrainedLimit;
+    }
+    
+    public DbEnv getDbEnv() {
+        return this.dbenv;
+    }
+    
+    public synchronized void close() {
+        if(this.dbenv != null) {
+            this.dbenv.close();
+            this.dbenv = null;
+        }
+        this.engine = null;
+    }
+    
+    public void train() {
+        this.train(true);
+    }
+    
+    public void train(boolean force) {   
+
+        final DataAccessor da = new DataAccessor(this.dbenv.getEntityStore());
+        final EntityIndex<String, fr.iri.thd.sonyengine.core.Tag> ei = da.tagByTrained.subIndex(Boolean.FALSE);
+        final EntityCursor<fr.iri.thd.sonyengine.core.Tag> cursor = ei.entities();
+        final long untrained = ei.count();
+        final List<fr.iri.thd.sonyengine.core.Tag> tagList = new ArrayList<fr.iri.thd.sonyengine.core.Tag>((int) untrained);
+        
+        try {
+                    
+            if(force || untrained>this.untrainedLimit)
+            {
+            
+                this.getEngine().reTrain();                
+
+                //final Transaction txn = this.dbenv.getEnv().beginTransaction(null, null);
+                
+                //try {
+                    for (fr.iri.thd.sonyengine.core.Tag tag : cursor) {
+                        tag.setTrained(Boolean.TRUE);
+                        tagList.add(tag);
+                        //da.tagById.put(txn,tag);
+                    }
+                    //txn.commit();
+                //}
+                //catch(Exception e) {
+                //    if (txn != null)
+                //        txn.abort();
+                //}
+            }
+        }
+        finally {
+            cursor.close();
+        }
+        
+        for (fr.iri.thd.sonyengine.core.Tag tag : tagList) {
+            da.tagById.put(tag);
+        }
+
+    }
+    
+    public void train(String tagName) {
+        this.getEngine().reTrain(tagName);
+    }
+    
+    public long getUntrainedCount() {
+        final DataAccessor da = new DataAccessor(this.dbenv.getEntityStore());
+        final EntityIndex<String, fr.iri.thd.sonyengine.core.Tag> ei = da.tagByTrained.subIndex(Boolean.FALSE);
+        return ei.count();        
+    }
+    
+    public void clearAll() {
+        this.getEngine().clearAll();
+    }
+    
+    public Neighbor findMore(String movieSegmentRef, String tagName, float percent, boolean moreOrLess) {
+        return this.getEngine().findMore(movieSegmentRef, tagName, percent, moreOrLess);
+    }
+    
+    public void clearTags(String movieSegmentRef) {
+        this.getEngine().clearTags(movieSegmentRef);
+    }
+
+    public ThdMovieFragment createMovieFragment(String ref) {
+        return this.getEngine().createMovieFragment(ref);
+    }
+    
+    
+    public void addTag(String tagName, String movieSegmentRef) {
+        this.getEngine().addTag(tagName, movieSegmentRef);
+    }
+    
+    public List<TransformableItem> getAllMovieFragments() {
+        return this.getEngine().getAllMovieFragments();
+    }
+
+    public void addTags(String[] tags, String ref) {
+        this.getEngine().addTags(tags, ref);
+    }
+    
+    public List<ThdMovieFragment> createMovieFragments(String refs) {
+        return this.getEngine().createMovieFragments(refs);
+    }
+    
+    public List<Neighbor> findSomeMore(String movieSegmentRef, String tagName, float percent, boolean moreOrLess, int nb) {
+        return this.getEngine().findSomeMore(movieSegmentRef, tagName, percent, moreOrLess, nb);
+    }
+    
+    public Tag tagNamed(String tagName) {
+        return this.getEngine().tagNamed(tagName);
+    }
+    
+    public ThdMovieFragment movieFragmentNamed(String movieSegmentRef) {
+        return this.getEngine().movieFragmentNamed(movieSegmentRef);
+    }
+    
+}
--- a/src/fr/iri/thd/sonyengine/core/MovieFragment.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/core/MovieFragment.java	Wed Apr 28 22:32:31 2010 +0200
@@ -1,8 +1,9 @@
 package fr.iri.thd.sonyengine.core;
 
+import javax.xml.bind.annotation.XmlRootElement;
+
 import com.sleepycat.persist.model.Entity;
 import com.sleepycat.persist.model.PrimaryKey;
-import javax.xml.bind.annotation.XmlRootElement;
 
 
 
--- a/src/fr/iri/thd/sonyengine/core/Tag.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/core/Tag.java	Wed Apr 28 22:32:31 2010 +0200
@@ -2,11 +2,12 @@
 
 import java.util.UUID;
 
+import javax.xml.bind.annotation.XmlRootElement;
+
 import com.sleepycat.persist.model.Entity;
 import com.sleepycat.persist.model.PrimaryKey;
 import com.sleepycat.persist.model.Relationship;
 import com.sleepycat.persist.model.SecondaryKey;
-import javax.xml.bind.annotation.XmlRootElement;
 
 
 @Entity
@@ -22,6 +23,8 @@
 	@SecondaryKey(relate=Relationship.MANY_TO_ONE, relatedEntity=MovieFragment.class)
 	private String segment;
 
+	@SecondaryKey(relate=Relationship.MANY_TO_ONE)
+	private Boolean trained = Boolean.FALSE;
 
 	public String getId() {
 		return id;
@@ -47,6 +50,15 @@
 		return segment;
 	}
 	
+
+	public void setTrained(Boolean trained) {
+		this.trained = trained;
+	}
+
+	public Boolean getTrained() {
+		return trained;
+	}
+
 	public Tag(String id, String name, String segment) {
 		this.setId(id);
 		this.setName(name);
@@ -60,5 +72,4 @@
 	
 	public Tag() {
 	}
-	
 }
--- a/src/fr/iri/thd/sonyengine/web/EngineResource.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/web/EngineResource.java	Wed Apr 28 22:32:31 2010 +0200
@@ -6,8 +6,8 @@
 
 import javax.servlet.ServletContext;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
-import javax.ws.rs.POST;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
@@ -25,14 +25,14 @@
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
+import thd.Neighbor;
+
 import com.sleepycat.je.Transaction;
 import com.sleepycat.persist.EntityCursor;
 
 import fr.iri.thd.sonyengine.core.DataAccessor;
 import fr.iri.thd.sonyengine.core.DbEnv;
-
-import thd.Neighbor;
-import thd.ThdEngine;
+import fr.iri.thd.sonyengine.core.EngineAccessor;
 
 @Path ("engine")
 public class EngineResource {
@@ -41,16 +41,16 @@
 	@Path("train")
     public String train() {    	
 
-		ThdEngine.getEngine().reTrain();    	
-    	return "ok\n";
+		EngineAccessor.getInstance().train();    	
+    	return "ok";
     }
     
 	@POST
 	@Path("train/{id}")
     public String train(@PathParam("id") String tagName) {    	
 
-		ThdEngine.getEngine().reTrain(tagName);
-    	return tagName + " : ok\n";
+	    EngineAccessor.getInstance().train(tagName);
+    	return tagName + " : ok";
     }
 	
 	@GET
@@ -64,7 +64,7 @@
 		
 		float percentf = Float.parseFloat(percent);
 		boolean moreb = Boolean.parseBoolean(more);
-		Neighbor n =  ThdEngine.getEngine().findMore(segment, tag, percentf, moreb);
+		Neighbor n =  EngineAccessor.getInstance().findMore(segment, tag, percentf, moreb);
 				
 		Document doc = null;
 		try {
@@ -148,8 +148,8 @@
 		
 		dbenv.sync();
 		
-		ThdEngine.getEngine().clearAll();
-		ThdEngine.getEngine().reTrain();
+		EngineAccessor.getInstance().clearAll();
+		EngineAccessor.getInstance().train();
 		
 		return "ok";
 	}
--- a/src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java	Wed Apr 28 22:32:31 2010 +0200
@@ -23,9 +23,9 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
-import thd.ThdEngine;
 import fr.iri.thd.sonyengine.core.DataAccessor;
 import fr.iri.thd.sonyengine.core.DbEnv;
+import fr.iri.thd.sonyengine.core.EngineAccessor;
 import fr.iri.thd.sonyengine.core.MovieFragment;
 import fr.iri.thd.sonyengine.core.SonyengineException;
 
@@ -34,7 +34,7 @@
 
 	private MovieFragment create(String id, boolean retrain, ServletContext context) {
 
-		ThdEngine.getEngine().createMovieFragment(id);
+		EngineAccessor.getInstance().createMovieFragment(id);
 		
 		DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
 		
@@ -45,7 +45,7 @@
 		dbenv.sync();
 		
 		if(retrain)
-			ThdEngine.getEngine().reTrain();
+		    EngineAccessor.getInstance().train();
 		
 		return fragment;
 		
@@ -110,7 +110,7 @@
     		Element node = (Element)tags.item(i);
     		String id = node.getAttribute("id");
     		MovieFragment sequence = new MovieFragment(id);
-    		ThdEngine.getEngine().createMovieFragment(id);
+    		EngineAccessor.getInstance().createMovieFragment(id);
     		da.movieFragmentById.put(sequence);
     		list.add(sequence);
     	}
@@ -118,7 +118,7 @@
 		dbenv.sync();
 
 		if(retrain != null && retrain.booleanValue())
-			ThdEngine.getEngine().reTrain();
+		    EngineAccessor.getInstance().train();
     	
     	return list;
 
@@ -126,7 +126,7 @@
 	
 	private void clearTags(String id, boolean retrain, ServletContext context) {
 
-		ThdEngine.getEngine().clearTags(id);
+	    EngineAccessor.getInstance().clearTags(id);
 		
 		DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
 		
@@ -135,7 +135,7 @@
 		dbenv.sync();
 		
 		if(retrain)
-			ThdEngine.getEngine().reTrain();
+		    EngineAccessor.getInstance().train();
 	}
 
 	@POST
--- a/src/fr/iri/thd/sonyengine/web/ServletContainer.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/web/ServletContainer.java	Wed Apr 28 22:32:31 2010 +0200
@@ -12,12 +12,11 @@
 import org.apache.log4j.BasicConfigurator;
 import org.apache.log4j.PropertyConfigurator;
 
-import thd.ThdEngine;
-
 import com.sleepycat.persist.EntityCursor;
 
 import fr.iri.thd.sonyengine.core.DataAccessor;
 import fr.iri.thd.sonyengine.core.DbEnv;
+import fr.iri.thd.sonyengine.core.EngineAccessor;
 import fr.iri.thd.sonyengine.core.MovieFragment;
 import fr.iri.thd.sonyengine.core.Tag;
 
@@ -25,6 +24,7 @@
 		com.sun.jersey.spi.container.servlet.ServletContainer {
 
 	public static final String INIT_DATABASE_PATH = "initDatabasePath";
+	public static final String UNTRAINED_LIMIT = "untrainedLimit";
 
 	/**
 	 * 
@@ -33,6 +33,8 @@
 
 	public static final String DB_ENV_ATTRIBUTE = "DB_ENV";
 	public static final String DB_PATH_ATTRIBUTE = "DB_PATH";
+	public static final String UNTRAINED_LIMIT_ATTRIBUTE = "UNTRAINED_LIMIT";
+	
 	
 	public ServletContainer() {
 	}
@@ -108,6 +110,18 @@
 		sc.setAttribute(ServletContainer.DB_ENV_ATTRIBUTE, env);
 		sc.setAttribute(ServletContainer.DB_PATH_ATTRIBUTE, databasePath);
 		
+		final String untrainedLimitStr = this.getServletContext().getInitParameter(UNTRAINED_LIMIT);
+		
+		int untrainedLimit = 0;
+		
+		if(untrainedLimitStr != null)
+		    untrainedLimit = Integer.parseInt(untrainedLimitStr);
+		
+		sc.setAttribute(ServletContainer.UNTRAINED_LIMIT_ATTRIBUTE, new Integer(untrainedLimit));
+		
+        EngineAccessor.getInstance().setUntrainedLimit(untrainedLimit);
+		EngineAccessor.getInstance().setDbEnv(env);
+		
 		//load all entities
 		DataAccessor da = new DataAccessor(env.getEntityStore());
 		
@@ -115,7 +129,7 @@
 		
 		try {
 			for(MovieFragment item : movieFragmentItems) {
-				ThdEngine.getEngine().createMovieFragment(item.getId());
+			    EngineAccessor.getInstance().createMovieFragment(item.getId());
 			}
 		}
 		finally {
@@ -126,14 +140,14 @@
 		
 		try {
 			for(Tag item : tagItems) {
-				ThdEngine.getEngine().addTag(item.getName(), item.getSegment());
+			    EngineAccessor.getInstance().addTag(item.getName(), item.getSegment());
 			}
 		}
 		finally {
 			tagItems.close();
 		}
 		
-		ThdEngine.getEngine().reTrain();
+		EngineAccessor.getInstance().train();
 	}
 	
 	public void destroy() {
--- a/src/fr/iri/thd/sonyengine/web/StatusResource.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/web/StatusResource.java	Wed Apr 28 22:32:31 2010 +0200
@@ -10,12 +10,11 @@
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 
-import thd.ThdEngine;
-
 import com.sun.jersey.api.view.Viewable;
 
 import fr.iri.thd.sonyengine.core.DataAccessor;
 import fr.iri.thd.sonyengine.core.DbEnv;
+import fr.iri.thd.sonyengine.core.EngineAccessor;
 
 @Path("status")
 public class StatusResource {
@@ -23,9 +22,12 @@
 	@GET
 	@Produces(MediaType.TEXT_HTML)
 	public Viewable info(@Context ServletContext context) {
-		String dbpath = (String)context.getAttribute(ServletContainer.DB_PATH_ATTRIBUTE);
-		
-		final int segments_nb = ThdEngine.getEngine().getAllMovieFragments().size();
+
+	    final String dbpath = (String)context.getAttribute(ServletContainer.DB_PATH_ATTRIBUTE);
+        final Integer untrained_limit = (Integer)context.getAttribute(ServletContainer.UNTRAINED_LIMIT_ATTRIBUTE);
+		final Long untrained_count = new Long(EngineAccessor.getInstance().getUntrainedCount());
+        
+		final int segments_nb = EngineAccessor.getInstance().getAllMovieFragments().size();
 		
 		final DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);		
 		final DataAccessor da = new DataAccessor(dbenv.getEntityStore());
@@ -36,6 +38,8 @@
 		final Map<String,Object> vars = new HashMap<String, Object>();
 		
 		vars.put("dbpath", dbpath);
+		vars.put("untrained_limit", untrained_limit);
+		vars.put("untrained_count", untrained_count);
 		vars.put("segments_nb", segments_nb);
 		vars.put("segments_db_nb", segments_db_nb);
 		vars.put("tags_nb", tags_nb);
--- a/src/fr/iri/thd/sonyengine/web/TagResource.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/src/fr/iri/thd/sonyengine/web/TagResource.java	Wed Apr 28 22:32:31 2010 +0200
@@ -25,14 +25,13 @@
 
 import fr.iri.thd.sonyengine.core.DataAccessor;
 import fr.iri.thd.sonyengine.core.DbEnv;
+import fr.iri.thd.sonyengine.core.EngineAccessor;
 import fr.iri.thd.sonyengine.core.SonyengineException;
 import fr.iri.thd.sonyengine.core.Tag;
 
-import thd.ThdEngine;
-
 @Path ("tag")
 public class TagResource {
-
+	
     @Path("add/{id}/{movieFragment}")
     @POST
     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@@ -46,7 +45,7 @@
     @POST
     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
     public Tag add(@PathParam("id") String tagName, @PathParam("movieFragment") String movieSegmentRef, @PathParam("retrain") Boolean retrain, @Context ServletContext context) {
-    	ThdEngine.getEngine().addTag(tagName, movieSegmentRef);
+        EngineAccessor.getInstance().addTag(tagName, movieSegmentRef);
 
     	DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
 		
@@ -58,7 +57,7 @@
 		dbenv.sync();
 
 		if(retrain != null && retrain.booleanValue())
-			ThdEngine.getEngine().reTrain();
+		    EngineAccessor.getInstance().train(false);
 
 		return tag;	
 		
@@ -99,7 +98,7 @@
     		String segment = node.getAttribute("segment");
     		String name = node.getAttribute("name");
     		Tag tag = new Tag(name, segment);
-    		ThdEngine.getEngine().addTag(name, segment);
+    		EngineAccessor.getInstance().addTag(name, segment);
     		da.tagById.put(tag);
     		list.add(tag);
     	}
@@ -107,7 +106,7 @@
 		dbenv.sync();
     	
 		if(retrain != null && retrain.booleanValue())
-			ThdEngine.getEngine().reTrain();
+		    EngineAccessor.getInstance().train(false);
     	
     	return list;
     }
--- a/test_src/fr/iri/thd/sonyengine/test/AllTests.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/test_src/fr/iri/thd/sonyengine/test/AllTests.java	Wed Apr 28 22:32:31 2010 +0200
@@ -6,7 +6,7 @@
 
 
 @RunWith(Suite.class)
-@SuiteClasses({TestEngineResource.class, TestMovieFragmentResource.class, TestTagResource.class})
+@SuiteClasses({TestEngineResource.class, TestMovieFragmentResource.class, TestTagResource.class, TestEngineAccessor.class})
 public class AllTests {
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test_src/fr/iri/thd/sonyengine/test/TestEngineAccessor.java	Wed Apr 28 22:32:31 2010 +0200
@@ -0,0 +1,154 @@
+package fr.iri.thd.sonyengine.test;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.core.MediaType;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.sleepycat.persist.EntityIndex;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.representation.Form;
+
+import fr.iri.thd.sonyengine.core.DataAccessor;
+import fr.iri.thd.sonyengine.core.DbEnv;
+
+public class TestEngineAccessor {
+
+    public static final String BASE_URL = "http://localhost:9080/sonyengine/";
+    public static final String DATABASE_PATH = "/Users/ymh/dev/tmp/db";
+
+    private DbEnv dbenv = null;
+    private File databasePathFile = null;
+    private String baseurl = null;
+            
+    @Before
+    public void setup()
+    {
+        String databasepath = System.getProperty("database_path");
+        if(databasepath == null)
+            databasepath = DATABASE_PATH;
+        databasePathFile = new File(databasepath);
+        
+        this.baseurl = System.getProperty("baseurl");
+        if(this.baseurl == null)
+            this.baseurl = BASE_URL;
+    }
+    
+    @After
+    public void teardown() {
+    
+        if(dbenv != null)
+            dbenv.close();
+    }
+    
+    @Test
+    public void testUntrainedBelowLimit() {
+        
+        Client c = Client.create();
+        WebResource res = c.resource(this.baseurl);
+        String response = res.path("engine").path("reset").accept(MediaType.TEXT_PLAIN).post(String.class);
+        
+        Assert.assertEquals(response, "ok");
+        
+        String movieName = "movie" + System.currentTimeMillis();
+        
+        List<String> tagNames = new ArrayList<String>();
+        
+        for(int i=0; i< 4; i++) {
+            try {
+                Thread.sleep(1);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            tagNames.add("tag" + System.currentTimeMillis());
+        }
+        
+        res = c.resource(this.baseurl + "segment/create/" + movieName);
+        response = res.accept(MediaType.APPLICATION_XML_TYPE).post(String.class);
+                
+        Assert.assertNotNull(response);
+        
+        String xml = "<tags>";
+        for(String tagName: tagNames) {
+            xml += String.format("<tag name=\"%s\" segment=\"%s\"/>", tagName, movieName);
+        }
+        xml += "</tags>";
+        
+        res = c.resource(this.baseurl).path("tag").path("add");
+        Form form = new Form();
+        form.add("xml", xml);
+        response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form);
+
+        Assert.assertNotNull(response);
+
+        dbenv = new DbEnv();
+        dbenv.setup(databasePathFile, true, true);
+
+        DataAccessor da = new DataAccessor(this.dbenv.getEntityStore());
+        
+        final EntityIndex<String, fr.iri.thd.sonyengine.core.Tag> ei = da.tagByTrained.subIndex(Boolean.FALSE);
+        Assert.assertEquals(4l, ei.count());
+        
+    }
+
+    @Test
+    public void testUntrainedOverLimit() {
+        
+        String movieName = "movie" + System.currentTimeMillis();
+        
+        List<String> tagNames = new ArrayList<String>();
+        
+        for(int i=0; i< 6; i++) {
+            try {
+                Thread.sleep(1);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            tagNames.add("tag" + System.currentTimeMillis());
+        }
+        
+        Client c = Client.create();
+        WebResource res = c.resource(this.baseurl + "segment/create/" + movieName);
+        String response = res.accept(MediaType.APPLICATION_XML_TYPE).post(String.class);
+                
+        Assert.assertNotNull(response);
+        
+        String xml = "<tags>";
+        for(String tagName: tagNames) {
+            xml += String.format("<tag name=\"%s\" segment=\"%s\"/>", tagName, movieName);
+        }
+        xml += "</tags>";
+        
+        res = c.resource(this.baseurl).path("tag").path("add");
+        Form form = new Form();
+        form.add("xml", xml);
+        response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form);
+
+        Assert.assertNotNull(response);
+                
+        res = c.resource(this.baseurl).path("segment").path("cleartags").path(movieName);
+        response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.TEXT_PLAIN).post(String.class);
+        
+        Assert.assertEquals("ok", response);
+
+        dbenv = new DbEnv();
+        dbenv.setup(databasePathFile, true, true);
+
+        DataAccessor da = new DataAccessor(this.dbenv.getEntityStore());
+                
+        Assert.assertFalse(da.tagBySegment.contains(movieName));
+        
+        final EntityIndex<String, fr.iri.thd.sonyengine.core.Tag> ei = da.tagByTrained.subIndex(Boolean.FALSE);
+        Assert.assertEquals(0l, ei.count());
+        
+    }
+
+    
+}
--- a/test_src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java	Wed Apr 14 17:34:44 2010 +0200
+++ b/test_src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java	Wed Apr 28 22:32:31 2010 +0200
@@ -292,7 +292,11 @@
 		response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form);
 
 		Assert.assertNotNull(response);
-
+		
+		response = c.resource(this.baseurl).path("engine").path("train").accept(MediaType.TEXT_PLAIN).post(String.class);
+        
+        Assert.assertEquals("ok", response);
+		
 		//"engine/find/{segment}/{tag}/{percent}/{more}"
 		//Neighbor neighbor = engine.findMore("ref1", "enfant", .05f, true);
 		res = c.resource(this.baseurl).path("engine").path("find").path("ref1").path("enfant").path("0.05").path("true");