<commit_before>/*******************************************************************************
 * Copyright (c) 2010-2013, <NAME>., Budapest University of Technology and Economics
 * 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 
 *
 * Contributors: 
 *     <NAME>, <NAME>, <NAME> - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import hu.bme.mit.massif.communication.command.MatlabCommand;
import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.communication.datatype.CellMatlabData;
import hu.bme.mit.massif.communication.datatype.Handle;
import hu.bme.mit.massif.communication.datatype.IVisitableMatlabData;
import hu.bme.mit.massif.communication.datatype.MatlabString;
import hu.bme.mit.massif.communication.datatype.StructMatlabData;
import hu.bme.mit.massif.simulink.Block;
import hu.bme.mit.massif.simulink.Property;
import hu.bme.mit.massif.simulink.PropertyType;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;

/**
 * Generic adapter for non-specific blocks. This adapter is used when no adapter is registered for a block type. The
 * process method should be called from every class subclassing it.
 */
public class DefaultBlockAdapter implements IBlockAdapter {

    @Override
    public Block getBlock(Importer traverser) {
        return SimulinkFactory.eINSTANCE.createBlock();
    }

    @Override
    public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
        // The default block processing implementation goes here

        MatlabCommandFactory commandFactory = traverser.getCommandFactory();
        String blockFQN = blockToProcess.getSimulinkRef().getFQN();
        
        List<Property> blockProperties = new LinkedList<Property>();

        MatlabCommand getAllBlockParameters = commandFactory.customCommand("get_all_block_parameters", 1).addParam(blockFQN);
        Map<String, IVisitableMatlabData> blockPropsMap = StructMatlabData.getStructMatlabDataData(getAllBlockParameters.execute());
        
        Set<Entry<String, IVisitableMatlabData>> entries = blockPropsMap.entrySet();
        for (Entry<String, IVisitableMatlabData> entry : entries) {
			IVisitableMatlabData value = entry.getValue();
			
			Property prop = SimulinkFactory.eINSTANCE.createProperty();
			prop.setName(entry.getKey());
			
			if (value == null) {
				// Default: empty string
				prop.setType(PropertyType.STRING_PROPERTY);
				prop.setValue("");				
				blockProperties.add(prop);	
			} else { 
				if(value instanceof MatlabString) {
					prop.setType(PropertyType.STRING_PROPERTY);
					prop.setValue(value.toString());				
					blockProperties.add(prop);
				} else if(value instanceof Handle) {
					prop.setType(PropertyType.DOUBLE_PROPERTY);
					prop.setValue(value.toString());				
					blockProperties.add(prop);
				} else if(value instanceof StructMatlabData) {
					prop.setType(PropertyType.STRING_PROPERTY);
					// TODO set this value and uncomment the following line
					// blockProperties.add(prop);
				} else if(value instanceof CellMatlabData) {
					prop.setType(PropertyType.STRING_PROPERTY);
					// TODO set this value and uncomment the following line
					// blockProperties.add(prop);
				}
			} 
        }
        
        blockToProcess.getProperties().addAll(blockProperties);<commit_msg>Processing params: basic Cell + Struct  #102<commit_after>/*******************************************************************************
 * Copyright (c) 2010-2013, <NAME>., Budapest University of Technology and Economics
 * 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 
 *
 * Contributors: 
 *     <NAME>, <NAME>, <NAME> - initial API and implementation 
 *******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringJoiner;

import hu.bme.mit.massif.communication.command.MatlabCommand;
import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.communication.datatype.CellMatlabData;
import hu.bme.mit.massif.communication.datatype.Handle;
import hu.bme.mit.massif.communication.datatype.IVisitableMatlabData;
import hu.bme.mit.massif.communication.datatype.MatlabString;
import hu.bme.mit.massif.communication.datatype.StructMatlabData;
import hu.bme.mit.massif.simulink.Block;
import hu.bme.mit.massif.simulink.Property;
import hu.bme.mit.massif.simulink.PropertyType;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;

/**
 * Generic adapter for non-specific blocks. This adapter is used when no adapter is registered for a block type. The
 * process method should be called from every class subclassing it.
 */
public class DefaultBlockAdapter implements IBlockAdapter {

    @Override
    public Block getBlock(Importer traverser) {
        return SimulinkFactory.eINSTANCE.createBlock();
    }

    @Override
    public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
        // The default block processing implementation goes here

        MatlabCommandFactory commandFactory = traverser.getCommandFactory();
        String blockFQN = blockToProcess.getSimulinkRef().getFQN();
        
        List<Property> blockProperties = new LinkedList<Property>();

        MatlabCommand getAllBlockParameters = commandFactory.customCommand("get_all_block_parameters", 1).addParam(blockFQN);
        Map<String, IVisitableMatlabData> blockPropsMap = StructMatlabData.getStructMatlabDataData(getAllBlockParameters.execute());
        
        Set<Entry<String, IVisitableMatlabData>> entries = blockPropsMap.entrySet();
        for (Entry<String, IVisitableMatlabData> entry : entries) {
			IVisitableMatlabData value = entry.getValue();
			
			Property prop = SimulinkFactory.eINSTANCE.createProperty();
			prop.setName(entry.getKey());
			
			if (value == null) {
				// Default: empty string
				prop.setType(PropertyType.STRING_PROPERTY);
				prop.setValue("");				
				blockProperties.add(prop);	
			} else { 
				if(value instanceof MatlabString) {
					prop.setType(PropertyType.STRING_PROPERTY);
					prop.setValue(value.toString());				
				} else if(value instanceof Handle) {
					prop.setType(PropertyType.DOUBLE_PROPERTY);
					prop.setValue(value.toString());				
				} else if (value instanceof CellMatlabData) {
					prop.setType(PropertyType.STRING_PROPERTY);
					CellMatlabData cell = (CellMatlabData) value;
					StringJoiner joiner = new StringJoiner(",", "[", "]");
					for (IVisitableMatlabData data : cell.getDatas()) {
						joiner.add(data.toString());
					}
					prop.setValue(joiner.toString());
				} else if(value instanceof StructMatlabData) {
					prop.setType(PropertyType.STRING_PROPERTY);
					StructMatlabData struct = (StructMatlabData) value;
					StringJoiner joiner = new StringJoiner(",", "{", "}");
					for (Entry<String, IVisitableMatlabData> structEntry : struct.getDatas().entrySet()) {
						joiner.add(structEntry.getKey().toString() + ":" + (structEntry.getValue() == null ? "" : structEntry.getValue().toString()));
					}
					prop.setValue(joiner.toString());
				}
				blockProperties.add(prop);
			} 
        }
        
        blockToProcess.getProperties().addAll(blockProperties);
{'fraction_non_alphanumeric': 0.14045559751492265, 'fraction_numerical': 0.0035327080034108903, 'mean_word_length': 4.252719129878439, 'pattern_counts': {'":': 1, '<': 24, '<?xml version=': 0, '>': 24, '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': '265647', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2319, 'n_words': 482}