Skip to main content



      Home
Home » Modeling » ATL » Regex to compare string
Regex to compare string [message #1349395] Mon, 12 May 2014 03:14 Go to next message
Eclipse UserFriend
Hi, i'm new in ATL and would like to know how i can compare a string to a regular expresion? i already have model-to-model transformation writen in MQL but i need to translate that transformation in ATL.

I use var regexType : java.util.regex.Pattern = java.util.regex.Pattern.compile
var matcherType : java.util.regex.Matcher; In MQL but how can i do the same in ATL?

Regards,

thomas
Re: Regex to compare string [message #1350194 is a reply to message #1349395] Mon, 12 May 2014 10:47 Go to previous messageGo to next message
Eclipse UserFriend
From: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.m2m.atl.doc%2Fguide%2Fuser%2FThe-ATL-Language.html

String::regexReplaceAll(regex : String, replacement : String) returns a copy of self in which each substring of this string that matches the given regular expression regex is replaced with the given replacement. Specification of regular expression must follow the definition of Java regular expressions.
Re: Regex to compare string [message #1351338 is a reply to message #1350194] Mon, 12 May 2014 22:12 Go to previous messageGo to next message
Eclipse UserFriend
I don't want to replace my string. Here is my code in MQL:

while(matcherType.find()) {
var aVar : sc_conceptual.Variable = gen.create("Variable"); if(matcherType.group(4) != null) { aVar.setName(matcherType.group(4));
} else aVar.setName("value"+matcherType.regionStart().toString());

I need this notion of groupe or something equivalent to match sting and use it as a condition.


Re: Regex to compare string [message #1369268 is a reply to message #1351338] Tue, 20 May 2014 09:59 Go to previous message
Eclipse UserFriend
I see. It's best to parse your string to an EMF model before feeding it into ATL. You can use xText, EMFText, or TCS for this.

If this is not feasible, you can still create native Java objects by switching to ATL/EMFTVM (https://wiki.eclipse.org/ATL/EMFTVM).
You can then write a recursive helper (while loops do not exist in ATL) to process your string:

helper context "#native"!"java::util::regex::Matcher" def : varNames() : Sequence(String) =
  if self.find() then
    Sequence{
      if self.group(4).oclIsUndefined() then
        'value' + self.regionStart().toString()
      else
        self.group(4)
      endif
    }->union(self.varNames())
  else
    Sequence{}
  endif;

rule VarParent {
  from s : MMIN!VarParent
  to t : MMOUT!NewVarParent (
    ...
    children <- 
      let regexType : "#native"!"java::util::regex::Pattern" = "#native"!"java::util::regex::Pattern".refInvokeStaticOperation('compile', Sequence{'pattern'}) in
      let matcherType : "#native"!"java::util::regex::Matcher" = regexType.matcher('string') in
      matcherType.varNames()->collect(n | thisModule.CreateVariable(n))
  )
}

lazy rule CreateVariable {
  from s : String
  to t : MMOUT!Variable (
    name <- s
  )
Previous Topic:[solved] Accessing to new model element
Next Topic:Ant build failed
Goto Forum:
  


Current Time: Wed Jul 23 16:31:36 EDT 2025

Powered by FUDForum. Page generated in 0.06421 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top