Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / GameEngineModelFileGenerator.java
package gameEngine;

import algorithms.TypeInference;
import models.algebra.Expression;
import models.algebra.Variable;
import models.dataConstraintModel.ChannelMember;
import models.dataConstraintModel.ResourcePath;
import models.dataFlowModel.DataTransferChannel;
import models.dataFlowModel.DataTransferModel;
import parser.Parser;
import parser.Parser.TokenStream;
import parser.exceptions.ExpectedColon;
import parser.exceptions.ExpectedDoubleQuotation;
import parser.exceptions.ExpectedRightBracket;
import parser.exceptions.WrongJsonExpression;
import simulator.Simulator;
import simulator.SystemState;

public class GameEngineModelFileGenerator {
    DataTransferModel model;

    public Runnable generate(){
        try {
            Parser.TokenStream stream = new Parser.TokenStream();
            Parser parser = new Parser(stream);

            model = new DataTransferModel();
            ResourcePath scene = new ResourcePath("scene");                        // "scene"
            ResourcePath scene_time = new ResourcePath(scene, "time");             // "scene.time"
            ResourcePath scene_camera = new ResourcePath(scene, "camera");         // "scene.camera"
            ResourcePath scene_camera_transform = new ResourcePath(scene_camera, "transform"); // "scene.camera.transform"
            ResourcePath scene_camera_transform_position = new ResourcePath(scene_camera_transform, "position"); // "scene.camera.transform.position"
            ResourcePath scene_camera_transform_rotation = new ResourcePath(scene_camera_transform, "rotation"); // "scene.camera.transform.rotation"
            ResourcePath scene_camera_transform_scale = new ResourcePath(scene_camera_transform, "scale"); // "scene.camera.transform.scale"
            ResourcePath scene_camera_projection = new ResourcePath(scene_camera, "projection"); // "scene.camera.projection"

            ResourcePath entities = new ResourcePath(scene, "entities");           // "scene.entities"
            ResourcePath entity = new ResourcePath(entities, new Variable("eid")); // "scene.entities.{eid}"
            ResourcePath entity_transform = new ResourcePath(entity, "transform"); // "scene.entities.{eid}.transform"
            ResourcePath entity_transform_position = new ResourcePath(entity_transform, "position"); // "scene.entities.{eid}.transform.position"
            ResourcePath entity_transform_rotation = new ResourcePath(entity_transform, "rotation"); // "scene.entities.{eid}.transform.rotation"
            ResourcePath entity_transform_scale = new ResourcePath(entity_transform, "scale"); // "scene.entities.{eid}.transform.scale"
            ResourcePath entity_mesh = new ResourcePath(entity, "mesh");           // "scene.entities.{eid}.mesh"

            ResourcePath keys = new ResourcePath(scene, "keys"); // "scene.keys"
            ResourcePath key = new ResourcePath(keys, new Variable("kno")); // "scene.keys.{kno}"
            ResourcePath key_state = new ResourcePath(key, "state"); // "scene.keys.{kno}.state

            ResourcePath timers = new ResourcePath("timers"); // "timers"
            ResourcePath timer = new ResourcePath(timers, new Variable("tid")); // "timers.{tid}"
            ResourcePath timer_count = new ResourcePath(timer, "count"); //"timer.{tid}.count

            model.addResourcePath(scene_time);
            model.addResourcePath(scene_camera_transform_position);
            model.addResourcePath(scene_camera_transform_rotation);
            model.addResourcePath(scene_camera_transform_scale);
            model.addResourcePath(scene_camera_projection);
            model.addResourcePath(entity_transform_position);
            model.addResourcePath(entity_transform_rotation);
            model.addResourcePath(entity_transform_scale);
            model.addResourcePath(entity_mesh);
            model.addResourcePath(key_state);
            model.addResourcePath(timer_count);

            addNativeOutputChannel(stream, parser, scene_time, "SceneUpdateEvent", "time: Long", "updateEvent(dt: Long)", "time + dt");

            TypeInference.infer(model);
            Simulator simulator = new Simulator(model);
            simulator.init();
            SystemState s = simulator.getCurState();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private void addNativeOutputChannel(TokenStream stream, Parser parser, ResourcePath path,
                                        String channelName, String curStateName, String messageName, String nextStateName) throws ExpectedRightBracket, WrongJsonExpression, ExpectedColon, ExpectedDoubleQuotation {
        DataTransferChannel outputChannel = new DataTransferChannel(channelName);
        outputChannel.setNative(true);
        ChannelMember out = new ChannelMember(path);
        stream.addLine(curStateName);
        Expression curState = parser.parseTerm(stream, model);
        stream.addLine(messageName);
        Expression message = parser.parseTerm(stream, model);
        stream.addLine(nextStateName);
        Expression nextState = parser.parseTerm(stream, model);
        out.getStateTransition().setCurStateExpression(curState);
        out.getStateTransition().setMessageExpression(message);
        out.getStateTransition().setNextStateExpression(nextState);
        outputChannel.addChannelMemberAsInput(out);
        model.addChannel(outputChannel);
    }

    private void addNativeInputChannel(TokenStream stream, Parser parser, ResourcePath path,
                                       String channelName, String curStateName, String messageName, String nextStateName) throws ExpectedRightBracket, WrongJsonExpression, ExpectedColon, ExpectedDoubleQuotation {
        DataTransferChannel inputChannel = new DataTransferChannel(channelName);
        inputChannel.setNative(true);
        ChannelMember in = new ChannelMember(path);
        stream.addLine(curStateName);
        Expression curState = parser.parseTerm(stream, model);
        stream.addLine(messageName);
        Expression message = parser.parseTerm(stream, model);
        stream.addLine(nextStateName);
        Expression nextState = parser.parseTerm(stream, model);
        in.getStateTransition().setCurStateExpression(curState);
        in.getStateTransition().setMessageExpression(message);
        in.getStateTransition().setNextStateExpression(nextState);
        inputChannel.addChannelMemberAsOutput(in);
        model.addInputChannel(inputChannel);
    }

}