<commit_before>/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015  
 */
package com.ibm.streamsx.topology.generator.spl;

import static com.ibm.streamsx.topology.builder.JParamTypes.TYPE_SUBMISSION_PARAMETER;
import static com.ibm.streamsx.topology.generator.spl.GsonUtilities.jstring;
import static com.ibm.streamsx.topology.generator.spl.GsonUtilities.objectArray;
import static com.ibm.streamsx.topology.generator.spl.SPLGenerator.splBasename;
import static com.ibm.streamsx.topology.internal.functional.ops.SubmissionParameterManager.NAME_SUBMISSION_PARAM_NAMES;
import static com.ibm.streamsx.topology.internal.functional.ops.SubmissionParameterManager.NAME_SUBMISSION_PARAM_VALUES;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
import com.ibm.streams.operator.window.StreamWindow;
import com.ibm.streams.operator.window.StreamWindow.Type;
import com.ibm.streamsx.topology.builder.JOperator;
import com.ibm.streamsx.topology.builder.JOperator.JOperatorConfig;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.generator.spl.SubmissionTimeValue.ParamsInfo;

class OperatorGenerator {
    
    private final SubmissionTimeValue stvHelper;
    
    OperatorGenerator(SPLGenerator splGenerator) {
        this.stvHelper = splGenerator.stvHelper();
    }

    String generate(JSONObject graphConfig, JSONObject op)
            throws IOException {
        JsonObject _op = GraphUtilities.gson(op);
        StringBuilder sb = new StringBuilder();
        noteAnnotations(_op, sb);
        parallelAnnotation(op, sb);
	viewAnnotation(op, sb);
        AutonomousRegions.autonomousAnnotation(_op, sb);
        outputClause(_op, sb);
        operatorNameAndKind(_op, sb);
        inputClause(op, sb);

        sb.append("  {\n");
        windowClause(op, sb);
        paramClause(graphConfig, op, sb);
        configClause(graphConfig, op, sb);
        sb.append("  }\n");

        return sb.toString();
    }

    private static void noteAnnotations(JsonObject op, StringBuilder sb)
            throws IOException {
        
        sourceLocationNote(op, sb);
        portTypesNote(op, sb);
    }
    
    private static void sourceLocationNote(JsonObject op, StringBuilder sb) throws IOException {
        
        JsonArray ja = GsonUtilities.array(op, "sourcelocation");
        if (ja == null)
            return;

        JsonElement jsource = ja.size() == 1 ? (JsonElement) ja.get(0) : ja;

        sb.append("@spl_note(id=\"__spl_sourcelocation\"");
        sb.append(", text=");
        String sourceInfo = jsource.toString();
        SPLGenerator.stringLiteral(sb, sourceInfo);
        sb.append(")\n");
    }
    
    private static void portTypesNote(JsonObject op, StringBuilder sb) {
        
        int[] id = new int[1];
        GsonUtilities.objectArray(op, "outputs",
                output -> {
                    
            String type = GsonUtilities.jstring(output, "type.native");
            if (type == null || type.isEmpty())
                return;
            sb.append("@spl_note(id=\"__spl_nativeType_output_" + id[0]++ + "\"");
            sb.append(", text=");
            SPLGenerator.stringLiteral(sb, type);
            sb.append(")\n");
        });
    }

    private void viewAnnotation(JSONObject op, StringBuilder sb){
	JSONObject config = (JSONObject)op.get("config");
	if(config == null)
	    return;

        JSONArray viewConfigs = (JSONArray) config.get("viewConfigs");
        if (viewConfigs == null || viewConfigs.isEmpty()) {
            return;
        }

	for (int i = 0; i < viewConfigs.size(); i++) {
            JSONObject viewConfig = (JSONObject) viewConfigs.get(i);
	    String name = (String)viewConfig.get("name");
	    String port = (String)viewConfig.get("port");
	    Double bufferTime = ((Number)viewConfig.get("bufferTime")).doubleValue();
	    Long sampleSize = ((Number)viewConfig.get("sampleSize")).longValue();
	    sb.append("@view(name = \"");
	    sb.append(splBasename(name));
	    sb.append("\", port = " + port);
	    sb.append(", bufferTime = " + bufferTime + ", ");
	    sb.append("sampleSize = " + sampleSize + ", ");
	    sb.append("activateOption = firstAccess)\n");
	}	
    }

    private void parallelAnnotation(JSONObject op, StringBuilder sb) {
        Boolean parallel = (Boolean) op.get("parallelOperator");
        if (parallel != null && parallel) {
            sb.append("@parallel(width=");
            Object width = op.get("width");
            if (width instanceof Integer) {
                sb.append(Integer.toString((int) width));
        	}
        	else if (width instanceof Long) {        		
            	sb.append(Integer.toString(((Long)width).intValue()));
            }
            else {
                JSONObject jo = (JSONObject) width;
                String jsonType = (String) jo.get("type");
                if (TYPE_SUBMISSION_PARAMETER.equals(jsonType))
                    sb.append(stvHelper.generateCompParamName(GraphUtilities.gson((JSONObject) jo.get("value"))));
                else
                    throw new IllegalArgumentException("Unsupported parallel width specification: " + jo);
            }
            Boolean partitioned = (Boolean) op.get("partitioned");
            if (partitioned != null && partitioned) {
                String parallelInputPortName = (String) op
                        .get("parallelInputPortName");<commit_msg>use gson for view annotations<commit_after>/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015  
 */
package com.ibm.streamsx.topology.generator.spl;

import static com.ibm.streamsx.topology.builder.JParamTypes.TYPE_SUBMISSION_PARAMETER;
import static com.ibm.streamsx.topology.generator.spl.GsonUtilities.jobject;
import static com.ibm.streamsx.topology.generator.spl.GsonUtilities.jstring;
import static com.ibm.streamsx.topology.generator.spl.GsonUtilities.objectArray;
import static com.ibm.streamsx.topology.generator.spl.SPLGenerator.splBasename;
import static com.ibm.streamsx.topology.internal.functional.ops.SubmissionParameterManager.NAME_SUBMISSION_PARAM_NAMES;
import static com.ibm.streamsx.topology.internal.functional.ops.SubmissionParameterManager.NAME_SUBMISSION_PARAM_VALUES;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
import com.ibm.streams.operator.window.StreamWindow;
import com.ibm.streams.operator.window.StreamWindow.Type;
import com.ibm.streamsx.topology.builder.JOperator;
import com.ibm.streamsx.topology.builder.JOperator.JOperatorConfig;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.generator.spl.SubmissionTimeValue.ParamsInfo;

class OperatorGenerator {
    
    private final SubmissionTimeValue stvHelper;
    
    OperatorGenerator(SPLGenerator splGenerator) {
        this.stvHelper = splGenerator.stvHelper();
    }

    String generate(JSONObject graphConfig, JSONObject op)
            throws IOException {
        JsonObject _op = GraphUtilities.gson(op);
        StringBuilder sb = new StringBuilder();
        noteAnnotations(_op, sb);
        parallelAnnotation(op, sb);
        viewAnnotation(_op, sb);
        AutonomousRegions.autonomousAnnotation(_op, sb);
        outputClause(_op, sb);
        operatorNameAndKind(_op, sb);
        inputClause(op, sb);

        sb.append("  {\n");
        windowClause(op, sb);
        paramClause(graphConfig, op, sb);
        configClause(graphConfig, op, sb);
        sb.append("  }\n");

        return sb.toString();
    }

    private static void noteAnnotations(JsonObject op, StringBuilder sb)
            throws IOException {
        
        sourceLocationNote(op, sb);
        portTypesNote(op, sb);
    }
    
    private static void sourceLocationNote(JsonObject op, StringBuilder sb) throws IOException {
        
        JsonArray ja = GsonUtilities.array(op, "sourcelocation");
        if (ja == null)
            return;

        JsonElement jsource = ja.size() == 1 ? (JsonElement) ja.get(0) : ja;

        sb.append("@spl_note(id=\"__spl_sourcelocation\"");
        sb.append(", text=");
        String sourceInfo = jsource.toString();
        SPLGenerator.stringLiteral(sb, sourceInfo);
        sb.append(")\n");
    }
    
    private static void portTypesNote(JsonObject op, StringBuilder sb) {
        
        int[] id = new int[1];
        GsonUtilities.objectArray(op, "outputs",
                output -> {
                    
            String type = GsonUtilities.jstring(output, "type.native");
            if (type == null || type.isEmpty())
                return;
            sb.append("@spl_note(id=\"__spl_nativeType_output_" + id[0]++ + "\"");
            sb.append(", text=");
            SPLGenerator.stringLiteral(sb, type);
            sb.append(")\n");
        });
    }

    private void viewAnnotation(JsonObject op, StringBuilder sb) {
        
        JsonObject config = jobject(op, "config");
        if (config == null)
            return;
        
        objectArray(config, "viewConfigs", viewConfig -> {

            String name = viewConfig.get("name").getAsString();
            String port = viewConfig.get("port").getAsString();
            Double bufferTime = viewConfig.get("bufferTime").getAsDouble();
            Long sampleSize = viewConfig.get("sampleSize").getAsLong();
            sb.append("@view(name = \"");
            sb.append(splBasename(name));
            sb.append("\", port = " + port);
            sb.append(", bufferTime = " + bufferTime + ", ");
            sb.append("sampleSize = " + sampleSize + ", ");
            sb.append("activateOption = firstAccess)\n");
        });
    }

    private void parallelAnnotation(JSONObject op, StringBuilder sb) {
        Boolean parallel = (Boolean) op.get("parallelOperator");
        if (parallel != null && parallel) {
            sb.append("@parallel(width=");
            Object width = op.get("width");
            if (width instanceof Integer) {
                sb.append(Integer.toString((int) width));
        	}
        	else if (width instanceof Long) {        		
            	sb.append(Integer.toString(((Long)width).intValue()));
            }
            else {
                JSONObject jo = (JSONObject) width;
                String jsonType = (String) jo.get("type");
                if (TYPE_SUBMISSION_PARAMETER.equals(jsonType))
                    sb.append(stvHelper.generateCompParamName(GraphUtilities.gson((JSONObject) jo.get("value"))));
                else
                    throw new IllegalArgumentException("Unsupported parallel width specification: " + jo);
            }
            Boolean partitioned = (Boolean) op.get("partitioned");
            if (partitioned != null && partitioned) {
                String parallelInputPortName = (String) op
                        .get("parallelInputPortName");
{'fraction_non_alphanumeric': 0.1210485064878516, 'fraction_numerical': 0.001480449359923365, 'mean_word_length': 2.6973599484868, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '5218798', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3257, 'n_words': 615}