<commit_before>/*
 * 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.neuron.activation;

import network.aika.Config;
import network.aika.Thought;
import network.aika.neuron.activation.visitor.ActVisitor;
import network.aika.neuron.activation.visitor.LinkVisitor;
import network.aika.utils.Utils;
import network.aika.neuron.sign.Sign;
import network.aika.neuron.Synapse;
import network.aika.neuron.activation.direction.Direction;
import network.aika.neuron.steps.VisitorStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Comparator;

import static network.aika.callbacks.VisitorEvent.AFTER;
import static network.aika.callbacks.VisitorEvent.BEFORE;
import static network.aika.neuron.activation.direction.Direction.INPUT;
import static network.aika.neuron.activation.direction.Direction.OUTPUT;
import static network.aika.neuron.sign.Sign.POS;

/**
 *
 * @author <NAME>
 */
public class Link extends Element<Link> {

    private static final Logger log = LoggerFactory.getLogger(Link.class);

    public static final Comparator<Link> COMPARE = Comparator.
            <Link, Activation>comparing(l -> l.output)
            .thenComparing(l -> l.input);

    private Synapse synapse;

    private Activation input;
    private Activation output;

    private boolean isSelfRef;

    private double lastIGGradient;

    public Link(Synapse s, Activation input, Activation output, boolean isSelfRef, LinkVisitor v) {
        this.synapse = s;
        this.input = input;
        this.output = output;
        this.isSelfRef = isSelfRef;

        if(input != null)
            linkInput();

        if(output != null)
            linkOutput();

        getSynapse().updateReference(this);

        if(v != null)
            v.setLink(this);

        getThought().onLinkCreationEvent(this, v);
    }

    @Override
    public Fired getFired() {
        return output.getFired();
    }

    public static boolean synapseExists(Activation iAct, Activation oAct) {
        return Synapse.synapseExists(iAct.getNeuron(), oAct.getNeuron());
    }

    public static boolean linkExists(Activation iAct, Activation oAct) {
        return iAct.outputLinkExists(oAct);
    }

    public static boolean linkExists(Synapse s, Activation iAct, Activation oAct) {
        Link ol = oAct.getInputLink(iAct.getNeuron());
        if (ol != null) {
            assert s == ol.getSynapse();
//                    toAct = oAct.cloneToReplaceLink(s);
            log.warn("Link already exists! ");
            return true;
        }
        return false;
    }

    public static Synapse getSynapse(Activation iAct, Activation oAct) {
        return oAct.getNeuron()
                .getInputSynapse(
                        iAct.getNeuronProvider()
                );
    }

    public void count() {
        if(synapse != null)
            synapse.count(this);
    }

    public void follow(VisitorStep p) {
        follow(p,
                synapse.isRecurrent() ? OUTPUT : INPUT
        );
    }

    public void follow(VisitorStep p, Direction startDir) {
        Activation startAct = startDir.invert().getActivation(this);
        ActVisitor v = new ActVisitor(p, startAct, startDir, startDir);

        startAct.setMarked(true);
        follow(v);
        startAct.setMarked(false);
    }

    public void follow(ActVisitor v) {
        LinkVisitor lv = synapse.transition(v, this);
        if(lv == null)<commit_msg>attempt to link neg. binding syn.<commit_after>/*
 * 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.neuron.activation;

import network.aika.Config;
import network.aika.Thought;
import network.aika.neuron.activation.visitor.ActVisitor;
import network.aika.neuron.activation.visitor.LinkVisitor;
import network.aika.utils.Utils;
import network.aika.neuron.sign.Sign;
import network.aika.neuron.Synapse;
import network.aika.neuron.activation.direction.Direction;
import network.aika.neuron.steps.VisitorStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Comparator;

import static network.aika.callbacks.VisitorEvent.AFTER;
import static network.aika.callbacks.VisitorEvent.BEFORE;
import static network.aika.neuron.activation.direction.Direction.INPUT;
import static network.aika.neuron.activation.direction.Direction.OUTPUT;
import static network.aika.neuron.sign.Sign.POS;

/**
 *
 * @author <NAME>
 */
public class Link extends Element<Link> {

    private static final Logger log = LoggerFactory.getLogger(Link.class);

    public static final Comparator<Link> COMPARE = Comparator.
            <Link, Activation>comparing(l -> l.output)
            .thenComparing(l -> l.input);

    private Synapse synapse;

    private Activation input;
    private Activation output;

    private boolean isSelfRef;

    private double lastIGGradient;

    public Link(Synapse s, Activation input, Activation output, boolean isSelfRef, LinkVisitor v) {
        this.synapse = s;
        this.input = input;
        this.output = output;
        this.isSelfRef = isSelfRef;

        if(input != null)
            linkInput();

        if(output != null)
            linkOutput();

        getSynapse().updateReference(this);

        if(v != null)
            v.setLink(this);

        getThought().onLinkCreationEvent(this, v);
    }

    @Override
    public Fired getFired() {
        return output.getFired();
    }

    public static boolean synapseExists(Activation iAct, Activation oAct) {
        return Synapse.synapseExists(iAct.getNeuron(), oAct.getNeuron());
    }

    public static boolean linkExists(Activation iAct, Activation oAct) {
        return iAct.outputLinkExists(oAct);
    }

    public static boolean linkExists(Synapse s, Activation iAct, Activation oAct) {
        Link ol = oAct.getInputLink(iAct.getNeuron());
        if (ol != null) {
            assert s == ol.getSynapse();
//                    toAct = oAct.cloneToReplaceLink(s);
            log.warn("Link already exists! ");
            return true;
        }
        return false;
    }

    public static Synapse getSynapse(Activation iAct, Activation oAct) {
        return oAct.getNeuron()
                .getInputSynapse(
                        iAct.getNeuronProvider()
                );
    }

    public void count() {
        if(synapse != null)
            synapse.count(this);
    }

    public void follow(VisitorStep p) {
        if(!synapse.isRecurrent())
            follow(p, INPUT, INPUT);

        if(output.isActive(false))
            follow(p, OUTPUT, synapse.isRecurrent() ? OUTPUT : INPUT);
    }

    public void follow(VisitorStep p, Direction startDir, Direction currentDir) {
        Activation startAct = currentDir.invert().getActivation(this);
        ActVisitor v = new ActVisitor(p, startAct, startDir, currentDir);

        startAct.setMarked(true);
        follow(v);
        startAct.setMarked(false);
    }

    public void follow(ActVisitor v) {
        LinkVisitor lv = synapse.transition(v, this);
        if(lv == null)
{'fraction_non_alphanumeric': 0.09420035149384885, 'fraction_numerical': 0.00140597539543058, 'mean_word_length': 2.8659420289855073, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '526976', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2355, 'n_words': 672}