--- /dev/null
+/*--------------------------------------------------------------------------\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
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
\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
}\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