OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3011 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Tue, 24 Feb 2009 09:45:43 +0000 (09:45 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Tue, 24 Feb 2009 09:45:43 +0000 (09:45 +0000)
src/main/java/org/xerial/lens/Lens.java
src/main/java/org/xerial/lens/Relation.java [new file with mode: 0644]
src/main/java/org/xerial/lens/Tuple.java [new file with mode: 0644]
src/main/java/org/xerial/lens/impl/StreamAmoebaJoin.java [new file with mode: 0644]
src/main/java/org/xerial/util/Pair.java
src/main/java/org/xerial/util/Triplet.java
src/main/java/org/xerial/util/bean/TypeInformation.java
src/test/java/org/xerial/lens/LensTest.java
src/test/java/org/xerial/silk/sequence.silk
src/test/java/org/xerial/util/bean/TypeInformationTest.java

index 26dca28..51660d4 100644 (file)
@@ -38,8 +38,6 @@ import java.util.Set;
 import org.xerial.core.XerialException;
 import org.xerial.silk.SilkWalker;
 import org.xerial.util.bean.TypeInformation;
-import org.xerial.util.tree.TreeVisitor;
-import org.xerial.util.tree.TreeWalker;
 
 /**
  * Lens is an O-X mapping utility. O stands for Objects, and X for structured
@@ -184,47 +182,10 @@ public class Lens
             throw new NullPointerException("silkFileResource");
 
         SilkWalker walker = new SilkWalker(silkFileResource);
-        RelationScanner scanner = new RelationScanner();
-        walker.walk(scanner);
 
         return null;
     }
 
-    private static class RelationScanner implements TreeVisitor
-    {
-
-        public void finish(TreeWalker walker) throws XerialException
-        {
-        // TODO Auto-generated method stub
-
-        }
-
-        public void init(TreeWalker walker) throws XerialException
-        {
-        // TODO Auto-generated method stub
-
-        }
-
-        public void visitNode(String nodeName, String immediateNodeValue, TreeWalker walker) throws XerialException
-        {
-        // TODO Auto-generated method stub
-
-        }
-
-        public void leaveNode(String nodeName, TreeWalker walker) throws XerialException
-        {
-        // TODO Auto-generated method stub
-
-        }
-
-        public void text(String textDataFragment, TreeWalker walker) throws XerialException
-        {
-        // TODO Auto-generated method stub
-
-        }
-
-    }
-
     private enum SetterType {
         ADDER, SETTER, GETTER, PUTTER, APPENDER
     };
@@ -273,7 +234,7 @@ public class Lens
         List<FieldSetter> setterContainer = new ArrayList<FieldSetter>();
 
         // look for all super classes
-        for (Class< ? > eachClass = type; eachClass != null; eachClass = type.getSuperclass())
+        for (Class< ? > eachClass = type; eachClass != null; eachClass = eachClass.getSuperclass())
         {
             // scan fields
             for (Field eachField : eachClass.getFields())
diff --git a/src/main/java/org/xerial/lens/Relation.java b/src/main/java/org/xerial/lens/Relation.java
new file mode 100644 (file)
index 0000000..e5189d7
--- /dev/null
@@ -0,0 +1,38 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// Relation.java
+// Since: Feb 24, 2009 4:08:00 PM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.lens;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target( { ElementType.FIELD, ElementType.METHOD })
+public @interface Relation {
+    String key() default "key";
+
+    String value() default "value";
+}
diff --git a/src/main/java/org/xerial/lens/Tuple.java b/src/main/java/org/xerial/lens/Tuple.java
new file mode 100644 (file)
index 0000000..39ce345
--- /dev/null
@@ -0,0 +1,155 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// Tuple.java
+// Since: Feb 24, 2009 5:31:10 PM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.lens;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xerial.util.StringUtil;
+
+/**
+ * Tuple is a data structure for holding a list of objects.
+ * 
+ * @author leo
+ * 
+ */
+public class Tuple
+{
+    private List<Object> tuple;
+
+    /**
+     * Set the tuple value at the specified index
+     * 
+     * @param tupleIndex
+     *            tuple index
+     * @param value
+     *            value object to set
+     */
+    public void set(int tupleIndex, Object value)
+    {
+        tuple.set(tupleIndex, value);
+    }
+
+    /**
+     * Append a new value to the tail of the tuple
+     * 
+     * @param value
+     *            value object to append
+     */
+    public void append(Object value)
+    {
+        tuple.add(value);
+    }
+
+    /**
+     * Get the tuple size
+     * 
+     * @return tuple size
+     */
+    public int size()
+    {
+        assert tuple != null;
+        return tuple.size();
+    }
+
+    /**
+     * Create a new fixed-size tuple. Use this factory method for
+     * space-efficiency.
+     * 
+     * @param tupleSize
+     *            size of a new tuple
+     * @return fixed-size tuple
+     */
+    public static Tuple newFixedSizeTuple(int tupleSize)
+    {
+        return new Tuple(tupleSize);
+    }
+
+    public static Tuple newPair(Object attribute1, Object attribute2)
+    {
+        Tuple t = newFixedSizeTuple(2);
+        t.set(0, attribute1);
+        t.set(1, attribute2);
+        return t;
+    }
+
+    private Tuple(int tupleSize)
+    {
+        tuple = new ArrayList<Object>(tupleSize);
+
+        // initialize the tuple with null values
+        for (int i = 0; i < tupleSize; ++i)
+            tuple.add(null);
+    }
+
+    /**
+     * Get the tuple value at the specified index
+     * 
+     * @param index
+     *            tuple index in the tuple to retrieve value
+     * @return value at the specified tuple index
+     * @throws IndexOutOfBoundsException
+     *             when index exceeds the tuple size
+     */
+    public Object get(int index)
+    {
+        return tuple.get(index);
+    }
+
+    @Override
+    public String toString()
+    {
+        return String.format("(%s)", StringUtil.join(tuple, ", "));
+    }
+
+    /*
+    public int compareTo(Tuple o)
+    {
+        if (o instanceof Tuple)
+        {
+            Tuple other = Tuple.class.cast(o);
+            int s1 = size();
+            int s2 = other.size();
+            if (s1 < s2)
+            {
+                return -1;
+            }
+            else if (s1 > s2)
+            {
+                return 1;
+            }
+            else
+            {
+                for (int i = 0; i < s1; ++i)
+                {
+
+                }
+            }
+
+        }
+    }
+    */
+
+}
diff --git a/src/main/java/org/xerial/lens/impl/StreamAmoebaJoin.java b/src/main/java/org/xerial/lens/impl/StreamAmoebaJoin.java
new file mode 100644 (file)
index 0000000..c1e51c8
--- /dev/null
@@ -0,0 +1,95 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// StreamAmoebaJoin.java
+// Since: Feb 24, 2009 2:00:28 PM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.lens.impl;
+
+import org.xerial.core.XerialException;
+import org.xerial.util.graph.Lattice;
+import org.xerial.util.graph.LatticeCursor;
+import org.xerial.util.tree.TreeVisitor;
+import org.xerial.util.tree.TreeWalker;
+
+/**
+ * Stream amoeba join processor
+ * 
+ * @author leo
+ * 
+ */
+public class StreamAmoebaJoin
+{
+
+    public StreamAmoebaJoin()
+    {
+
+    }
+
+    public void process(TreeWalker walker) throws XerialException
+    {
+        TreeStreamProcessor processor = new TreeStreamProcessor();
+        walker.walk(processor);
+    }
+
+    private class TreeStreamProcessor implements TreeVisitor
+    {
+        Lattice<String> contextNodeSet = new Lattice<String>();
+        LatticeCursor<String> cursor;
+
+        public void finish(TreeWalker walker) throws XerialException
+        {
+        // TODO Auto-generated method stub
+
+        }
+
+        public void init(TreeWalker walker) throws XerialException
+        {
+            cursor = contextNodeSet.cursor();
+        }
+
+        public void leaveNode(String nodeName, TreeWalker walker) throws XerialException
+        {
+            int prevStateID = cursor.getNodeID();
+            int nextStateID = cursor.back(nodeName).getID();
+
+        }
+
+        public void text(String textDataFragment, TreeWalker walker) throws XerialException
+        {
+
+        }
+
+        public void visitNode(String nodeName, String immediateNodeValue, TreeWalker walker) throws XerialException
+        {
+            int prevStateID = cursor.getNodeID();
+            int nextStateID = cursor.next(nodeName).getID();
+
+        }
+
+    }
+
+    private class ActionTable
+    {
+
+    }
+
+}
index 3091c7d..c5ad837 100644 (file)
 //--------------------------------------
 package org.xerial.util;
 
-/** 
+/**
  * Pair of two objects
+ * 
  * @author leo
- *
+ * 
  */
 public class Pair<X, Y>
 {
     private X x;
     private Y y;
-    
+
     /**
      * 
      */
@@ -42,24 +43,35 @@ public class Pair<X, Y>
         this.x = x;
         this.y = y;
     }
+
     public X getFirst()
     {
         return x;
     }
+
     public Y getSecond()
     {
         return y;
     }
+
     public void setFirst(X x)
     {
         this.x = x;
     }
+
     public void setSecond(Y y)
     {
         this.y = y;
     }
-        
-}
-
 
+    public static <X, Y> Pair<X, Y> newPair(X x, Y y)
+    {
+        return new Pair<X, Y>(x, y);
+    }
 
+    @Override
+    public String toString()
+    {
+        return String.format("(%s, %s)", x, y);
+    }
+}
index ab8fb92..40a51e1 100644 (file)
 package org.xerial.util;\r
 \r
 /**\r
- * Triplet structure  \r
+ * Triplet structure\r
  * \r
  * @author leo\r
- *\r
+ * \r
  */\r
-public class Triplet<E, F, J> \r
+public class Triplet<E, F, J>\r
 {\r
     private E first;\r
     private F second;\r
     private J third;\r
+\r
     public Triplet(E first, F second, J third)\r
     {\r
         this.first = first;\r
         this.second = second;\r
         this.third = third;\r
     }\r
-    \r
+\r
     /**\r
      * @return Returns the first.\r
      */\r
@@ -48,13 +49,16 @@ public class Triplet<E, F, J>
     {\r
         return first;\r
     }\r
+\r
     /**\r
-     * @param first The first to set.\r
+     * @param first\r
+     *            The first to set.\r
      */\r
     public void setFirst(E first)\r
     {\r
         this.first = first;\r
     }\r
+\r
     /**\r
      * @return Returns the second.\r
      */\r
@@ -62,13 +66,16 @@ public class Triplet<E, F, J>
     {\r
         return second;\r
     }\r
+\r
     /**\r
-     * @param second The second to set.\r
+     * @param second\r
+     *            The second to set.\r
      */\r
     public void setSecond(F second)\r
     {\r
         this.second = second;\r
     }\r
+\r
     /**\r
      * @return Returns the third.\r
      */\r
@@ -76,12 +83,25 @@ public class Triplet<E, F, J>
     {\r
         return third;\r
     }\r
+\r
     /**\r
-     * @param third The third to set.\r
+     * @param third\r
+     *            The third to set.\r
      */\r
     public void setThird(J third)\r
     {\r
         this.third = third;\r
     }\r
-}\r
 \r
+    public static <E, F, J> Triplet<E, F, J> newTriplet(E first, F second, J third)\r
+    {\r
+        return new Triplet<E, F, J>(first, second, third);\r
+    }\r
+\r
+    @Override\r
+    public String toString()\r
+    {\r
+        return String.format("(%s, %s, %s)", first, second, third);\r
+    }\r
+\r
+}\r
index f523710..c79b94e 100644 (file)
@@ -44,6 +44,8 @@ import java.util.TreeSet;
 
 import org.w3c.dom.Element;
 import org.xerial.util.ArrayDeque;
+import org.xerial.util.Pair;
+import org.xerial.util.Triplet;
 
 /**
  * BasicType class holds information of standard types that can be directly
@@ -143,6 +145,16 @@ public class TypeInformation
         return Element.class.isAssignableFrom(c);
     }
 
+    public static boolean isPair(Class< ? > c)
+    {
+        return Pair.class.isAssignableFrom(c);
+    }
+
+    public static boolean isTriplet(Class< ? > c)
+    {
+        return Triplet.class.isAssignableFrom(c);
+    }
+
     public static boolean hasPublicConstructor(Class< ? > c)
     {
         for (Constructor< ? > constructor : c.getConstructors())
index 07c57d7..fb46fef 100644 (file)
@@ -25,8 +25,8 @@
 package org.xerial.lens;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -35,6 +35,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.xerial.silk.SilkUtilTest;
 import org.xerial.util.FileResource;
+import org.xerial.util.Pair;
 import org.xerial.util.log.Logger;
 
 public class LensTest
@@ -51,13 +52,53 @@ public class LensTest
 
     @Test
     public void testTranslateSilk()
+    {}
+
+    static class GeneTableOneToMany
     {
-        fail("Not yet implemented");
+        @Relation(key = "coordinate", value = "gene")
+        public Map<Coordinate, List<GeneSequence>> sequenceTable;
     }
 
-    static class GeneTable
+    /**
+     * Example of showing that adder with two arguments corresponds to a
+     * Map<Key, Collection<Value>> parameter.
+     * 
+     * @author leo
+     * 
+     */
+    static class GeneTableWithMapAdder
     {
         private Map<Coordinate, List<GeneSequence>> sequenceTable;
+
+        @Relation(key = "coordinate", value = "gene")
+        public void add(Coordinate coordinate, GeneSequence gene)
+        {
+            List<GeneSequence> geneList = sequenceTable.get(coordinate);
+            if (geneList == null)
+            {
+                geneList = new ArrayList<GeneSequence>();
+                sequenceTable.put(coordinate, geneList);
+            }
+            geneList.add(gene);
+        }
+    }
+
+    static class GeneTableOneToOne
+    {
+        @Relation(key = "coordinate", value = "gene")
+        public List<Pair<Coordinate, GeneSequence>> geneList;
+    }
+
+    static class GeneTableOneToOneWithAdder
+    {
+        public List<Pair<Coordinate, GeneSequence>> geneList;
+
+        @Relation(key = "coordinate", value = "gene")
+        public void add(Coordinate coordinate, GeneSequence gene)
+        {
+            geneList.add(Pair.newPair(coordinate, gene));
+        }
     }
 
     static class GeneSequence
index c3cf0a5..ee87cbc 100644 (file)
@@ -1,13 +1,10 @@
-\r
--coordinate(group:utgb, type:chromosome, species:human, revision:hg18)\r
- -coordinate.name:chr1\r
-  -entry(name, start, strand, sequence, param[json])|\r
+-coordinate(group:utgb, type:chromosome, species:human, revision:hg18, name:chr1)\r
+ -gene(name, start, strand, sequence, param[json])|\r
 TAG1   100     +       ACCCGGTTTTGGCGCTTTCCTTTC[----]TTGGCCTTGGGGCCCATCB       {"link":"http://somewhere.org/", "description":"reference sequence"}\r
 TAG2   100     -       ACCCGG---GGCGCTTTCCCTTTC --AC TTGGCCTTGGGGCCCATCG       \r
 \r
-#-coordinate.name:chr2\r
-# -entry(name, start, strand, sequence, param[json])|\r
-#TAG3  100     -       ACCCGGTTTTGGCGCTTTCCTTTC[----]TTGGCCTTGGGGCCCATCB       {"memo":"in/del"}        \r
-#TAG4  100     +       ACCCGG---GGCGCTTTCCCTTTC --AC TTGGCCTTGGGGCCCATCG       \r
-\r
-\r
+-(group:utgb, type:chromosome, species:human, revision:hg18)\r
+ -coordinate(name:chr2)\r
+  -gene(name, start, strand, sequence, param[json])|\r
+TAG3   100     -       ACCCGGTTTTGGCGCTTTCCTTTC[----]TTGGCCTTGGGGCCCATCB       {"memo":"in/del"}        \r
+TAG4   100     +       ACCCGG---GGCGCTTTCCCTTTC --AC TTGGCCTTGGGGCCCATCG       \r
index e81c265..d4afbd3 100644 (file)
@@ -47,6 +47,8 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.Text;
 import org.xerial.util.ArrayDeque;
+import org.xerial.util.Pair;
+import org.xerial.util.Triplet;
 import org.xerial.util.bean.sample.Book;
 import org.xerial.util.log.Logger;
 
@@ -157,6 +159,20 @@ public class TypeInformationTest
     }
 
     @Test
+    public void testIsPair()
+    {
+        assertTrue(TypeInformation.isPair(Pair.class));
+        assertTrue(!TypeInformation.isPair(Triplet.class));
+    }
+
+    @Test
+    public void testIsTriplet()
+    {
+        assertTrue(!TypeInformation.isTriplet(Pair.class));
+        assertTrue(TypeInformation.isTriplet(Triplet.class));
+    }
+
+    @Test
     public void testIsQueue() throws Exception
     {
         assertTrue(TypeInformation.isQueue(Queue.class));