OSDN Git Service

collection setter
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Wed, 20 May 2009 08:38:41 +0000 (08:38 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Wed, 20 May 2009 08:38:41 +0000 (08:38 +0000)
git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3318 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd

12 files changed:
src/main/java/org/xerial/lens/Lens.java
src/main/java/org/xerial/lens/ObjectLens.java
src/main/java/org/xerial/lens/ObjectMapper.java
src/main/java/org/xerial/relation/query/AmoebaJoinHandler.java
src/main/java/org/xerial/relation/query/AmoebaJoinHandlerBase.java
src/main/java/org/xerial/relation/query/StreamAmoebaJoin.java
src/main/java/org/xerial/silk/SilkWalker.java
src/main/java/org/xerial/util/bean/TypeInfo.java
src/test/java/org/xerial/lens/LensTest.java
src/test/java/org/xerial/lens/sample.bed.silk [new file with mode: 0644]
src/test/java/org/xerial/relation/query/StreamAmoebaJoinTest.java
src/test/java/org/xerial/relation/query/gene.silk [new file with mode: 0644]

index e0ed54a..bc415dd 100644 (file)
@@ -189,6 +189,17 @@ public class Lens
         return load(result, new SilkWalker(silkResource));
     }
 
+    public static <Result> Result loadSilk(Result result, Reader silkReader) throws XerialException, IOException
+    {
+        return load(result, new SilkWalker(silkReader));
+    }
+
+    public static <Result> Result loadSilk(Class<Result> resultType, Reader silkReader) throws XerialException,
+            IOException
+    {
+        return loadSilk(TypeInfo.createInstance(resultType), silkReader);
+    }
+
     public static <Result> Result loadXML(Result result, URL xmlResource) throws IOException, XerialException
     {
         if (xmlResource == null)
index 713470c..29093d9 100644 (file)
@@ -35,6 +35,7 @@ import java.util.regex.Pattern;
 \r
 import org.xerial.util.Pair;\r
 import org.xerial.util.bean.TypeInfo;\r
+import org.xerial.util.reflect.ReflectionUtil;\r
 \r
 /**\r
  * Tree to Object lens\r
@@ -117,7 +118,6 @@ public class ObjectLens
                 {\r
                     Class< ? > fieldType = eachField.getType();\r
                     String paramName = getCanonicalParameterName(eachField.getName());\r
-                    SetterType setterType = SetterType.SETTER;\r
 \r
                     if (TypeInfo.isArray(fieldType))\r
                     {\r
@@ -126,14 +126,17 @@ public class ObjectLens
                     }\r
                     else if (TypeInfo.isMap(fieldType))\r
                     {\r
-                        setterType = SetterType.PUTTER;\r
+\r
+                        // TODO map putter\r
+\r
                     }\r
                     if (TypeInfo.isCollection(fieldType))\r
                     {\r
-                        setterType = SetterType.ADDER;\r
+                        Class< ? > elementType = ReflectionUtil.getGenericCollectionElementType(eachField);\r
+                        setterContainer.add(ParameterSetter.newSetter(elementType, paramName, eachField));\r
                     }\r
-\r
-                    setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField));\r
+                    else\r
+                        setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField));\r
 \r
                 }\r
 \r
@@ -162,7 +165,8 @@ public class ObjectLens
                         if (relName == null)\r
                         {\r
                             // infer relation node names\r
-                            relName = new Pair<String, String>(argTypes[0].getSimpleName(), argTypes[1].getSimpleName());\r
+                            relName = new Pair<String, String>(getCanonicalParameterName(argTypes[0].getSimpleName()),\r
+                                    getCanonicalParameterName(argTypes[1].getSimpleName()));\r
                         }\r
 \r
                         relationSetterContainer.add(RelationSetter.newRelationSetter(relName.getFirst(), relName\r
@@ -240,8 +244,4 @@ public class ObjectLens
         return paramName.toLowerCase();\r
     }\r
 \r
-    private enum SetterType {\r
-        ADDER, SETTER, PUTTER, APPENDER\r
-    };\r
-\r
 }\r
index fc73c41..96bad41 100644 (file)
@@ -34,7 +34,7 @@ import org.xerial.core.XerialError;
 import org.xerial.core.XerialErrorCode;
 import org.xerial.core.XerialException;
 import org.xerial.relation.Node;
-import org.xerial.relation.query.AmoebaJoinHandler;
+import org.xerial.relation.query.AmoebaJoinHandlerBase;
 import org.xerial.relation.query.QuerySet;
 import org.xerial.relation.query.StreamAmoebaJoin;
 import org.xerial.relation.query.QuerySet.QuerySetBuilder;
@@ -199,7 +199,7 @@ public class ObjectMapper
 
         public void build(Class< ? > targetType, String alias)
         {
-            if (TypeInfo.isBasicType(targetType))
+            if (TypeInfo.isBasicType(targetType) || TypeInfo.isCollection(targetType))
                 return;
 
             if (processedClasses.contains(targetType))
@@ -208,8 +208,8 @@ public class ObjectMapper
             processedClasses.add(targetType);
 
             ObjectLens lens = ObjectLens.getObjectLens(targetType);
-            if (_logger.isDebugEnabled())
-                _logger.debug(String.format("class %s: %s\n", targetType.getSimpleName(), lens));
+            if (_logger.isTraceEnabled())
+                _logger.trace(String.format("class %s: %s\n", targetType.getSimpleName(), lens));
 
             for (ParameterSetter each : lens.getSetterList())
             {
@@ -263,7 +263,7 @@ public class ObjectMapper
 
     }
 
-    private class RelationExtracter implements AmoebaJoinHandler
+    private class RelationExtracter extends AmoebaJoinHandlerBase
     {
 
         public void leaveNode(Schema schema, Node node) throws Exception
@@ -277,8 +277,8 @@ public class ObjectMapper
 
         public void newAmoeba(Schema schema, Node coreNode, Node attributeNode) throws Exception
         {
-            if (_logger.isTraceEnabled())
-                _logger.trace(String.format("amoeba: (%s, %s) in %s", coreNode, attributeNode, schema));
+            if (_logger.isDebugEnabled())
+                _logger.debug(String.format("amoeba: (%s, %s) in %s", coreNode, attributeNode, schema));
 
             Binder binder = schema2binder.get(schema);
             if (binder == null)
index bc050b3..7d3b024 100644 (file)
@@ -29,9 +29,13 @@ import org.xerial.relation.schema.Schema;
 
 public interface AmoebaJoinHandler
 {
+    public void init();
+
     public void newAmoeba(Schema schema, Node coreNode, Node attributeNode) throws Exception;
 
     public void leaveNode(Schema schema, Node node) throws Exception;
 
     public void text(Schema schema, Node coreNode, String nodeName, String text) throws Exception;
+
+    public void finish();
 }
index 3ca6a4d..106b863 100644 (file)
@@ -51,4 +51,14 @@ public class AmoebaJoinHandlerBase implements AmoebaJoinHandler
 \r
     }\r
 \r
+    public void finish()\r
+    {\r
+\r
+    }\r
+\r
+    public void init()\r
+    {\r
+\r
+    }\r
+\r
 }\r
index a9b11d4..b029413 100644 (file)
@@ -333,6 +333,7 @@ public class StreamAmoebaJoin implements TreeVisitor
     {
         leaveNode("root", walker);
         _logger.debug("sweep finished");
+        handler.finish();
     }
 
     public void init(TreeWalker walker) throws XerialException
@@ -341,6 +342,8 @@ public class StreamAmoebaJoin implements TreeVisitor
         latticeCursor = nodeNameLattice.cursor();
         stateStack.addLast(latticeCursor.getNode());
 
+        handler.init();
+
         visitNode("root", null, walker);
     }
 
@@ -411,7 +414,10 @@ public class StreamAmoebaJoin implements TreeVisitor
         }
         catch (Exception e)
         {
-            throw new XerialError(XerialErrorCode.INHERITED, e);
+            if (e instanceof XerialException)
+                throw (XerialException) e;
+            else
+                throw new XerialException(XerialErrorCode.INHERITED, e);
         }
     }
 
@@ -420,17 +426,22 @@ public class StreamAmoebaJoin implements TreeVisitor
         Deque<Node> nodeStack = getNodeStack(nodeName);
         Node currentNode = nodeStack.getLast();
 
-        currentPath.removeLast();
-
         try
         {
             back(currentNode);
         }
         catch (Exception e)
         {
-            throw new XerialException(XerialErrorCode.INHERITED, e);
+            if (e instanceof XerialException)
+                throw (XerialException) e;
+            else if (e instanceof XerialError)
+                throw (XerialError) e;
+            else
+                throw new XerialException(XerialErrorCode.INHERITED, e);
         }
 
+        currentPath.removeLast();
+
         nodeStack.removeLast();
     }
 
index 2e59bae..e8cd765 100644 (file)
@@ -58,7 +58,7 @@ public class SilkWalker extends TreeWalkerImpl
      * @param input
      * @throws IOException
      */
-    protected SilkWalker(Reader input) throws IOException
+    public SilkWalker(Reader input) throws IOException
     {
         super(new SilkStreamReader(input));
     }
index 619b282..4b39e9d 100644 (file)
@@ -324,6 +324,7 @@ public class TypeInfo
             if (constractableClass != null)
             {
                 cons = getPublicDefaultConstructor(constractableClass);
+                assert cons != null;
                 constructorTable.put(c, cons);
                 return constractableClass.newInstance();
             }
@@ -344,6 +345,22 @@ public class TypeInfo
                     }
                 }
 
+                // search hidden constructors
+                for (Constructor< ? > constructor : c.getDeclaredConstructors())
+                {
+                    try
+                    {
+                        constructor.setAccessible(true);
+                        Object instance = createInstance(constructor);
+                        constructorTable.put(c, constructor);
+                        return (T) instance;
+                    }
+                    catch (Exception e)
+                    {
+
+                    }
+                }
+
                 throw new BeanException(BeanErrorCode.NoPublicConstructor, "No public constructor for the class: "
                         + c.getName() + " is available");
 
index e807ac5..d102365 100644 (file)
@@ -27,10 +27,10 @@ package org.xerial.lens;
 import static org.junit.Assert.*;
 
 import java.io.IOException;
+import java.util.List;
 
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.xerial.core.XerialException;
 import org.xerial.silk.SilkUtilTest;
@@ -52,7 +52,7 @@ public class LensTest
 
     static class GeneTable
     {
-        private HashedArrayList<Coordinate, Gene> sequenceTable;
+        private HashedArrayList<Coordinate, Gene> sequenceTable = new HashedArrayList<Coordinate, Gene>();
 
         public void add(Coordinate coordinate, Gene gene)
         {
@@ -71,7 +71,6 @@ public class LensTest
         {}
     }
 
-    @Ignore
     @Test
     public void testTranslateSilk() throws IOException, XerialException
     {
@@ -79,11 +78,14 @@ public class LensTest
 
         assertNotNull(g);
         assertEquals(2, g.sequenceTable.size());
-        assertEquals(4, g.sequenceTable.values().size());
+        for (Coordinate each : g.sequenceTable.keySet())
+        {
+            List<Gene> gl = g.sequenceTable.get(each);
+            assertEquals(2, gl.size());
+        }
 
     }
 
-    @Ignore
     @Test
     public void testMapping() throws Exception
     {
@@ -97,4 +99,30 @@ public class LensTest
         assertEquals("chr1", c.name);
 
     }
+
+    @Test
+    public void testBED() throws Exception
+    {
+        BEDGene g = Lens.loadSilk(BEDGene.class, FileResource.find(LensTest.class, "sample.bed.silk"));
+    }
+
+    public static class CDS
+    {
+        public long start;
+        public long end;
+    }
+
+    public static class Exon extends CDS
+    {
+
+    }
+
+    public static class BEDGene
+    {
+
+        public List<CDS> cds;
+        public List<Exon> exon;
+
+    }
+
 }
diff --git a/src/test/java/org/xerial/lens/sample.bed.silk b/src/test/java/org/xerial/lens/sample.bed.silk
new file mode 100644 (file)
index 0000000..949cfb9
--- /dev/null
@@ -0,0 +1,27 @@
+%silk(version:1.0)\r
+\r
+-track(name:"Item,RGB,Demo2", description:"Item RGBdemonstration2", visibility:2, itemRgb:"On", useScore:1, color:"0,128,0", url:"http://genome.ucsc.edu/goldenPath/help/clones.html#$$")\r
+ -gene(coordinate, start, end, name, strand, cds(start, end), exon(start, end)*, color, _[json])|\r
+chr7   127471197       127472364       Pos1    +       [127471197, 127472364]          #ff0000 {"score":300}\r
+chr7   127472364       127473531       Pos2    +       [127472364, 127473531]          #ff0000 {"score":200}\r
+chr7   127473531       127474698       Pos3    +       [127473531, 127474698]          #ff0000 {"score":900}\r
+chr7   127474698       127475865       Pos4    +       [127474698, 127475865]          #ff0000 {"score":400}\r
+chr7   127475865       127477032       Neg1    -       [127475865, 127477032]          #0000ff {"score":100}\r
+chr7   127477032       127478199       Neg2    -       [127477032, 127478199]          #0000ff {"score":500}\r
+chr7   127478199       127479366       Neg3    -       [127478199, 127479366]          #0000ff {"score":200}\r
+chr7   127479366       127480533       Pos5    +       [127479366, 127480533]          #ff0000 {"score":200}\r
+chr7   127480533       127481700       Neg4    -       [127480533, 127481700]          #0000ff {"score":600}\r
+chr22  2001    7001    itemB   -       [2201, 6951]    [[1, 433], [501, 600], [2001, 2550], [3501, 5000]]              {"score":200}\r
+chr22  2001    6001    cloneB  -       [2001, 6001]    [[1, 433], [3602, 4000]]                {"score":900}\r
+chr22  1001    5001    itemA   +       [1101, 4701]    [[1, 1567], [2513, 4000]]               {"score":960}\r
+chr22  1001    5001    itemA   +       [1101, 4701]    [[1, 1567], [2513, 4000]]               {"score":960}\r
+chr22  2001    7001    itemB   -       [2201, 6951]    [[1, 433], [501, 600], [2001, 2550], [3501, 5000]]              {"score":200}\r
+chr22  20100001        20100002                                        \r
+chr22  20110001        20110002                                        \r
+chr22  20100001        20100101        first                                   \r
+chr22  20100201        20100301        second                                  \r
+chr22  20100401        20100501        thirds                                  \r
+chr7   127472364       127473531       Pos2                                    {"score":200}\r
+chr7   127472364       127473531       Pos2    +                               {"score":200}\r
+chr7   127472364       127473531       Pos2    +       [127472364, 127473531]                  {"score":200}\r
+chr7   127472364       127473531       Pos2    +       [127472364, 127473531]          #ff0000 {"score":200}
\ No newline at end of file
index ddb163a..a4951bf 100644 (file)
@@ -24,6 +24,8 @@
 //--------------------------------------\r
 package org.xerial.relation.query;\r
 \r
+import static org.junit.Assert.*;\r
+\r
 import org.junit.After;\r
 import org.junit.Before;\r
 import org.junit.Test;\r
@@ -59,7 +61,7 @@ public class StreamAmoebaJoinTest
         qs.addQueryTarget(new SchemaBuilder().add("gene").add("id").add("name").add("start").add("end").add("sequence")\r
                 .build());\r
 \r
-        StreamAmoebaJoin aj = new StreamAmoebaJoin(qs.build(), new AmoebaJoinHandler() {\r
+        StreamAmoebaJoin aj = new StreamAmoebaJoin(qs.build(), new AmoebaJoinHandlerBase() {\r
 \r
             public void newAmoeba(Schema schema, Node n1, Node n2)\r
             {\r
@@ -79,4 +81,32 @@ public class StreamAmoebaJoinTest
 \r
         aj.sweep(new SilkWalker(FileResource.find(StreamAmoebaJoinTest.class, "sample.silk")));\r
     }\r
+\r
+    @Test\r
+    public void amoebaTest() throws Exception\r
+    {\r
+        QuerySetBuilder qs = new QuerySetBuilder();\r
+        qs.addQueryTarget(new SchemaBuilder().add("coordinate").add("revision").build());\r
+\r
+        StreamAmoebaJoin aj = new StreamAmoebaJoin(qs.build(), new AmoebaJoinHandlerBase() {\r
+\r
+            int count = 0;\r
+\r
+            public void newAmoeba(Schema schema, Node n1, Node n2)\r
+            {\r
+                _logger.debug(String.format("relation (%s, %s)", n1, n2));\r
+                count++;\r
+            }\r
+\r
+            public void finish()\r
+            {\r
+                assertEquals(2, count);\r
+            }\r
+\r
+        });\r
+\r
+        aj.sweep(new SilkWalker(FileResource.find(StreamAmoebaJoinTest.class, "gene.silk")));\r
+\r
+    }\r
+\r
 }\r
diff --git a/src/test/java/org/xerial/relation/query/gene.silk b/src/test/java/org/xerial/relation/query/gene.silk
new file mode 100644 (file)
index 0000000..def877a
--- /dev/null
@@ -0,0 +1,5 @@
+-coordinate(revision:hg18)\r
+  \r
+-(revision:hg17)\r
+ -coordinate\r
\ No newline at end of file