<commit_before>/**
 * Copyright (C) 2014 SINTEF <<EMAIL>>
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.thingml.compilers.behavior;

import org.sintef.thingml.*;
import org.thingml.compilers.Context;
import org.thingml.compilers.ThingMLCompiler;

import java.util.List;
import java.util.Map;

/**
 * Created by bmori on 16.04.2015.
 */
public class JSBehaviorCompiler extends BehaviorCompiler {

    private int ti = 0;

    protected void generateStateMachine(StateMachine sm, StringBuilder builder, Context ctx) {
        ti = 0;
        builder.append("this." + sm.qname("_") + " = StateFactory.buildRegion(\"" + sm.getName() + "\");\n");
        if (sm.isHistory())
            builder.append("this._initial_" + sm.qname("_") + " = StateFactory.buildHistoryState(\"_initial\", this." + sm.qname("_") + ");\n");
        else
            builder.append("this._initial_" + sm.qname("_") + " = StateFactory.buildInitialState(\"_initial\", this." + sm.qname("_") + ");\n");
        if (sm.hasSeveralRegions()) {
            builder.append("var _orth_" + sm.qname("_") + " = StateFactory.buildOrthogonalState(\"_orth_" + sm.qname("_") + "\", this." + sm.qname("_") + " );\n");
            builder.append("var t0 = new StateFactory.buildEmptyTransition(this._initial_" + sm.qname("_") + ", _orth_" + sm.qname("_") + ");\n");
            builder.append("var " + sm.qname("_") + "_default = StateFactory.buildRegion(\"_default\", _orth_" + sm.qname("_") + ");\n");
            if (sm.isHistory())
                builder.append("var _initial_" + sm.qname("_") + "_default = StateFactory.buildHistoryState(\"_initial\", " + sm.qname("_") + "_default);\n");
            else
                builder.append("var _initial_" + sm.qname("_") + "_default = StateFactory.buildInitialState(\"_initial\", " + sm.qname("_") + "_default);\n");
            for (State s : sm.getSubstate()) {
                ctx.addProperty("container", sm.qname("_") + "_default");
                generateState(s, builder, ctx);
            }
            builder.append("var t0_" + sm.qname("_") + "_default = StateFactory.buildEmptyTransition(_initial_" + sm.qname("_") + "_default, " + sm.getInitial().qname("_") + ");\n");
            for (Region r : sm.getRegion()) {
                ctx.addProperty("container", "_orth_" + sm.qname("_"));
                generateRegion(r, builder, ctx);
            }
        } else {
            for (State s : sm.getSubstate()) {
                ctx.addProperty("container", "this." + sm.qname("_"));
                generateState(s, builder, ctx);
            }
            builder.append("var t0 = new StateFactory.buildEmptyTransition(this._initial_" + sm.qname("_") + ", " + sm.getInitial().qname("_") + ");\n");
        }
        //TODO: we should revise some derived properties, not so nice to use in Java...
        final Map<Port, Map<Message, List<Handler>>> allHanders = sm.allMessageHandlers();
        for(Map.Entry<Port, Map<Message, List<Handler>>> entry : allHanders.entrySet()) {
            final Port p = entry.getKey();
            final Map<Message, List<Handler>> map = entry.getValue();
            for(Map.Entry<Message, List<Handler>> entry2 : map.entrySet()) {
                final List<Handler> handlers = entry2.getValue();
                final Message m = entry2.getKey();
                for(Handler h : handlers) {
                    generateHandler(h, m, p, builder, ctx);
                }
            }
        }
        generateActionsForState(sm, builder, ctx);
    }

    private void generateActionsForState(State s, StringBuilder builder, Context ctx) {
        if (s.getEntry() != null) {
            builder.append("function " + s.qname("_") + "_entry(context, message) {\n");
            ctx.getCompiler().getActionCompiler().generate(s.getEntry(),builder, ctx);
            builder.append("}\n\n");
        }
        if (s.getExit() != null) {
            builder.append("function " + s.qname("_") + "_exit(context, message) {\n");
            ctx.getCompiler().getActionCompiler().generate(s.getExit(), builder, ctx);
            builder.append("}\n\n");
        }
        if (s.getEntry() != null) {
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + ".entry = [");
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + "_entry];\n");
        }
        if (s.getExit() != null) {
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + ".exit = [");
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + "_exit];\n");

        }
    }

    protected void generateCompositeState(CompositeState c, StringBuilder builder, Context ctx) {
        String containerName = ctx.getProperty("container").get();
        if (c.hasSeveralRegions()) {
            builder.append("var " + c.qname("_") + " = StateFactory.buildOrthogonalState(\"" + c.getName() + "\", " + containerName + ");\n");
            builder.append("var " + c.qname("_") + "_default = StateFactory.buildRegion(\"_default\", " + c.qname("_") + ");\n");<commit_msg>migration of JS compiler almost complete (needs to be tested in details before merge)<commit_after>/**
 * Copyright (C) 2014 SINTEF <<EMAIL>>
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.thingml.compilers.behavior;

import org.sintef.thingml.*;
import org.thingml.compilers.Context;
import org.thingml.compilers.ThingMLCompiler;

import java.util.List;
import java.util.Map;

/**
 * Created by bmori on 16.04.2015.
 */
public class JSBehaviorCompiler extends BehaviorCompiler {

    private int ti = 0;

    protected void generateStateMachine(StateMachine sm, StringBuilder builder, Context ctx) {
        ti = 0;
        builder.append("this." + sm.qname("_") + " = StateFactory.buildRegion(\"" + sm.getName() + "\");\n");
        if (sm.isHistory())
            builder.append("this._initial_" + sm.qname("_") + " = StateFactory.buildHistoryState(\"_initial\", this." + sm.qname("_") + ");\n");
        else
            builder.append("this._initial_" + sm.qname("_") + " = StateFactory.buildInitialState(\"_initial\", this." + sm.qname("_") + ");\n");
        if (sm.hasSeveralRegions()) {
            builder.append("var _orth_" + sm.qname("_") + " = StateFactory.buildOrthogonalState(\"_orth_" + sm.qname("_") + "\", this." + sm.qname("_") + " );\n");
            builder.append("var t0 = new StateFactory.buildEmptyTransition(this._initial_" + sm.qname("_") + ", _orth_" + sm.qname("_") + ");\n");
            builder.append("var " + sm.qname("_") + "_default = StateFactory.buildRegion(\"_default\", _orth_" + sm.qname("_") + ");\n");
            if (sm.isHistory())
                builder.append("var _initial_" + sm.qname("_") + "_default = StateFactory.buildHistoryState(\"_initial\", " + sm.qname("_") + "_default);\n");
            else
                builder.append("var _initial_" + sm.qname("_") + "_default = StateFactory.buildInitialState(\"_initial\", " + sm.qname("_") + "_default);\n");
            for (State s : sm.getSubstate()) {
                ctx.addProperty("container", sm.qname("_") + "_default");
                generateState(s, builder, ctx);
            }
            builder.append("var t0_" + sm.qname("_") + "_default = StateFactory.buildEmptyTransition(_initial_" + sm.qname("_") + "_default, " + sm.getInitial().qname("_") + ");\n");
            for (Region r : sm.getRegion()) {
                ctx.addProperty("container", "_orth_" + sm.qname("_"));
                generateRegion(r, builder, ctx);
            }
        } else {
            for (State s : sm.getSubstate()) {
                ctx.addProperty("container", "this." + sm.qname("_"));
                generateState(s, builder, ctx);
            }
            builder.append("var t0 = new StateFactory.buildEmptyTransition(this._initial_" + sm.qname("_") + ", " + sm.getInitial().qname("_") + ");\n");
        }
        //TODO: we should revise some derived properties, not so nice to use in Java...
        final Map<Port, Map<Message, List<Handler>>> allHanders = sm.allMessageHandlers();
        for(Map.Entry<Port, Map<Message, List<Handler>>> entry : allHanders.entrySet()) {
            final Port p = entry.getKey();
            final Map<Message, List<Handler>> map = entry.getValue();
            for(Map.Entry<Message, List<Handler>> entry2 : map.entrySet()) {
                final List<Handler> handlers = entry2.getValue();
                final Message m = entry2.getKey();
                for(Handler h : handlers) {
                    generateHandler(h, m, p, builder, ctx);
                }
            }
        }
        generateActionsForState(sm, builder, ctx);
    }

    private void generateActionsForState(State s, StringBuilder builder, Context ctx) {
        if (s.getEntry() != null) {
            builder.append("function " + s.qname("_") + "_entry(context, message) {\n");
            ctx.getCompiler().getActionCompiler().generate(s.getEntry(),builder, ctx);
            builder.append("}\n\n");
        }
        if (s.getExit() != null) {
            builder.append("function " + s.qname("_") + "_exit(context, message) {\n");
            ctx.getCompiler().getActionCompiler().generate(s.getExit(), builder, ctx);
            builder.append("}\n\n");
        }
        if (s.getEntry() != null) {
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + ".entry = [");
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + "_entry];\n");
        }
        if (s.getExit() != null) {
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + ".exit = [");
            if (s instanceof StateMachine)//FIXME: ugly
                builder.append("this.");
            builder.append(s.qname("_") + "_exit];\n");


        }
    }

    protected void generateCompositeState(CompositeState c, StringBuilder builder, Context ctx) {
        String containerName = ctx.getProperty("container").get();
        if (c.hasSeveralRegions()) {
            builder.append("var " + c.qname("_") + " = StateFactory.buildOrthogonalState(\"" + c.getName() + "\", " + containerName + ");\n");
            builder.append("var " + c.qname("_") + "_default = StateFactory.buildRegion(\"_default\", " + c.qname("_") + ");\n");
{'fraction_non_alphanumeric': 0.16784047518031395, 'fraction_numerical': 0.004921510394569368, 'mean_word_length': 2.4942187963237474, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '2281811', 'n_tokens_mistral': 3521, 'n_tokens_neox': 3371, 'n_words': 780}