# HG changeset patch # User IRI # Date 1270559019 -7200 # Node ID 9f3b0b3a56a4fc16b32697b406c929c5e8fdc5c9 # Parent 41b17723d6c8097e4237549734b82c50b91651fa add control to retrain the engine diff -r 41b17723d6c8 -r 9f3b0b3a56a4 .classpath --- a/.classpath Tue Mar 30 17:53:36 2010 +0200 +++ b/.classpath Tue Apr 06 15:03:39 2010 +0200 @@ -1,6 +1,7 @@ - + + @@ -13,6 +14,15 @@ - + + + + + + + + + + diff -r 41b17723d6c8 -r 9f3b0b3a56a4 .settings/org.eclipse.wst.common.component --- a/.settings/org.eclipse.wst.common.component Tue Mar 30 17:53:36 2010 +0200 +++ b/.settings/org.eclipse.wst.common.component Tue Apr 06 15:03:39 2010 +0200 @@ -1,9 +1,10 @@ - - - - - - - - - + + + + + + + + + + diff -r 41b17723d6c8 -r 9f3b0b3a56a4 WebContent/WEB-INF/lib/jackson-core-asl-1.1.1.jar Binary file WebContent/WEB-INF/lib/jackson-core-asl-1.1.1.jar has changed diff -r 41b17723d6c8 -r 9f3b0b3a56a4 WebContent/WEB-INF/lib/jackson-core-asl-1.5.0.jar Binary file WebContent/WEB-INF/lib/jackson-core-asl-1.5.0.jar has changed diff -r 41b17723d6c8 -r 9f3b0b3a56a4 lib/jackson-mapper-asl-1.5.0.jar Binary file lib/jackson-mapper-asl-1.5.0.jar has changed diff -r 41b17723d6c8 -r 9f3b0b3a56a4 src/fr/iri/thd/sonyengine/test/TestEngine.java --- a/src/fr/iri/thd/sonyengine/test/TestEngine.java Tue Mar 30 17:53:36 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,547 +0,0 @@ -package fr.iri.thd.sonyengine.test; - -import java.io.File; -import java.io.IOException; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.List; - -import javax.ws.rs.core.MediaType; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import com.sleepycat.persist.EntityCursor; -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; - -/* - * - * 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: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 testCreateMovieFragment() { - - String movieName = "movie" + 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); - - - dbenv = new DbEnv(); - dbenv.setup(databasePathFile, true, true); - - DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); - - - EntityCursor cursor = da.movieFragmentById.keys(); - - for (String key : cursor) { - Assert.assertNotNull(key); - } - - cursor.close(); - - Assert.assertTrue(da.movieFragmentById.contains(movieName)); - - } - - @Test - public void testCreateMovieFragmentForm() { - - String movieName = "movie" + System.currentTimeMillis(); - - Client c = Client.create(); - - Form form = new Form(); - form.add("id", movieName); - - WebResource res = c.resource(this.baseurl + "segment/create"); - - String response = res.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form); - - Assert.assertNotNull(response); - - dbenv = new DbEnv(); - dbenv.setup(databasePathFile, true, true); - - DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); - - - EntityCursor cursor = da.movieFragmentById.keys(); - - for (String key : cursor) { - Assert.assertNotNull(key); - } - - cursor.close(); - - Assert.assertTrue(da.movieFragmentById.contains(movieName)); - - } - - - @Test - public void testCreateTag() { - - String movieName = "movie" + System.currentTimeMillis(); - String tagName = "tag" + System.currentTimeMillis();; - - Client c = Client.create(); - WebResource res = c.resource(this.baseurl + "segment/create/" + movieName); - String response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); - - Assert.assertNotNull(response); - - res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName); - response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); - - Assert.assertNotNull(response); - - dbenv = new DbEnv(); - dbenv.setup(databasePathFile, true, true); - - DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); - - Assert.assertTrue(da.tagByName.contains(tagName)); - - } - - @Test - public void testCreateTagList() { - - String movieName = "movie" + System.currentTimeMillis(); - - List tagNames = new ArrayList(); - - for(int i=0; i< 10; 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 = ""; - for(String tagName: tagNames) { - xml += String.format("", tagName, movieName); - } - xml += ""; - - 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()); - - for(String tagName: tagNames) { - Assert.assertTrue(da.tagByName.contains(tagName)); - } - - } - - @Test - public void testCreateSequenceList() { - - List movieIds = new ArrayList(); - - for(int i=0; i< 10; i++) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } - movieIds.add("movie" + System.currentTimeMillis()); - } - - String xml = ""; - for(String sequenceId: movieIds) { - xml += String.format("", sequenceId); - } - xml += ""; - - Client c = Client.create(); - WebResource res = c.resource(this.baseurl).path("segment").path("createall"); - Form form = new Form(); - form.add("xml", xml); - String 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()); - - for(String sequenceId: movieIds) { - Assert.assertTrue(da.movieFragmentById.contains(sequenceId)); - } - - } - - - @Test - public void testReset() { - 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"); - - dbenv = new DbEnv(); - dbenv.setup(databasePathFile, true, true); - - DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); - - Assert.assertEquals(0, da.movieFragmentById.count()); - Assert.assertEquals(0, da.tagById.count()); - Assert.assertEquals(0, da.tagByName.count()); - Assert.assertEquals(0, da.tagBySegment.count()); - - } - - @Test - public void testTrain() { - - Client c = Client.create(); - WebResource res = c.resource(this.baseurl); - String response = res.path("engine").path("reset").accept(MediaType.TEXT_PLAIN).post(String.class); - - String[] ids = new String[]{"ref1","ref2","ref3","ref4","ref5","ref6", "ref7"}; - - String xmlSegment = ""; - for(String sequenceId: ids) { - xmlSegment += String.format("", sequenceId); - } - xmlSegment += ""; - - c = Client.create(); - res = c.resource(this.baseurl).path("segment").path("createall"); - Form form = new Form(); - form.add("xml", xmlSegment); - response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form); - - Assert.assertNotNull(response); - - - ArrayList tags = new ArrayList(); - tags.add(new String[]{"crayon", "ref1"}); - tags.add(new String[]{"carte", "ref1"}); - tags.add(new String[]{"bateau", "ref1"}); - tags.add(new String[]{"voiture", "ref1"}); - tags.add(new String[]{"car", "ref1"}); - - tags.add(new String[]{"enfant", "ref2"}); - tags.add(new String[]{"famille", "ref2"}); - tags.add(new String[]{"violon", "ref2"}); - tags.add(new String[]{"maison", "ref2"}); - tags.add(new String[]{"hopital", "ref2"}); - - tags.add(new String[]{"enfant", "ref3"}); - tags.add(new String[]{"famille", "ref3"}); - tags.add(new String[]{"violon", "ref3"}); - tags.add(new String[]{"maison", "ref3"}); - tags.add(new String[]{"ordinateur", "ref3"}); - tags.add(new String[]{"fenetre", "ref3"}); - tags.add(new String[]{"voiture", "ref3"}); - tags.add(new String[]{"guitare", "ref3"}); - tags.add(new String[]{"carte", "ref3"}); - - tags.add(new String[]{"enfant", "ref4"}); - tags.add(new String[]{"famille", "ref4"}); - tags.add(new String[]{"violon", "ref4"}); - tags.add(new String[]{"bateau", "ref4"}); - tags.add(new String[]{"ordinateur", "ref4"}); - tags.add(new String[]{"fenetre", "ref4"}); - tags.add(new String[]{"stylo", "ref4"}); - tags.add(new String[]{"perroquet", "ref4"}); - tags.add(new String[]{"crayon", "ref4"}); - tags.add(new String[]{"tele", "ref4"}); - tags.add(new String[]{"bateau", "ref4"}); - - tags.add(new String[]{"ordinateur", "ref5"}); - tags.add(new String[]{"crayon", "ref5"}); - tags.add(new String[]{"fenetre", "ref5"}); - tags.add(new String[]{"hopital", "ref5"}); - tags.add(new String[]{"carte", "ref5"}); - tags.add(new String[]{"tele", "ref5"}); - tags.add(new String[]{"stylo", "ref5"}); - tags.add(new String[]{"perroquet", "ref5"}); - tags.add(new String[]{"bateau", "ref5"}); - tags.add(new String[]{"guitare", "ref5"}); - - tags.add(new String[]{"maison", "ref6"}); - tags.add(new String[]{"perroquet", "ref6"}); - tags.add(new String[]{"stylo", "ref6"}); - tags.add(new String[]{"guitare", "ref6"}); - tags.add(new String[]{"hopital", "ref6"}); - tags.add(new String[]{"tele", "ref6"}); - tags.add(new String[]{"car", "ref6"}); - tags.add(new String[]{"voiture", "ref6"}); - tags.add(new String[]{"fenetre", "ref6"}); - - tags.add(new String[]{"car", "ref7"}); - tags.add(new String[]{"voiture", "ref7"}); - - String xmlTags = ""; - for(String[] tagdef: tags) { - xmlTags += String.format("", (Object[])tagdef); - } - xmlTags += ""; - - res = c.resource(this.baseurl).path("tag").path("add"); - form = new Form(); - form.add("xml", xmlTags); - response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form); - - Assert.assertNotNull(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"); - response = res.accept(MediaType.APPLICATION_XML_TYPE).get(String.class); - Assert.assertNotNull(response); - - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - InputSource source = new InputSource(new StringReader(response)); - Document doc = null; - try { - doc = factory.newDocumentBuilder().parse(source); - } catch (SAXException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (ParserConfigurationException e) { - e.printStackTrace(); - } - - NodeList nodes = doc.getElementsByTagName("neighbor"); - - Assert.assertEquals(1, nodes.getLength()); - - } - - -} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 src/fr/iri/thd/sonyengine/test/TestEngineResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fr/iri/thd/sonyengine/test/TestEngineResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,390 @@ +package fr.iri.thd.sonyengine.test.old; + +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.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; + +/* + * + * 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 TestEngineResource { + + 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 testCreateSequenceList() { + + List movieIds = new ArrayList(); + + for(int i=0; i< 10; i++) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + movieIds.add("movie" + System.currentTimeMillis()); + } + + String xml = ""; + for(String sequenceId: movieIds) { + xml += String.format("", sequenceId); + } + xml += ""; + + Client c = Client.create(); + WebResource res = c.resource(this.baseurl).path("segment").path("createall"); + Form form = new Form(); + form.add("xml", xml); + String 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()); + + for(String sequenceId: movieIds) { + Assert.assertTrue(da.movieFragmentById.contains(sequenceId)); + } + + } + + + @Test + public void testReset() { + 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"); + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertEquals(0, da.movieFragmentById.count()); + Assert.assertEquals(0, da.tagById.count()); + Assert.assertEquals(0, da.tagByName.count()); + Assert.assertEquals(0, da.tagBySegment.count()); + + } + + + @Test + public void testClearTagsForm() { + + String movieName = "movie" + System.currentTimeMillis(); + + List tagNames = new ArrayList(); + + for(int i=0; i< 10; 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 = ""; + for(String tagName: tagNames) { + xml += String.format("", tagName, movieName); + } + xml += ""; + + 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); + + form = new Form(); + form.add("id", movieName); + + res = c.resource(this.baseurl).path("segment").path("cleartags"); + response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.TEXT_PLAIN).post(String.class,form); + + Assert.assertEquals("ok", response); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertFalse(da.tagBySegment.contains(movieName)); + + + } + + @Test + public void testClearTagsPath() { + + String movieName = "movie" + System.currentTimeMillis(); + + List tagNames = new ArrayList(); + + for(int i=0; i< 10; 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 = ""; + for(String tagName: tagNames) { + xml += String.format("", tagName, movieName); + } + xml += ""; + + 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)); + + } + + +} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,323 @@ +package fr.iri.thd.sonyengine.test.old; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.core.MediaType; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.sleepycat.persist.EntityCursor; +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 TestMovieFragmentResource { + + 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; + this.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 testCreateMovieFragment() { + + String movieName = "movie" + 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); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + EntityCursor cursor = da.movieFragmentById.keys(); + + for (String key : cursor) { + Assert.assertNotNull(key); + } + + cursor.close(); + + Assert.assertTrue(da.movieFragmentById.contains(movieName)); + + } + + @Test + public void testCreateMovieFragmentForm() { + + String movieName = "movie" + System.currentTimeMillis(); + + Client c = Client.create(); + + Form form = new Form(); + form.add("id", movieName); + + WebResource res = c.resource(this.baseurl + "segment/create"); + + String response = res.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form); + + Assert.assertNotNull(response); + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + EntityCursor cursor = da.movieFragmentById.keys(); + + for (String key : cursor) { + Assert.assertNotNull(key); + } + + cursor.close(); + + Assert.assertTrue(da.movieFragmentById.contains(movieName)); + + } + + @Test + public void testCreateMovieFragmentFormRetrain() throws InterruptedException { + + Client c = Client.create(); + WebResource.Builder res = c.resource(this.baseurl + "segment/create").type(MediaType.APPLICATION_FORM_URLENCODED); + List movieNames = new ArrayList(); + + String movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + Form form = new Form(); + form.add("id", movieName); + form.add("retrain", "true"); + + String response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + Thread.sleep(1); + movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + form = new Form(); + form.add("id", movieName); + form.add("retrain", "false"); + + response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + Thread.sleep(1); + movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + form = new Form(); + form.add("id", movieName); + form.add("retrain", "foo"); + + response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + Thread.sleep(1); + movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + form = new Form(); + form.add("id", movieName); + form.add("retrain", "1"); + + response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + EntityCursor cursor = da.movieFragmentById.keys(); + + for (String key : cursor) { + Assert.assertNotNull(key); + } + + cursor.close(); + + for (String name : movieNames) { + Assert.assertTrue(da.movieFragmentById.contains(name)); + } + + + } + + @Test + public void testTrain() { + + Client c = Client.create(); + WebResource res = c.resource(this.baseurl); + String response = res.path("engine").path("reset").accept(MediaType.TEXT_PLAIN).post(String.class); + + String[] ids = new String[]{"ref1","ref2","ref3","ref4","ref5","ref6", "ref7"}; + + String xmlSegment = ""; + for(String sequenceId: ids) { + xmlSegment += String.format("", sequenceId); + } + xmlSegment += ""; + + c = Client.create(); + res = c.resource(this.baseurl).path("segment").path("createall"); + Form form = new Form(); + form.add("xml", xmlSegment); + response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form); + + Assert.assertNotNull(response); + + + ArrayList tags = new ArrayList(); + tags.add(new String[]{"crayon", "ref1"}); + tags.add(new String[]{"carte", "ref1"}); + tags.add(new String[]{"bateau", "ref1"}); + tags.add(new String[]{"voiture", "ref1"}); + tags.add(new String[]{"car", "ref1"}); + + tags.add(new String[]{"enfant", "ref2"}); + tags.add(new String[]{"famille", "ref2"}); + tags.add(new String[]{"violon", "ref2"}); + tags.add(new String[]{"maison", "ref2"}); + tags.add(new String[]{"hopital", "ref2"}); + + tags.add(new String[]{"enfant", "ref3"}); + tags.add(new String[]{"famille", "ref3"}); + tags.add(new String[]{"violon", "ref3"}); + tags.add(new String[]{"maison", "ref3"}); + tags.add(new String[]{"ordinateur", "ref3"}); + tags.add(new String[]{"fenetre", "ref3"}); + tags.add(new String[]{"voiture", "ref3"}); + tags.add(new String[]{"guitare", "ref3"}); + tags.add(new String[]{"carte", "ref3"}); + + tags.add(new String[]{"enfant", "ref4"}); + tags.add(new String[]{"famille", "ref4"}); + tags.add(new String[]{"violon", "ref4"}); + tags.add(new String[]{"bateau", "ref4"}); + tags.add(new String[]{"ordinateur", "ref4"}); + tags.add(new String[]{"fenetre", "ref4"}); + tags.add(new String[]{"stylo", "ref4"}); + tags.add(new String[]{"perroquet", "ref4"}); + tags.add(new String[]{"crayon", "ref4"}); + tags.add(new String[]{"tele", "ref4"}); + tags.add(new String[]{"bateau", "ref4"}); + + tags.add(new String[]{"ordinateur", "ref5"}); + tags.add(new String[]{"crayon", "ref5"}); + tags.add(new String[]{"fenetre", "ref5"}); + tags.add(new String[]{"hopital", "ref5"}); + tags.add(new String[]{"carte", "ref5"}); + tags.add(new String[]{"tele", "ref5"}); + tags.add(new String[]{"stylo", "ref5"}); + tags.add(new String[]{"perroquet", "ref5"}); + tags.add(new String[]{"bateau", "ref5"}); + tags.add(new String[]{"guitare", "ref5"}); + + tags.add(new String[]{"maison", "ref6"}); + tags.add(new String[]{"perroquet", "ref6"}); + tags.add(new String[]{"stylo", "ref6"}); + tags.add(new String[]{"guitare", "ref6"}); + tags.add(new String[]{"hopital", "ref6"}); + tags.add(new String[]{"tele", "ref6"}); + tags.add(new String[]{"car", "ref6"}); + tags.add(new String[]{"voiture", "ref6"}); + tags.add(new String[]{"fenetre", "ref6"}); + + tags.add(new String[]{"car", "ref7"}); + tags.add(new String[]{"voiture", "ref7"}); + + String xmlTags = ""; + for(String[] tagdef: tags) { + xmlTags += String.format("", (Object[])tagdef); + } + xmlTags += ""; + + res = c.resource(this.baseurl).path("tag").path("add"); + form = new Form(); + form.add("xml", xmlTags); + response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form); + + Assert.assertNotNull(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"); + response = res.accept(MediaType.APPLICATION_XML_TYPE).get(String.class); + Assert.assertNotNull(response); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + InputSource source = new InputSource(new StringReader(response)); + Document doc = null; + try { + doc = factory.newDocumentBuilder().parse(source); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + + NodeList nodes = doc.getElementsByTagName("neighbor"); + + Assert.assertEquals(1, nodes.getLength()); + + } + + +} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 src/fr/iri/thd/sonyengine/test/TestTagResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fr/iri/thd/sonyengine/test/TestTagResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,224 @@ +package fr.iri.thd.sonyengine.test.old; + + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonLocation; +import org.codehaus.jackson.JsonParseException; +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +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.api.representation.Form; + +import fr.iri.thd.sonyengine.core.DataAccessor; +import fr.iri.thd.sonyengine.core.DbEnv; + +public class TestTagResource { + + 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 testCreateTag() { + + String movieName = "movie" + System.currentTimeMillis(); + String tagName = "tag" + System.currentTimeMillis();; + + Client c = Client.create(); + WebResource res = c.resource(this.baseurl + "segment/create/" + movieName); + String response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertTrue(da.tagByName.contains(tagName)); + + } + + @Test + public void testCreateTagRetrain() throws InterruptedException, JsonParseException, JsonMappingException, IOException { + + List tagNames = new ArrayList(); + + Client c = Client.create(); + ObjectMapper mapper = new ObjectMapper(); + + WebResource res = null; + String response = null; + String tagName = null; + Map resMap = null; + + String movieName = "movie" + System.currentTimeMillis(); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl + "segment/create/" + movieName); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(movieName, resMap.get("id")); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("true"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + Thread.sleep(1); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("false"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + Thread.sleep(1); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("foo"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + Thread.sleep(1); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("1"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + for (String tag : tagNames) { + Assert.assertTrue(da.tagByName.contains(tag)); + } + + } + + + @Test + public void testCreateTagList() throws JsonParseException, JsonMappingException, IOException { + + String movieName = "movie" + System.currentTimeMillis(); + + List tagNames = new ArrayList(); + + for(int i=0; i< 10; 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 = ""; + for(String tagName: tagNames) { + xml += String.format("", tagName, movieName); + } + xml += ""; + + 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_JSON_TYPE).post(String.class, form); + + Assert.assertNotNull(response); + + ObjectMapper mapper = new ObjectMapper(); + List> resList = mapper.readValue(response, new TypeReference>>(){}); + + Assert.assertEquals(tagNames, resList.size()); + for (Map map : resList) { + Assert.assertEquals(movieName,map.get("segment")); + Assert.assertTrue(tagNames.contains(map.get("name"))); + } + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertTrue(da.tagBySegment.contains(movieName)); + + for(String tagName: tagNames) { + Assert.assertTrue(da.tagByName.contains(tagName)); + } + + } + +} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java --- a/src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java Tue Mar 30 17:53:36 2010 +0200 +++ b/src/fr/iri/thd/sonyengine/web/MovieFragmentResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -32,7 +32,7 @@ public class MovieFragmentResource { - private MovieFragment create(String id, ServletContext context) { + private MovieFragment create(String id, boolean retrain, ServletContext context) { ThdEngine.getEngine().createMovieFragment(id); @@ -44,7 +44,8 @@ da.movieFragmentById.put(fragment); dbenv.sync(); - ThdEngine.getEngine().reTrain(); + if(retrain) + ThdEngine.getEngine().reTrain(); return fragment; @@ -55,22 +56,31 @@ @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public MovieFragment createPath(@PathParam("id") String id, @Context ServletContext context) { - return this.create(id, context); + return this.create(id, true, context); } + + @POST + @Path("create/{id}/{retrain}") + @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) + public MovieFragment createPath(@PathParam("id") String id, @PathParam("retrain") Boolean retrain, @Context ServletContext context) { + + return this.create(id, (retrain != null && retrain.booleanValue()), context); + } + @POST @Path("create") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public MovieFragment createForm(@FormParam("id") String id, @Context ServletContext context) { - return this.create(id, context); + public MovieFragment createForm(@FormParam("id") String id, @FormParam("retrain") Boolean retrain, @Context ServletContext context) { + return this.create(id, (retrain != null && retrain.booleanValue()), context); } @POST @Path("createall") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public List createAll(@FormParam("xml") String xmldocstr, @Context ServletContext context) { + public List createAll(@FormParam("xml") String xmldocstr, @FormParam("retrain") Boolean retrain ,@Context ServletContext context) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); InputSource source = new InputSource(new StringReader(xmldocstr)); @@ -103,19 +113,58 @@ } dbenv.sync(); - - ThdEngine.getEngine().reTrain(); + + if(retrain != null && retrain.booleanValue()) + ThdEngine.getEngine().reTrain(); return list; } + + private void clearTags(String id, boolean retrain, ServletContext context) { - public void find(String id, String tag, Float separation, Boolean b) { - } + ThdEngine.getEngine().clearTags(id); + + DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE); + + DataAccessor da = new DataAccessor(dbenv.getEntityStore()); + da.tagBySegment.delete(id); + dbenv.sync(); + + if(retrain) + ThdEngine.getEngine().reTrain(); + } + @POST + @Path("cleartags/{id}") + @Produces(MediaType.TEXT_PLAIN) + public String clearTagsPath(@PathParam("id") String id, @Context ServletContext context) { + + this.clearTags(id, true, context); + return "ok"; + } - public void get(String id) { - } + + @POST + @Path("cleartags/{id}/{retrain}") + @Produces(MediaType.TEXT_PLAIN) + public String clearTagsPath(@PathParam("id") String id, @PathParam("retrain") Boolean retrain, @Context ServletContext context) { + + this.clearTags(id, (retrain != null && retrain.booleanValue()), context); + return "ok"; + } + + @POST + @Path("cleartags") + @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public String clearTagsForm(@FormParam("id") String id, @FormParam("retrain") Boolean retrain, @Context ServletContext context) { + + this.clearTags(id, (retrain != null && retrain.booleanValue()), context); + return "ok"; + } + + } diff -r 41b17723d6c8 -r 9f3b0b3a56a4 src/fr/iri/thd/sonyengine/web/TagResource.java --- a/src/fr/iri/thd/sonyengine/web/TagResource.java Tue Mar 30 17:53:36 2010 +0200 +++ b/src/fr/iri/thd/sonyengine/web/TagResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -32,11 +32,19 @@ @Path ("tag") public class TagResource { - @Path("add/{id}/{movieFragment}") @POST @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) - public Tag add(@PathParam("id") String tagName, @PathParam("movieFragment") String movieSegmentRef,@Context ServletContext context) { + public Tag add(@PathParam("id") String tagName, @PathParam("movieFragment") String movieSegmentRef, @Context ServletContext context) { + return this.add(tagName, movieSegmentRef, Boolean.TRUE, context); + } + + + + @Path("add/{id}/{movieFragment}/{retrain}") + @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); DbEnv dbenv = (DbEnv)context.getAttribute(ServletContainer.DB_ENV_ATTRIBUTE); @@ -48,8 +56,8 @@ da.tagById.put(tag); dbenv.sync(); - - ThdEngine.getEngine().reTrain(); + if(retrain != null && retrain.booleanValue()) + ThdEngine.getEngine().reTrain(); return tag; @@ -59,7 +67,7 @@ @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) - public List add(@FormParam("xml") String xmldocstr, @Context ServletContext context) { + public List add(@FormParam("xml") String xmldocstr, @FormParam("retrain") Boolean retrain, @Context ServletContext context) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); InputSource source = new InputSource(new StringReader(xmldocstr)); @@ -94,7 +102,8 @@ dbenv.sync(); - ThdEngine.getEngine().reTrain(); + if(retrain != null && retrain.booleanValue()) + ThdEngine.getEngine().reTrain(); return list; } diff -r 41b17723d6c8 -r 9f3b0b3a56a4 test_src/fr/iri/thd/sonyengine/test/AllTests.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test_src/fr/iri/thd/sonyengine/test/AllTests.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,13 @@ +package fr.iri.thd.sonyengine.test; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + + +@RunWith(Suite.class) +@SuiteClasses({TestEngineResource.class, TestMovieFragmentResource.class, TestTagResource.class}) +public class AllTests { + + +} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 test_src/fr/iri/thd/sonyengine/test/TestEngineResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test_src/fr/iri/thd/sonyengine/test/TestEngineResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,389 @@ +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.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; + +/* + * + * 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 TestEngineResource { + + 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 testCreateSequenceList() { + + List movieIds = new ArrayList(); + + for(int i=0; i< 10; i++) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + movieIds.add("movie" + System.currentTimeMillis()); + } + + String xml = ""; + for(String sequenceId: movieIds) { + xml += String.format("", sequenceId); + } + xml += ""; + + Client c = Client.create(); + WebResource res = c.resource(this.baseurl).path("segment").path("createall"); + Form form = new Form(); + form.add("xml", xml); + String 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()); + + for(String sequenceId: movieIds) { + Assert.assertTrue(da.movieFragmentById.contains(sequenceId)); + } + + } + + + @Test + public void testReset() { + 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"); + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertEquals(0, da.movieFragmentById.count()); + Assert.assertEquals(0, da.tagById.count()); + Assert.assertEquals(0, da.tagByName.count()); + Assert.assertEquals(0, da.tagBySegment.count()); + + } + + + @Test + public void testClearTagsForm() { + + String movieName = "movie" + System.currentTimeMillis(); + + List tagNames = new ArrayList(); + + for(int i=0; i< 10; 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 = ""; + for(String tagName: tagNames) { + xml += String.format("", tagName, movieName); + } + xml += ""; + + 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); + + form = new Form(); + form.add("id", movieName); + + res = c.resource(this.baseurl).path("segment").path("cleartags"); + response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.TEXT_PLAIN).post(String.class,form); + + Assert.assertEquals("ok", response); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertFalse(da.tagBySegment.contains(movieName)); + + + } + + @Test + public void testClearTagsPath() { + + String movieName = "movie" + System.currentTimeMillis(); + + List tagNames = new ArrayList(); + + for(int i=0; i< 10; 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 = ""; + for(String tagName: tagNames) { + xml += String.format("", tagName, movieName); + } + xml += ""; + + 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)); + + } + + +} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 test_src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test_src/fr/iri/thd/sonyengine/test/TestMovieFragmentResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,322 @@ +package fr.iri.thd.sonyengine.test; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.core.MediaType; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.sleepycat.persist.EntityCursor; +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 TestMovieFragmentResource { + + 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; + this.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 testCreateMovieFragment() { + + String movieName = "movie" + 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); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + EntityCursor cursor = da.movieFragmentById.keys(); + + for (String key : cursor) { + Assert.assertNotNull(key); + } + + cursor.close(); + + Assert.assertTrue(da.movieFragmentById.contains(movieName)); + + } + + @Test + public void testCreateMovieFragmentForm() { + + String movieName = "movie" + System.currentTimeMillis(); + + Client c = Client.create(); + + Form form = new Form(); + form.add("id", movieName); + + WebResource res = c.resource(this.baseurl + "segment/create"); + + String response = res.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form); + + Assert.assertNotNull(response); + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + EntityCursor cursor = da.movieFragmentById.keys(); + + for (String key : cursor) { + Assert.assertNotNull(key); + } + + cursor.close(); + + Assert.assertTrue(da.movieFragmentById.contains(movieName)); + + } + + @Test + public void testCreateMovieFragmentFormRetrain() throws InterruptedException { + + Client c = Client.create(); + WebResource.Builder res = c.resource(this.baseurl + "segment/create").type(MediaType.APPLICATION_FORM_URLENCODED); + List movieNames = new ArrayList(); + + String movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + Form form = new Form(); + form.add("id", movieName); + form.add("retrain", "true"); + + String response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + Thread.sleep(1); + movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + form = new Form(); + form.add("id", movieName); + form.add("retrain", "false"); + + response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + Thread.sleep(1); + movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + form = new Form(); + form.add("id", movieName); + form.add("retrain", "foo"); + + response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + Thread.sleep(1); + movieName = "movie" + System.currentTimeMillis(); + movieNames.add(movieName); + + form = new Form(); + form.add("id", movieName); + form.add("retrain", "1"); + + response = res.post(String.class, form); + + Assert.assertEquals(""+movieName+"",response); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + + EntityCursor cursor = da.movieFragmentById.keys(); + + for (String key : cursor) { + Assert.assertNotNull(key); + } + + cursor.close(); + + for (String name : movieNames) { + Assert.assertTrue(da.movieFragmentById.contains(name)); + } + + + } + + @Test + public void testTrain() { + + Client c = Client.create(); + WebResource res = c.resource(this.baseurl); + String response = res.path("engine").path("reset").accept(MediaType.TEXT_PLAIN).post(String.class); + + String[] ids = new String[]{"ref1","ref2","ref3","ref4","ref5","ref6", "ref7"}; + + String xmlSegment = ""; + for(String sequenceId: ids) { + xmlSegment += String.format("", sequenceId); + } + xmlSegment += ""; + + c = Client.create(); + res = c.resource(this.baseurl).path("segment").path("createall"); + Form form = new Form(); + form.add("xml", xmlSegment); + response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form); + + Assert.assertNotNull(response); + + + ArrayList tags = new ArrayList(); + tags.add(new String[]{"crayon", "ref1"}); + tags.add(new String[]{"carte", "ref1"}); + tags.add(new String[]{"bateau", "ref1"}); + tags.add(new String[]{"voiture", "ref1"}); + tags.add(new String[]{"car", "ref1"}); + + tags.add(new String[]{"enfant", "ref2"}); + tags.add(new String[]{"famille", "ref2"}); + tags.add(new String[]{"violon", "ref2"}); + tags.add(new String[]{"maison", "ref2"}); + tags.add(new String[]{"hopital", "ref2"}); + + tags.add(new String[]{"enfant", "ref3"}); + tags.add(new String[]{"famille", "ref3"}); + tags.add(new String[]{"violon", "ref3"}); + tags.add(new String[]{"maison", "ref3"}); + tags.add(new String[]{"ordinateur", "ref3"}); + tags.add(new String[]{"fenetre", "ref3"}); + tags.add(new String[]{"voiture", "ref3"}); + tags.add(new String[]{"guitare", "ref3"}); + tags.add(new String[]{"carte", "ref3"}); + + tags.add(new String[]{"enfant", "ref4"}); + tags.add(new String[]{"famille", "ref4"}); + tags.add(new String[]{"violon", "ref4"}); + tags.add(new String[]{"bateau", "ref4"}); + tags.add(new String[]{"ordinateur", "ref4"}); + tags.add(new String[]{"fenetre", "ref4"}); + tags.add(new String[]{"stylo", "ref4"}); + tags.add(new String[]{"perroquet", "ref4"}); + tags.add(new String[]{"crayon", "ref4"}); + tags.add(new String[]{"tele", "ref4"}); + tags.add(new String[]{"bateau", "ref4"}); + + tags.add(new String[]{"ordinateur", "ref5"}); + tags.add(new String[]{"crayon", "ref5"}); + tags.add(new String[]{"fenetre", "ref5"}); + tags.add(new String[]{"hopital", "ref5"}); + tags.add(new String[]{"carte", "ref5"}); + tags.add(new String[]{"tele", "ref5"}); + tags.add(new String[]{"stylo", "ref5"}); + tags.add(new String[]{"perroquet", "ref5"}); + tags.add(new String[]{"bateau", "ref5"}); + tags.add(new String[]{"guitare", "ref5"}); + + tags.add(new String[]{"maison", "ref6"}); + tags.add(new String[]{"perroquet", "ref6"}); + tags.add(new String[]{"stylo", "ref6"}); + tags.add(new String[]{"guitare", "ref6"}); + tags.add(new String[]{"hopital", "ref6"}); + tags.add(new String[]{"tele", "ref6"}); + tags.add(new String[]{"car", "ref6"}); + tags.add(new String[]{"voiture", "ref6"}); + tags.add(new String[]{"fenetre", "ref6"}); + + tags.add(new String[]{"car", "ref7"}); + tags.add(new String[]{"voiture", "ref7"}); + + String xmlTags = ""; + for(String[] tagdef: tags) { + xmlTags += String.format("", (Object[])tagdef); + } + xmlTags += ""; + + res = c.resource(this.baseurl).path("tag").path("add"); + form = new Form(); + form.add("xml", xmlTags); + response = res.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML_TYPE).post(String.class, form); + + Assert.assertNotNull(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"); + response = res.accept(MediaType.APPLICATION_XML_TYPE).get(String.class); + Assert.assertNotNull(response); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + InputSource source = new InputSource(new StringReader(response)); + Document doc = null; + try { + doc = factory.newDocumentBuilder().parse(source); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + + NodeList nodes = doc.getElementsByTagName("neighbor"); + + Assert.assertEquals(1, nodes.getLength()); + + } + + +} diff -r 41b17723d6c8 -r 9f3b0b3a56a4 test_src/fr/iri/thd/sonyengine/test/TestTagResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test_src/fr/iri/thd/sonyengine/test/TestTagResource.java Tue Apr 06 15:03:39 2010 +0200 @@ -0,0 +1,227 @@ +package fr.iri.thd.sonyengine.test; + + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +import org.codehaus.jackson.JsonParseException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +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.api.representation.Form; + +import fr.iri.thd.sonyengine.core.DataAccessor; +import fr.iri.thd.sonyengine.core.DbEnv; + +public class TestTagResource { + + 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 testCreateTag() { + + String movieName = "movie" + System.currentTimeMillis(); + String tagName = "tag" + System.currentTimeMillis();; + + Client c = Client.create(); + WebResource res = c.resource(this.baseurl + "segment/create/" + movieName); + String response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertTrue(da.tagByName.contains(tagName)); + + } + + @Test + public void testCreateTagRetrain() throws InterruptedException, JsonParseException, JsonMappingException, IOException { + + List tagNames = new ArrayList(); + + Client c = Client.create(); + ObjectMapper mapper = new ObjectMapper(); + + WebResource res = null; + String response = null; + String tagName = null; + Map resMap = null; + + String movieName = "movie" + System.currentTimeMillis(); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl + "segment/create/" + movieName); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(movieName, resMap.get("id")); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("true"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + Thread.sleep(1); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("false"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + Thread.sleep(1); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("foo"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + Thread.sleep(1); + tagName = "tag" + System.currentTimeMillis(); + tagNames.add(tagName); + + res = c.resource(this.baseurl).path("tag").path("add").path(tagName).path(movieName).path("1"); + response = res.accept(MediaType.APPLICATION_JSON_TYPE).post(String.class); + + Assert.assertNotNull(response); + resMap = mapper.readValue(response, new TypeReference>(){} ); + Assert.assertEquals(tagName,resMap.get("name")); + Assert.assertEquals(movieName, resMap.get("segment")); + + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + for (String tag : tagNames) { + Assert.assertTrue(da.tagByName.contains(tag)); + } + + } + + + @SuppressWarnings("unchecked") + @Test + public void testCreateTagList() throws JsonParseException, JsonMappingException, IOException { + + String movieName = "movie" + System.currentTimeMillis(); + + List tagNames = new ArrayList(); + + for(int i=0; i< 10; 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 = ""; + for(String tagName: tagNames) { + xml += String.format("", tagName, movieName); + } + xml += ""; + + 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_JSON_TYPE).post(String.class, form); + + Assert.assertNotNull(response); + + ObjectMapper mapper = new ObjectMapper(); + TypeReference> typeref = new TypeReference>(){}; + Map resList = mapper.readValue(response, typeref); + + Assert.assertNotNull(resList); + + List> tagList = (List>)resList.get("tag"); + + Assert.assertEquals(tagNames.size(), tagList.size()); + for (Map map : tagList) { + Assert.assertEquals(movieName,map.get("segment")); + Assert.assertTrue(tagNames.contains(map.get("name"))); + } + + dbenv = new DbEnv(); + dbenv.setup(databasePathFile, true, true); + + DataAccessor da = new DataAccessor(this.dbenv.getEntityStore()); + + Assert.assertTrue(da.tagBySegment.contains(movieName)); + + for(String tagName: tagNames) { + Assert.assertTrue(da.tagByName.contains(tagName)); + } + + } + +}