OSDN Git Service

text fragment handler
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Mon, 18 May 2009 09:20:43 +0000 (09:20 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Mon, 18 May 2009 09:20:43 +0000 (09:20 +0000)
git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3295 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd

src/main/java/org/xerial/relation/query/StreamAmoebaJoin.java
src/test/java/org/xerial/relation/query/StreamAmoebaJoinTest.java
src/test/java/org/xerial/relation/query/sample.silk

index 6637043..3d9d062 100644 (file)
@@ -31,6 +31,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map.Entry;
 
 import org.xerial.core.XerialError;
 import org.xerial.core.XerialErrorCode;
@@ -88,8 +89,7 @@ public class StreamAmoebaJoin implements TreeVisitor
 
     HashMap<Edge, List<Operation>> operationSetOnForward = new HashMap<Edge, List<Operation>>();
     HashMap<Edge, List<Operation>> operationSetOnBack = new HashMap<Edge, List<Operation>>();
-
-    List<Operation> forwardActionList = null;
+    HashMap<Edge, List<TextOperation>> operatSetOnText = new HashMap<Edge, List<TextOperation>>();
 
     public StreamAmoebaJoin(QuerySet query, AmoebaJoinHandler handler) throws IOException
     {
@@ -98,6 +98,57 @@ public class StreamAmoebaJoin implements TreeVisitor
 
     }
 
+    static interface TextOperation
+    {
+        void execute(String nodeName, String textData);
+    }
+
+    class SimpleTextOperation implements TextOperation
+    {
+        final Schema schema;
+
+        public SimpleTextOperation(Schema schema)
+        {
+            this.schema = schema;
+        }
+
+        public SimpleTextOperation(PushRelation pr)
+        {
+            this.schema = pr.schema;
+        }
+
+        public void execute(String nodeName, String textData)
+        {
+            handler.text(schema, nodeName, textData);
+        }
+    }
+
+    class ContextBasedTextOperation implements TextOperation
+    {
+        final HashMap<String, TextOperation> coreNode_action = new HashMap<String, TextOperation>();
+
+        public ContextBasedTextOperation(ScopedPushRelation scopedPushOperation)
+        {
+            for (Entry<String, PushRelation> each : scopedPushOperation.coreNode_action.entrySet())
+            {
+                coreNode_action.put(each.getKey(), new SimpleTextOperation(each.getValue()));
+            }
+        }
+
+        public void execute(String nodeName, String textData)
+        {
+            for (Iterator<String> it = currentPath.descendingIterator(); it.hasNext();)
+            {
+                String contextNode = it.next();
+                if (coreNode_action.containsKey(contextNode))
+                {
+                    coreNode_action.get(contextNode).execute(nodeName, textData);
+                }
+            }
+        }
+
+    }
+
     /**
      * Defines an operation assigned to an currentEdge of the node name lattice
      * 
@@ -309,16 +360,37 @@ public class StreamAmoebaJoin implements TreeVisitor
 
     public void text(String nodeName, String textDataFragment, TreeWalker walker) throws XerialException
     {
-        if (forwardActionList == null)
-            throw new XerialError(XerialErrorCode.INVALID_STATE, "null action list: for text node " + nodeName);
-
         Iterator<LatticeNode<String>> it = stateStack.descendingIterator();
         LatticeNode<String> currentState = it.next();
         LatticeNode<String> prevState = it.next();
 
         Edge currentEdge = new Edge(prevState.getID(), currentState.getID());
+        List<TextOperation> textOperation = operatSetOnText.get(currentEdge);
+        if (textOperation == null)
+        {
+            textOperation = new ArrayList<TextOperation>();
+            operatSetOnText.put(currentEdge, textOperation);
+
+            List<Operation> forwardAction = getForwardActionList(prevState, currentState, nodeName);
+            for (Operation each : forwardAction)
+            {
+                if (each instanceof PushRelation)
+                {
+                    textOperation.add(new SimpleTextOperation((PushRelation) each));
+                }
+                else if (each instanceof ScopedPushRelation)
+                {
+                    textOperation.add(new ContextBasedTextOperation((ScopedPushRelation) each));
+                }
+                else
+                    throw new XerialError(XerialErrorCode.INVALID_STATE, "unknown operation: " + each);
+            }
+        }
+
+        assert textOperation != null;
 
-        handler.text(nodeName, textDataFragment);
+        for (TextOperation each : textOperation)
+            each.execute(nodeName, textDataFragment);
     }
 
     public void leaveNode(String nodeName, TreeWalker walker) throws XerialException
@@ -334,7 +406,11 @@ public class StreamAmoebaJoin implements TreeVisitor
 
     void forward(Node node)
     {
-        forwardActionList = getForwardActionList(node);
+        LatticeNode<String> prevState = latticeCursor.getNode();
+        LatticeNode<String> nextState = latticeCursor.next(node.nodeName);
+        stateStack.addLast(nextState);
+
+        List<Operation> forwardActionList = getForwardActionList(prevState, nextState, node.nodeName);
         assert forwardActionList != null;
 
         for (Operation each : forwardActionList)
@@ -343,20 +419,17 @@ public class StreamAmoebaJoin implements TreeVisitor
         }
     }
 
-    private List<Operation> getForwardActionList(Node nextNode)
+    private List<Operation> getForwardActionList(LatticeNode<String> prevState, LatticeNode<String> nextState,
+            String nodeName)
     {
-        LatticeNode<String> prevState = latticeCursor.getNode();
-        LatticeNode<String> nextState = latticeCursor.next(nextNode.nodeName);
-
-        stateStack.addLast(nextState);
-
         Edge currentEdge = new Edge(prevState.getID(), nextState.getID());
+
         List<Operation> actionList = operationSetOnForward.get(currentEdge);
         if (actionList != null)
             return actionList;
 
-        int prevNodeID = prevState.getID();
-        int nextNodeID = nextState.getID();
+        int prevNodeID = currentEdge.getSourceNodeID();
+        int nextNodeID = currentEdge.getDestNodeID();
 
         // lazily prepare the action list
         actionList = new ArrayList<Operation>();
@@ -365,12 +438,12 @@ public class StreamAmoebaJoin implements TreeVisitor
         operationSetOnBack.put(new Edge(nextNodeID, prevNodeID), backActionList);
 
         // search for the corresponding relations to newly found two node pair 
-        String newlyFoundTag = nextNode.nodeName;
+        String newlyFoundTag = nodeName;
 
         if (_logger2.isTraceEnabled())
             _logger2.trace("crate actions for " + newlyFoundTag);
 
-        if (prevState != nextState)
+        if (prevNodeID != nextNodeID)
         {
             List<PushRelation> foundAction = new ArrayList<PushRelation>();
             // (core node, attribute node)
index a634a29..060f0e7 100644 (file)
@@ -55,7 +55,8 @@ public class StreamAmoebaJoinTest
         qs.addQueryTarget(new SchemaBuilder().add("coordinate").add("group").add("species").add("revision").add("name")\r
                 .build());\r
         qs.addQueryTarget(new SchemaBuilder().add("coordinate").add("gene", DataType.STRUCT, FD.ONE_OR_MORE).build());\r
-        qs.addQueryTarget(new SchemaBuilder().add("gene").add("id").add("name").add("start").add("end").build());\r
+        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, new AmoebaJoinHandler() {\r
 \r
@@ -69,10 +70,9 @@ public class StreamAmoebaJoinTest
                 _logger.info(String.format("leave %s in %s", node, schema));\r
             }\r
 \r
-            public void text(String nodeName, String text)\r
+            public void text(Schema schema, String nodeName, String text)\r
             {\r
-            // TODO Auto-generated method stub\r
-\r
+                _logger.info(String.format("text %s in %s: %s", text, nodeName, schema));\r
             }\r
         });\r
 \r
index b26298e..ee2a189 100644 (file)
@@ -1,3 +1,6 @@
 -coordinate(group:utgb, species:human, revision:hg18, name:chr1)\r
  -gene(id:1, name:gene1, start:100000, end:200000)\r
  -gene(id:2, name:gene2, start:500000, end:700000)\r
+  -sequence>\r
+ ACCGCTT\r
+ GGCCTTA\r