/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aika.network;


import org.aika.Model;
import org.aika.SuspensionHook;
import org.aika.corpus.Document;
import org.aika.neuron.InputNeuron;
import org.junit.Test;

import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author Lukas Molzberger
 */
public class SuspensionTest {


    @Test
    public void testSuspendInputNeuron() {
        Model m = new Model();
        m.suspensionHook = new DummySuspensionHook();

        InputNeuron n = m.createOrLookupInputNeuron("A");
        n.suspend(m);

        n = m.createOrLookupInputNeuron("A");

        Document doc = m.createDocument("Bla");
        n.addInput(doc, 0, 1);
    }



    public static class DummySuspensionHook implements SuspensionHook {

        Map<Key, byte[]> storage = new TreeMap<>();

        @Override
        public void store(long id, Type t, byte[] data) {
            storage.put(new Key(id, t), data);
        }

        @Override
        public byte[] retrieve(long id, Type t) {
            return storage.get(new Key(id, t));
        }

        private static class Key implements Comparable<Key> {
            public Key(long id, Type t) {
                this.id = id;
                this.t = t;
            }

            long id;
            Type t;

            @Override
            public int compareTo(Key k) {
                int r = Long.compare(id, k.id);
                if(r != 0) return r;
                return t.compareTo(k.t);
            }
        }
    }
}

@@ -823,16 +823,39 @@
 work;%0A%0A%0A
+import org.aika.Input;%0A
 import o
@@ -869,16 +869,16 @@
 .Model;%0A
-
 import o
@@ -934,16 +934,46 @@
 cument;%0A
+import org.aika.corpus.Range;%0A
 import o
@@ -1000,16 +1000,72 @@
 Neuron;%0A
+import org.aika.neuron.Neuron;%0Aimport org.junit.Assert;%0A
 import o
@@ -1129,16 +1129,70 @@
 eeMap;%0A%0A
+import static org.aika.corpus.Range.Operator.EQUALS;%0A%0A
 /**%0A *%0A 
@@ -1604,16 +1604,16 @@
 1);%0A
-
     %7D%0A%0A%0A
 %0A   
@@ -1608,16 +1608,1812 @@
     %7D%0A%0A%0A
+    @Test%0A    public void testSuspendAndNeuron() %7B%0A        Model m = new Model();%0A        m.suspensionHook = new DummySuspensionHook();%0A%0A        InputNeuron inA = m.createOrLookupInputNeuron(%22A%22);%0A        InputNeuron inB = m.createOrLookupInputNeuron(%22B%22);%0A%0A        Neuron nC = m.createAndNeuron(new Neuron(%22C%22), 0.5,%0A                new Input()%0A                        .setNeuron(inA)%0A                        .setWeight(10.0)%0A                        .setMinInput(0.9)%0A                        .setRelativeRid(0)%0A                        .setRecurrent(false)%0A                        .setStartRangeMatch(EQUALS)%0A                        .setStartRangeOutput(true),%0A                new Input()%0A                        .setNeuron(inB)%0A                        .setWeight(10.0)%0A                        .setMinInput(0.9)%0A                        .setRelativeRid(null)%0A                        .setRecurrent(false)%0A                        .setEndRangeMatch(EQUALS)%0A                        .setEndRangeOutput(true)%0A        );%0A%0A%0A        Neuron outD = m.createAndNeuron(new Neuron(%22D%22), 0.5,%0A                new Input()%0A                        .setNeuron(nC)%0A                        .setWeight(10.0)%0A                        .setMinInput(0.9)%0A                        .setRecurrent(false)%0A                        .setRangeMatch(Input.RangeRelation.EQUALS)%0A                        .setRangeOutput(true)%0A        );%0A%0A%0A//        inA.suspend(m);%0A//        inB.suspend(m);%0A%0A%0A        // Reactivate%0A%0A        Document doc = m.createDocument(%22Bla%22);%0A%0A        inA = m.createOrLookupInputNeuron(%22A%22);%0A        inA.addInput(doc, 0, 1, 0);%0A%0A        inB = m.createOrLookupInputNeuron(%22B%22);%0A        inB.addInput(doc, 1, 2, 1);%0A%0A        doc.process();%0A%0A        Assert.assertFalse(outD.getFinalActivations(doc).isEmpty());%0A    %7D%0A%0A%0A%0A
 %0A    pub

work on a suspension test case