<commit_before>package xal.app.lossviewer.signals;

import xal.ca.*;
import java.util.*;
import java.util.logging.*;

public class ChargeNormalizer extends AbstractSignal implements ConnectionListener {

    public void connectionMade(Channel channel) {
        if (channel == machineModeChannel) {
            connectMachineMode();
        } else {
            connectBCM(channel);
        }
    }
    private String currentBCM = "";

    private void connectBCM(final Channel channel) {
        //	System.out.println("Connected BCM: "+channel.channelName());
        Monitor m = bcmMonitors.get(channel);
        if (m == null) {
            try {
                m = channel.addMonitorValTime(new IEventSinkValTime() {

                    public void eventValue(ChannelTimeRecord record, Channel chan) {
                        boolean amIcurrentBCM = false;
                        synchronized (currentBCM) {
                            amIcurrentBCM = currentBCM.equals(channel.channelName());
                        }
                        if (amIcurrentBCM) {
                            long tst = (long) (record.getTimestamp().getSeconds() * 1000);
                            double v = record.doubleValue();
                            dispatcher.processNewValue(new ScalarSignalValue(ChargeNormalizer.this, tst, v));
                        //		System.out.println(chan.channelName()+" is used, charge is "+v);
                        }
                    }
                }, 1);
                bcmMonitors.put(channel, m);
            } catch (MonitorException e) {
            } catch (ConnectionException e) {
            }
        }
    }
    
    public  void setBCM(String newBCM){
        synchronized(currentBCM){
            currentBCM=newBCM;
            Logger.getLogger(ChargeNormalizer.this.getClass().getCanonicalName()).log(Level.INFO, ("Normalization BCM changed to " + currentBCM));
        }
    }
    private void connectMachineMode() {
        //	System.out.println("Connected MachineMode");
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.INFO, ("Machine mode connected"));

        if (machineModeMonitor == null) {
            try {
                machineModeMonitor = machineModeChannel.addMonitorValTime(new IEventSinkValTime() {

                    public void eventValue(ChannelTimeRecord record, Channel chan) {
                        
                            String mode = record.stringValue();
                            // set which BCM to use based on machine mode
                            setBCM(bcmChannelNames.get(mode));
                            
                        

                    }
                }, 1);

            } catch (MonitorException e) {
            } catch (ConnectionException e) {
            }
        }


    }

    public void connectionDropped(Channel channel) {
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.WARNING, ("Connection dropped " + channel.channelName()));
    //	System.out.println("Connection dropped: " + getID() + " " + channel.channelName());
    }
    Channel machineModeChannel;
    Monitor machineModeMonitor;
    Map<String, Channel> bcmChannels = new HashMap<String, Channel>();
    Map<Channel, Monitor> bcmMonitors = new HashMap<Channel, Monitor>();
    Map<String, String> bcmChannelNames;
    String machineModeName;

    public ChargeNormalizer(String machineModeName, Map<String, String> bcmNames) {
        this.machineModeName = machineModeName;
        bcmChannelNames = bcmNames;
        setName("Charge");
    }

    public void close() {
        machineModeChannel.disconnect();
        machineModeChannel.flushIO();
        for (String bcmName : bcmChannels.keySet()) {
            bcmChannels.get(bcmName).disconnect();
            bcmChannels.get(bcmName).flushIO();
        }
    }

    /**
     * Method start
     *
     */
    public void start() {

        ChannelFactory cf = ChannelFactory.defaultFactory();
        machineModeChannel = cf.getChannel(machineModeName);
        machineModeChannel.addConnectionListener(this);
        machineModeChannel.requestConnection();
        for (String bcmName : bcmChannelNames.keySet()) {
            Channel ch = cf.getChannel(bcmChannelNames.get(bcmName));
            ch.addConnectionListener(this);
            ch.requestConnection();
            bcmChannels.put(bcmName, ch);
            bcmMonitors.put(ch, null);

        }

    }

    public double doubleValue() {
        return 1.0;
    }

    public boolean setValue(double v) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}<commit_msg>Some clean up in Normalizer code.<commit_after>package xal.app.lossviewer.signals;

import xal.ca.*;
import java.util.*;
import java.util.logging.*;

public class ChargeNormalizer extends AbstractSignal implements ConnectionListener {

    public void connectionMade(Channel channel) {
            connectBCM(channel);
        
    }
    private String currentBCM = "";

    private void connectBCM(final Channel channel) {
        //	System.out.println("Connected BCM: "+channel.channelName());
        Monitor m = bcmMonitors.get(channel);
        if (m == null) {
            try {
                m = channel.addMonitorValTime(new IEventSinkValTime() {

                    public void eventValue(ChannelTimeRecord record, Channel chan) {
                        boolean amIcurrentBCM = false;
                        synchronized (currentBCM) {
                            amIcurrentBCM = currentBCM.equals(channel.channelName());
                        }
                        if (amIcurrentBCM) {
                            long tst = (long) (record.getTimestamp().getSeconds() * 1000);
                            double v = record.doubleValue();
                            dispatcher.processNewValue(new ScalarSignalValue(ChargeNormalizer.this, tst, v));
                        //		System.out.println(chan.channelName()+" is used, charge is "+v);
                        }
                    }
                }, 1);
                bcmMonitors.put(channel, m);
            } catch (MonitorException e) {
            } catch (ConnectionException e) {
            }
        }
    }
    
    public  void setBCM(String newBCM){
        synchronized(currentBCM){
            currentBCM=newBCM;
            Logger.getLogger(ChargeNormalizer.this.getClass().getCanonicalName()).log(Level.INFO, ("Normalization BCM changed to " + currentBCM));
        }
    }
   

    public void connectionDropped(Channel channel) {
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.WARNING, ("Connection dropped " + channel.channelName()));
    //	System.out.println("Connection dropped: " + getID() + " " + channel.channelName());
    }
    Channel machineModeChannel;
    Monitor machineModeMonitor;
    Map<String, Channel> bcmChannels = new HashMap<String, Channel>();
    Map<Channel, Monitor> bcmMonitors = new HashMap<Channel, Monitor>();
    Map<String, String> bcmChannelNames;
    String machineModeName;

    public ChargeNormalizer(String machineModeName, Map<String, String> bcmNames) {
        this.machineModeName = machineModeName;
        bcmChannelNames = bcmNames;
        setName("Charge");
    }

    public void close() {
        machineModeChannel.disconnect();
        machineModeChannel.flushIO();
        for (String bcmName : bcmChannels.keySet()) {
            bcmChannels.get(bcmName).disconnect();
            bcmChannels.get(bcmName).flushIO();
        }
    }

    /**
     * Method start
     *
     */
    public void start() {

        ChannelFactory cf = ChannelFactory.defaultFactory();
        for (String bcmName : bcmChannelNames.keySet()) {
            Channel ch = cf.getChannel(bcmChannelNames.get(bcmName));
            ch.addConnectionListener(this);
            ch.requestConnection();
            bcmChannels.put(bcmName, ch);
            bcmMonitors.put(ch, null);

        }

    }

    public double doubleValue() {
        return 1.0;
    }

    public boolean setValue(double v) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
{'fraction_non_alphanumeric': 0.10389451837382493, 'fraction_numerical': 0.0018312782322060799, 'mean_word_length': 1.9574007220216607, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7060110', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2104, 'n_words': 426}