# HG changeset patch # User shoshi # Date 1341309568 -32400 # Node ID abed5bd92fcb0bdd0f26d8b2d0da79469f06b8bd # Parent c3c65308a11b53999a551eb3162db5336eb14ef0 commit diff -r c3c65308a11b -r abed5bd92fcb memo.txt --- a/memo.txt Fri Jun 29 00:03:12 2012 +0900 +++ b/memo.txt Tue Jul 03 18:59:28 2012 +0900 @@ -53,4 +53,8 @@ ・SimpleTreeGroup , SimpleTree , SimpleNode をどうするか 2012/06/27 - ・Graph を用いた SimpleJungle の設計をもっと考える \ No newline at end of file + ・Graph を用いた SimpleJungle の設計をもっと考える + +2012/06/28 + ・SimpleJungle 実装を完成させた。テストコードはまだ書いていない + ・SimpleEditor の部分がまだだった。明日完成させよう \ No newline at end of file diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/EdgeList.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jungle/core/graph/EdgeList.java Tue Jul 03 18:59:28 2012 +0900 @@ -0,0 +1,26 @@ +package jungle.core.graph; + +import java.util.Collection; + +public interface EdgeList extends Iterable +{ + public int getLength(); + public Vertex at(int _i); + public Collection asVertexCollection(); + + public void appendVertex(Vertex _vertex); + public void appendVertexAt(Vertex _vertex,int _index); + public void appendVertexCollection(Collection _collection); + + public boolean removeFirst(Vertex _vertex); + public boolean removeFirst(Collection _collection); + public Vertex remove(int _index); + + public Vertex replace(Vertex _vertex,int _index); + public boolean replaceFirst(Vertex _vertex,Vertex _newVertex); + + public boolean compareAndSwap(int _index,Vertex _vertex,Vertex _newVertex); + public boolean contains(Vertex _vertex); + + public String getName(); +} diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/Graph.java --- a/src/main/java/jungle/core/graph/Graph.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/main/java/jungle/core/graph/Graph.java Tue Jul 03 18:59:28 2012 +0900 @@ -1,13 +1,15 @@ package jungle.core.graph; -import java.util.Iterator; - +/* + * multiedge property graph + */ public interface Graph { public Vertex getVertex(String _id); public Vertex createVertex(String _id); public Vertex createVertex(); + public Vertex cloneVertex(Vertex _base); + public Vertex cloneVertex(String _id,Vertex _base); - public boolean isSame(Graph _g); - public Iterator vertexes(); + public boolean isSameGraph(Graph _g); } diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/Vertex.java --- a/src/main/java/jungle/core/graph/Vertex.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/main/java/jungle/core/graph/Vertex.java Tue Jul 03 18:59:28 2012 +0900 @@ -3,15 +3,20 @@ public interface Vertex { public String getID(); - public Graph getGraph(); + public boolean isSameVertex(); + public Graph getOwnerGraph(); - public String getProperty(String _key); - public void setProperty(String _key,String _value); - public String setPropertyIfAbsent(String _key,String _value); - public String removeProperty(String _key); - public boolean compareAndSwapProprety(String _key,String _except,String _value); + public String getAttribute(String _key); + public void setAttribute(String _key,String _value); + public String setAttributeIfAbsent(String _key,String _value); + public Iterable getAttributeKeys(); + public String removeAttribute(String _key); + public boolean replaceAttribute(String _key,String _except,String _value); - public Vertexes removeVertexes(String _type); - public Vertexes getVertexes(String _type); - public Vertexes createVertexes(String _type); + public Vertex getNamedEdgeAt(String _name,int _index); + public void appendEdge(String _name,Vertex _vertex); + public void insertEdge(String _name,int _index,Vertex _vertex); + public void removeEdge(String _name,Vertex _vertex); + public void replaceEdge(String _name,Vertex _expectVertex,Vertex _updateVertex); + public Iterable getEdgeNames(); } diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/Vertexes.java --- a/src/main/java/jungle/core/graph/Vertexes.java Fri Jun 29 00:03:12 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -package jungle.core.graph; - -import java.util.Collection; - -public interface Vertexes extends Iterable -{ - public int size(); - public Vertex at(int _i); - - public void add(Vertex _v); - public void add(Vertex _v,int _i); - public void add(Collection _vs); - public void add(Vertexes _vs); - - public boolean removeFirst(Vertex _v); - public boolean removeFirst(Collection _vs); - public boolean removeFirst(Vertexes _vs); - public Vertex remove(int _i); - - public Vertex replace(Vertex _v,int _i); - public boolean replaceFirst(Vertex _vertex,Vertex _newVertex); - - public boolean compareAndSwap(int _index,Vertex _vertex,Vertex _newVertex); - public boolean contains(Vertex _v); - - public Graph getGraph(); - public String type(); -} diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/simple/SimpleGraph.java --- a/src/main/java/jungle/core/graph/simple/SimpleGraph.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/main/java/jungle/core/graph/simple/SimpleGraph.java Tue Jul 03 18:59:28 2012 +0900 @@ -32,7 +32,7 @@ } @Override - public boolean isSame(Graph _g) + public boolean isSameGraph(Graph _g) { if(_g instanceof SimpleGraph){ return _g == this; @@ -80,4 +80,27 @@ }; return wrapper; } + + @Override + public Vertex createVertexFromTemplate(Vertex _template) + { + String newID = Long.toString(ids.incrementAndGet()); + + } + + @Override + public Vertex createVertexFromTemplate(String _id,Vertex _template) + { + if(_template instanceof SimpleVertex){ + SimpleVertex template = (SimpleVertex)_template; + if(template.getGraph().equals(this)){ + SimpleVertex newVertex = new SimpleVertex(this,_id,_template); + + } + + throw new IllegalArgumentException("_template is a vertex from other graph."); + } + + throw new IllegalArgumentException("_template is not a instance of SimpleVertex"); + } } diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/simple/SimpleVertex.java --- a/src/main/java/jungle/core/graph/simple/SimpleVertex.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/main/java/jungle/core/graph/simple/SimpleVertex.java Tue Jul 03 18:59:28 2012 +0900 @@ -1,24 +1,52 @@ package jungle.core.graph.simple; -import java.util.concurrent.ConcurrentHashMap; - +import java.util.Iterator; +import java.util.concurrent.atomic.AtomicReference; +import fj.Ord; +import fj.P2; +import fj.data.Option; +import fj.data.TreeMap; import jungle.core.graph.Graph; import jungle.core.graph.Vertex; import jungle.core.graph.Vertexes; +import jungle.util.Pair; public class SimpleVertex implements Vertex { private String id; private final SimpleGraph parent; - private final ConcurrentHashMap properties; - private final ConcurrentHashMap relations; + private final AtomicReference> propertiesHolder; + private final AtomicReference> relationsHolder; + + private static final TreeMap EMPTY_PROPERTIES_MAP = TreeMap.empty(Ord.stringOrd); + private static final TreeMap EMPTY_RELATIONS_MAP = TreeMap.empty(Ord.stringOrd); public SimpleVertex(SimpleGraph _parent,String _id) { id = _id; parent = _parent; - properties = new ConcurrentHashMap(); - relations = new ConcurrentHashMap(); + propertiesHolder = new AtomicReference>(EMPTY_PROPERTIES_MAP); + relationsHolder = new AtomicReference>(EMPTY_RELATIONS_MAP); + } + + public SimpleVertex(SimpleGraph _parent,String _id,SimpleVertex _template) + { + id = _id; + parent = _parent; + + //get snapshot + TreeMap snapshotProperties = _template. + + propertiesHolder = new AtomicReference>(); + relationsHolder = new AtomicReference>(); + } + + private Pair,TreeMap> createVertexSnapshot() + { + TreeMap snapshotProperties = propertiesHolder.get(); + TreeMap snapshotRelations = relationsHolder.get(); + + return new Pair> } void setID(String _id) @@ -29,8 +57,18 @@ @Override public String setPropertyIfAbsent(String _key,String _value) { - String previousValue = properties.putIfAbsent(_key,_value); - return previousValue; + TreeMap current,update; + do{ + current = propertiesHolder.get(); + Option value = current.get(_key); + if(value.isSome()){ + return value.some(); + } + + update = current.set(_key,_value); + }while(!propertiesHolder.compareAndSet(current,update)); + + return null; } @Override @@ -48,38 +86,111 @@ @Override public String getProperty(String _key) { - return properties.get(_key); + TreeMap current = propertiesHolder.get(); + Option nullOrValue = current.get(_key); + if(nullOrValue.isNone()){ + return null; + } + + return nullOrValue.some(); } @Override public void setProperty(String _key, String _value) { - properties.put(_key,_value); + TreeMap current , update; + do{ + current = propertiesHolder.get(); + update = current.set(_key,_value); + }while(!propertiesHolder.compareAndSet(current,update)); } @Override public String removeProperty(String _key) { - return properties.remove(_key); + if(_key == null){ + throw new NullPointerException("_key is null"); + } + + String remove; + TreeMap current , update; + do{ + current = propertiesHolder.get(); + Option nullOrValue = current.get(_key); + if(nullOrValue.isNone()){ + return null; + } + + remove = nullOrValue.some(); + update = current.delete(_key); + }while(!propertiesHolder.compareAndSet(current,update)); + + return remove; } @Override public Vertexes getVertexes(String _key) { - return relations.get(_key); + if(_key == null){ + throw new NullPointerException("_key is null"); + } + + TreeMap current = relationsHolder.get(); + Option nullOrValue = current.get(_key); + if(nullOrValue.isNone()){ + return null; + } + + return nullOrValue.some(); } @Override public Vertexes removeVertexes(String _type) { - SimpleVertexes vertexes = relations.remove(_type); - return vertexes; + if(_type == null){ + throw new NullPointerException("_type is null"); + } + + SimpleVertexes remove; + TreeMap current , update; + do{ + current = relationsHolder.get(); + Option nullOrValue = current.get(_type); + if(nullOrValue.isNone()){ + return null; + } + + remove = nullOrValue.some(); + update = current.delete(_type); + }while(!relationsHolder.compareAndSet(current,update)); + + return remove; } @Override - public boolean compareAndSwapProprety(String _key, String _except,String _value) + public boolean compareAndSwapProprety(String _key,final String _except,final String _value) { - return properties.replace(_key,_except,_value); + if(_key == null || _except == null || _value == null){ + throw new NullPointerException("_key or _except or _value is null"); + } + + TreeMap current , update; + do{ + current = propertiesHolder.get(); + Option nullOrValue = current.get(_key); + if(nullOrValue.isNone()){ + return false; + } + + String value = nullOrValue.some(); + if(!value.equals(_except)){ + return false; + } + + update = current.set(_key,_value); + }while(!propertiesHolder.compareAndSet(current,update)); + + return true; } @Override @@ -91,8 +202,48 @@ @Override public Vertexes createVertexes(String _type) { - SimpleVertexes vertexes = new SimpleVertexes(_type,parent); - SimpleVertexes result = relations.putIfAbsent(_type,vertexes); - return result == null ? vertexes : null; + TreeMap current , update; + + SimpleVertexes newVertexes; + do{ + current = relationsHolder.get(); + Option nullOrValue = current.get(_type); + if(nullOrValue.isSome()){ + return null; + } + + newVertexes = new SimpleVertexes(_type,parent); + update = current.set(_type,newVertexes); + }while(!relationsHolder.compareAndSet(current,update)); + + return newVertexes; + } + + @Override + public Iterable> getProperties() + { + final TreeMap snapshot = propertiesHolder.get(); + Iterable> snapshotIterableWrapper = new Iterable>(){ + @Override + public Iterator> iterator(){ + return new Iterator>(){ + Iterator> snapshotKeyValueIterator = snapshot.iterator(); + @Override + public boolean hasNext(){ + return snapshotKeyValueIterator.hasNext(); + } + @Override + public Pair next(){ + P2 keyValue = snapshotKeyValueIterator.next(); + return new Pair(keyValue._1(),keyValue._2()); + } + @Override + public void remove() { + throw new UnsupportedOperationException("removing is not supported."); + } + }; + } + }; + return snapshotIterableWrapper; } } diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/core/graph/simple/SimpleVertexes.java --- a/src/main/java/jungle/core/graph/simple/SimpleVertexes.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/main/java/jungle/core/graph/simple/SimpleVertexes.java Tue Jul 03 18:59:28 2012 +0900 @@ -11,6 +11,7 @@ import fj.F; import fj.P2; import fj.data.List; +import fj.data.Option; import jungle.core.graph.Graph; import jungle.core.graph.Vertex; @@ -19,41 +20,20 @@ public class SimpleVertexes implements Vertexes { private final String type; - private final AtomicReference> vertexes; + private final SimpleVertexContext context; private final SimpleGraph parent; - public SimpleVertexes(String _type,SimpleGraph _parent) + public SimpleVertexes(String _type,SimpleGraph _parent,SimpleVertexContext _context) { parent = _parent; type = _type; - List nil = List.nil(); - vertexes = new AtomicReference>(nil); - } - - public static void main(String _args[]) - { - SimpleGraph g = new SimpleGraph(); - Vertex v = g.createVertex(); - Vertexes vs = v.createVertexes("test"); - Vertex a = g.createVertex("a"); - Vertex b = g.createVertex("b"); - Vertex c = g.createVertex("c"); - Vertex d = g.createVertex("d"); - vs.add(Arrays.asList(a,b,c,d)); - - System.out.println(vs); - - if(vs.compareAndSwap(2,c,b) == false){ - System.out.println("^o^..."); - } - - System.out.println(vs); + context = _context; } @Override public String toString() { - return vertexes.get().toString(); + return context.getRelations().get(type).toString(); } @@ -63,10 +43,12 @@ if(_index < 0){ throw new IndexOutOfBoundsException("_index < 0"); } + SimpleVertex head = null; - List current = null,update = null; + TreeMap> current = null,update = null; do{ - current = vertexes.get(); + Option> value = context.getRelations().get(type); + if(current.length() < _index + 1){ throw new IndexOutOfBoundsException("list.length() < _index"); } @@ -433,4 +415,10 @@ return true; } + + @Override + public int hashCode() + { + return context. + } } \ No newline at end of file diff -r c3c65308a11b -r abed5bd92fcb src/main/java/jungle/impl/SimpleEditor.java --- a/src/main/java/jungle/impl/SimpleEditor.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleEditor.java Tue Jul 03 18:59:28 2012 +0900 @@ -1,30 +1,29 @@ package jungle.impl; +import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; - import jungle.core.Editor; - import jungle.core.Link; import jungle.core.Tree; import jungle.core.TreeGroup; import jungle.core.TreeNode; import jungle.core.Node; +import jungle.core.graph.Graph; +import jungle.core.graph.Vertex; import jungle.util.Pair; public class SimpleEditor implements Editor { - private final ConcurrentHashMap changeset; - private final TreeGroup group; - private final Tree tree; - private final AtomicReference current; + private final SimpleTreeGroup group; + private final Graph graph; + private AtomicReference current; - public SimpleEditor(TreeGroup _group,Tree _tree) + public SimpleEditor(SimpleTree _tree,Graph _graph) { - tree = _tree; - group = _group; - changeset = new ConcurrentHashMap(); - current = new AtomicReference(); + graph = _graph; + group = (SimpleTreeGroup)_tree.getGroup(); + current = new AtomicReference(_tree); } public Tree commit() @@ -36,10 +35,53 @@ { return null; } - - public Link createLinkAt(TreeNode _target, Tree _linkTarget) + + private boolean checkTreeNode(TreeNode _target) + { + if(_target instanceof SimpleTreeNode){ + SimpleTreeNode converted = (SimpleTreeNode)_target; + return converted.getGroup().equals(group); + } + + return false; + } + + private SimpleTreeNode convertTreeNode(TreeNode _target) + { + if(_target instanceof SimpleTreeNode){ + return (SimpleTreeNode)_target; + } + + return null; + } + + private Vertex cloneTreeNode(Iterable _path) { - return null; + Iterator itr = _path.iterator(); + String newID = group.newTreeID(); + + if(!itr.hasNext()){ + return null; + } + + SimpleTreeNode root = convertTreeNode(itr.next()); + if(root == null){ + return null; + } + + Vertex rootVertex = root.getVertex(); + + + for(TreeNode node : _path){ + if(checkTreeNode(node)){ + } + } + } + + public Link createLinkAt(Iterable _path,TreeNode _target, Tree _linkTarget) + { + + throw new IllegalArgumentException("wrong tree group."); } public void removeLinkAt(TreeNode _target, Link _removeTarget) diff -r c3c65308a11b -r abed5bd92fcb src/test/java/jungle/misc/fj/ImmutableSetExample.java --- a/src/test/java/jungle/misc/fj/ImmutableSetExample.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/test/java/jungle/misc/fj/ImmutableSetExample.java Tue Jul 03 18:59:28 2012 +0900 @@ -1,9 +1,36 @@ package jungle.misc.fj; +import fj.F; +import fj.Ord; +import fj.P2; +import fj.Show; +import fj.data.List; +import fj.data.TreeMap; + public class ImmutableSetExample { public static void main(String _args[]) { + TreeMap map = TreeMap.empty(Ord.stringOrd); + Show>> s = Show.listShow(Show.p2Show(Show.stringShow,Show.stringShow)); + + TreeMap second = map.set("hoge","fuga"); + TreeMap third = second.set("fuga","fuga"); + s.println(List.iterableList(second)); + s.println(List.iterableList(third)); + System.out.println(second.get("hoge").some()); + + F predicate = new F(){ + @Override + public String f(String str){ + return null; + } + }; + + P2> transformResult = third.update("hoge",predicate); + if(transformResult._1()){ + s.println(List.iterableList(transformResult._2())); + } } } diff -r c3c65308a11b -r abed5bd92fcb src/test/java/jungle/misc/fj/XMLNodeTest.java --- a/src/test/java/jungle/misc/fj/XMLNodeTest.java Fri Jun 29 00:03:12 2012 +0900 +++ b/src/test/java/jungle/misc/fj/XMLNodeTest.java Tue Jul 03 18:59:28 2012 +0900 @@ -12,12 +12,18 @@ public static void main(String _args[]) throws Exception { DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document d = b.newDocument(); + Document g = b.newDocument(); - Element e = d.createElement("hoge"); + Element e = g.createElement("hoge"); + g.appendChild(e); + e.appendChild(g.createElement("hoge")); NodeList l1 = e.getElementsByTagName("hoge"); + System.out.println(l1.getLength()); + e.appendChild(g.createElement("hoge")); + e.appendChild(g.createElement("hoge")); NodeList l2 = e.getElementsByTagName("hoge"); - System.out.println(l1.toString()); - System.out.println(l2.toString()); + + System.out.println(l1.getLength()); + System.out.println(l2.getLength()); } }