<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package getsurface;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import jolie.lang.parse.ast.InputPortInfo;
import jolie.lang.parse.ast.InterfaceDefinition;
import jolie.lang.parse.ast.OneWayOperationDeclaration;
import jolie.lang.parse.ast.OperationDeclaration;
import jolie.lang.parse.ast.OutputPortInfo;
import jolie.lang.parse.ast.RequestResponseOperationDeclaration;
import jolie.lang.parse.ast.types.TypeDefinition;
import jolie.lang.parse.ast.types.TypeDefinitionLink;
import jolie.lang.parse.ast.types.TypeInlineDefinition;
import jolie.lang.parse.util.ProgramInspector;
import jolie.util.Range;

/**
 *
 * @author claudio
 */
public class SurfaceCreator {
    
    private ProgramInspector inspector;
    private URI originalFile;
    private ArrayList<RequestResponseOperationDeclaration> rr_vector;
    private ArrayList<OneWayOperationDeclaration> ow_vector;
    private ArrayList<String> types_vector;
    private ArrayList<TypeDefinition> aux_types_vector;
    private int MAX_CARD = 2147483647;
    private boolean aux_flag = false;

    public SurfaceCreator( ProgramInspector inspector, URI originalFile )
	{

                this.inspector=inspector;
		this.originalFile=originalFile;

	}

    public void ConvertDocument( String inputPortToCreate ) throws Exception {

        ArrayList<InterfaceDefinition> interface_vector = new ArrayList<InterfaceDefinition>();
        rr_vector = new ArrayList<RequestResponseOperationDeclaration>();
        ow_vector = new ArrayList<OneWayOperationDeclaration>();
        types_vector = new ArrayList<String>();
        aux_types_vector = new ArrayList<TypeDefinition>();

        // find inputPort
         InputPortInfo[] inputPortList = inspector.getInputPorts( originalFile );
         InputPortInfo inputPort = null;
         for( InputPortInfo iP : inputPortList ) {
            if ( iP.id().equals( inputPortToCreate ) ) {
                inputPort = iP;
            }
        }
        if ( inputPort == null ) {
            throw( new Exception("Error! inputPort not found!"));
        }

        // extracts the list of all the interfaces to be parsed
        // extracts interfaces declared into Interfaces
        for ( InterfaceDefinition interfaceDefinition:inputPort.getInterfaceList() ) {
            interface_vector.add( interfaceDefinition );
        }
        OutputPortInfo[] outputPortList = inspector.getOutputPorts( originalFile );
        // extracts interfaces from aggregated outputPorts
        for ( int x = 0; x < inputPort.aggregationList().length; x++ ) {
            int i = 0;
            while ( !inputPort.aggregationList()[x].equals( outputPortList[i].id()) ) {
                i++;
            }
            for ( InterfaceDefinition interfaceDefinition:outputPortList[i].getInterfaceList() ) {
                interface_vector.add( interfaceDefinition );
            }
        }

        //  for each interface extract the list of all the available operations and types
        for ( InterfaceDefinition interfaceDefinition: interface_vector ) {
            addOperation( interfaceDefinition );
        }

        // create oputput
        createOutput(inputPort);

    }

    private boolean isNativeType ( String type ) {
        if ( type.equals("any") ||  type.equals("string") ||  type.equals("double") ||  type.equals("int") ||  type.equals("void")) {
            return true;
        } else {
            return false;
        }
    }

    private void addOperation( InterfaceDefinition interfaceDefinition ) {
        for ( OperationDeclaration op: interfaceDefinition.operationsMap().values() ) {
            if ( op instanceof RequestResponseOperationDeclaration ) {
                rr_vector.add( (RequestResponseOperationDeclaration) op );
            } else {
                ow_vector.add( (OneWayOperationDeclaration) op );
            }
        }
    }

    private String getOWString( OneWayOperationDeclaration ow ) {
        String ret = ow.id() + "( " + ow.requestType().id() + " )";
        return ret;
    }

     private String getRRString( RequestResponseOperationDeclaration rr ) {
        String ret = rr.id() + "( " + rr.requestType().id() + " )( " + rr.responseType().id() + " )";
        if ( rr.faults().size() > 0 ) {
            ret = ret + " throws ";
            boolean flag= false;
            for ( Entry<String, TypeDefinition> fault: rr.faults().entrySet() ) {
                if ( flag == false ) {
                    flag = true;
                } else {
                    ret = ret + " ";
                }
                ret = ret + fault.getKey();
                if ( fault.getValue() != null ) {
                    ret = ret + "( " + fault.getValue().id() + " )";
                }
            }
        }
        return ret;
    }

    private String getMax( int max ) {
        if ( max == MAX_CARD ) {
            return "*";
        } else {
            return new Integer( max ).toString();
        }
    }
    private String getCardinality( Range card ) {
        return "[" + card.min() + "," + getMax( card.max() ) + "]";
    }

    private  String getSubType( TypeDefinition type, int indent ) {
        String ret = "";
        for ( int y=0; y < indent; y++ ) {
            ret = ret + "\t";
        }
         ret = ret + "." + type.id() + getCardinality( type.cardinality() ) + ":";
         if ( type instanceof TypeDefinitionLink ) {
                ret = ret + ((TypeDefinitionLink) type).linkedTypeName();
                if ( aux_flag == false ) { aux_types_vector.add( ((TypeDefinitionLink) type).linkedType() ); }
                
            } else {
                ret = ret + type.nativeType().id();
                if ( ((TypeInlineDefinition) type).hasSubTypes() ) {
                    ret = ret + "{ \n";
                    for ( Entry<String, TypeDefinition> entry: ((TypeInlineDefinition) type).subTypes() ) {
                        ret = ret + getSubType( entry.getValue(), indent + 1 ) +"\n";
                    }
                    for ( int y = 0; y < indent; y++ ) {
                        ret = ret + "\t";
                    }
                    ret = ret +"}";
                }
            };
          return ret;
    }

    private String getType( TypeDefinition type ) {
        String ret = "";
        if ( !types_vector.contains( type.id() ) && !isNativeType( type.id() )) {
            System.out.print("type " + type.id() +":" );
            if ( type instanceof TypeDefinitionLink ) {
                System.out.println( ((TypeDefinitionLink) type).linkedTypeName() );
                if ( aux_flag == false ) { aux_types_vector.add( ((TypeDefinitionLink) type).linkedType() ); }
            } else {<commit_msg>minor bug fix<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package getsurface;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import jolie.lang.NativeType;
import jolie.lang.parse.ast.InputPortInfo;
import jolie.lang.parse.ast.InterfaceDefinition;
import jolie.lang.parse.ast.OneWayOperationDeclaration;
import jolie.lang.parse.ast.OperationDeclaration;
import jolie.lang.parse.ast.OutputPortInfo;
import jolie.lang.parse.ast.RequestResponseOperationDeclaration;
import jolie.lang.parse.ast.types.TypeDefinition;
import jolie.lang.parse.ast.types.TypeDefinitionLink;
import jolie.lang.parse.ast.types.TypeInlineDefinition;
import jolie.lang.parse.util.ProgramInspector;
import jolie.util.Range;

/**
 *
 * @author claudio
 */
public class SurfaceCreator {
    
    private ProgramInspector inspector;
    private URI originalFile;
    private ArrayList<RequestResponseOperationDeclaration> rr_vector;
    private ArrayList<OneWayOperationDeclaration> ow_vector;
    private ArrayList<String> types_vector;
    private ArrayList<TypeDefinition> aux_types_vector;
    private int MAX_CARD = 2147483647;
    private boolean aux_flag = false;

    public SurfaceCreator( ProgramInspector inspector, URI originalFile )
	{

                this.inspector=inspector;
		this.originalFile=originalFile;

	}

    public void ConvertDocument( String inputPortToCreate ) throws Exception {

        ArrayList<InterfaceDefinition> interface_vector = new ArrayList<InterfaceDefinition>();
        rr_vector = new ArrayList<RequestResponseOperationDeclaration>();
        ow_vector = new ArrayList<OneWayOperationDeclaration>();
        types_vector = new ArrayList<String>();
        aux_types_vector = new ArrayList<TypeDefinition>();

        // find inputPort
         InputPortInfo[] inputPortList = inspector.getInputPorts( originalFile );
         InputPortInfo inputPort = null;
         for( InputPortInfo iP : inputPortList ) {
            if ( iP.id().equals( inputPortToCreate ) ) {
                inputPort = iP;
            }
        }
        if ( inputPort == null ) {
            throw( new Exception("Error! inputPort not found!"));
        }

        // extracts the list of all the interfaces to be parsed
        // extracts interfaces declared into Interfaces
        for ( InterfaceDefinition interfaceDefinition:inputPort.getInterfaceList() ) {
            interface_vector.add( interfaceDefinition );
        }
        OutputPortInfo[] outputPortList = inspector.getOutputPorts( originalFile );
        // extracts interfaces from aggregated outputPorts
        for ( int x = 0; x < inputPort.aggregationList().length; x++ ) {
            int i = 0;
            while ( !inputPort.aggregationList()[x].equals( outputPortList[i].id()) ) {
                i++;
            }
            for ( InterfaceDefinition interfaceDefinition:outputPortList[i].getInterfaceList() ) {
                interface_vector.add( interfaceDefinition );
            }
        }

        //  for each interface extract the list of all the available operations and types
        for ( InterfaceDefinition interfaceDefinition: interface_vector ) {
            addOperation( interfaceDefinition );
        }

        // create oputput
        createOutput(inputPort);

    }

    private void addOperation( InterfaceDefinition interfaceDefinition ) {
        for ( OperationDeclaration op: interfaceDefinition.operationsMap().values() ) {
            if ( op instanceof RequestResponseOperationDeclaration ) {
                rr_vector.add( (RequestResponseOperationDeclaration) op );
            } else {
                ow_vector.add( (OneWayOperationDeclaration) op );
            }
        }
    }

    private String getOWString( OneWayOperationDeclaration ow ) {
        String ret = ow.id() + "( " + ow.requestType().id() + " )";
        return ret;
    }

     private String getRRString( RequestResponseOperationDeclaration rr ) {
        String ret = rr.id() + "( " + rr.requestType().id() + " )( " + rr.responseType().id() + " )";
        if ( rr.faults().size() > 0 ) {
            ret = ret + " throws ";
            boolean flag= false;
            for ( Entry<String, TypeDefinition> fault: rr.faults().entrySet() ) {
                if ( flag == false ) {
                    flag = true;
                } else {
                    ret = ret + " ";
                }
                ret = ret + fault.getKey();
                if ( fault.getValue() != null ) {
                    ret = ret + "( " + fault.getValue().id() + " )";
                }
            }
        }
        return ret;
    }

    private String getMax( int max ) {
        if ( max == MAX_CARD ) {
            return "*";
        } else {
            return new Integer( max ).toString();
        }
    }
    private String getCardinality( Range card ) {
        return "[" + card.min() + "," + getMax( card.max() ) + "]";
    }

    private  String getSubType( TypeDefinition type, int indent ) {
        String ret = "";
        for ( int y=0; y < indent; y++ ) {
            ret = ret + "\t";
        }
         ret = ret + "." + type.id() + getCardinality( type.cardinality() ) + ":";
         if ( type instanceof TypeDefinitionLink ) {
                ret = ret + ((TypeDefinitionLink) type).linkedTypeName();
                if ( aux_flag == false ) { aux_types_vector.add( ((TypeDefinitionLink) type).linkedType() ); }
                
            } else {
                ret = ret + type.nativeType().id();
                if ( ((TypeInlineDefinition) type).hasSubTypes() ) {
                    ret = ret + "{ \n";
                    for ( Entry<String, TypeDefinition> entry: ((TypeInlineDefinition) type).subTypes() ) {
                        ret = ret + getSubType( entry.getValue(), indent + 1 ) +"\n";
                    }
                    for ( int y = 0; y < indent; y++ ) {
                        ret = ret + "\t";
                    }
                    ret = ret +"}";
                }
            };
          return ret;
    }

    private String getType( TypeDefinition type ) {
        String ret = "";
        if ( !types_vector.contains( type.id() ) && !NativeType.isNative( type.id() )) {
            System.out.print("type " + type.id() +":" );
            if ( type instanceof TypeDefinitionLink ) {
                System.out.println( ((TypeDefinitionLink) type).linkedTypeName() );
                if ( aux_flag == false ) { aux_types_vector.add( ((TypeDefinitionLink) type).linkedType() ); }
            } else {
{'fraction_non_alphanumeric': 0.113423363589403, 'fraction_numerical': 0.002361449339532138, 'mean_word_length': 2.0737128600589703, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 29, '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': '3133545', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3489, 'n_words': 832}