HEADS-project / training

Training material to get started with the HEADS technologies
10 stars 16 forks source link

How to manipulate thingml #119

Closed jobs10000 closed 7 years ago

jobs10000 commented 7 years ago

This post proceeds the question at here: https://github.com/HEADS-project/training/issues/118

Would you mind giving a little more explanation on how to work on that (extracting ThingML elements) using new helper function? What is ThingMLElement self, Configuration self and etc? I mean, what kind of parameter am I supposed to pass? If possible, could you please rewrite the program provided at here https://github.com/HEADS-project/training/tree/79d92e02eaee41a548dd7ee3448ccc6b9642cc6e/1.ThingML_Basics/7.ReadThingMLModel? I am really interested in working on that, because someone here at my university has published a paper on that? Thanks a lot!

brice-morin commented 7 years ago

The self parameter corresponds to the former this on which the function was called

jobs10000 commented 7 years ago
public static void main(String[] args) {

    File input = new File("models/Basics/Hello_World/Hello_World.thingml");

    try {

    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    reg.getExtensionToFactoryMap().put("thingml", new ThingmlResourceFactory());

    System.out.println("Loading input file: " + input.getAbsolutePath() + "...");

    ResourceSet rs = new ResourceSetImpl();
    URI xmiuri = URI.createFileURI(input.getAbsolutePath());
    Resource model = rs.createResource(xmiuri);
    model.load(null);

    if (model.getErrors().size() > 0) {
        System.out.println("ERROR: The input model contains " + model.getErrors().size() + " errors:");
        for (Resource.Diagnostic d : model.getErrors()) {
            System.out.println("  " + d.getLocation() + " : " + d.getMessage());
        }
        return;
    }

    ThingMLModel input_model = (ThingMLModel) model.getContents().get(0);

    System.out.println("Model loaded successfully.");
        processThingMLModel(input_model);

    }
    catch(Throwable t) {
        System.err.println(t.getMessage());
        t.printStackTrace();
    }

}

public static void processThingMLModel(ThingMLModel input_model) {

    //System.out.println("The model contains " + input_model.eContents() + " Things");
    System.out.println("Here is the list: ");

    for (Thing t : ConfigurationHelper.allThings()) {
        processThing(t);
   }
}

Still cannot compile. Thanks! Would you please rewrite just 2 or 3 lines of code to set an example for me to follow.

brice-morin commented 7 years ago

I'll leave this as an exercise for you, as writing this code now will help you later on to manipulate ThingML models. You are pretty close!