Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » ATL » Regex to compare string
Regex to compare string [message #1349395] Mon, 12 May 2014 07:14 Go to next message
thomas nobre is currently offline thomas nobreFriend
Messages: 5
Registered: May 2014
Junior Member
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 14:47 Go to previous messageGo to next message
Dennis Wagelaar is currently offline Dennis WagelaarFriend
Messages: 171
Registered: September 2012
Senior Member

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.


Cheers,
Dennis
Re: Regex to compare string [message #1351338 is a reply to message #1350194] Tue, 13 May 2014 02:12 Go to previous messageGo to next message
thomas nobre is currently offline thomas nobreFriend
Messages: 5
Registered: May 2014
Junior Member
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 13:59 Go to previous message
Dennis Wagelaar is currently offline Dennis WagelaarFriend
Messages: 171
Registered: September 2012
Senior Member

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
  )


Cheers,
Dennis
Previous Topic:[solved] Accessing to new model element
Next Topic:Ant build failed
Goto Forum:
  


Current Time: Fri Dec 19 08:07:30 GMT 2014

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

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