public static ArrayList<StateMachine> allStateMachines(Thing thing) {
   ArrayList<StateMachine> result = new ArrayList<StateMachine>();
   for (Thing t : allThingFragments(thing)) {
     result.addAll(t.getBehaviour());
   }
   return result;
 }
 public static ArrayList<Function> allFunctions(Thing thing) {
   ArrayList<Function> result = new ArrayList<Function>();
   for (Thing t : allThingFragments(thing)) {
     result.addAll(t.getFunctions());
   }
   return result;
 }
 public static ArrayList<Port> allPorts(Thing thing) {
   ArrayList<Port> result = new ArrayList<Port>();
   for (Thing t : allThingFragments(thing)) {
     result.addAll(t.getPorts());
   }
   return result;
 }
 public static ArrayList<Property> allProperties(Thing thing) {
   ArrayList<Property> result = new ArrayList<Property>();
   for (Thing t : allThingFragments(thing)) {
     result.addAll(t.getProperties());
   }
   return result;
 }
 public static ArrayList<Thing> findThing(ThingMLModel model, String name, boolean fuzzy) {
   ArrayList<Thing> result = new ArrayList<Thing>();
   for (Thing t : allThings(model)) {
     if (t.getName().startsWith(name)) {
       if (fuzzy) result.add(t);
       else if (t.getName().equals(name)) result.add(t);
     }
   }
   return result;
 }
 public static ArrayList<Message> allMessages(Thing thing) {
   ArrayList<Message> result = new ArrayList<Message>();
   for (Thing t : allThingFragments(thing)) {
     for (Message msg : t.getMessages()) {
       boolean isPresent = false;
       for (Message m : result) {
         if (EcoreUtil.equals(msg, m)) {
           isPresent = true;
           break;
         }
       }
       if (!isPresent) result.add(msg);
     }
   }
   return result;
 }
 public static ArrayList<Thing> allThingFragments(Thing thing) {
   ArrayList<Thing> result = new ArrayList<Thing>();
   result.add(thing);
   for (Thing t : thing.getIncludes())
     if (t != thing) {
       for (Thing c : allThingFragments(t)) if (!result.contains(c)) result.add(c);
     }
   return result;
 }
  public void resolve(
      String identifier,
      org.sintef.thingml.MessageParameter container,
      org.eclipse.emf.ecore.EReference reference,
      int position,
      boolean resolveFuzzy,
      final org.sintef.thingml.resource.thingml.IThingmlReferenceResolveResult<
              org.sintef.thingml.Message>
          result) {
    Thing thing = ThingMLHelpers.findContainingThing(container);

    for (Message m : thing.allMessages()) {
      if (resolveFuzzy && m.getName().startsWith(identifier)) {
        result.addMapping(m.getName(), m);
      } else if (!resolveFuzzy && m.getName().equals(identifier)) {
        result.addMapping(m.getName(), m);
      }
    }

    if (!result.wasResolved()) result.setErrorMessage("Cannot resolve message name: " + identifier);
  }
  @Override
  public void generateBuildScript(Configuration cfg, Context ctx) {
    try {
      final InputStream input =
          this.getClass().getClassLoader().getResourceAsStream("javascript/lib/package.json");
      final List<String> packLines = IOUtils.readLines(input);
      String pack = "";
      for (String line : packLines) {
        pack += line + "\n";
      }
      input.close();
      pack = pack.replace("<NAME>", cfg.getName());

      final JsonObject json = JsonObject.readFrom(pack);
      final JsonValue deps = json.get("dependencies");
      for (Thing t : cfg.allThings()) {
        for (String dep : t.annotation("js_dep")) {
          deps.asObject().add(dep.split(":")[0].trim(), dep.split(":")[1].trim());
        }
      }

      for (Thing t : cfg.allThings()) {
        if (t.getStreams().size() > 0) {
          deps.asObject().add("rx", "^2.5.3");
          deps.asObject().add("events", "^1.0.2");
          break;
        }
      }

      final File f = new File(ctx.getOutputDirectory() + "/" + cfg.getName() + "/package.json");
      f.setWritable(true);
      final PrintWriter w = new PrintWriter(new FileWriter(f));
      w.println(json.toString());
      w.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }