# # Copyright (c) 2003 by Benja Fallenstein # # This file is part of Gzz. # # Gzz is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Gzz is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General # Public License along with Gzz; if not, write to the Free # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, # MA 02111-1307 USA # import gzz, java import jarray from test.tools.gfx import getvs from com.hp.hpl.mesa.rdf import jena model = jena.mem.ModelMem() p = model.createProperty("http://fenfire.org/2003/02/test#", "test-property") n = [model.createResource() for i in range(10)] n[0].addProperty(p, n[1]) n[1].addProperty(p, n[2]) for i in range(3,10): n[2].addProperty(p, n[i]) class Cmp(java.util.Comparator): def compare(self, r1, r2): if (not r1) or (not r2): raise java.lang.NullPointerException("r1=%r, r2=%r" % (r1, r2)) # Nodes are compared by order of index: # n[4] > n[2], n[7] > n[4] etc. return n.index(r1) - n.index(r2) c = gzz.loom.Cursor(Cmp()) def list(set): l = []; i = set.iterator() while i.hasNext(): l.append(i.next()) return l def testGetConnections(): n[6].addProperty(p, n[1]) c.set(n[0], 1, n[1]) assert list(c.getConnections(1)) == [n[1]] assert list(c.getConnections(-1)) == [] c.set(n[1], 1, n[2]) assert list(c.getConnections(1)) == [n[2]] assert list(c.getConnections(-1)) == [n[0], n[6]] c.set(n[2], 1, n[3]) assert list(c.getConnections(1)) == n[3:] assert list(c.getConnections(-1)) == [n[1]] c.set(n[6], 1, n[1]) assert list(c.getConnections(1)) == [n[1]] assert list(c.getConnections(-1)) == [n[2]] def testConnectedTwoWays(): n[5].addProperty(p, n[0]) n[5].addProperty(p, n[2]) c.set(n[2], 1, n[5]) c.move(1) assert c.focus == n[5] and c.getRotationNode(-1) == n[2] assert c.getRotationIndex(-1) == 0 c.rotate(1) assert c.focus == n[5] and c.getRotationNode(1) == n[2] assert c.getRotationIndex(1) == 1 c.rotate(-1) assert c.focus == n[5] and c.getRotationNode(1) == n[0] assert c.getRotationIndex(1) == 0 def testRotationIndex(): c.set(n[1], 1, n[2]) assert c.rotation == 0 assert c.getRotationIndex(1) == 0 assert c.getRotationIndex(-1) == 0 c.set(n[2], 1, n[5]) assert c.rotation == -1 assert c.getRotationIndex(1) == 2 assert c.getRotationIndex(-1) == -1 c.set(n[2], 1, n[9]) assert c.rotation == 3 assert c.getRotationIndex(1) == 6 assert c.getRotationIndex(-1) == 3 c.set(n[4]) assert c.rotation == 0 assert c.getRotationIndex(1) == 0 assert c.getRotationIndex(-1) == 0 def testRotateMoveLeft(): """ > the point in this test is, > in the others we've had a tree > n[0] connected to n[1], n[1] to n[2], n[2] to the others (n[3:]) > so we never had more than one backward conn > in this test, n[5] has three backward conns, and that's what's tested (- benja) """ n[4].addProperty(p, n[5]) n[6].addProperty(p, n[5]) c.set(n[5], -1, n[4]) assert c.getRotationIndex(-1) == 1 assert c.rotation == 0 c.rotate(-1) assert c.getRotationIndex(-1) == 0 assert c.rotation == -1 assert c.getRotationNode(-1) == n[2] c.rotate(-1) assert c.getRotationIndex(-1) == 0 c.rotate(1) assert c.getRotationIndex(-1) == 1 c.rotate(1) assert c.getRotationIndex(-1) == 2 c.rotate(1) assert c.getRotationIndex(-1) == 2 assert c.getRotationNode(-1) == n[6] c.set(n[5]) assert c.rotation == 0 assert c.getRotationIndex(-1) == 1 c.rotate(-1) assert c.rotation == -1 assert c.getRotationIndex(-1) == 0 c.rotate(-1) assert c.rotation == -1 assert c.getRotationIndex(-1) == 0 c.rotate(1) assert c.rotation == 0 assert c.getRotationIndex(-1) == 1 assert c.getRotationNode(-1) == n[4] c.set(n[5]) assert c.getRotationIndex(-1) == 1 assert c.getRotationNode(-1) == n[4] c.move(-1) assert c.focus == n[4] and c.getRotationNode(1) == n[5] def testRotateMove(): c.set(n[2], 1, n[5]) c.rotate(1) assert c.focus == n[2] and c.getRotationNode(1) == n[6] c.rotate(1) assert c.focus == n[2] and c.getRotationNode(1) == n[7] c.rotate(-1) assert c.focus == n[2] and c.getRotationNode(1) == n[6] c.set(n[2], 1, n[9]) c.rotate(1) assert c.focus == n[2] and c.getRotationNode(1) == n[9] c.rotate(-1) assert c.focus == n[2] and c.getRotationNode(1) == n[8] c.rotate(1) assert c.focus == n[2] and c.getRotationNode(1) == n[9] c.rotate(1) assert c.focus == n[2] and c.getRotationNode(1) == n[9] c.set(n[2], -1, n[1]) assert c.rotation == 0 assert c.getRotationIndex(-1) == 0 assert c.getRotationIndex(1) == 3 c.rotate(1) assert c.rotation == 1 assert c.getRotationIndex(1) == 4 c.rotate(1) assert c.rotation == 2 assert c.getRotationIndex(1) == 5 c.move(-1) assert c.focus == n[2] and c.getRotationIndex(1) == 5 assert c.getRotationNode(1) == n[8] c.move(1) assert c.focus == n[8] and c.rotation == 0 and \ c.getRotationNode(-1) == n[2] c.set(n[2], 1, n[3]) c.rotate(-1) assert c.focus == n[2] and c.getRotationNode(1) == n[3] c.set(n[2], 1, n[6]) c.move(-1) assert c.focus == n[1] and c.getRotationNode(1) == n[2] c.move(-1) assert c.focus == n[0] and c.getRotationNode(1) == n[1] c.move(1) assert c.focus == n[1] and c.getRotationNode(-1) == n[0] assert c.getRotationNode(1) == n[2] c.move(1) assert c.focus == n[2] and c.getRotationNode(-1) == n[1] assert c.getRotationNode(1) == n[6] # if we cannot move there, we remain where we are: c.set(n[2], 1, n[7]) c.move(-1) assert c.focus == n[2] and c.getRotationNode(1) == n[7] c.set(n[0], 1, n[1]) c.move(-1) assert c.focus == n[0] and c.getRotationNode(1) == n[1] def testMoveOnLiteral(): """ Trying to move onto a literal should do nothing. """ n[8].addProperty(p, "foo"); # literal c.set(n[8]) assert c.focus == n[8] and c.rotation == 0 c.move(1) assert c.focus == n[8] and c.rotation == 0 def testGetRotationNode(): c.set(n[2]) assert c.getRotationNode(-1) == n[1] assert c.getRotationNode(0) == n[2] assert c.getRotationNode(1) == n[6] c.rotation = 3 assert c.getRotationNode(-1) == None assert c.getRotationNode(0) == n[2] assert c.getRotationNode(1) == n[9] c.rotation = -2 assert c.getRotationNode(-1) == None assert c.getRotationNode(0) == n[2] assert c.getRotationNode(1) == n[4]