From 234ee0f18decf928388b69704e8668d6b9bf057d Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 13 May 2009 10:03:22 +0000 Subject: [PATCH] git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3282 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd --- src/main/java/org/xerial/lens/ObjectLens.java | 13 +- src/main/java/org/xerial/relation/FD.java | 59 +++++++ src/main/java/org/xerial/relation/Node.java | 99 ++++++++++- src/main/java/org/xerial/relation/Tuple.java | 191 +++++++++++++++++++-- .../xerial/relation/schema/RelationalSchema.java | 36 ++++ src/test/java/org/xerial/lens/LensTest.java | 2 +- 6 files changed, 373 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/xerial/relation/FD.java create mode 100644 src/main/java/org/xerial/relation/schema/RelationalSchema.java diff --git a/src/main/java/org/xerial/lens/ObjectLens.java b/src/main/java/org/xerial/lens/ObjectLens.java index e171fcb..58782bf 100644 --- a/src/main/java/org/xerial/lens/ObjectLens.java +++ b/src/main/java/org/xerial/lens/ObjectLens.java @@ -33,7 +33,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.xerial.util.bean.TypeInformation; -import org.xerial.util.bean.impl.Setter; public class ObjectLens { @@ -44,7 +43,7 @@ public class ObjectLens private static void createBindRules(Class< ? > targetType) { - List setterContainer = new ArrayList(); + List setterContainer = new ArrayList(); // look for all super classes for (Class< ? > eachClass = targetType; eachClass != null; eachClass = eachClass.getSuperclass()) @@ -73,7 +72,7 @@ public class ObjectLens setterType = SetterType.ADDER; } - setterContainer.add(new Setter(eachClass, paramName, eachField, setterType)); + setterContainer.add(ParameterSetter.newSetter(eachClass, paramName, eachField)); } @@ -88,7 +87,7 @@ public class ObjectLens { // adder String paramName = getCanonicalParameterName(methodName.substring(3)); - setterContainer.add(ParameterSetter.newSetter(paramName, eachMethod, SetterType.ADDER)); + setterContainer.add(ParameterSetter.newSetter(eachClass, paramName, eachMethod)); } else if (methodName.startsWith("set")) @@ -132,7 +131,7 @@ public class ObjectLens else { if (m.group(2) != null) - return m.group(3).toLowerCase() + m.group(4); + return getCanonicalParameterName(m.group(3) + m.group(4)); else return ""; } @@ -140,8 +139,8 @@ public class ObjectLens public static String getCanonicalParameterName(String paramName) { - paramName = paramName.replaceAll("\\s", "_"); - paramName = paramName.replaceAll("-", "_"); + paramName = paramName.replaceAll("\\s", ""); + paramName = paramName.replaceAll("-", ""); return paramName.toLowerCase(); } diff --git a/src/main/java/org/xerial/relation/FD.java b/src/main/java/org/xerial/relation/FD.java new file mode 100644 index 0000000..e899d3e --- /dev/null +++ b/src/main/java/org/xerial/relation/FD.java @@ -0,0 +1,59 @@ +/*-------------------------------------------------------------------------- + * Copyright 2009 Taro L. Saito + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *--------------------------------------------------------------------------*/ +//-------------------------------------- +// XerialJ +// +// FD.java +// Since: May 13, 2009 5:39:16 PM +// +// $URL$ +// $Author$ +//-------------------------------------- +package org.xerial.relation; + +/** + * Functional dependency that holds in the relation + * + * @author leo + * + */ +public enum FD { + ONE_TO_ONE("."), ONE_OR_MORE("+"), ZERO_OR_ONE("?"), ZERO_OR_MORE("*"); + + private final String symbol; + + private FD(String displaySymbol) + { + this.symbol = displaySymbol; + } + + public boolean isOneToOne() + { + return this == ONE_TO_ONE; + } + + public boolean isOneToMany() + { + return this != ONE_TO_ONE; + } + + @Override + public String toString() + { + return symbol; + } + +} diff --git a/src/main/java/org/xerial/relation/Node.java b/src/main/java/org/xerial/relation/Node.java index f8c82e1..7a1e4b6 100644 --- a/src/main/java/org/xerial/relation/Node.java +++ b/src/main/java/org/xerial/relation/Node.java @@ -24,14 +24,73 @@ //-------------------------------------- package org.xerial.relation; +import org.xerial.core.XerialError; +import org.xerial.core.XerialErrorCode; + /** - * Node is a relation element. + * Node is an element ({@link Cell}) of a relation. * * @author leo * */ public class Node implements Cell { + public static final int INVALID_ID = -1; + public static final String NULL_TEXT = null; + + public final int nodeID; + public final String nodeName; + public final String nodeValue; + + private Node(String nodeName, int nodeID, String nodeValue) + { + this.nodeID = nodeID; + this.nodeName = nodeName; + this.nodeValue = nodeValue; + } + + /** + * Builder class for {@link Node} + * + * @author leo + * + */ + public static class NodeBuilder + { + private int nodeID = INVALID_ID; + private String nodeValue = NULL_TEXT; + private final String nodeName; + + public NodeBuilder(String nodeName) + { + this.nodeName = nodeName; + } + + public NodeBuilder(Node node) + { + this.nodeID = node.nodeID; + this.nodeName = node.nodeName; + this.nodeValue = node.nodeValue; + } + + public NodeBuilder nodeID(int nodeID) + { + this.nodeID = nodeID; + return this; + } + + public NodeBuilder nodeValue(String nodeValue) + { + this.nodeValue = nodeValue; + return this; + } + + public Node build() + { + return new Node(nodeName, nodeID, nodeValue); + } + + } public boolean isAtom() { @@ -43,7 +102,32 @@ public class Node implements Cell return false; } + public static Node newNode(String nodeName, int nodeID) + { + return new Node(nodeName, nodeID, null); + } + + public static Node newNodeWithValue(String nodeName, int nodeID, String nodeValue) + { + return new Node(nodeName, nodeID, nodeValue); + } + + @Override + public String toString() + { + StringBuilder builder = new StringBuilder(); + builder.append(String.format("%s(%d)", nodeName, nodeID)); + if (nodeValue != null) + { + builder.append("=\""); + builder.append(nodeValue); + builder.append("\""); + } + return builder.toString(); + } + /** + * Always return 1 * * @return */ @@ -59,13 +143,18 @@ public class Node implements Cell public Cell get(TupleIndex index) { - return null; + if (index.size() == 0 && index.get(0) == 0) + return this; + else + return null; } public Node getNode(TupleIndex index) { - // TODO Auto-generated method stub - return null; + if (!(index.size() == 1 && index.get(0) == 0)) + throw new XerialError(XerialErrorCode.INVALID_STATE); + else + return this; } public Node getNode() @@ -75,7 +164,7 @@ public class Node implements Cell public Tuple getTuple() { - return null; + throw new XerialError(XerialErrorCode.UNSUPPORTED); } } diff --git a/src/main/java/org/xerial/relation/Tuple.java b/src/main/java/org/xerial/relation/Tuple.java index 8ae4a60..7e4306c 100644 --- a/src/main/java/org/xerial/relation/Tuple.java +++ b/src/main/java/org/xerial/relation/Tuple.java @@ -25,51 +25,214 @@ package org.xerial.relation; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import org.xerial.core.XerialError; +import org.xerial.core.XerialErrorCode; + +/** + * Tuple is a list of {@link Cell}s. Tuple class allows Non-1NF representation + * of the data. + * + * @author leo + * + */ public class Tuple implements Cell { - private final static ArrayList EMPTY_TUPLE = new ArrayList(0); - private final ArrayList tuple; + private final List nodeList; public Tuple() { - this.tuple = EMPTY_TUPLE; + this.nodeList = new ArrayList(); } - public boolean isAtom() + public Tuple(Tuple other) { - return false; + this(other.nodeList); } - public boolean isTuple() + public Tuple(int tupleSize) { - return true; + this.nodeList = new ArrayList(tupleSize); + } + + public Tuple(List nodeList) + { + this.nodeList = new ArrayList(nodeList.size()); + for (Cell each : nodeList) + { + this.nodeList.add(each); + } + } + + // public static Tuple emptyTuple(Schema schema) + // { + // List nodeList = new ArrayList(schema.size()); + // for (int i = 0; i < schema.size(); i++) + // { + // Schema subSchema = schema.get(i); + // if (subSchema.isAtom()) + // nodeList.add(null); + // else + // nodeList.add(emptyTuple(subSchema)); + // } + // return new Tuple(nodeList); + // } + + public void add(Cell node) + { + nodeList.add(node); + } + + public void set(int index, Cell node) + { + nodeList.set(index, node); + } + + public void set(TupleIndex index, Cell node) + { + if (!index.hasTail()) + { + set(index.get(0), node); + return; + } + + // nested node + Cell target = get(index.get(0)); + if (target == null || !target.isTuple()) + throw new XerialError(XerialErrorCode.INVALID_STATE, String.format( + "set to invalid element: index = %s in %s", index, this)); + + ((Tuple) target).set(index.tail(), node); } public int size() { - return tuple.size(); + return nodeList.size(); } - public void accept(CellVisitor visitor) + public void clear() { - visitor.visitTuple(this); + nodeList.clear(); + } + + public boolean isEmpty() + { + return nodeList.isEmpty(); + } + + public void sort(Comparator comparator) + { + Collections.sort(nodeList, comparator); + } + + public Iterator iterator() + { + return nodeList.iterator(); + } + + public Cell get(int index) + { + return nodeList.get(index); + } + + private static String join(Collection c, String concatinator) + { + if (c == null) + return ""; + int size = c.size(); + if (size == 0) + return ""; + + Iterator it = c.iterator(); + StringBuilder buf = new StringBuilder(); + for (int i = 0; it.hasNext() && i < size - 1; i++) + { + Object data = it.next(); + if (data != null) + buf.append(data.toString()); + else + buf.append("null"); + buf.append(concatinator); + } + Object lastData = it.next(); + if (lastData != null) + buf.append(lastData.toString()); + else + buf.append("null"); + return buf.toString(); + } + + @Override + public String toString() + { + return String.format("[%s]", join(nodeList, ", ")); + } + + public boolean addAll(List relationFragment) + { + return nodeList.addAll(relationFragment); + } + + public Node getNode() + { + throw new XerialError(XerialErrorCode.UNSUPPORTED); + } + + public List getNodeList() + { + return nodeList; + } + + public boolean isAtom() + { + return true; + } + + public boolean isTuple() + { + return true; } public Cell get(TupleIndex index) { - return null; + Cell cell = nodeList.get(index.get(0)); + if (index.hasTail()) + return cell.get(index.tail()); + else + return cell; + } + + public Node getNode(int index) + { + Cell node = get(index); + if (node.isAtom()) + return Node.class.cast(node); + else + throw new XerialError(XerialErrorCode.MISSING_ELEMENT, "node is not found: " + index); } public Node getNode(TupleIndex index) { - return null; + Cell node = get(index); + if (node == null) + return null; + + if (node.isAtom()) + return Node.class.cast(node); + else + throw new XerialError(XerialErrorCode.MISSING_ELEMENT, "node is not found: " + index); + } - public Node getNode() + public void accept(CellVisitor visitor) { - return null; + visitor.visitTuple(this); } public Tuple getTuple() diff --git a/src/main/java/org/xerial/relation/schema/RelationalSchema.java b/src/main/java/org/xerial/relation/schema/RelationalSchema.java new file mode 100644 index 0000000..d85f84c --- /dev/null +++ b/src/main/java/org/xerial/relation/schema/RelationalSchema.java @@ -0,0 +1,36 @@ +/*-------------------------------------------------------------------------- + * Copyright 2009 Taro L. Saito + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *--------------------------------------------------------------------------*/ +//-------------------------------------- +// XerialJ +// +// RelationalSchema.java +// Since: May 13, 2009 6:03:45 PM +// +// $URL$ +// $Author$ +//-------------------------------------- +package org.xerial.relation.schema; + +/** + * non-1NF relational schema + * + * @author leo + * + */ +public class RelationalSchema +{ + +} diff --git a/src/test/java/org/xerial/lens/LensTest.java b/src/test/java/org/xerial/lens/LensTest.java index 7ed29f7..4be32a7 100644 --- a/src/test/java/org/xerial/lens/LensTest.java +++ b/src/test/java/org/xerial/lens/LensTest.java @@ -54,7 +54,7 @@ public class LensTest { private HashedArrayList sequenceTable; - public void add(Coordinate coordinate, Gene gene) + public void addCoordinate_Gene(Coordinate coordinate, Gene gene) { sequenceTable.put(coordinate, gene); } -- 2.11.0