/*
 * 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 network.aika.text;

import network.aika.neuron.activation.Activation;
import network.aika.neuron.activation.BindingSignal;
import network.aika.neuron.activation.InhibitoryActivation;
import network.aika.neuron.activation.PatternActivation;
import network.aika.neuron.activation.direction.Direction;
import network.aika.neuron.excitatory.PatternNeuron;
import network.aika.neuron.inhibitory.InhibitoryNeuron;

import java.util.Optional;

import static network.aika.neuron.activation.direction.Direction.INPUT;
import static network.aika.neuron.activation.direction.Direction.OUTPUT;


/**
 *
 * @author Lukas Molzberger
 */
public class TokenActivation extends PatternActivation {

    private int rangeBegin;
    private int rangeEnd;
    private TokenActivation previousToken;
    private TokenActivation nextToken;


    public TokenActivation(int id, int begin, int end, Document doc, PatternNeuron patternNeuron) {
        super(id, doc, patternNeuron);
        rangeBegin = begin;
        rangeEnd = end;
    }

    public static void addRelation(TokenActivation prev, TokenActivation next) {
        if(prev == null || next == null)
            return;

        prev.nextToken = next;
        next.previousToken = prev;

        TextModel model = (TextModel) prev.getModel();
        InhibitoryActivation inhibActPrev = next.getInhibTokenAct(model.getPrevTokenInhib());
        InhibitoryActivation inhibActNext = prev.getInhibTokenAct(model.getNextTokenInhib());


    }


    private InhibitoryActivation getInhibTokenAct(InhibitoryNeuron inhibitoryNeuron) {
        return (InhibitoryActivation) reverseBindingSignals
                .values()
                .stream()
                .map(bs -> bs.getCurrentAct())
                .filter(act -> act.getNeuron() == inhibitoryNeuron)
                .findFirst()
                .orElse(null);
    }

    public TokenActivation getPreviousToken() {
        return previousToken;
    }

    public TokenActivation getNextToken() {
        return nextToken;
    }

    @Override
    public int[] getRange() {
        return new int[]{rangeBegin, rangeEnd};
    }
}

@@ -854,29 +854,50 @@
 ron.
-activation.Activation
+Neuron;%0Aimport network.aika.neuron.Synapse
 ;%0Aim
@@ -932,29 +932,17 @@
 ivation.
-BindingSignal
+*
 ;%0Aimport
@@ -973,32 +973,31 @@
 ivation.
-InhibitoryActiva
+direction.Direc
 tion;%0Aim
@@ -1025,34 +1025,30 @@
 ron.
-activation.PatternActivati
+excitatory.BindingNeur
 on;%0A
@@ -1078,36 +1078,33 @@
 ron.
-activation.direction.Directi
+excitatory.ExcitatoryNeur
 on;%0A
@@ -1148,32 +1148,88 @@
 .PatternNeuron;%0A
+import network.aika.neuron.excitatory.RelatedBNSynapse;%0A
 import network.a
@@ -1442,16 +1442,89 @@
 OUTPUT;%0A
+import static network.aika.neuron.linker.LinkingTask.addNextLinkerSteps;%0A
 %0A%0A/**%0A *
@@ -1942,32 +1942,379 @@
 d = end;%0A    %7D%0A%0A
+    private static void addLink(Synapse s, Activation iAct, Activation oAct) %7B%0A        Link nl = oAct.addLink(s, iAct, false);%0A%0A        addNextLinkerSteps(nl);%0A    %7D%0A%0A    private static RelatedBNSynapse getRelSynapse(BindingNeuron n, InhibitoryNeuron relInhib) %7B%0A        return (RelatedBNSynapse) n.getInputSynapse(relInhib.getProvider());%0A    %7D%0A%0A
     public stati
@@ -2562,16 +2562,17 @@
 odel();%0A
+%0A
         
@@ -2604,37 +2604,205 @@
 bAct
-Prev = next.getInhibTokenAct(
+Next = prev.getInhibTokenAct(model.getNextTokenInhib());%0A        BindingActivation relActNext = getRelAct(inhibActNext);%0A        RelatedBNSynapse relSynNext = getRelSynapse(relActNext.getNeuron(), 
 mode
@@ -2817,32 +2817,33 @@
 vTokenInhib());%0A
+%0A
         Inhibito
@@ -2859,35 +2859,35 @@
 ion inhibAct
-Next = prev
+Prev = next
 .getInhibTok
@@ -2905,35 +2905,563 @@
 .get
-NextTokenInhib());%0A%0A%0A    %7D%0A
+PrevTokenInhib());%0A        BindingActivation relActPrev = getRelAct(inhibActPrev);%0A        RelatedBNSynapse relSynPrev = getRelSynapse(relActPrev.getNeuron(), model.getNextTokenInhib());%0A%0A        addLink(relSynNext, inhibActPrev, relActNext);%0A        addLink(relSynPrev, inhibActNext, relActPrev);%0A    %7D%0A%0A    private static BindingActivation getRelAct(InhibitoryActivation inhibAct) %7B%0A        return (BindingActivation) inhibAct%0A                .getInputLinks()%0A                .findFirst()%0A                .orElse(null)%0A                .getInput();%0A    %7D
 %0A%0A  

work on relation links