<commit_before>package org.csstudio.utility.pvmanager.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.csstudio.utility.pvmanager.ui.SWTUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.epics.pvmanager.PVManager;
import org.epics.pvmanager.PVReader;
import org.epics.pvmanager.PVReaderEvent;
import org.epics.pvmanager.PVReaderListener;
import org.epics.pvmanager.PVWriter;
import org.epics.pvmanager.WriteFunction;
import org.epics.pvmanager.expression.DesiredRateExpression;
import org.epics.pvmanager.expression.ReadMap;
import org.epics.pvmanager.expression.ReadWriteMap;
import org.epics.pvmanager.expression.WriteExpression;
import org.epics.pvmanager.expression.WriteMap;
import org.epics.pvmanager.service.ServiceMethod;
import org.epics.pvmanager.service.ServiceRegistry;
import org.epics.util.time.TimeDuration;
import org.epics.vtype.VNumber;

import static org.epics.pvmanager.ExpressionLanguage.*;

/**
 * Basic ui component that can display a VTable on screen.
 * 
 * @author carcassi
 */
public class ServicePanel extends Composite {
	private Text serviceField;
	private Text argumentField;
	private Text resultsField;
	
	private DesiredRateExpression<Map<String, Object>> argumentExpression;
	private WriteExpression<Map<String, Object>> resultExpression;
	private String serviceName;

	/**
	 * Creates a new display.
	 * 
	 * @param parent
	 */
	public ServicePanel(Composite parent) {
		super(parent, SWT.NONE);
		setLayout(new GridLayout(2, false));
		
		Label lblService = new Label(this, SWT.NONE);
		lblService.setText("Service:");
		
		serviceField = new Text(this, SWT.BORDER);
		serviceField.setEditable(false);
		serviceField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblArguments = new Label(this, SWT.NONE);
		lblArguments.setText("Arguments:");
		
		argumentField = new Text(this, SWT.BORDER);
		argumentField.setEditable(false);
		argumentField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblResult = new Label(this, SWT.NONE);
		lblResult.setText("Results:");
		
		resultsField = new Text(this, SWT.BORDER);
		resultsField.setEditable(false);
		resultsField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(this, SWT.NONE);
		
		executeButton = new Button(this, SWT.NONE);
		executeButton.setEnabled(false);
		executeButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Map<String, Object> args = adaptArguments(argReader.getValue());
				System.out.println("Arguments: " + args);
				serviceMethod.execute(args, new WriteFunction<Map<String,Object>>() {
					
					@Override
					public void writeValue(Map<String, Object> newValue) {
						System.out.println("Result: " + newValue);
						resultWriter.write(newValue);
					}
				}, new WriteFunction<Exception>() {

					@Override
					public void writeValue(Exception newValue) {
						// Ignore for now
						System.out.println("Error: " + newValue);
					}
					
				});
			}
		});
		executeButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		executeButton.setText("Execute");
	}
	
	private PVReader<Map<String, Object>> argReader;
	private PVWriter<Map<String, Object>> resultWriter;
	private ServiceMethod serviceMethod;
	private Button executeButton;
	
	private void reconnect() {
		executeButton.setEnabled(false);
		if (argReader != null) {
			argReader.close();
			argReader = null;
		}
		
		if (resultWriter != null) {
			resultWriter.close();
			resultWriter = null;
		}
		
		serviceMethod = null;
		
		if (serviceName == null) {
			return;
		}
		
		try {
			serviceMethod = ServiceRegistry.getDefault().findServiceMethod(serviceName);
		} catch(Exception ex) {
			// Exception should be displayed
			return;
		}
		
		if (argumentExpression == null || resultExpression == null) {
			return;
		}
		
		argReader = PVManager.read(argumentExpression)
				.readListener(new PVReaderListener<Map<String, Object>>() {

					@Override
					public void pvChanged(final PVReaderEvent<Map<String, Object>> event) {
						SWTUtil.swtThread().execute(new Runnable() {
							
							@Override
							public void run() {
								argumentField.setText(String.valueOf(event.getPvReader().getValue()));
							}
						});
					}
				}).maxRate(TimeDuration.ofHertz(25));
		
		resultWriter = PVManager.write(resultExpression).async();
		executeButton.setEnabled(true);
	}

	public DesiredRateExpression<Map<String, Object>> getArgumentExpression() {
		return argumentExpression;
	}
<commit_msg>o.c.u.pvmanager.widgets: ServicePanel displays the method name<commit_after>package org.csstudio.utility.pvmanager.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.csstudio.utility.pvmanager.ui.SWTUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.epics.pvmanager.PVManager;
import org.epics.pvmanager.PVReader;
import org.epics.pvmanager.PVReaderEvent;
import org.epics.pvmanager.PVReaderListener;
import org.epics.pvmanager.PVWriter;
import org.epics.pvmanager.WriteFunction;
import org.epics.pvmanager.expression.DesiredRateExpression;
import org.epics.pvmanager.expression.ReadMap;
import org.epics.pvmanager.expression.ReadWriteMap;
import org.epics.pvmanager.expression.WriteExpression;
import org.epics.pvmanager.expression.WriteMap;
import org.epics.pvmanager.service.ServiceMethod;
import org.epics.pvmanager.service.ServiceRegistry;
import org.epics.util.time.TimeDuration;
import org.epics.vtype.VNumber;

import static org.epics.pvmanager.ExpressionLanguage.*;

/**
 * Basic ui component that can display a VTable on screen.
 * 
 * @author carcassi
 */
public class ServicePanel extends Composite {
	private Text serviceField;
	private Text argumentField;
	private Text resultsField;
	
	private DesiredRateExpression<Map<String, Object>> argumentExpression;
	private WriteExpression<Map<String, Object>> resultExpression;
	private String serviceName;

	/**
	 * Creates a new display.
	 * 
	 * @param parent
	 */
	public ServicePanel(Composite parent) {
		super(parent, SWT.NONE);
		setLayout(new GridLayout(2, false));
		
		Label lblService = new Label(this, SWT.NONE);
		lblService.setText("Service:");
		
		serviceField = new Text(this, SWT.BORDER);
		serviceField.setEditable(false);
		serviceField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblArguments = new Label(this, SWT.NONE);
		lblArguments.setText("Arguments:");
		
		argumentField = new Text(this, SWT.BORDER);
		argumentField.setEditable(false);
		argumentField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblResult = new Label(this, SWT.NONE);
		lblResult.setText("Results:");
		
		resultsField = new Text(this, SWT.BORDER);
		resultsField.setEditable(false);
		resultsField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(this, SWT.NONE);
		
		executeButton = new Button(this, SWT.NONE);
		executeButton.setEnabled(false);
		executeButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Map<String, Object> args = adaptArguments(argReader.getValue());
				System.out.println("Arguments: " + args);
				serviceMethod.execute(args, new WriteFunction<Map<String,Object>>() {
					
					@Override
					public void writeValue(Map<String, Object> newValue) {
						System.out.println("Result: " + newValue);
						resultWriter.write(newValue);
					}
				}, new WriteFunction<Exception>() {

					@Override
					public void writeValue(Exception newValue) {
						// Ignore for now
						System.out.println("Error: " + newValue);
					}
					
				});
			}
		});
		executeButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		executeButton.setText("Execute");
	}
	
	private PVReader<Map<String, Object>> argReader;
	private PVWriter<Map<String, Object>> resultWriter;
	private ServiceMethod serviceMethod;
	private Button executeButton;
	
	private void reconnect() {
		executeButton.setEnabled(false);
		if (argReader != null) {
			argReader.close();
			argReader = null;
		}
		
		if (resultWriter != null) {
			resultWriter.close();
			resultWriter = null;
		}
		
		serviceMethod = null;
		
		if (serviceName == null) {
			serviceField.setText("");
			return;
		}
		
		try {
			serviceMethod = ServiceRegistry.getDefault().findServiceMethod(serviceName);
			serviceField.setText(serviceName);
		} catch(Exception ex) {
			serviceField.setText(ex.getMessage());
			return;
		}
		
		if (argumentExpression == null || resultExpression == null) {
			return;
		}
		
		argReader = PVManager.read(argumentExpression)
				.readListener(new PVReaderListener<Map<String, Object>>() {

					@Override
					public void pvChanged(final PVReaderEvent<Map<String, Object>> event) {
						SWTUtil.swtThread().execute(new Runnable() {
							
							@Override
							public void run() {
								argumentField.setText(String.valueOf(event.getPvReader().getValue()));
							}
						});
					}
				}).maxRate(TimeDuration.ofHertz(25));
		
		resultWriter = PVManager.write(resultExpression).async();
		executeButton.setEnabled(true);
	}

	public DesiredRateExpression<Map<String, Object>> getArgumentExpression() {
		return argumentExpression;
	}

{'fraction_non_alphanumeric': 0.12008690499703734, 'fraction_numerical': 0.0021726249259332413, 'mean_word_length': 4.450484391819161, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6083378', 'n_tokens_mistral': 3639, 'n_tokens_neox': 3087, 'n_words': 612}