diff --git a/src/main/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandler.java b/src/main/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandler.java index 536a93f73ccbe02b8d940c25bb9a2d7ce98b0380..f232c1a1cb1c81665a87af27ce8d331000879e14 100644 --- a/src/main/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandler.java +++ b/src/main/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandler.java @@ -31,8 +31,7 @@ public class TypedAdjacencyMethodHandler implements MethodHandler<TypedAdjacency throw new IllegalStateException("element is not a type of Vertex " + element.getClass().getName()); final Vertex vertex = (Vertex) element; - if(annotation.label() == null) - throw new IllegalStateException("method " + method.getName() + " label must be specified on @TypedAdjacency annotation"); + assert annotation.label() != null; if( ClassUtilities.isAddMethod(method) ) { if( arguments == null ) diff --git a/src/test/java/com/syncleus/grail/graph/ExceptionGod.java b/src/test/java/com/syncleus/grail/graph/ExceptionGod.java new file mode 100644 index 0000000000000000000000000000000000000000..7d3ad424d5ba4fb241f70bbcd1c27ddaca801bfc --- /dev/null +++ b/src/test/java/com/syncleus/grail/graph/ExceptionGod.java @@ -0,0 +1,36 @@ +package com.syncleus.grail.graph; + +import com.tinkerpop.blueprints.Direction; +import com.tinkerpop.frames.Property; +import com.tinkerpop.frames.modules.typedgraph.*; + +@TypeField("classType") +@TypeValue("God") +public interface ExceptionGod { + @Property("name") + String getName(); + + @TypedAdjacency(label="") + <N extends ExceptionGod> Iterable<? extends N> getNoLabel(Class<? extends N> type); + + @TypedAdjacency(label="Father", direction = Direction.IN) + <N extends ExceptionGod> Iterable<? extends N> getSons(); + + @TypedAdjacency(label="Father", direction = Direction.IN) + <N extends ExceptionGod> Iterable<? extends N> getSons(Class<? extends N> type, String badStuff); + + @TypedAdjacency(label="Father", direction = Direction.IN) + <N extends ExceptionGod> Iterable<? extends N> getSons(String badStuff); + + @TypedAdjacency(label="Father", direction = Direction.IN) + <N extends ExceptionGod> Iterable<? extends N> badSons(Class<? extends N> type); + + @TypedIncidence(label="") + <N extends FatherEdge> Iterable<? extends N> getNoLabelEdges(Class<? extends N> type); + + @TypedIncidence(label="Father", direction = Direction.IN) + <N extends FatherEdge> Iterable<? extends N> getSonEdges(Class<? extends N> type); + + @TypedIncidence(label="Father", direction = Direction.IN) + <N extends FatherEdge> Iterable<? extends N> badSonEdges(Class<? extends N> type); +} diff --git a/src/test/java/com/syncleus/grail/graph/titangraph/TypedAdjacencyHandlerTest.java b/src/test/java/com/syncleus/grail/graph/titangraph/TypedAdjacencyHandlerTest.java index 335e68c82bc7d2f5af5c4667edabe1125469e191..6bd6484e5fe67f696833768f9222544a324eb50c 100644 --- a/src/test/java/com/syncleus/grail/graph/titangraph/TypedAdjacencyHandlerTest.java +++ b/src/test/java/com/syncleus/grail/graph/titangraph/TypedAdjacencyHandlerTest.java @@ -10,6 +10,7 @@ import java.util.*; public class TypedAdjacencyHandlerTest { private static final Set<Class<?>> TEST_TYPES = new HashSet<Class<?>>(Arrays.asList(new Class<?>[]{God.class, FatherEdge.class, GodExtended.class})); + private static final Set<Class<?>> EXCEPTION_TEST_TYPES = new HashSet<Class<?>>(Arrays.asList(new Class<?>[]{ExceptionGod.class, FatherEdge.class, GodExtended.class})); @Test public void testGetSons() { @@ -28,6 +29,81 @@ public class TypedAdjacencyHandlerTest { Assert.assertTrue(child instanceof GodExtended); } + @Test + public void testGetSonsNoLabel() { + final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); + final FramedGraphFactory factory = new GrailGraphFactory(Collections.<Module>emptyList(), TypedAdjacencyHandlerTest.EXCEPTION_TEST_TYPES); + + final FramedGraph<?> framedGraph = factory.create(godGraph); + + final Iterable<ExceptionGod> gods = (Iterable<ExceptionGod>) framedGraph.getVertices("name", "jupiter", ExceptionGod.class); + final ExceptionGod father = gods.iterator().next(); + Assert.assertEquals(father.getName(), "jupiter"); + + final Iterable<? extends ExceptionGod> children = father.getNoLabel(ExceptionGod.class); + Assert.assertTrue(!children.iterator().hasNext()); + } + + @Test(expected = IllegalStateException.class) + public void testGetSonsNoArgument() { + final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); + final FramedGraphFactory factory = new GrailGraphFactory(Collections.<Module>emptyList(), TypedAdjacencyHandlerTest.EXCEPTION_TEST_TYPES); + + final FramedGraph<?> framedGraph = factory.create(godGraph); + + final Iterable<ExceptionGod> gods = (Iterable<ExceptionGod>) framedGraph.getVertices("name", "jupiter", ExceptionGod.class); + final ExceptionGod father = gods.iterator().next(); + Assert.assertEquals(father.getName(), "jupiter"); + + final Iterable<? extends ExceptionGod> children = father.getSons(); + Assert.assertTrue(!children.iterator().hasNext()); + } + + @Test(expected = IllegalStateException.class) + public void testGetSonsExtraArgument() { + final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); + final FramedGraphFactory factory = new GrailGraphFactory(Collections.<Module>emptyList(), TypedAdjacencyHandlerTest.EXCEPTION_TEST_TYPES); + + final FramedGraph<?> framedGraph = factory.create(godGraph); + + final Iterable<ExceptionGod> gods = (Iterable<ExceptionGod>) framedGraph.getVertices("name", "jupiter", ExceptionGod.class); + final ExceptionGod father = gods.iterator().next(); + Assert.assertEquals(father.getName(), "jupiter"); + + final Iterable<? extends ExceptionGod> children = father.getSons(ExceptionGod.class, "bad stuff will now happen"); + Assert.assertTrue(!children.iterator().hasNext()); + } + + @Test(expected = IllegalStateException.class) + public void testGetSonsWrongArgument() { + final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); + final FramedGraphFactory factory = new GrailGraphFactory(Collections.<Module>emptyList(), TypedAdjacencyHandlerTest.EXCEPTION_TEST_TYPES); + + final FramedGraph<?> framedGraph = factory.create(godGraph); + + final Iterable<ExceptionGod> gods = (Iterable<ExceptionGod>) framedGraph.getVertices("name", "jupiter", ExceptionGod.class); + final ExceptionGod father = gods.iterator().next(); + Assert.assertEquals(father.getName(), "jupiter"); + + final Iterable<? extends ExceptionGod> children = father.getSons("bad stuff will now happen"); + Assert.assertTrue(!children.iterator().hasNext()); + } + + @Test(expected = IllegalStateException.class) + public void testGetSonsWrongPrefix() { + final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); + final FramedGraphFactory factory = new GrailGraphFactory(Collections.<Module>emptyList(), TypedAdjacencyHandlerTest.EXCEPTION_TEST_TYPES); + + final FramedGraph<?> framedGraph = factory.create(godGraph); + + final Iterable<ExceptionGod> gods = (Iterable<ExceptionGod>) framedGraph.getVertices("name", "jupiter", ExceptionGod.class); + final ExceptionGod father = gods.iterator().next(); + Assert.assertEquals(father.getName(), "jupiter"); + + final Iterable<? extends ExceptionGod> children = father.badSons(ExceptionGod.class); + Assert.assertTrue(!children.iterator().hasNext()); + } + @Test public void testGetSonsBuiltIn() { final TitanGraph godGraph = TitanGods.create("./target/TitanTestDB"); diff --git a/src/test/java/com/syncleus/grail/neural/backprop/SimpleXor3InputTest.java b/src/test/java/com/syncleus/grail/neural/backprop/SimpleXor3InputTest.java index 9e48852dc9bbb252703a1c76c3cf1d1e87af3ca5..b344e35fac32f3f767581c61d3dd44f51fb54e44 100644 --- a/src/test/java/com/syncleus/grail/neural/backprop/SimpleXor3InputTest.java +++ b/src/test/java/com/syncleus/grail/neural/backprop/SimpleXor3InputTest.java @@ -8,7 +8,7 @@ import org.junit.*; import java.util.*; public class SimpleXor3InputTest { - private static final ActivationFunction activationFunction = new SineActivationFunction(); + private static final ActivationFunction activationFunction = new HyperbolicTangentActivationFunction(); @Test public void testXor() { @@ -44,7 +44,7 @@ public class SimpleXor3InputTest { graph.commit(); for(int i = 0; i < 10000 ; i++) { - SimpleXor3InputTest.train(graph, 1.0, 1.0, 1.0, -1.0); + SimpleXor3InputTest.train(graph, 1.0, 1.0, 1.0, 1.0); SimpleXor3InputTest.train(graph, -1.0, 1.0, 1.0, -1.0); SimpleXor3InputTest.train(graph, 1.0, -1.0, 1.0, -1.0); SimpleXor3InputTest.train(graph, 1.0, 1.0, -1.0, -1.0); @@ -55,7 +55,7 @@ public class SimpleXor3InputTest { if( i%50 == 0 && SimpleXor3InputTest.calculateError(graph) < 0.1 ) break; } - Assert.assertTrue(SimpleXor3InputTest.propagate(graph, 1.0, 1.0, 1.0) < 0.0); + Assert.assertTrue(SimpleXor3InputTest.propagate(graph, 1.0, 1.0, 1.0) > 0.0); Assert.assertTrue(SimpleXor3InputTest.propagate(graph, -1.0, 1.0, 1.0) < 0.0); Assert.assertTrue(SimpleXor3InputTest.propagate(graph, 1.0, -1.0, 1.0) < 0.0); Assert.assertTrue(SimpleXor3InputTest.propagate(graph, 1.0, 1.0, -1.0) < 0.0); @@ -67,7 +67,7 @@ public class SimpleXor3InputTest { private static double calculateError(FramedTransactionalGraph<?> graph) { double actual = SimpleXor3InputTest.propagate(graph, 1.0, 1.0, 1.0); - double error = Math.abs(actual + 1.0) / Math.abs(actual); + double error = Math.abs(actual - 1.0) / 2.0; actual = SimpleXor3InputTest.propagate(graph, -1.0, 1.0, 1.0); error += Math.abs(actual + 1.0) / 2.0;