<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;


import network.aika.callbacks.EventListener;
import network.aika.neuron.Neuron;
import network.aika.neuron.NeuronProvider;
import network.aika.neuron.Range;
import network.aika.neuron.Synapse;
import network.aika.neuron.activation.*;
import network.aika.neuron.steps.Phase;
import network.aika.neuron.steps.QueueKey;
import network.aika.neuron.steps.Step;
import network.aika.neuron.steps.StepType;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @author <NAME>
 */
public abstract class Thought<M extends Model> {

    protected final M model;
    private long absoluteBegin;

    private Timestamp timestampOnProcess = new Timestamp(0);
    private long timestampCounter = 0;
    private int activationIdCounter = 0;

    private final NavigableMap<QueueKey, Step> queue = new TreeMap<>(QueueKey.COMPARATOR);

    private final Set<StepType> filters = new TreeSet<>();

    private final TreeMap<Integer, Activation> activationsById = new TreeMap<>();
    private final Map<NeuronProvider, SortedSet<Activation<?>>> actsPerNeuron = new HashMap<>();
    private final List<EventListener> eventListeners = new ArrayList<>();

    private Config config;


    public Thought(M m) {
        model = m;
        absoluteBegin = m.getN();
        m.setCurrentThought(this);
    }

    public void updateModel() {
        model.addToN(length());
    }

    public M getModel() {
        return model;
    }

    public abstract int length();

    public Config getConfig() {
        return config;
    }

    public void setConfig(Config config) {
        this.config = config;
    }

    public void addFilters(StepType... st) {
        filters.addAll(Set.of(st));
    }

    public void onActivationCreationEvent(Activation act, Synapse originSynapse, Activation originAct) {
        getEventListeners()
                .forEach(
                        el -> el.onActivationCreationEvent(act, originSynapse, originAct)
                );
    }

    public void beforeProcessedEvent(Step s) {
        getEventListeners()
                .forEach(
                        el -> el.beforeProcessedEvent(s)
                );
    }

    public void afterProcessedEvent(Step s) {
        getEventListeners()
                .forEach(
                        el -> el.afterProcessedEvent(s)
                );
    }

    public void onLinkCreationEvent(Link l) {
        getEventListeners()
                .forEach(
                        el -> el.onLinkCreationEvent(l)
                );
    }

    public synchronized Collection<EventListener> getEventListeners() {
        return new ArrayList<>(eventListeners);
    }

    public synchronized void addEventListener(EventListener l) {
        eventListeners.add(l);
    }

    public synchronized void removeEventListener(EventListener l) {
        eventListeners.remove(l);
    }

    public void registerActivation(Activation act) {
        activationsById.put(act.getId(), act);

        Set<Activation<?>> acts = actsPerNeuron
                .computeIfAbsent(
                        act.getNeuronProvider(),
                        n -> new TreeSet<>()
                );
        acts.add(act);
    }

    public void registerBindingSignal(Activation act, BindingSignal bs) {
    }

    public void addStep(Step s) {
        if(filters.contains(s.getStepType()))
            return;

        s.setTimeStamp(getNextTimestamp());
        queue.put(s, s);
    }

    public void removeQueueEntry(Step s) {
        Step removedStep = queue.remove(s);
        assert removedStep != null;
    }
/*
    public void removeQueueEntries(Collection<Step> s) {
        queue.removeAll(s);
    }
*/
    public Collection<Step> getQueue() {
        return queue.values();
    }

    public Range getRange() {
        return new Range(absoluteBegin, absoluteBegin + length());
    }<commit_msg>work on fields<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;


import network.aika.callbacks.EventListener;
import network.aika.neuron.Neuron;
import network.aika.neuron.NeuronProvider;
import network.aika.neuron.Range;
import network.aika.neuron.Synapse;
import network.aika.neuron.activation.*;
import network.aika.neuron.steps.Phase;
import network.aika.neuron.steps.QueueKey;
import network.aika.neuron.steps.Step;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 *
 * @author <NAME>
 */
public abstract class Thought<M extends Model> {

    protected final M model;
    private long absoluteBegin;

    private Timestamp timestampOnProcess = new Timestamp(0);
    private long timestampCounter = 0;
    private int activationIdCounter = 0;

    private final NavigableMap<QueueKey, Step> queue = new TreeMap<>(QueueKey.COMPARATOR);

    private final TreeMap<Integer, Activation> activationsById = new TreeMap<>();
    private final Map<NeuronProvider, SortedSet<Activation<?>>> actsPerNeuron = new HashMap<>();
    private final List<EventListener> eventListeners = new ArrayList<>();

    private Config config;


    public Thought(M m) {
        model = m;
        absoluteBegin = m.getN();
        m.setCurrentThought(this);
    }

    public void updateModel() {
        model.addToN(length());
    }

    public M getModel() {
        return model;
    }

    public abstract int length();

    public Config getConfig() {
        return config;
    }

    public void setConfig(Config config) {
        this.config = config;
    }

    public void onActivationCreationEvent(Activation act, Synapse originSynapse, Activation originAct) {
        callEventListener(el ->
                el.onActivationCreationEvent(act, originSynapse, originAct)
        );
    }

    public void beforeProcessedEvent(Step s) {
        callEventListener(el ->
                el.beforeProcessedEvent(s)
        );
    }

    public void afterProcessedEvent(Step s) {
        callEventListener(el ->
                el.afterProcessedEvent(s)
        );
    }

    private void callEventListener(Consumer<EventListener> el) {
        getEventListeners().forEach(el);
    }

    public void onLinkCreationEvent(Link l) {
        getEventListeners()
                .forEach(
                        el -> el.onLinkCreationEvent(l)
                );
    }

    public synchronized Collection<EventListener> getEventListeners() {
        return new ArrayList<>(eventListeners);
    }

    public synchronized void addEventListener(EventListener l) {
        eventListeners.add(l);
    }

    public synchronized void removeEventListener(EventListener l) {
        eventListeners.remove(l);
    }

    public void registerActivation(Activation act) {
        activationsById.put(act.getId(), act);

        Set<Activation<?>> acts = actsPerNeuron
                .computeIfAbsent(
                        act.getNeuronProvider(),
                        n -> new TreeSet<>()
                );
        acts.add(act);
    }

    public void registerBindingSignal(Activation act, BindingSignal bs) {
    }

    public void addStep(Step s) {
        s.setTimeStamp(getNextTimestamp());
        queue.put(s, s);
    }

    public void removeQueueEntry(Step s) {
        Step removedStep = queue.remove(s);
        assert removedStep != null;
    }

    public Collection<Step> getQueue() {
        return queue.values();
    }

    public Range getRange() {
        return new Range(absoluteBegin, absoluteBegin + length());
    }
{'fraction_non_alphanumeric': 0.09248618784530387, 'fraction_numerical': 0.0015469613259668509, 'mean_word_length': 2.6451872734595248, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 53, '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': '3298869', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2357, 'n_words': 693}