Code Monkey home page Code Monkey logo

pml's Introduction

Moved to https://github.com/prochitecture/pml

PML2PythonTranslator (Development Step I)

The goal of the PML2PythonTranslator is to translate PML-code (PML stands for Prochitecture Markup Language) into Python code that describes the style of a building and is used in the blender-osm plugin for Blender. A description of the concept of this style can be found at here. An additional goal of this development step was to study, how classes could made reusable for the node-based visual editor, which can be found in the repository blosm-nodes, and for other side projects of the blender-osm plugin. Only a subset of PML has been realized in this development step in order to get a feeling of the problems of this concept and to enable a discussion.

Content

pml_grammar (folder): This folder contains the file pml.g4, which defines the grammar of PML, written for the ANTLR parser generator. All the other files in this folder have been generated by the ANTLR4 parser generator (see below) for the target language Python. Do not change the files in this folder.

example.pml: This file contains an example description of a style, written in PML, as far as the translator already has been developed.

PML2PythonTranslator.py: This file contains a demo main program that starts the translation process. The parser created by ANTLR4 creates a parse tree from any file containing PML style code and the walker class then calls the translating functions using the generated listener class.

PythonListener.py: This file contains an interface class between the ANTLR4 listnener pmlListener and the coder class PythonCoder. It just calls the parser rule functions with the same name in the coder class, transferring the retrieved content from the parse tree. In principle, all the code in PythonCoder could be written directly in PythonListener, but as PythonListener depends on the ANTLR4 classes, it could not be reused in other programs without the ANTLR4 runtime for Python. PythonListener provides a method getCode() that gets the result of the translation for the PythonCoder.

PythonCoder.py: This class provides all rule functions that have to create Python code for the blender-osm plugin. Actually, it provides a write() method, that writes the translated text into a string, accessible by the getCode() method of the class. The write() method could be overwritten by a derived class to produce other kind of output. As for instance parsing the nodes in the node-based visual editor is very similar to parsing PML style code, PythonCoder.py can be reused by this editor to create the same code.

Dictionaries.py: This file contains only a fragement of code. It holds the data types and ranges of OSM attributes required by the osm-plugin. It could later be used to hold the information about allowed attribute names of the style blocks, to check if they are allowed in the context of the actual element, and so on. This will be object of future discussions.

Prerequisites

To run the PML2PythonTranslator, a Python 3 interpreter is required.

If you like to change the grammar, ANTLR4 has to be installed (see here). The parser files for the Python 3 runtime can then be generated using

java org.antlr.v4.Tool -Dlanguage=Python3 pml.g4

Running the translator

To run the translator using the example style example.pml, execute

python PML2PythonTranslator.py example.pml

The translated code will be written to stdout.

pml's People

Contributors

polarkernel avatar vvoovv avatar zkir avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pml's Issues

Syntax Checks

Actually, the ANTLR4 parser checks only the syntax of the grammar given in the grammar file. However, additional checks could be required, for instance:

  • Existence and spelling of the attribute names (minHeight, roofShape, etc.)
  • Allowed attributes in a given context (for instance Footprint allows other attributes than Facade).
  • Existence and spelling of attribute values (roofShape -> "dome", "flat", "saltbox", etc.)
  • Eventually range limits of values (positive, non-negative, etc.)

Which checks like this could be required, helpful, ...?

class

I am going to introduce the concept of class. It's better to explain it with an example.

PML:

facade{
    class: front_facade;
}

It means that a texture or a mesh of class front_facade should be looked up in the list of assets and applied to the facade as whole. There is no need to specify the content of the facade via the markup.

bldgAttr(..)

The function attr(..) returns an attribute value bound to the footprint.
A similar function that returns an attribute value bound to the whole building is needed.

buildingAttr(attr) should be mapped to FromBldgAttr(..) with exactly the same parameters as FromAttr(..)

Bugs in first almost complete version

I committed a first almost complete version of the translator. However, it is only very few tested. I propose to let it stay as is for a while and to collect found bugs in this issue, as long as they are not vital for the application.

Macros

These issue is used to discuss macros.
A macro is intended to reuse PML code similar to C++ or C.

A macro for inclusion (like #include in C++ or C) would simplify many things.

notes about example.pml

PML is intended to be easy to type manually.

So I suggest to use lowe case letters as much as possible to avoid switching from the lower case to capital case and back.

Item names should be lower-cased: footprint, facade instead of Footprint, Facade
Also
attr instead of Attr,
random_normal instead of RandomNormal,
random_weighted instead RandomWeighted

Conditional and FromStyleBlockAttr

claddingMaterial = PerBuilding(Value(Alternatives(
    FromAttr("roof:material", FromAttr.String, CladdingMaterials),
    Conditional(
        lambda roof: roof.footprint.getStyleBlockAttr("roofShape") == "flat",
        RandomWeighted(( ("concrete", 1), ("gravel", 1) ))
    ),
    Conditional(
        lambda roof: roof.footprint.getStyleBlockAttr("roofShape") == "dome",
        Constant("glass")
    ),
    FromStyleBlockAttr("claddingMaterial", FromStyleBlockAttr.Footprint)
)))

@include "path/to/file.pml";

@include inserts grammar definition from another file defined by the path:

@include "path";

Includes can be recursive.

It's desired to track down the original PML file and the line number in the original PML file for each line in the resulting PML definition.


Examples:

@include "a/b/c.pml";
@include "a.pml";
@include  "../../a/b/c.pml";

Below is the special case. The leading slash means lookup in the folder where the asset packages are stored:

@include "/default/a/b/c.pml"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.