changeset 6:1a5eaf5ce085

modified AbstractVertexesTest and SimpleVertexes to pass the unit test.
author shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
date Mon, 25 Jun 2012 23:48:53 +0900
parents 07b26b4b21e0
children c3c65308a11b
files memo.txt src/main/java/jungle/core/graph/simple/SimpleVertexes.java src/main/java/jungle/impl/SimpleJungle.java src/main/java/jungle/impl/SimpleTree.java src/main/java/jungle/impl/SimpleTreeGroup.java src/main/java/jungle/impl/Simples.java src/test/java/jungle/core/graph/AbstractVertexesTest.java src/test/java/jungle/misc/fj/ImmutableListExample.java
diffstat 8 files changed, 69 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/memo.txt	Sat Jun 23 01:54:20 2012 +0900
+++ b/memo.txt	Mon Jun 25 23:48:53 2012 +0900
@@ -45,4 +45,9 @@
 2012/06/22
  ・Vertexes のテストコードをほぼ仕上げた.
  ・次は,テストコードが正しいか,SimpleVertexes を動作させてみる.
- ・その次に,Jungle の実装を書く.
\ No newline at end of file
+ ・その次に,Jungle の実装を書く.
+ 
+2012/06/25
+ ・Vertexes のテストコードが動いた.
+ ・SimpleJungle などを GraphAPI を用いて記述する.
+ ・SimpleTreeGroup , SimpleTree , SimpleNode をどうするか
\ No newline at end of file
--- a/src/main/java/jungle/core/graph/simple/SimpleVertexes.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/main/java/jungle/core/graph/simple/SimpleVertexes.java	Mon Jun 25 23:48:53 2012 +0900
@@ -60,11 +60,14 @@
 	@Override
 	public Vertex remove(final int _index)
 	{
+		if(_index < 0){
+			throw new IndexOutOfBoundsException("_index < 0");
+		}
 		SimpleVertex head = null;
 		List<SimpleVertex> current = null,update = null;
 		do{
 			current = vertexes.get();
-			if(current.length() < _index){
+			if(current.length() < _index + 1){
 				throw new IndexOutOfBoundsException("list.length() < _index");
 			}
 			
@@ -125,8 +128,8 @@
 	@Override
 	public boolean contains(final Vertex _v)
 	{
-		if(_v.getGraph() == parent){
-			throw new IllegalArgumentException("Vertex _v is not a number of graph");
+		if(_v.getGraph() != parent){
+			throw new IllegalArgumentException("Vertex _v is not a member of graph");
 		}
 		
 		List<SimpleVertex> list = vertexes.get();
@@ -224,6 +227,10 @@
 	@Override
 	public boolean removeFirst(Vertexes _vs)
 	{
+		if(_vs == null){
+			throw new NullPointerException("_vs is null");
+		}
+		
 		if(_vs instanceof SimpleVertexes){
 			Collection<Vertex> vs = ((SimpleVertexes)_vs).toCollection();
 			return removeFirst(vs);
@@ -261,11 +268,15 @@
 	{
 		List<SimpleVertex> list = vertexes.get();
 		
-		if(list.length() < _index){
+		if(_index < 0){
+			throw new IndexOutOfBoundsException("index is muinus.");
+		}
+		
+		if(list.length() < _index + 1){
 			throw new IndexOutOfBoundsException("vertex.length() < _index");
 		}
 		
-		return list.take(_index).last();
+		return list.take(_index + 1).last();
 	}
 
 	@Override
@@ -333,6 +344,10 @@
 	@Override
 	public Vertex replace(Vertex _v, int _index)
 	{
+		if(_index < 0){
+			throw new IndexOutOfBoundsException("_index is muinus");
+		}
+		
 		if(_v == null){
 			throw new NullPointerException("_v is null");
 		}
@@ -345,7 +360,7 @@
 		List<SimpleVertex> current = null,update = null;
 		do{
 			current = vertexes.get();
-			if(current.length() < _index){
+			if(current.length() < _index + 1){
 				throw new IndexOutOfBoundsException("current.length() < _index");
 			}
 		
@@ -393,29 +408,27 @@
 	public boolean compareAndSwap(int _index, Vertex _vertex, Vertex _newVertex)
 	{
 		List<SimpleVertex> current = vertexes.get();
-		if(current.length() < _index || _index < 0){
+		
+		if(_vertex == null || _newVertex == null){
+			throw new NullPointerException("_vertex or _newVertex is null");
+		}
+		
+		if(current.length() < _index + 1 || _index < 0){
 			throw new IndexOutOfBoundsException("list.length() < _index or _index < 0");
 		}
 		
 		final SimpleVertex v1 = (SimpleVertex)_vertex;
 		SimpleVertex v2 = (SimpleVertex)_newVertex;
 		
-		F<SimpleVertex,Boolean> predicate = new F<SimpleVertex,Boolean>(){
-			@Override
-			public Boolean f(SimpleVertex _vertex2){
-				return v1 != _vertex2;
-			}
-		};
-		
 		List<SimpleVertex> update = null;
 		do{
-			P2<List<SimpleVertex>,List<SimpleVertex>> slice = current.span(predicate);
+			P2<List<SimpleVertex>,List<SimpleVertex>> slice = current.splitAt(_index);
 			SimpleVertex head = slice._2().head();
 			if(head != v1){
 				return false;
 			}
 			
-			update = slice._1().append(slice._2().drop(1).snoc(v2));
+			update = slice._1().append(slice._2().drop(1).cons(v2));
 		}while(!vertexes.compareAndSet(current,update));
 		
 		return true;
--- a/src/main/java/jungle/impl/SimpleJungle.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/main/java/jungle/impl/SimpleJungle.java	Mon Jun 25 23:48:53 2012 +0900
@@ -3,23 +3,24 @@
 import java.util.UUID;
 import jungle.core.Jungle;
 import jungle.core.TreeGroup;
+import jungle.core.graph.Graph;
 import jungle.core.table.Record;
 import jungle.core.table.Table;
 
 public class SimpleJungle implements Jungle
 {
-	private final Table tb;
+	private final Graph graph;
 	
-	public SimpleJungle(Table _tb)
+	public SimpleJungle(Graph _graph)
 	{
-		tb = _tb;
+		graph = _graph;
 	}
 
 	public TreeGroup createTreeGroup()
 	{
 		String groupID = UUID.randomUUID().toString();
-		Record tg = tb.create(groupID);
+		SimpleTreeGroup group = new SimpleTreeGroup(groupID,graph);
 		
-		return new SimpleTreeGroup(groupID,tg,tb);
+		return new SimpleTreeGroup(groupID,graph);
 	}
 }
--- a/src/main/java/jungle/impl/SimpleTree.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/main/java/jungle/impl/SimpleTree.java	Mon Jun 25 23:48:53 2012 +0900
@@ -6,24 +6,25 @@
 import jungle.core.Links;
 import jungle.core.Tree;
 import jungle.core.TreeGroup;
+import jungle.core.graph.Graph;
+import jungle.core.graph.Vertex;
 import jungle.core.table.Record;
 
 public class SimpleTree implements Tree
 {
 	private final String treeID;
-	private final TreeGroup group;
-	private final Record record;
-	
+	private final Graph graph;
+	private final Vertex vertex;
 	private final SimpleChildren children;
 	private final SimpleLinks links;
 	
-	public SimpleTree(TreeGroup _group,String _treeID,Record _r)
+	public SimpleTree(String _treeID,Graph _graph)
 	{
 		treeID = _treeID;
-		group = _group;
-		record = _r;
-		children = new SimpleChildren(_r.createSequenceIfAbsent(Simples.TREENODE_CHILDREN_KEY));
-		links = new SimpleLinks(_r.createSequenceIfAbsent(Simples.TREENODE_LINKS_KEY));
+		graph = _graph;
+		vertex = graph.createVertex(_treeID);
+		children = new SimpleChildren(vertex.createVertexes(Simples.TREENODE_CHILDREN_KEY),graph);
+		links = new SimpleLinks(vertex.createVertexes(Simples.TREENODE_LINKS_KEY));
 	}
 	
 	public TreeGroup getGroup()
--- a/src/main/java/jungle/impl/SimpleTreeGroup.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/main/java/jungle/impl/SimpleTreeGroup.java	Mon Jun 25 23:48:53 2012 +0900
@@ -6,22 +6,27 @@
 import jungle.core.Editor;
 import jungle.core.Tree;
 import jungle.core.TreeGroup;
+import jungle.core.graph.Graph;
 import jungle.core.table.Record;
 import jungle.core.table.Table;
 
 public class SimpleTreeGroup implements TreeGroup
 {
 	private final String groupID;
-	private final Record record;
+	private final Graph graph;
 	private final AtomicLong revisionCounter;
-	private final Table table;
+	private final AtomicReference<SimpleTree> tip;
 	
-	public SimpleTreeGroup(String _groupID,Record _r,Table _tb)
+	public SimpleTreeGroup(String _groupID,Graph _graph)
 	{
 		groupID = _groupID;
-		record = _r;
-		table = _tb;
+		graph = _graph;
 		revisionCounter = new AtomicLong();
+		tip = new AtomicReference<SimpleTree>();
+		
+		String treeID = String.format(Simples.TREEGROUP_TREEID_FORMAT,groupID,Long.toHexString(revisionCounter.getAndIncrement()));
+		SimpleTree first = new SimpleTree(treeID,graph);
+		tip.set(first);
 	}
 
 	public String getID()
@@ -54,4 +59,9 @@
 		SimpleEditor editor = new SimpleEditor(this,_t);
 		return editor;
 	}
+	
+	public AtomicReference<SimpleTree> getLatestTreeHolder()
+	{
+		return tip;
+	}
 }
--- a/src/main/java/jungle/impl/Simples.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/main/java/jungle/impl/Simples.java	Mon Jun 25 23:48:53 2012 +0900
@@ -6,10 +6,6 @@
 
 public class Simples
 {
-	public static final Attributes EMPTY_ATTRIBUTES = new SimpleAttributes();
-	public static final Children EMPTY_CHILDREN = new SimpleChildren();
-	public static final Links EMPTY_LINKS = new SimpleLinks();
-	
 	public static final String PROPERTY_KEY_PREFIX = "@";
 	
 	public static final String TREENODE_CHILDREN_KEY = "CHILDREN";
--- a/src/test/java/jungle/core/graph/AbstractVertexesTest.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/test/java/jungle/core/graph/AbstractVertexesTest.java	Mon Jun 25 23:48:53 2012 +0900
@@ -3,9 +3,6 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
-
-import javax.swing.plaf.basic.BasicScrollPaneUI.VSBChangeListener;
-
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
@@ -233,7 +230,6 @@
 		Graph g = newInstance();
 		Vertex v = g.createVertex();
 		Vertexes vs = v.createVertexes(RELATION);
-		Vertex a = g.createVertex("a");
 		Vertex c = g.createVertex("c");
 		
 		vs.add(Arrays.asList(g.createVertex(),g.createVertex(),c,g.createVertex(),c,c));
@@ -326,6 +322,7 @@
 		set.add(g.createVertex());
 		
 		Vertex c = g.createVertex();
+		vs.add(set);
 		vs.add(c);
 		Assert.assertEquals(4,vs.size());
 		
@@ -333,7 +330,7 @@
 		Assert.assertEquals(3,vs.size());
 		
 		for(Vertex x : vs){
-			Assert.assertTrue(set.contains(x));
+			Assert.assertTrue(set.remove(x));
 		}
 		
 		Assert.assertEquals(0,set.size());
--- a/src/test/java/jungle/misc/fj/ImmutableListExample.java	Sat Jun 23 01:54:20 2012 +0900
+++ b/src/test/java/jungle/misc/fj/ImmutableListExample.java	Mon Jun 25 23:48:53 2012 +0900
@@ -1,8 +1,5 @@
 package jungle.misc.fj;
 
-import fj.Equal;
-import fj.F;
-import fj.P2;
 import fj.data.List;
 
 public class ImmutableListExample
@@ -10,34 +7,7 @@
 	public static void main(String[] _args)
 	{
 		List<String> first = List.list("1","3","2","3");
-		List<String> two = first.snoc("4");
-		F<String,Boolean> predicate = new F<String,Boolean>(){
-			@Override
-			public Boolean f(String arg0)
-			{
-				return "3".equals(arg0);
-			}
-		};
-		
-		List<String> three = two.removeAll(predicate);
-		List<String> four = two.delete("3",Equal.equal(
-			new F<String,F<String,Boolean>>(){
-				@Override
-				public F<String, Boolean> f(final String _str){
-					return new F<String,Boolean>(){
-						@Override
-						public Boolean f(String _str1){
-							return _str.equals(_str1);
-						}
-					};
-				}
-			}));
-		
-		System.out.println(first.toString());
-		System.out.println(two.toString());
-		P2<List<String>,List<String>> p = two.splitAt(2);
-		System.out.println(p._1().snoc("hey!").append(p._2()).toString());
-		System.out.println(three.toString());
-		System.out.println(four.toString());
+		System.out.println(first.take(3+1).last());
+		System.out.println(first.splitAt(2)._1());
 	}
 }