<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * The scan engine idea is based on the "ScanEngine" developed
 * by the Software Services Group (SSG),  Advanced Photon Source,
 * Argonne National Laboratory,
 * Copyright (c) 2011 , UChicago Argonne, LLC.
 *
 * This implementation, however, contains no SSG "ScanEngine" source code
 * and is not endorsed by the SSG authors.
 ******************************************************************************/
package org.csstudio.scan.device;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.csstudio.scan.ScanSystemPreferences;
import org.csstudio.scan.server.internal.PathStreamTool;

/** Context that maintains {@link Device}s: Create, start, get, stop.
 *
 *  <p>This is currently handling PV devices.
 *  Fundamentally, another type of device might
 *  be added in the future.
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class DeviceContext
{
    /** Map of device names to {@link Device} */
    final private Map<String, Device> devices = new HashMap<String, Device>();

    /** @return Default {@link DeviceContext}, initialized from preferences */
    public static DeviceContext getDefault() throws Exception
    {
    	final String path = ScanSystemPreferences.getBeamlineConfigPath();
        final InputStream config_stream = PathStreamTool.openStream(path);
        final DeviceInfo[] infos = BeamlineDeviceInfoReader.read(config_stream);

        // Create context with those devices
        final DeviceContext context = new DeviceContext();
        for (DeviceInfo info : infos)
            context.addPVDevice(info);
		return context;
    }

    /** Initialize empty device context
     *  @see #getDefault()
     */
    public DeviceContext()
    {
        // NOP
    }

    /** Add a PV-based {@link Device} to the context.
     *  When adding a device with a name
     *  that is already in the context,
     *  the original device will be replaced.
     *
     *  @param info {@link DeviceInfo}
     */
    public synchronized void addPVDevice(final DeviceInfo info) throws Exception
    {
        devices.put(info.getAlias(), new PVDevice(info));
    }

    /** Get a device by alias
     *  @param alias
     *  @return {@link Device} with that name
     *  @throws Exception when device name not known
     */
    public synchronized Device getDeviceByAlias(final String alias) throws Exception
    {
        final Device device = devices.get(alias);
        if (device == null)
            throw new Exception("Unknown device '" + alias + "'");
        return device;
    }

    /** @return All Devices */
    public synchronized Device[] getDevices()
    {
        final Collection<Device> devs = devices.values();
        return devs.toArray(new Device[devs.size()]);
    }

    /** Start all devices
     *  @throws Exception on error with a device
     */
    public synchronized void startDevices() throws Exception
    {
        for (Device device : devices.values())
            device.start();
    }

    /** Stop all devices */
    public synchronized void stopDevices()
    {
        for (Device device : devices.values())<commit_msg>o.c.scan.server: Support write confirmation for alias<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * The scan engine idea is based on the "ScanEngine" developed
 * by the Software Services Group (SSG),  Advanced Photon Source,
 * Argonne National Laboratory,
 * Copyright (c) 2011 , UChicago Argonne, LLC.
 *
 * This implementation, however, contains no SSG "ScanEngine" source code
 * and is not endorsed by the SSG authors.
 ******************************************************************************/
package org.csstudio.scan.device;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.csstudio.scan.ScanSystemPreferences;
import org.csstudio.scan.server.internal.PathStreamTool;

/** Context that maintains {@link Device}s: Create, start, get, stop.
 *
 *  <p>This is currently handling PV devices.
 *  Fundamentally, another type of device might
 *  be added in the future.
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class DeviceContext
{
    /** Map of device names to {@link Device} */
    final private Map<String, Device> devices = new HashMap<String, Device>();

    /** @return Default {@link DeviceContext}, initialized from preferences */
    public static DeviceContext getDefault() throws Exception
    {
    	final String path = ScanSystemPreferences.getBeamlineConfigPath();
        final InputStream config_stream = PathStreamTool.openStream(path);
        final DeviceInfo[] infos = BeamlineDeviceInfoReader.read(config_stream);

        // Create context with those devices
        final DeviceContext context = new DeviceContext();
        for (DeviceInfo info : infos)
            context.addPVDevice(info);
		return context;
    }

    /** Initialize empty device context
     *  @see #getDefault()
     */
    public DeviceContext()
    {
        // NOP
    }

    /** Add a PV-based {@link Device} to the context.
     *  When adding a device with a name
     *  that is already in the context,
     *  the original device will be replaced.
     *
     *  @param info {@link DeviceInfo}
     *  @return Device that was added
     */
    public synchronized Device addPVDevice(final DeviceInfo info) throws Exception
    {
        final PVDevice device = new PVDevice(info);
        devices.put(device.getAlias(), device);
        return device;
    }

    /** Get a device by alias
     *  @param alias
     *  @return {@link Device} with that name
     *  @throws Exception when device name not known
     */
    public synchronized Device getDeviceByAlias(final String alias) throws Exception
    {
        Device device = devices.get(alias);
        if (device != null)
            return device;
        // Name could be 'alias {"putCallback":true}' where
        // the basic alias is known, but without the annotation.
        // Silently add that as a new device, since PVManager treats it as
        // separate channel
        if (alias.endsWith(PVDevice.PUT_CALLBACK_ANNOTATION))
        {
            device = devices.get(alias.substring(0, alias.length() - PVDevice.PUT_CALLBACK_ANNOTATION.length()));
            if (device != null)
                return addPVDevice(new DeviceInfo(device.getName() + PVDevice.PUT_CALLBACK_ANNOTATION, alias));
        }
        // Not a known alias, nor magically added
        throw new Exception("Unknown device '" + alias + "'");
    }

    /** @return All Devices */
    public synchronized Device[] getDevices()
    {
        final Collection<Device> devs = devices.values();
        return devs.toArray(new Device[devs.size()]);
    }

    /** Start all devices
     *  @throws Exception on error with a device
     */
    public synchronized void startDevices() throws Exception
    {
        for (Device device : devices.values())
            device.start();
    }

    /** Stop all devices */
    public synchronized void stopDevices()
    {
        for (Device device : devices.values())
{'fraction_non_alphanumeric': 0.13160239277077765, 'fraction_numerical': 0.0031818760341097113, 'mean_word_length': 3.0071392146863847, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 13, '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': '1699731', 'n_tokens_mistral': 2062, 'n_tokens_neox': 2003, 'n_words': 714}