add test and move resources
authorIRI
Tue, 23 Mar 2010 12:31:57 +0100
changeset 2 4a089c49eb44
parent 1 25dda14508ec
child 3 52964a651638
add test and move resources
.classpath
WebContent/WEB-INF/web.xml
lib/junit-4.8.1.jar
src/fr/iri/thd/sonyengine/DataAccessor.java
src/fr/iri/thd/sonyengine/DbEnv.java
src/fr/iri/thd/sonyengine/EngineResource.java
src/fr/iri/thd/sonyengine/MovieFragment.java
src/fr/iri/thd/sonyengine/MovieFragmentResource.java
src/fr/iri/thd/sonyengine/ServletContainer.java
src/fr/iri/thd/sonyengine/Tag.java
src/fr/iri/thd/sonyengine/TagResource.java
src/fr/iri/thd/sonyengine/core/DataAccessor.java
src/fr/iri/thd/sonyengine/core/DbEnv.java
src/fr/iri/thd/sonyengine/core/MovieFragment.java
src/fr/iri/thd/sonyengine/core/Tag.java
src/fr/iri/thd/sonyengine/test/TestEngine.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/TagResource.java
--- a/.classpath	Sat Mar 20 03:54:43 2010 +0100
+++ b/.classpath	Tue Mar 23 12:31:57 2010 +0100
@@ -13,5 +13,6 @@
 	</classpathentry>
 	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
 	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
+	<classpathentry kind="lib" path="lib/junit-4.8.1.jar"/>
 	<classpathentry kind="output" path="build/classes"/>
 </classpath>
--- a/WebContent/WEB-INF/web.xml	Sat Mar 20 03:54:43 2010 +0100
+++ b/WebContent/WEB-INF/web.xml	Tue Mar 23 12:31:57 2010 +0100
@@ -12,7 +12,7 @@
   <servlet>
     <servlet-name>sonyengine</servlet-name>
     <servlet-class>
-        fr.iri.thd.sonyengine.ServletContainer
+        fr.iri.thd.sonyengine.web.ServletContainer
     </servlet-class>
     <init-param>
       <param-name>initDatabasePath</param-name>
Binary file lib/junit-4.8.1.jar has changed
--- a/src/fr/iri/thd/sonyengine/DataAccessor.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import com.sleepycat.je.DatabaseException;
-import com.sleepycat.persist.EntityStore;
-import com.sleepycat.persist.PrimaryIndex;
-import com.sleepycat.persist.SecondaryIndex;
-
-public class DataAccessor {
-
-	public PrimaryIndex<String, MovieFragment> movieFragmentById;
-	
-	public PrimaryIndex<String, Tag> tagById;
-	public SecondaryIndex<String, String, Tag> tagByName;
-	public SecondaryIndex<String, String, Tag> tagBySegment;
-	
-	public DataAccessor(EntityStore store) throws DatabaseException {
-		
-		movieFragmentById = store.getPrimaryIndex(String.class, MovieFragment.class);
-		
-		tagById = store.getPrimaryIndex(String.class, Tag.class);
-		tagByName = store.getSecondaryIndex(tagById, String.class, "name");
-		tagBySegment = store.getSecondaryIndex(tagById, String.class, "segment");	
-		
-	}
-	
-}
--- a/src/fr/iri/thd/sonyengine/DbEnv.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,77 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import java.io.File;
-
-import com.sleepycat.je.DatabaseException;
-import com.sleepycat.je.Environment;
-import com.sleepycat.je.EnvironmentConfig;
-import com.sleepycat.persist.EntityStore;
-import com.sleepycat.persist.StoreConfig;
-
-public class DbEnv {
-
-    private Environment env;
-    private EntityStore store;
-
-    // Our constructor does nothing
-    public DbEnv() {}
-
-    // The setup() method opens the environment and store
-    // for us.
-    public void setup(File envHome, boolean readOnly)
-        throws DatabaseException {
-
-        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
-        StoreConfig storeConfig = new StoreConfig();
-
-        myEnvConfig.setReadOnly(readOnly);
-        storeConfig.setReadOnly(readOnly);
-
-        // If the environment is opened for write, then we want to be 
-        // able to create the environment and entity store if 
-        // they do not exist.
-        myEnvConfig.setAllowCreate(!readOnly);
-        storeConfig.setAllowCreate(!readOnly);
-
-        // Open the environment and entity store
-        env = new Environment(envHome, myEnvConfig);
-        store = new EntityStore(env, "EntityStore", storeConfig);
-
-    }
-
-    // Return a handle to the entity store
-    public EntityStore getEntityStore() {
-        return store;
-    }
-
-    // Return a handle to the environment
-    public Environment getEnv() {
-        return env;
-    }
-
-    // Close the store and environment.
-    public void close() {
-        if (store != null) {
-            try {
-                store.close();
-            } catch(DatabaseException dbe) {
-                System.err.println("Error closing store: " +
-                                    dbe.toString());
-                throw dbe;
-            }
-        }
-
-        if (env != null) {
-            try {
-                // Finally, close the environment.
-                env.close();
-            } catch(DatabaseException dbe) {
-            	System.err.println("Error closing DbEnv: " +
-                                    dbe.toString());
-            	throw dbe;
-            }
-        }
-    }
-	
-	
-}
--- a/src/fr/iri/thd/sonyengine/EngineResource.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import javax.ws.rs.Path;
-import javax.ws.rs.POST;
-import javax.ws.rs.PathParam;
-
-import thd.ThdEngine;
-
-@Path ("engine")
-public class EngineResource {
-
-	@POST
-	@Path("train")
-    public String train() {    	
-
-		ThdEngine.getEngine().reTrain();    	
-    	return "ok\n";
-    }
-    
-	@POST
-	@Path("train/{id}")
-    public String train(@PathParam("id") String tagName) {    	
-
-		ThdEngine.getEngine().reTrain(tagName);
-    	return tagName + " : ok\n";
-    }
-
-}
-
-
--- a/src/fr/iri/thd/sonyengine/MovieFragment.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import com.sleepycat.persist.model.Entity;
-import com.sleepycat.persist.model.PrimaryKey;
-
-@Entity
-public class MovieFragment {
-
-	public String getId() {
-		return id;
-	}
-
-	public void setId(String id) {
-		this.id = id;
-	}
-
-	@PrimaryKey
-	private String id;
-
-	public MovieFragment(String id) {
-		this.id = id;
-	}
-	
-}
--- a/src/fr/iri/thd/sonyengine/MovieFragmentResource.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import javax.servlet.ServletContext;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.core.Context;
-
-import thd.ThdEngine;
-
-@Path ("segment")
-public class MovieFragmentResource {
-
-	@POST
-	@Path("create/{id}")
-    public void create(@PathParam("id") String id, @Context ServletContext context) {
-    	
-		ThdEngine.getEngine().createMovieFragment(id);
-		
-		DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
-		
-		DataAccessor da = new DataAccessor(dbenv.getEntityStore());
-		
-		MovieFragment fragment = new MovieFragment(id);
-		da.movieFragmentById.put(fragment);
-		
-		ThdEngine.getEngine().reTrain();
-		
-    }
-
-    public void find(String id, String tag, Float separation, Boolean b) {
-    }
-
-
-    public void get(String id) {
-    }
-}
-
-
--- a/src/fr/iri/thd/sonyengine/ServletContainer.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,90 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import java.io.File;
-
-import javax.servlet.ServletException;
-import javax.ws.rs.core.Application;
-
-import thd.ThdEngine;
-
-import com.sleepycat.persist.EntityCursor;
-
-public class ServletContainer extends
-		com.sun.jersey.spi.container.servlet.ServletContainer {
-
-	/**
-	 * 
-	 */
-	private static final long serialVersionUID = -447765110849619632L;
-
-	public static final String DB_ENV_ATTRIBUTE = "DB_ENV";
-	
-	public ServletContainer() {
-	}
-
-	public ServletContainer(Class<? extends Application> appClass) {
-		super(appClass);
-	}
-
-	public ServletContainer(Application app) {
-		super(app);
-	}
-
-
-	public void init() throws ServletException {
-		super.init();
-		
-		String databasePath = this.getInitParameter("initDatabasePath");
-		
-		if(databasePath == null)
-			return;
-
-		File databasePathFile = new File(databasePath);
-		
-		if(!databasePathFile.exists())
-		{
-			databasePathFile.mkdirs();
-		}
-		DbEnv env = new DbEnv();
-		env.setup(databasePathFile, false);
-		
-		// set DbEnv in servlet context
-		this.getServletContext().setAttribute(ServletContainer.DB_ENV_ATTRIBUTE, env);
-		
-		//load all entities
-		DataAccessor da = new DataAccessor(env.getEntityStore());
-		
-		EntityCursor<MovieFragment> movieFragmentItems = da.movieFragmentById.entities();
-		
-		try {
-			for(MovieFragment item : movieFragmentItems) {
-				ThdEngine.getEngine().createMovieFragment(item.getId());
-			}
-		}
-		finally {
-			movieFragmentItems.close();
-		}
-		
-		EntityCursor<Tag> tagItems = da.tagById.entities();
-		
-		try {
-			for(Tag item : tagItems) {
-				ThdEngine.getEngine().addTag(item.getName(), item.getSegment());
-			}
-		}
-		finally {
-			tagItems.close();
-		}
-		
-		ThdEngine.getEngine().reTrain();
-	}
-	
-	public void destroy() {
-		DbEnv env = (DbEnv)this.getServletContext().getAttribute(DB_ENV_ATTRIBUTE);
-		
-		if(env != null) {
-			env.close();
-		}
-	}
-	
-}
--- a/src/fr/iri/thd/sonyengine/Tag.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import java.util.UUID;
-
-import com.sleepycat.persist.model.Entity;
-import com.sleepycat.persist.model.PrimaryKey;
-import com.sleepycat.persist.model.Relationship;
-import com.sleepycat.persist.model.SecondaryKey;
-
-@Entity
-public class Tag {
-
-	@PrimaryKey
-	private String id = UUID.randomUUID().toString();
-	
-	@SecondaryKey(relate=Relationship.MANY_TO_ONE)
-	private String name;
-	
-	@SecondaryKey(relate=Relationship.MANY_TO_ONE, relatedEntity=MovieFragment.class)
-	private String segment;
-
-
-	public String getId() {
-		return id;
-	}
-
-	public void setName(String name) {
-		this.name = name;
-	}
-
-	public String getName() {
-		return name;
-	}
-
-	public void setSegment(String segment) {
-		this.segment = segment;
-	}
-
-	public String getSegment() {
-		return segment;
-	}
-}
--- a/src/fr/iri/thd/sonyengine/TagResource.java	Sat Mar 20 03:54:43 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-package fr.iri.thd.sonyengine;
-
-import javax.ws.rs.Path;
-
-@Path ("tag")
-public class TagResource {
-
-    public void get(String id) {
-    }
-
-}
-
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/core/DataAccessor.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,27 @@
+package fr.iri.thd.sonyengine.core;
+
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.persist.EntityStore;
+import com.sleepycat.persist.PrimaryIndex;
+import com.sleepycat.persist.SecondaryIndex;
+
+
+public class DataAccessor {
+
+	public PrimaryIndex<String, MovieFragment> movieFragmentById;
+	
+	public PrimaryIndex<String, Tag> tagById;
+	public SecondaryIndex<String, String, Tag> tagByName;
+	public SecondaryIndex<String, String, Tag> tagBySegment;
+	
+	public DataAccessor(EntityStore store) throws DatabaseException {
+		
+		movieFragmentById = store.getPrimaryIndex(String.class, MovieFragment.class);
+		
+		tagById = store.getPrimaryIndex(String.class, Tag.class);
+		tagByName = store.getSecondaryIndex(tagById, String.class, "name");
+		tagBySegment = store.getSecondaryIndex(tagById, String.class, "segment");	
+		
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/core/DbEnv.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,77 @@
+package fr.iri.thd.sonyengine.core;
+
+import java.io.File;
+
+import com.sleepycat.je.DatabaseException;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
+import com.sleepycat.persist.EntityStore;
+import com.sleepycat.persist.StoreConfig;
+
+public class DbEnv {
+
+    private Environment env;
+    private EntityStore store;
+
+    // Our constructor does nothing
+    public DbEnv() {}
+
+    // The setup() method opens the environment and store
+    // for us.
+    public void setup(File envHome, boolean readOnly)
+        throws DatabaseException {
+
+        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
+        StoreConfig storeConfig = new StoreConfig();
+
+        myEnvConfig.setReadOnly(readOnly);
+        storeConfig.setReadOnly(readOnly);
+
+        // If the environment is opened for write, then we want to be 
+        // able to create the environment and entity store if 
+        // they do not exist.
+        myEnvConfig.setAllowCreate(!readOnly);
+        storeConfig.setAllowCreate(!readOnly);
+
+        // Open the environment and entity store
+        env = new Environment(envHome, myEnvConfig);
+        store = new EntityStore(env, "EntityStore", storeConfig);
+
+    }
+
+    // Return a handle to the entity store
+    public EntityStore getEntityStore() {
+        return store;
+    }
+
+    // Return a handle to the environment
+    public Environment getEnv() {
+        return env;
+    }
+
+    // Close the store and environment.
+    public void close() {
+        if (store != null) {
+            try {
+                store.close();
+            } catch(DatabaseException dbe) {
+                System.err.println("Error closing store: " +
+                                    dbe.toString());
+                throw dbe;
+            }
+        }
+
+        if (env != null) {
+            try {
+                // Finally, close the environment.
+                env.close();
+            } catch(DatabaseException dbe) {
+            	System.err.println("Error closing DbEnv: " +
+                                    dbe.toString());
+            	throw dbe;
+            }
+        }
+    }
+	
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/core/MovieFragment.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,27 @@
+package fr.iri.thd.sonyengine.core;
+
+import com.sleepycat.persist.model.Entity;
+import com.sleepycat.persist.model.PrimaryKey;
+
+@Entity
+public class MovieFragment {
+
+	public String getId() {
+		return id;
+	}
+
+	public void setId(String id) {
+		this.id = id;
+	}
+
+	@PrimaryKey
+	private String id;
+
+	public MovieFragment(String id) {
+		this.id = id;
+	}
+	
+	public MovieFragment() {
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/core/Tag.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,52 @@
+package fr.iri.thd.sonyengine.core;
+
+import java.util.UUID;
+
+import com.sleepycat.persist.model.Entity;
+import com.sleepycat.persist.model.PrimaryKey;
+import com.sleepycat.persist.model.Relationship;
+import com.sleepycat.persist.model.SecondaryKey;
+
+
+@Entity
+public class Tag {
+
+	@PrimaryKey
+	private String id = UUID.randomUUID().toString();
+	
+	@SecondaryKey(relate=Relationship.MANY_TO_ONE)
+	private String name;
+	
+	@SecondaryKey(relate=Relationship.MANY_TO_ONE, relatedEntity=MovieFragment.class)
+	private String segment;
+
+
+	public String getId() {
+		return id;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setSegment(String segment) {
+		this.segment = segment;
+	}
+
+	public String getSegment() {
+		return segment;
+	}
+	
+	public Tag(String name, String segment) {
+		this.setName(name);
+		this.setSegment(segment);
+	}
+	
+	public Tag() {
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/test/TestEngine.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,225 @@
+package fr.iri.thd.sonyengine.test;
+
+import java.io.File;
+
+import javax.ws.rs.core.MediaType;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer;
+
+import fr.iri.thd.sonyengine.core.DataAccessor;
+import fr.iri.thd.sonyengine.core.DbEnv;
+
+/*
+ * 
+ * import java.io.File;
+
+import core.Tag;
+
+/**
+ * A class to test ThdEngine
+ *
+ * @author pachet
+ *
+public class ThdEngineTest {
+
+ public static void main(String[] args) {
+ testSimpleExampleManual();
+//   testSimpleExampleFile();
+ // testUniversCineFile();
+ }
+
+ /**
+ * Tests the engine with a small set of movies and tags, entered
+"manually", one by one
+ private static void testSimpleExampleManual() {
+ ThdEngine engine = ThdEngine.getEngine();
+ engine.createMovieFragment("ref1");
+ engine.createMovieFragment("ref2");
+ engine.createMovieFragment("ref3");
+ engine.createMovieFragment("ref4");
+ engine.createMovieFragment("ref5");
+ engine.createMovieFragment("ref6");
+ engine.createMovieFragment("ref7");
+
+ engine.addTag("crayon", "ref1");
+ engine.addTag("carte", "ref1");
+ engine.addTag("bateau", "ref1");
+ engine.addTag("voiture", "ref1");
+ engine.addTag("car", "ref1");
+
+ engine.addTag("enfant", "ref2");
+ engine.addTag("famille", "ref2");
+ engine.addTag("violon", "ref2");
+ engine.addTag("maison", "ref2");
+ engine.addTag("hopital", "ref2");
+
+ engine.addTag("enfant", "ref3");
+ engine.addTag("famille", "ref3");
+ engine.addTag("violon", "ref3");
+ engine.addTag("maison", "ref3");
+ engine.addTag("ordinateur", "ref3");
+ engine.addTag("fenetre", "ref3");
+ engine.addTag("voiture", "ref3");
+ engine.addTag("guitare", "ref3");
+ engine.addTag("carte", "ref3");
+
+ engine.addTag("enfant", "ref4");
+ engine.addTag("famille", "ref4");
+ engine.addTag("violon", "ref4");
+ engine.addTag("bateau", "ref4");
+ engine.addTag("ordinateur", "ref4");
+ engine.addTag("fenetre", "ref4");
+ engine.addTag("stylo", "ref4");
+ engine.addTag("perroquet", "ref4");
+ engine.addTag("crayon", "ref4");
+ engine.addTag("tele", "ref4");
+ engine.addTag("bateau", "ref4");
+
+ engine.addTag("ordinateur", "ref5");
+ engine.addTag("crayon", "ref5");
+ engine.addTag("fenetre", "ref5");
+ engine.addTag("hopital", "ref5");
+ engine.addTag("carte", "ref5");
+ engine.addTag("tele", "ref5");
+ engine.addTag("stylo", "ref5");
+ engine.addTag("perroquet", "ref5");
+ engine.addTag("bateau", "ref5");
+ engine.addTag("guitare", "ref5");
+
+ engine.addTag("maison", "ref6");
+ engine.addTag("perroquet", "ref6");
+ engine.addTag("stylo", "ref6");
+ engine.addTag("guitare", "ref6");
+ engine.addTag("hopital", "ref6");
+ engine.addTag("tele", "ref6");
+ engine.addTag("car", "ref6");
+ engine.addTag("voiture", "ref6");
+ engine.addTag("fenetre", "ref6");
+
+ engine.addTag("car", "ref7");
+ engine.addTag("voiture", "ref7");
+
+ engine.reTrain();
+ engine.showAllTagValues();
+
+ Neighbor neighbor = engine.findMore("ref1", "enfant", .05f, true);
+
+ while (neighbor != null) {
+  System.out.println(neighbor.getMovieRef() + " distance: "
+    + neighbor.getDistance());
+  neighbor = engine.findMore(neighbor.getMovieRef(), "enfant", .05f,
+    true);
+ }
+ System.out.println("cannot find any better");
+ }
+
+ /**
+ * Tests the engine with a xcl file containing references of movies and
+ * their tags. esult should be the same than testSimpleExampleManual()
+ public static void testSimpleExampleFile() {
+ ThdEngine engine = ThdEngine.getEngine();
+ engine.loadFromTextFile(new File("./Sessions/Essai.csv"));
+ System.out.println("number of movies read: "
+   + engine.getAllMovieFragments().size());
+
+ engine.reTrain();
+ engine.showAllTagValues();
+
+ Neighbor neighbor = engine.findMore("ref1", "enfant", .05f, true);
+
+ while (neighbor != null) {
+  System.out.println(neighbor.getMovieRef() + " distance: "
+    + neighbor.getDistance());
+  neighbor = engine.findMore(neighbor.getMovieRef(), "enfant", .05f,
+    true);
+ }
+ System.out.println("cannot find any better");
+ }
+
+ /**
+ * Tests the engine with a xcl file containing references of movies and
+ * their tags
+ public static void testUniversCineFile() {
+ ThdEngine engine = ThdEngine.getEngine();
+ engine.loadFromTextFile(new File("./Sessions/MovieFrame2.csv"));
+ System.out.println("number of movies read: "
+   + engine.getAllMovieFragments().size());
+ ThdMovieFragment sobibor = engine
+   .movieFragmentNamed("Sobibor, 14 octobre 1943, 16 heures");
+ engine.reTrain();
+
+ Tag T_enfant = engine.tagNamed("Enfant");
+ System.out.println(sobibor);
+ System.out.println(T_enfant);
+
+ engine.tagNamed("Enfant").valueFor(sobibor);
+
+ Neighbor neighbor = engine.findMore(sobibor.getName(), "Enfant", .05f,
+   true);
+
+ while (neighbor != null) {
+  System.out.println(neighbor.getMovieRef() + " distance: "
+    + neighbor.getDistance());
+  neighbor = engine.findMore(neighbor.getMovieRef(), "Enfant", .05f,
+    true);
+ }
+ System.out.println("cannot find any better");
+ }
+
+}
+
+ * 
+ * 
+ * 
+ * 
+ * 
+ */
+
+
+
+public class TestEngine {
+
+	public static final String BASE_URL = "http://localhost:8080/mosatags.sonyengine/";
+	public static final String DATABASE_PATH = "c:/tmp/db";
+
+	private DbEnv dbenv = null;
+	
+	
+	@Before
+	public void setup()
+	{
+		File databasePathFile = new File(DATABASE_PATH);
+		dbenv = new DbEnv();
+		dbenv.setup(databasePathFile, true);
+	}
+	
+	@After
+	public void teardown() {
+		dbenv.close();
+	}
+	
+	@Test
+	public void testCreateMovieFragment() {
+		
+		DataAccessor da = new DataAccessor(this.dbenv.getEntityStore());
+				
+		Client c = Client.create();
+		WebResource res = c.resource(BASE_URL + "segment/create/movie1");
+		
+		String response = res.accept(MediaType.TEXT_PLAIN_TYPE).post(String.class);
+		
+		Assert.assertNotNull(response);
+		
+		Assert.assertTrue(da.movieFragmentById.contains("movie1"));
+	}
+
+	
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/web/EngineResource.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,128 @@
+package fr.iri.thd.sonyengine.web;
+
+import java.io.StringWriter;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.POST;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Context;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.sleepycat.je.Transaction;
+
+import fr.iri.thd.sonyengine.core.DataAccessor;
+import fr.iri.thd.sonyengine.core.DbEnv;
+
+import thd.Neighbor;
+import thd.ThdEngine;
+
+@Path ("engine")
+public class EngineResource {
+
+	@POST
+	@Path("train")
+    public String train() {    	
+
+		ThdEngine.getEngine().reTrain();    	
+    	return "ok\n";
+    }
+    
+	@POST
+	@Path("train/{id}")
+    public String train(@PathParam("id") String tagName) {    	
+
+		ThdEngine.getEngine().reTrain(tagName);
+    	return tagName + " : ok\n";
+    }
+	
+	@GET
+	@Path("find/{segment}/{tag}/{percent}/{more}")
+	public String find(
+			@PathParam("segment") String segment, 
+			@PathParam("tag") String tag,
+			@PathParam("percent") String percent,
+			@PathParam("more") String more) {
+		
+		float percentf = (float)Integer.parseInt(percent) / 100.0f;
+		boolean moreb = Boolean.parseBoolean(more);
+		Neighbor n =  ThdEngine.getEngine().findMore(segment, tag, percentf, moreb);
+				
+		Document doc = null;
+		try {
+			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+		} catch (ParserConfigurationException e) {
+			e.printStackTrace();
+		}
+		
+		Element root = doc.createElement("sonyengine");
+		doc.appendChild(root);
+		
+		if(n!= null)
+		{
+			Element neighborElement = doc.createElement("neighbor");
+			neighborElement.setAttribute("segment", n.getMovieRef());
+			neighborElement.setAttribute("distance", Double.toString(n.getDistance()));
+			root.appendChild(neighborElement);
+		}
+		
+		TransformerFactory transfac = TransformerFactory.newInstance();
+        Transformer trans = null;
+		try {
+			trans = transfac.newTransformer();
+		} catch (TransformerConfigurationException e) {
+			e.printStackTrace();
+		}
+        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
+        trans.setOutputProperty(OutputKeys.INDENT, "yes");
+
+        //create string from xml tree
+        StringWriter sw = new StringWriter();
+        StreamResult result = new StreamResult(sw);
+        DOMSource source = new DOMSource(doc);
+        try {
+			trans.transform(source, result);
+		} catch (TransformerException e) {
+			e.printStackTrace();
+		}
+        return sw.toString();
+		
+	}
+	
+	@POST
+	@Path("reset")
+	public String ResetDatabase(@Context ServletContext context) {
+		
+    	DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
+		DataAccessor da = new DataAccessor(dbenv.getEntityStore());
+		
+		Transaction trans = dbenv.getEntityStore().getEnvironment().beginTransaction(null,null);
+		
+		for (String key : da.tagById.keys()) {
+			da.tagById.delete(trans, key);
+		}
+		
+		for (String key : da.movieFragmentById.keys()) {
+			da.movieFragmentById.delete(trans, key);
+		}
+		
+		trans.commit();
+		
+		return "ok";
+	}
+
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,45 @@
+package fr.iri.thd.sonyengine.web;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Context;
+
+import fr.iri.thd.sonyengine.core.DataAccessor;
+import fr.iri.thd.sonyengine.core.DbEnv;
+import fr.iri.thd.sonyengine.core.MovieFragment;
+
+import thd.ThdEngine;
+
+@Path ("segment")
+public class MovieFragmentResource {
+
+	@POST
+	@Path("create/{id}")
+    public String create(@PathParam("id") String id, @Context ServletContext context) {
+    	
+		ThdEngine.getEngine().createMovieFragment(id);
+		
+		DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
+		
+		DataAccessor da = new DataAccessor(dbenv.getEntityStore());
+		
+		MovieFragment fragment = new MovieFragment(id);
+		da.movieFragmentById.put(fragment);
+		
+		ThdEngine.getEngine().reTrain();
+		
+		return "ok";
+		
+    }
+
+    public void find(String id, String tag, Float separation, Boolean b) {
+    }
+
+
+    public void get(String id) {
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/web/ServletContainer.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,95 @@
+package fr.iri.thd.sonyengine.web;
+
+import java.io.File;
+
+import javax.servlet.ServletException;
+import javax.ws.rs.core.Application;
+
+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.MovieFragment;
+import fr.iri.thd.sonyengine.core.Tag;
+
+public class ServletContainer extends
+		com.sun.jersey.spi.container.servlet.ServletContainer {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = -447765110849619632L;
+
+	public static final String DB_ENV_ATTRIBUTE = "DB_ENV";
+	
+	public ServletContainer() {
+	}
+
+	public ServletContainer(Class<? extends Application> appClass) {
+		super(appClass);
+	}
+
+	public ServletContainer(Application app) {
+		super(app);
+	}
+
+
+	public void init() throws ServletException {
+		super.init();
+		
+		String databasePath = this.getInitParameter("initDatabasePath");
+		
+		if(databasePath == null)
+			return;
+
+		File databasePathFile = new File(databasePath);
+		
+		if(!databasePathFile.exists())
+		{
+			databasePathFile.mkdirs();
+		}
+		DbEnv env = new DbEnv();
+		env.setup(databasePathFile, false);
+		
+		// set DbEnv in servlet context
+		this.getServletContext().setAttribute(ServletContainer.DB_ENV_ATTRIBUTE, env);
+		
+		//load all entities
+		DataAccessor da = new DataAccessor(env.getEntityStore());
+		
+		EntityCursor<MovieFragment> movieFragmentItems = da.movieFragmentById.entities();
+		
+		try {
+			for(MovieFragment item : movieFragmentItems) {
+				ThdEngine.getEngine().createMovieFragment(item.getId());
+			}
+		}
+		finally {
+			movieFragmentItems.close();
+		}
+		
+		EntityCursor<Tag> tagItems = da.tagById.entities();
+		
+		try {
+			for(Tag item : tagItems) {
+				ThdEngine.getEngine().addTag(item.getName(), item.getSegment());
+			}
+		}
+		finally {
+			tagItems.close();
+		}
+		
+		ThdEngine.getEngine().reTrain();
+	}
+	
+	public void destroy() {
+		DbEnv env = (DbEnv)this.getServletContext().getAttribute(DB_ENV_ATTRIBUTE);
+		
+		if(env != null) {
+			env.close();
+		}
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fr/iri/thd/sonyengine/web/TagResource.java	Tue Mar 23 12:31:57 2010 +0100
@@ -0,0 +1,88 @@
+package fr.iri.thd.sonyengine.web;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Context;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import fr.iri.thd.sonyengine.core.DataAccessor;
+import fr.iri.thd.sonyengine.core.DbEnv;
+import fr.iri.thd.sonyengine.core.Tag;
+
+import thd.ThdEngine;
+
+@Path ("tag")
+public class TagResource {
+
+    
+    @Path("add/{id}/{movieFragment}")
+    @POST
+    public String add(@PathParam("id") String tagName, @PathParam("movieFragment") String movieSegmentRef,@Context ServletContext context) {
+    	ThdEngine.getEngine().addTag(tagName, movieSegmentRef);
+
+    	DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);
+		
+		DataAccessor da = new DataAccessor(dbenv.getEntityStore());
+		
+		
+		Tag tag = new Tag(tagName, movieSegmentRef);
+		da.tagById.put(tag);
+		
+		ThdEngine.getEngine().reTrain();
+
+		return "ok";	
+		
+    }
+    
+    @Path("add")
+    @POST
+    public String add(@FormParam("xml") String xmldocstr, @Context ServletContext context) {
+    	
+    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+    	InputSource source = new InputSource(new StringReader(xmldocstr));
+    	Document doc = null;
+		try {
+			doc = factory.newDocumentBuilder().parse(source);
+		} catch (SAXException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} catch (ParserConfigurationException e) {
+			e.printStackTrace();
+		}
+    	DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE);		
+		DataAccessor da = new DataAccessor(dbenv.getEntityStore());
+
+    	
+    	NodeList tags = doc.getElementsByTagName("tag");
+    	for(int i = 0; i<tags.getLength(); i++)
+    	{
+    		Element node = (Element)tags.item(i);
+    		String segment = node.getAttribute("segment");
+    		String name = node.getAttribute("name");
+    		Tag tag = new Tag(name, segment);
+    		ThdEngine.getEngine().addTag(name, segment);
+    		da.tagById.put(tag);
+    	}
+
+    	ThdEngine.getEngine().reTrain();
+    	
+    	return "ok";
+    }
+
+}
+
+