From 2ef11ae1ac74fa755d5b899baf6d16caf85d500e Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 18 May 2009 09:20:43 +0000 Subject: [PATCH] text fragment handler git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3295 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd --- .../xerial/relation/query/StreamAmoebaJoin.java | 107 +++++++++++++++++---- .../relation/query/StreamAmoebaJoinTest.java | 8 +- .../java/org/xerial/relation/query/sample.silk | 3 + 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/xerial/relation/query/StreamAmoebaJoin.java b/src/main/java/org/xerial/relation/query/StreamAmoebaJoin.java index 6637043..3d9d062 100644 --- a/src/main/java/org/xerial/relation/query/StreamAmoebaJoin.java +++ b/src/main/java/org/xerial/relation/query/StreamAmoebaJoin.java @@ -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> operationSetOnForward = new HashMap>(); HashMap> operationSetOnBack = new HashMap>(); - - List forwardActionList = null; + HashMap> operatSetOnText = new HashMap>(); 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 coreNode_action = new HashMap(); + + public ContextBasedTextOperation(ScopedPushRelation scopedPushOperation) + { + for (Entry each : scopedPushOperation.coreNode_action.entrySet()) + { + coreNode_action.put(each.getKey(), new SimpleTextOperation(each.getValue())); + } + } + + public void execute(String nodeName, String textData) + { + for (Iterator 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> it = stateStack.descendingIterator(); LatticeNode currentState = it.next(); LatticeNode prevState = it.next(); Edge currentEdge = new Edge(prevState.getID(), currentState.getID()); + List textOperation = operatSetOnText.get(currentEdge); + if (textOperation == null) + { + textOperation = new ArrayList(); + operatSetOnText.put(currentEdge, textOperation); + + List 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 prevState = latticeCursor.getNode(); + LatticeNode nextState = latticeCursor.next(node.nodeName); + stateStack.addLast(nextState); + + List forwardActionList = getForwardActionList(prevState, nextState, node.nodeName); assert forwardActionList != null; for (Operation each : forwardActionList) @@ -343,20 +419,17 @@ public class StreamAmoebaJoin implements TreeVisitor } } - private List getForwardActionList(Node nextNode) + private List getForwardActionList(LatticeNode prevState, LatticeNode nextState, + String nodeName) { - LatticeNode prevState = latticeCursor.getNode(); - LatticeNode nextState = latticeCursor.next(nextNode.nodeName); - - stateStack.addLast(nextState); - Edge currentEdge = new Edge(prevState.getID(), nextState.getID()); + List 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(); @@ -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 foundAction = new ArrayList(); // (core node, attribute node) diff --git a/src/test/java/org/xerial/relation/query/StreamAmoebaJoinTest.java b/src/test/java/org/xerial/relation/query/StreamAmoebaJoinTest.java index a634a29..060f0e7 100644 --- a/src/test/java/org/xerial/relation/query/StreamAmoebaJoinTest.java +++ b/src/test/java/org/xerial/relation/query/StreamAmoebaJoinTest.java @@ -55,7 +55,8 @@ public class StreamAmoebaJoinTest qs.addQueryTarget(new SchemaBuilder().add("coordinate").add("group").add("species").add("revision").add("name") .build()); qs.addQueryTarget(new SchemaBuilder().add("coordinate").add("gene", DataType.STRUCT, FD.ONE_OR_MORE).build()); - qs.addQueryTarget(new SchemaBuilder().add("gene").add("id").add("name").add("start").add("end").build()); + qs.addQueryTarget(new SchemaBuilder().add("gene").add("id").add("name").add("start").add("end").add("sequence") + .build()); StreamAmoebaJoin aj = new StreamAmoebaJoin(qs, new AmoebaJoinHandler() { @@ -69,10 +70,9 @@ public class StreamAmoebaJoinTest _logger.info(String.format("leave %s in %s", node, schema)); } - public void text(String nodeName, String text) + public void text(Schema schema, String nodeName, String text) { - // TODO Auto-generated method stub - + _logger.info(String.format("text %s in %s: %s", text, nodeName, schema)); } }); diff --git a/src/test/java/org/xerial/relation/query/sample.silk b/src/test/java/org/xerial/relation/query/sample.silk index b26298e..ee2a189 100644 --- a/src/test/java/org/xerial/relation/query/sample.silk +++ b/src/test/java/org/xerial/relation/query/sample.silk @@ -1,3 +1,6 @@ -coordinate(group:utgb, species:human, revision:hg18, name:chr1) -gene(id:1, name:gene1, start:100000, end:200000) -gene(id:2, name:gene2, start:500000, end:700000) + -sequence> + ACCGCTT + GGCCTTA -- 2.11.0