OSDN Git Service

das test passed
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 22 May 2009 05:27:05 +0000 (05:27 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Fri, 22 May 2009 05:27:05 +0000 (05:27 +0000)
git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3334 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd

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/test/java/org/xerial/lens/LensTest.java

index 402de4a..d2d0384 100644 (file)
@@ -81,6 +81,7 @@ public class ObjectLens
     private List<ParameterGetter> getterContainer = new ArrayList<ParameterGetter>();\r
     private List<ParameterSetter> setterContainer = new ArrayList<ParameterSetter>();\r
     private List<RelationSetter> relationSetterContainer = new ArrayList<RelationSetter>();\r
+    private ParameterSetter valueSetter = null;\r
 \r
     public List<ParameterSetter> getSetterList()\r
     {\r
@@ -97,6 +98,11 @@ public class ObjectLens
         return Collections.unmodifiableList(getterContainer);\r
     }\r
 \r
+    public ParameterSetter getValueSetter()\r
+    {\r
+        return valueSetter;\r
+    }\r
+\r
     public boolean hasAttributes()\r
     {\r
         return !getterContainer.isEmpty();\r
@@ -164,7 +170,10 @@ public class ObjectLens
                 }\r
                 else\r
                 {\r
-                    setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField));\r
+                    if (!paramName.equals("value"))\r
+                        setterContainer.add(ParameterSetter.newSetter(fieldType, paramName, eachField));\r
+                    else\r
+                        valueSetter = ParameterSetter.newSetter(fieldType, paramName, eachField);\r
                     getterContainer.add(ParameterGetter.newFieldGetter(eachField, paramName));\r
                 }\r
 \r
index 5248772..487c40b 100644 (file)
@@ -66,7 +66,7 @@ public class ObjectMapper
     {
         public void bind(Schema schema, Node coreNode, Node attributeNode) throws XerialException;
 
-        public void bindText(Schema schema, Node coreNode, String attributeValue) throws XerialException;
+        public void bindText(Schema schema, Node coreNode, Node textNode, String textValue) throws XerialException;
 
     }
 
@@ -109,7 +109,7 @@ public class ObjectMapper
             return null;
         }
 
-        public void bindText(Schema schema, Node coreNode, String attributeValue) throws XerialException
+        public void bindText(Schema schema, Node coreNode, Node textNode, String textValue) throws XerialException
         {
             throw new XerialError(XerialErrorCode.UNSUPPORTED);
         }
@@ -138,11 +138,15 @@ public class ObjectMapper
                 setter.bind(coreNodeInstance, attributeNodeInstance);
         }
 
-        public void bindText(Schema schema, Node coreNode, String textFragment) throws XerialException
+        public void bindText(Schema schema, Node coreNode, Node textNode, String textValue) throws XerialException
         {
             Object coreNodeInstance = getNodeInstance(coreNode, coreNodeType);
+            Object textNodeInstance = getNodeInstance(textNode, attributeNodeType);
 
-            setter.bind(coreNodeInstance, textFragment);
+            if (textValue != null && !TypeInfo.isBasicType(attributeNodeType))
+                setTextValue(textNodeInstance, attributeNodeType, textValue);
+            else
+                setter.bind(coreNodeInstance, textValue);
         }
     }
 
@@ -194,7 +198,9 @@ public class ObjectMapper
     private class QueryBuilder
     {
         QuerySetBuilder qs = new QuerySetBuilder();
-        private Set<Class< ? >> processedClasses = new HashSet<Class< ? >>();
+        private HashMap<String, Set<Class< ? >>> processedClassTable = new HashMap<String, Set<Class< ? >>>();
+
+        //Set<Class< ? >> processedClasses = new HashSet<Class< ? >>();
 
         public QueryBuilder()
         {
@@ -208,10 +214,22 @@ public class ObjectMapper
             if (TypeInfo.isBasicType(targetType) || targetType == MapEntry.class)
                 return;
 
-            if (processedClasses.contains(targetType))
+            Set<Class< ? >> processed = processedClassTable.get(alias);
+            if (processed == null)
+            {
+                processed = new HashSet<Class< ? >>();
+                processedClassTable.put(alias, processed);
+            }
+
+            if (processed.contains(targetType))
                 return;
+            else
+                processed.add(targetType);
 
-            processedClasses.add(targetType);
+            //            if (processedClasses.contains(targetType))
+            //                return;
+            //
+            //            processedClasses.add(targetType);
 
             ObjectLens lens = ObjectLens.getObjectLens(targetType);
             if (_logger.isTraceEnabled())
@@ -258,7 +276,7 @@ public class ObjectMapper
 
     }
 
-    public Object getNodeInstance(Node node, Class< ? > nodeType) throws XerialException
+    private Object getNodeInstance(Node node, Class< ? > nodeType) throws XerialException
     {
         Object instance = objectHolder.get(node.nodeID);
         if (instance != null)
@@ -274,12 +292,55 @@ public class ObjectMapper
         else
         {
             instance = TypeInfo.createInstance(nodeType);
-            // TODO bind the node value to the instance
+
+            if (node.nodeValue != null)
+            {
+                setTextValue(instance, nodeType, node.nodeValue);
+            }
 
         }
         objectHolder.put(node.nodeID, instance);
         return instance;
+    }
+
+    private void setTextValue(Object instance, Class< ? > textNodeType, String textValue) throws XerialException
+    {
+        // bind the node value to the instance
+        ObjectLens lens = ObjectLens.getObjectLens(textNodeType);
+        ParameterSetter valueSetter = lens.getValueSetter();
+        if (valueSetter != null)
+            valueSetter.bind(instance, TypeConverter.convertToBasicType(valueSetter.getParameterType(), textValue));
+
+    }
+
+    private Object getTextNodeInstance(String nodeName, String nodeValue, Class< ? > nodeType) throws XerialException
+    {
+        Object instance = null;
 
+        if (TypeInfo.isBasicType(nodeType))
+        {
+            if (nodeValue == null)
+                return null;
+            else
+                instance = TypeConverter.convertToBasicType(nodeType, nodeValue);
+        }
+        else
+        {
+            instance = TypeInfo.createInstance(nodeType);
+
+            if (nodeValue != null)
+            {
+                // bind the node value to the instance
+                ObjectLens lens = ObjectLens.getObjectLens(nodeType);
+                ParameterSetter valueSetter = lens.getValueSetter();
+                if (valueSetter != null)
+                    valueSetter.bind(instance, TypeConverter.convertToBasicType(valueSetter.getParameterType(),
+                            nodeValue));
+            }
+
+        }
+
+        return instance;
     }
 
     private class RelationExtracter extends AmoebaJoinHandlerBase
@@ -315,10 +376,10 @@ public class ObjectMapper
 
         }
 
-        public void text(Schema schema, Node coreNode, String nodeName, String text) throws Exception
+        public void text(Schema schema, Node coreNode, Node textNode, String textFragment) throws Exception
         {
             if (_logger.isTraceEnabled())
-                _logger.trace(String.format("text:   (%s, %s:%s) in %s", coreNode, nodeName, text, schema));
+                _logger.trace(String.format("text:   (%s, %s:%s) in %s", coreNode, textNode, textFragment, schema));
 
             Binder binder = schema2binder.get(schema);
             if (binder == null)
@@ -326,12 +387,12 @@ public class ObjectMapper
 
             try
             {
-                binder.bindText(schema, coreNode, text);
+                binder.bindText(schema, coreNode, textNode, textFragment);
             }
             catch (XerialException e)
             {
                 _logger.warn(String.format("failed to bind text: core node=%s, attributeName=%s, text=%s\n%s",
-                        coreNode, nodeName, text, e));
+                        coreNode, textNode, textFragment, e));
             }
         }
 
index 7d3b024..0ee7f61 100644 (file)
@@ -35,7 +35,16 @@ public interface AmoebaJoinHandler
 
     public void leaveNode(Schema schema, Node node) throws Exception;
 
-    public void text(Schema schema, Node coreNode, String nodeName, String text) throws Exception;
+    /**
+     * @param schema
+     * @param coreNode
+     * @param textNode
+     *            text node may not contain a text value
+     * @param text
+     *            text fragment data of the text node
+     * @throws Exception
+     */
+    public void text(Schema schema, Node coreNode, Node textNode, String text) throws Exception;
 
     public void finish();
 }
index 106b863..4a00faa 100644 (file)
@@ -46,17 +46,17 @@ public class AmoebaJoinHandlerBase implements AmoebaJoinHandler
 \r
     }\r
 \r
-    public void text(Schema schema, Node coreNode, String nodeName, String text) throws Exception\r
+    public void finish()\r
     {\r
 \r
     }\r
 \r
-    public void finish()\r
+    public void init()\r
     {\r
 \r
     }\r
 \r
-    public void init()\r
+    public void text(Schema schema, Node coreNode, Node textNode, String text) throws Exception\r
     {\r
 \r
     }\r
index d1abc0b..825f75a 100644 (file)
@@ -105,7 +105,7 @@ public class StreamAmoebaJoin implements TreeVisitor
 
     static interface TextOperation
     {
-        void execute(String nodeName, String textData) throws Exception;
+        void execute(String testNodeName, String textData) throws Exception;
     }
 
     class SimpleTextOperation implements TextOperation
@@ -125,11 +125,15 @@ public class StreamAmoebaJoin implements TreeVisitor
             this.coreNodeName = pr.coreNodeName;
         }
 
-        public void execute(String nodeName, String textData) throws Exception
+        public void execute(String textNodeName, String textData) throws Exception
         {
             Deque<Node> nodeStack = getNodeStack(coreNodeName);
             Node contextNode = nodeStack.getLast();
-            handler.text(schema, contextNode, nodeName, textData);
+
+            Deque<Node> textNodeStack = getNodeStack(textNodeName);
+            Node textNode = textNodeStack.getLast();
+
+            handler.text(schema, contextNode, textNode, textData);
         }
     }
 
@@ -395,6 +399,8 @@ public class StreamAmoebaJoin implements TreeVisitor
 
         Edge currentEdge = new Edge(prevState.getID(), currentState.getID());
         List<TextOperation> textOperation = operatSetOnText.get(currentEdge);
+
+        // generate a text operation set
         if (textOperation == null)
         {
             textOperation = new ArrayList<TextOperation>();
index a09d5cc..397b5f2 100644 (file)
@@ -24,8 +24,7 @@
 //--------------------------------------
 package org.xerial.lens;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -218,7 +217,13 @@ public class LensTest
     {
         public DASGFF gff;
         public Segment segment;
-        public List<Feature> feature;
+
+        @Override
+        public String toString()
+        {
+            return ObjectLens.toJSON(this);
+        }
+
     }
 
     public static class DASGFF
@@ -233,6 +238,7 @@ public class LensTest
         public String id;
         public long start;
         public long stop;
+        public List<Feature> feature;
     }
 
     public static class Feature
@@ -244,8 +250,18 @@ public class LensTest
         public String score;
         public String orientation;
 
+        public Method method;
         public FeatureType type;
-        public Segment target;
+        public Group group;
+        public Target target;
+
+    }
+
+    public static class Target
+    {
+        public String id;
+        public long start;
+        public long stop;
     }
 
     public static class FeatureType
@@ -256,6 +272,27 @@ public class LensTest
 
     }
 
+    public static class Group
+    {
+        public String id;
+        public String type;
+        public String label;
+        public Link link;
+
+    }
+
+    public static class Link
+    {
+        public String href;
+        public String value;
+    }
+
+    public static class Method
+    {
+        public String id;
+        public String value;
+    }
+
     /*
      * <pre>
     <SEGMENT id="13" start="1800000" stop="18100000"> 
@@ -278,17 +315,18 @@ public class LensTest
     public void dasTest() throws Exception
     {
         DASFeature das = Lens.loadXML(DASFeature.class, FileResource.find(LensTest.class, "das.xml"));
-        assertEquals(1, das.feature.size());
-        Feature f = das.feature.get(0);
+        assertEquals(1, das.segment.feature.size());
+        Feature f = das.segment.feature.get(0);
+        _logger.debug(das);
         assertEquals("ENSE00001471274", f.id);
-        assertEquals(17957458, f.start);
-        assertEquals(17957578, f.end);
+        assertEquals(17957458L, f.start);
+        assertEquals(17957578L, f.end);
         assertEquals("-", f.score);
         assertEquals("-", f.orientation);
-        Segment t = f.target;
+        Target t = f.target;
         assertEquals("ENST00000342944", t.id);
-        assertEquals(1, t.start);
-        assertEquals(121, t.stop);
+        assertEquals(1L, t.start);
+        assertEquals(121L, t.stop);
 
     }