changeset 3:9eb9fabd9f29

added Table framework
author shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
date Wed, 13 Jun 2012 01:29:02 +0900
parents cc30c2773c09
children 761d04aecfcb
files memo.txt pom.xml src/main/java/jungle/core/table/PropertySequence.java src/main/java/jungle/core/table/Record.java src/main/java/jungle/core/table/Table.java src/main/java/jungle/core/table/simple/SimplePropertySequence.java src/main/java/jungle/core/table/simple/SimpleRecord.java src/main/java/jungle/core/table/simple/SimpleTable.java src/main/java/jungle/impl/SimpleChildren.java src/main/java/jungle/parsist/NodeProvider.java src/test/java/jungle/core/table/AbstractPropertySequenceTestTemplate.java src/test/java/jungle/core/table/AbstractRecordTestTemplate.java src/test/java/jungle/core/table/AbstractTableTestTemplate.java src/test/java/jungle/core/table/simple/SimplePropertySequenceTest.java src/test/java/jungle/core/table/simple/SimpleRecordTest.java src/test/java/jungle/core/table/simple/SimpleTableTest.java
diffstat 16 files changed, 567 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/memo.txt	Tue Jun 12 01:33:51 2012 +0900
+++ b/memo.txt	Wed Jun 13 01:29:02 2012 +0900
@@ -12,4 +12,13 @@
    ・NodeProvider は、Tree/TreeNode/Link を作成する。
    ・NodeProvider で、createTreeGroup , createTree , createTreeNode , createLink を依頼する
    ・初期では、パスの検索は深さ優先探索で行う
-  ・TreeGroupについて、TreeGroupは最初に初期Treeを1つ作らなければならない?
\ No newline at end of file
+  ・TreeGroupについて、TreeGroupは最初に初期Treeを1つ作らなければならない?
+  
+2012/06/12
+ ・NodeTable と NodeTable の状態を同期させる。
+ ・Table , Record , PropertySequence をつくる、Record がひとつの Node に対応する
+  ・これを用いて Node , TreeGroup , Tree , TreeNode , Link を作成する
+  ・ Node : Record の Property で Attribute を表現
+  ・ TreeGroup : Node で表すべき?
+  ・ Tree : TreeNode を継承している よって Node で表す、 Children や Links は PropertySequence による.
+  ・ Link : Node で表す。
\ No newline at end of file
--- a/pom.xml	Tue Jun 12 01:33:51 2012 +0900
+++ b/pom.xml	Wed Jun 13 01:29:02 2012 +0900
@@ -31,6 +31,11 @@
     	<artifactId>commons-collections</artifactId>
     	<version>3.2.1</version>
     </dependency>
+    <dependency>
+    	<groupId>net.sf.jpf</groupId>
+    	<artifactId>jpf</artifactId>
+    	<version>1.5</version>
+    </dependency>
   </dependencies>
   <build>
   	<plugins>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jungle/core/table/PropertySequence.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,9 @@
+package jungle.core.table;
+
+public interface PropertySequence extends Iterable<String>
+{
+	public void add(String _value);
+	public String get(int _pos);
+	public String remove(int _pos);
+	public int size();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jungle/core/table/Record.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,18 @@
+package jungle.core.table;
+
+import java.util.Iterator;
+
+import jungle.util.Pair;
+
+public interface Record
+{
+	public void setProperty(String _key,String _value);
+	public String getProperty(String _key);
+	public String removeProperty(String _key);
+	public PropertySequence createSequence(String _key);
+	public PropertySequence getSequence(String _key);
+	public PropertySequence removeSequence(String _key);
+	
+	public Iterator<Pair<String,String>> properties();
+	public Iterator<Pair<String,PropertySequence>> sequences();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jungle/core/table/Table.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,8 @@
+package jungle.core.table;
+
+public interface Table
+{
+	public Record create(String _key);
+	public Record find(String _key);
+	public Record remove(String _key);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jungle/core/table/simple/SimplePropertySequence.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,53 @@
+package jungle.core.table.simple;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.concurrent.CopyOnWriteArrayList;
+import javax.annotation.concurrent.ThreadSafe;
+
+import jungle.core.table.PropertySequence;
+
+@ThreadSafe
+public class SimplePropertySequence implements PropertySequence
+{
+	private final CopyOnWriteArrayList<String> sequence;
+	private final Collection<String> readonlyWrapper;
+	
+	public SimplePropertySequence()
+	{
+		sequence = new CopyOnWriteArrayList<String>();
+		readonlyWrapper = Collections.unmodifiableCollection(sequence);
+	}
+
+	@Override
+	public Iterator<String> iterator()
+	{
+		return readonlyWrapper.iterator();
+	}
+
+	@Override
+	public void add(String _value)
+	{
+		sequence.add(_value);
+	}
+
+	@Override
+	public String get(int _pos)
+	{
+		return sequence.get(_pos);
+	}
+
+	@Override
+	public String remove(int _pos)
+	{
+		return sequence.remove(_pos);
+	}
+
+	@Override
+	public int size()
+	{
+		return sequence.size();
+	}
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jungle/core/table/simple/SimpleRecord.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,119 @@
+package jungle.core.table.simple;
+
+import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import jungle.core.table.PropertySequence;
+import jungle.core.table.Record;
+import jungle.util.Pair;
+
+@ThreadSafe
+public class SimpleRecord implements Record
+{
+	private final ConcurrentHashMap<String,String> properties;
+	private final ConcurrentHashMap<String,PropertySequence> sequences;
+	
+	public SimpleRecord()
+	{
+		properties = new ConcurrentHashMap<String,String>();
+		sequences = new ConcurrentHashMap<String,PropertySequence>();
+	}
+
+	@Override
+	public void setProperty(String _key, String _value)
+	{
+		properties.put(_key,_value);
+	}
+
+	@Override
+	public String getProperty(String _key)
+	{
+		return properties.get(_key);
+	}
+	
+	@Override
+	public String removeProperty(String _key)
+	{
+		return properties.remove(_key);
+	}
+
+	@Override
+	public PropertySequence createSequence(String _key)
+	{
+		SimplePropertySequence seq = new SimplePropertySequence();
+		sequences.put(_key,seq);
+		return seq;
+	}
+
+	@Override
+	public PropertySequence getSequence(String _key)
+	{
+		return sequences.get(_key);
+	}
+
+	@Override
+	public PropertySequence removeSequence(String _key)
+	{
+		return sequences.remove(_key);
+	}
+
+	@Override
+	public Iterator<Pair<String, String>> properties()
+	{
+		final Set<Entry<String,String>> entries = properties.entrySet();
+		final Iterator<Entry<String,String>> itr = entries.iterator();
+		return new Iterator<Pair<String,String>>(){
+
+			@Override
+			public boolean hasNext()
+			{
+				return itr.hasNext();
+			}
+
+			@Override
+			public Pair<String, String> next()
+			{
+				Entry<String,String> ent = itr.next();
+				return new Pair<String,String>(ent.getKey(),ent.getValue());
+			}
+
+			@Override
+			public void remove()
+			{
+				throw new UnsupportedOperationException("removing is not supported");
+			}
+		};
+	}
+
+	@Override
+	public Iterator<Pair<String, PropertySequence>> sequences()
+	{
+		final Set<Entry<String,PropertySequence>> entries = sequences.entrySet();
+		final Iterator<Entry<String,PropertySequence>> itr = entries.iterator();
+		return new Iterator<Pair<String,PropertySequence>>(){
+
+			@Override
+			public boolean hasNext()
+			{
+				return itr.hasNext();
+			}
+
+			@Override
+			public Pair<String, PropertySequence> next()
+			{
+				Entry<String,PropertySequence> ent = itr.next();
+				return new Pair<String,PropertySequence> (ent.getKey(),ent.getValue());
+			}
+
+			@Override
+			public void remove()
+			{
+				throw new UnsupportedOperationException("removeing is not supported.");
+			}
+		};
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jungle/core/table/simple/SimpleTable.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,36 @@
+package jungle.core.table.simple;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+import jungle.core.table.Record;
+import jungle.core.table.Table;
+
+public class SimpleTable implements Table
+{
+	private final ConcurrentHashMap<String,Record> table;
+	
+	public SimpleTable()
+	{
+		table = new ConcurrentHashMap<String,Record>();
+	}
+
+	@Override
+	public Record create(String _key)
+	{
+		SimpleRecord r = new SimpleRecord();
+		Record b = table.putIfAbsent(_key,r);
+		return (b == null) ? r : null;
+	}
+
+	@Override
+	public Record find(String _key)
+	{
+		return table.get(_key);
+	}
+
+	@Override
+	public Record remove(String _key)
+	{
+		return table.remove(_key);
+	}
+}
--- a/src/main/java/jungle/impl/SimpleChildren.java	Tue Jun 12 01:33:51 2012 +0900
+++ b/src/main/java/jungle/impl/SimpleChildren.java	Wed Jun 13 01:29:02 2012 +0900
@@ -40,4 +40,6 @@
 	{
 		return sets.size();
 	}
+	
+	
 }
--- a/src/main/java/jungle/parsist/NodeProvider.java	Tue Jun 12 01:33:51 2012 +0900
+++ b/src/main/java/jungle/parsist/NodeProvider.java	Wed Jun 13 01:29:02 2012 +0900
@@ -2,4 +2,5 @@
 
 public interface NodeProvider
 {
+	
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jungle/core/table/AbstractPropertySequenceTestTemplate.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,73 @@
+package jungle.core.table;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public abstract class AbstractPropertySequenceTestTemplate extends TestCase
+{
+	public abstract PropertySequence newInstance();
+	
+	public void testInitialSizeIsZero()
+	{
+		PropertySequence seq = newInstance();
+		Assert.assertEquals(0,seq.size());
+	}
+	
+	public static final String ONE = "one";
+	public static final String TWO = "two";
+	public static final String THREE = "three";
+	
+	public PropertySequence prepare()
+	{
+		PropertySequence seq = newInstance();
+		seq.add(ONE);
+		seq.add(TWO);
+		seq.add(THREE);
+		
+		return seq;
+	}
+	
+	public void testAdd()
+	{
+		PropertySequence seq = prepare();
+		
+		int size = seq.size();
+		Assert.assertEquals(3,size);
+		
+		String one = seq.get(0);
+		String two = seq.get(1);
+		String three = seq.get(2);
+		
+		Assert.assertEquals(ONE,one);
+		Assert.assertEquals(TWO,two);
+		Assert.assertEquals(THREE,three);
+	}
+	
+	public void testGet()
+	{
+		PropertySequence seq = prepare();
+		Assert.assertEquals(ONE,seq.get(0));
+	}
+	
+	public void testSize()
+	{
+		PropertySequence seq = prepare();
+		Assert.assertEquals(3,seq.size());
+	}
+	
+	public void testRemove()
+	{
+		PropertySequence seq = prepare();
+		
+		String removed = seq.remove(1);
+		Assert.assertEquals(removed,TWO);
+		
+		Assert.assertEquals(ONE,seq.get(0));
+		Assert.assertEquals(THREE,seq.get(1));
+	}
+	
+	public void testThreadSafety()
+	{
+		Assert.assertTrue(true);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jungle/core/table/AbstractRecordTestTemplate.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,128 @@
+package jungle.core.table;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import jungle.util.Pair;
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public abstract class AbstractRecordTestTemplate extends TestCase
+{
+	public abstract Record newInstance();
+	
+	public static final String ONE = "one";
+	public static final String TWO = "two";
+	public static final String THREE = "three";
+	
+	public Record prepare()
+	{
+		Record r = newInstance();
+		
+		r.setProperty(ONE,ONE);
+		r.setProperty(TWO,TWO);
+		r.setProperty(THREE,THREE);
+		
+		r.createSequence(ONE);
+		r.createSequence(TWO);
+		r.createSequence(THREE);
+		
+		return r;
+	}
+	
+	public void testSetProperty()
+	{
+		Record r = prepare();
+		r.setProperty("TEST","TEST");
+		
+		Assert.assertEquals(r.getProperty("TEST"),"TEST");
+	}
+	
+	public void testGetProperty()
+	{
+		Record r = prepare();
+		Assert.assertEquals(r.getProperty(ONE),ONE);
+		Assert.assertEquals(r.getProperty(TWO),TWO);
+		Assert.assertEquals(r.getProperty(THREE),THREE);
+	}
+	
+	public void testRemoveProperty()
+	{
+		Record r = prepare();
+		String removed = r.removeProperty(TWO);
+		Assert.assertEquals(TWO,removed);
+		Assert.assertNull(r.getProperty(TWO));
+	}
+	
+	public void testCreateSequence()
+	{
+		Record r = prepare();
+		PropertySequence seq = r.createSequence("TEST");
+		
+		Assert.assertNotNull(seq);
+		Assert.assertEquals(seq,r.getSequence("TEST"));
+	}
+	
+	public void testGetSequence()
+	{
+		Record r = newInstance();
+		PropertySequence one = r.createSequence(ONE);
+		PropertySequence two = r.createSequence(TWO);
+		PropertySequence three = r.createSequence(THREE);
+		
+		Assert.assertEquals(r.getSequence(ONE),one);
+		Assert.assertEquals(r.getSequence(TWO),two);
+		Assert.assertEquals(r.getSequence(THREE),three);
+	}
+	
+	public void testRemoveSequence()
+	{
+		Record r = prepare();
+		PropertySequence removed = r.removeSequence(TWO);
+		
+		Assert.assertNotNull(removed);
+		Assert.assertNull(r.removeSequence(TWO));
+	}
+	
+	public void testProperties()
+	{
+		Record r = prepare();
+		
+		HashSet<String> props = new HashSet<String>();
+		props.add(ONE);
+		props.add(TWO);
+		props.add(THREE);
+		
+		Iterator<Pair<String,String>> itr = r.properties();
+		while(itr.hasNext()){
+			Pair<String,String> p = itr.next();
+			Assert.assertEquals(p.left(),p.right());
+			Assert.assertTrue(props.contains(p.left()));
+			props.remove(p.left());
+		}
+		
+		Assert.assertEquals(0,props.size());
+	}
+	
+	public void testSequences()
+	{
+		Record r = newInstance();
+		PropertySequence one = r.createSequence(ONE);
+		PropertySequence two = r.createSequence(TWO);
+		PropertySequence three = r.createSequence(THREE);
+		
+		HashSet<PropertySequence> props = new HashSet<PropertySequence>();
+		props.add(one);
+		props.add(two);
+		props.add(three);
+		
+		Iterator<Pair<String,PropertySequence>> itr = r.sequences();
+		while(itr.hasNext()){
+			Pair<String,PropertySequence> p = itr.next();
+			PropertySequence seq = p.right();
+			Assert.assertTrue(props.contains(seq));
+			props.remove(seq);
+		}
+		
+		Assert.assertEquals(0,props.size());
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jungle/core/table/AbstractTableTestTemplate.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,62 @@
+package jungle.core.table;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public abstract class AbstractTableTestTemplate extends TestCase
+{
+	public abstract Table newInstance();
+	
+	public Table prepare()
+	{
+		Table t = newInstance();
+		
+		t.create(ONE);
+		t.create(TWO);
+		t.create(THREE);
+		
+		return t;
+	}
+	
+	public static final String ONE = "one";
+	public static final String TWO = "two";
+	public static final String THREE = "three";
+	
+	public void testCreateRecord()
+	{
+		Table t = newInstance();
+		
+		Record one = t.create(ONE);
+		Record two = t.create(TWO);
+		Record three = t.create(THREE);
+		
+		Assert.assertNotNull(one);
+		Assert.assertNotNull(two);
+		Assert.assertNotNull(three);
+	}
+	
+	public void testFindRecord()
+	{
+		Table t = newInstance();
+		
+		Record one = t.create(ONE);
+		Assert.assertNotNull(one);
+		Assert.assertEquals(one,t.find(ONE));
+	}
+	
+	public void testRemoveRecord()
+	{
+		Table t = newInstance();
+		
+		Record one = t.create(ONE);
+		Record two = t.create(TWO);
+		
+		Assert.assertNotNull(one);
+		Assert.assertNotNull(two);
+		
+		Assert.assertEquals(one,t.remove(ONE));
+		Assert.assertEquals(two,t.remove(TWO));
+		Assert.assertNull(t.remove(ONE));
+		Assert.assertNull(t.remove(TWO));
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jungle/core/table/simple/SimplePropertySequenceTest.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,13 @@
+package jungle.core.table.simple;
+
+import jungle.core.table.AbstractPropertySequenceTestTemplate;
+import jungle.core.table.PropertySequence;
+
+public class SimplePropertySequenceTest extends AbstractPropertySequenceTestTemplate
+{
+	@Override
+	public PropertySequence newInstance()
+	{
+		return new SimplePropertySequence();
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jungle/core/table/simple/SimpleRecordTest.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,15 @@
+package jungle.core.table.simple;
+
+import jungle.core.table.AbstractRecordTestTemplate;
+import jungle.core.table.Record;
+
+public class SimpleRecordTest extends AbstractRecordTestTemplate
+{
+
+	@Override
+	public Record newInstance()
+	{
+		return new SimpleRecord();
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jungle/core/table/simple/SimpleTableTest.java	Wed Jun 13 01:29:02 2012 +0900
@@ -0,0 +1,15 @@
+package jungle.core.table.simple;
+
+import jungle.core.table.AbstractTableTestTemplate;
+import jungle.core.table.Table;
+
+public class SimpleTableTest extends AbstractTableTestTemplate
+{
+
+	@Override
+	public Table newInstance()
+	{
+		return new SimpleTable();
+	}
+
+}