OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3524 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Thu, 6 Aug 2009 07:43:18 +0000 (07:43 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Thu, 6 Aug 2009 07:43:18 +0000 (07:43 +0000)
src/main/java/org/xerial/lens/RelationLens.java [new file with mode: 0644]
src/main/java/org/xerial/lens/relation/query/lang/RelationExpr.java
src/main/java/org/xerial/lens/relation/schema/Schema.java
src/main/java/org/xerial/lens/relation/schema/SchemaArray.java
src/main/java/org/xerial/lens/relation/schema/SchemaAtom.java
src/test/java/org/xerial/lens/relation/query/lang/RelationExprTest.java

diff --git a/src/main/java/org/xerial/lens/RelationLens.java b/src/main/java/org/xerial/lens/RelationLens.java
new file mode 100644 (file)
index 0000000..7475393
--- /dev/null
@@ -0,0 +1,97 @@
+/*--------------------------------------------------------------------------\r
+ *  Copyright 2009 Taro L. Saito\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// RelationLens.java\r
+// Since: Aug 6, 2009 4:02:39 PM\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.lens;\r
+\r
+import java.io.IOException;\r
+\r
+import org.xerial.core.XerialException;\r
+import org.xerial.lens.relation.Node;\r
+import org.xerial.lens.relation.query.AmoebaJoinHandler;\r
+import org.xerial.lens.relation.query.QuerySet;\r
+import org.xerial.lens.relation.query.StreamAmoebaJoin;\r
+import org.xerial.lens.relation.query.lang.RelationExpr;\r
+import org.xerial.lens.relation.schema.Schema;\r
+import org.xerial.util.log.Logger;\r
+import org.xerial.util.tree.TreeParser;\r
+\r
+/**\r
+ * For extracting relation from tree-structured data\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public class RelationLens {\r
+\r
+    private static Logger _logger = Logger.getLogger(RelationLens.class);\r
+\r
+    private final QuerySet qs;\r
+\r
+    public RelationLens(String lensQuery) throws XerialException {\r
+        RelationExpr re = RelationExpr.parse(lensQuery);\r
+        qs = re.buildQuerySet();\r
+    }\r
+\r
+    public void map(TreeParser input) {\r
+\r
+        AmoebaJoinHandler relationExtracter = new RelationExtracter();\r
+        try {\r
+            StreamAmoebaJoin aj = new StreamAmoebaJoin(qs, relationExtracter);\r
+        }\r
+        catch (IOException e) {\r
+            _logger.error(e);\r
+        }\r
+\r
+    }\r
+\r
+    public static class RelationExtracter implements AmoebaJoinHandler {\r
+\r
+        public void finish() {\r
+        // TODO Auto-generated method stub\r
+\r
+        }\r
+\r
+        public void init() {\r
+        // TODO Auto-generated method stub\r
+\r
+        }\r
+\r
+        public void leaveNode(Schema schema, Node node) throws Exception {\r
+        // TODO Auto-generated method stub\r
+\r
+        }\r
+\r
+        public void newAmoeba(Schema schema, Node coreNode, Node attributeNode) throws Exception {\r
+        // TODO Auto-generated method stub\r
+\r
+        }\r
+\r
+        public void text(Schema schema, Node coreNode, Node textNode, String text) throws Exception {\r
+        // TODO Auto-generated method stub\r
+\r
+        }\r
+\r
+    }\r
+\r
+}\r
index 094cba6..0289465 100644 (file)
@@ -36,6 +36,8 @@ import org.antlr.runtime.tree.Tree;
 import org.xerial.core.XerialErrorCode;\r
 import org.xerial.core.XerialException;\r
 import org.xerial.lens.Lens;\r
+import org.xerial.lens.relation.query.QuerySet;\r
+import org.xerial.lens.relation.query.QuerySet.QuerySetBuilder;\r
 import org.xerial.lens.relation.query.impl.LensQueryLexer;\r
 import org.xerial.lens.relation.query.impl.LensQueryParser;\r
 import org.xerial.lens.relation.schema.Schema;\r
@@ -110,8 +112,7 @@ public class RelationExpr extends RelationItem {
 \r
     }\r
 \r
-    public Schema toSchema() {\r
-\r
+    private List<RelationItem> sortedNodeList() {\r
         ArrayList<RelationItem> sortedRelationItem = new ArrayList<RelationItem>();\r
         sortedRelationItem.addAll(node);\r
         sortedRelationItem.addAll(relation);\r
@@ -122,22 +123,51 @@ public class RelationExpr extends RelationItem {
             }\r
         });\r
 \r
+        return sortedRelationItem;\r
+    }\r
+\r
+    public Schema toSchema() {\r
+\r
+        List<RelationItem> sortedRelationItem = sortedNodeList();\r
+\r
         SchemaBuilder parent = new SchemaBuilder();\r
         parent.add(name);\r
-        SchemaBuilder sibling = new SchemaBuilder();\r
+\r
         for (RelationItem each : sortedRelationItem) {\r
             if (each.isRelation()) {\r
                 RelationExpr re = RelationExpr.class.cast(each);\r
-                sibling.add(re.toSchema());\r
+                parent.add(re.toSchema());\r
             }\r
             else\r
-                sibling.add(each.name);\r
+                parent.add(each.name);\r
         }\r
 \r
-        parent.add(sibling.build());\r
-\r
         return parent.build();\r
 \r
     }\r
 \r
+    public QuerySet buildQuerySet() {\r
+\r
+        QuerySetBuilder b = new QuerySetBuilder();\r
+\r
+        List<RelationItem> sortedRelationItem = sortedNodeList();\r
+\r
+        SchemaBuilder parent = new SchemaBuilder();\r
+        parent.add(name);\r
+        for (RelationItem each : sortedRelationItem) {\r
+            parent.add(each.name);\r
+\r
+            if (each.isRelation()) {\r
+\r
+                RelationExpr re = RelationExpr.class.cast(each);\r
+                for (Schema s : re.buildQuerySet().getTargetQuerySet()) {\r
+                    b.addQueryTarget(s);\r
+                }\r
+            }\r
+        }\r
+        b.addQueryTarget(parent.build());\r
+\r
+        return b.build();\r
+    }\r
+\r
 }\r
index 3429e09..e74be0d 100644 (file)
@@ -56,7 +56,7 @@ public interface Schema
 
     public boolean isAtom();
 
-    public boolean isNested();
+    public boolean isTuple();
 
     public boolean isOneToMany();
 
index 4832846..8145c01 100644 (file)
@@ -153,7 +153,7 @@ public class SchemaArray implements Schema, Iterable<Schema>
         return false;
     }
 
-    public boolean isNested()
+    public boolean isTuple()
     {
         return true;
     }
index 03bfa7f..c7976fb 100644 (file)
@@ -78,7 +78,7 @@ public class SchemaAtom implements Schema {
         return true;
     }
 
-    public boolean isNested() {
+    public boolean isTuple() {
         return false;
     }
 
index 5d5925f..85abaaf 100644 (file)
@@ -59,6 +59,6 @@ public class RelationExprTest {
     public void q3() throws Exception {\r
         RelationExpr e = parse("entry(dc.subject:sigmod09, link(rel:related, href), summary)");\r
         _logger.debug(e.toSchema());\r
+        _logger.debug(e.buildQuerySet());\r
     }\r
-\r
 }\r