Mostrando entradas con la etiqueta SUP. Mostrar todas las entradas
Mostrando entradas con la etiqueta SUP. Mostrar todas las entradas

viernes, 9 de noviembre de 2012

Consuming R from SAP Mobile Platform


Early this year, in March, I was visiting my team mates in SAP Labs Palo Alto, and my good friend a team mate Rui Nogueira asked to participate in his most excellent Technology Innovation Podcast show where we spoke about R and SAP HANA. By the end of the interview I said Rui that I was going to try to connect R and SUP (Which is now called SMP)...but actually...never did because my lack of time and specially and most important...because I didn't have a clue on how to do it...

So...yesterday, while I was reading the Steve Jobs book on my Kindle, I head a voice inside my head saying..."Dude! What the SAP? Where's R and SMP?"...at that moment...I knew I had to do something about it...

Again...I didn't have a clue on how to do it or how to really start working about it...but as I have already used Rook (R WebServer) in my blog RSAP, Rook and ERP and also Sinatra (Ruby WebServer) in my blog PowerBuilder and Gateway - The Sinatra style I knew, that was the way to go.

I knew that I needed to develop a Rook application to expose the data as JSON and pass it to SMP. Well...that failed quickly, because as far as I know and also my tests failed, SMP doesn't support JSON yet.

My next thought was to make the Rook application to expose the data as XML, which of course worked fine, but without using the standard XML library from R because the response is an C object that really looks bad when converted to a string.

First thing, was to think about a good example for this blog...but as they say, you have to teach a man how to fish...so I came up with a fairly simple example. Let's say you're a professor and my wife Milly, my daughter Kiara and myself are students. You have an Excel file where you will put the names and the grades and you want a mobile application that will read the file, calculate the means and provide the final score by simply passing the name of the student. I know...Excel? Why not SAP HANA? Well...you're an old fashion teacher...SAP HANA is so fast that you cannot even see it...so you stick to the most basic tools...


Of course, in R, we prefer to work with .CSV files, so being a good teacher, you create the file and give to us, so we can play with it.

To make thing simple for myself, I used my CloudShare.com account to start the work...I create my SMP application, called the Rook WebPage and test it on my BlackBerry emulator...everything worked like a charm...but...there's always one...the real thing came when I decided to move everything to AWS...

Thing is...and I didn't realize it in time...in CloudShare.com everything worked because both the SMP Server and the Rook Server are in the same place...the same localhost environment...in AWS, things change because the SMP Server is in the cloud while my Rook Server is in my localhost (laptop)...big problem...

I stayed yesterday working until 10:00 pm trying to figure out how to solve this big problem...maybe that's why I came up with a really solution...I said...Ok...let's move the Rook Server to the cloud as well! So I logged into my SMP Server, installed R and everything and run the Rook Server...back in my laptop...of course it failed miserably...the new Rook server was localhost but for my AWS server...so no way my Android emulator was going to be able to see it...

I said...Ok...don't panic...I have an R Server on AWS...let's do it there...another fail (getting used to it)...the R Server in AWS is headless, so no browser is allowed to work...also...Rook is always localhost, so there was no way to make the call...

I started to panic...so I went to sleep...at least for a while...

Today, I woke up at 5:30 am and start browsing hoping to see the light at the end of the tunnel...and I did...thank God...I did...

An amazing fella called Noah Lorang managed to make Rook work on Heroku...everything explain on his Github account...so my life was saved (not for long, sadly)...




I have never used Heroku before...and I gotta admit...is not for newbies...it really took me a long time to make it work...but I finally did it (Obviously...otherwise I wouldn't be boring you with all my senseless ranting)...

So...here's what I did...

  • I create myself a Heroku account and installed the Heroku Tool Belt.
  • I create myself a public key.
  • Inside the Git Bash application installed by the Heroku Tool Belt I log myself in.


Clone and create application in Heroku
git clone git://github.com/noahhl/rookonheroku.git blagcodes 
#(This will clone Noah's Github and create a folder called blagcodes 
#to store the codes)
heroku create blagrook 
#(I create an application for my Rook script)
git push heroku master 
#(This allow me to pass everything from my blagcodes folder 
#to my Github account)

With that, I was almost ready to rock...but I needed to do something else first...pass my own Rook script...

Summarize.R
library(Rook)
 
newapp<-function(env){
  req<-Rook::Request$new(env)
  res<-Rook::Response$new()
 
  name_param = req$params()$name
 
  Grades_Source = read.csv(file="Grades.csv",header=TRUE)
  Name<-Grades_Source$Name
  Grades<-Grades_Source$Grades
  Mean<-aggregate(Grades~Name,data=Grades_Source,FUN=mean)
  Mean_Result<-c(subset(Mean,Name == name_param))
 
  res$write("<root>")
  res$write("<Name>")
  res$write(Mean_Result$Name)
  res$write("</Name>")
  res$write("<Final_Grade>")
  res$write(as.character(Mean_Result$Grade))
  res$write("</Final_Grade>")
  res$write("</root>")
 
  res$finish()
}
 
 
server = Rhttpd$new()
server$add(app = newapp, name = "summarize")
server$start(listen="0.0.0.0", port=as.numeric(Sys.getenv("PORT")))
 
 
while(T) {
  Sys.sleep(10000)
}


Despise the name, I actually made an easier thing a just grabbed the file called demo.R and replace it with my own source code. Also, I copied the Grades.csv file to my blagcodes folder (which is located in my laptop).

The code is simple, we create a Rook application called "summarize" that will read the Grades.csv file, aggregate it using the mean function and print a basic XML structure passing the Name and Grade of the person we're passing a parameter. We need to pass the listen="0.0.0.0" and the port=as.numeric(Sys.getenv("PORT")) so Heroku knows how to call the page.

Back into the Git Bash I did the following to pass my changes back to Heroku...

Passing back to Heroku
git add .
git commit -am "message"
git push heroku

With this, everything was set-up and ready...so let's see how it looks...


(I'm using IE just because I wanted to show that the response from the Rook Application might look different than an XML response, but by looking at the source code you can actually see that's is an XML...also, because IE doesn't try to melt the tabs when putting them together as Chrome does).

When I start developing the SMP application I realized that calling a WebService wasn't an option...as this is of course not a WebService...so instead I used a REST Web Service...but it was asking me for XSD structures...so after another long time...I find a nice on-line tool to do that...

I simply pass an XML structure and let the tool work for me...



Of course...I have never worked with XSD before...so I didn't knew what to expect...thing is...if you use this structure...is going to fail...first because of the Final_Grade being xs:byte and second because when I was loading the parameters, Final_Grade was showing as well...and I didn't want it...so I made a copy of the file, change a bit here and there and came with Request.txt and Response.txt as you can see here...



So...as I was telling you...I create a REST WebService...


After this...came another tricky part as I wasn't sure how to make the parameter worked for me...gladly...I manage to make it work...


I load up my Request.txt and get the Root element...


Then I repeat the same for Response.txt and ended up with this...


After I create my Mobile Application, I create a Personalization Key to keep track of the parameter.


This is the look and feel of the application. Something very simple, you are requested a name, you press Get Grades and the result will be shown in a list.


Now...we're ready to test the application...



I guess...it didn't went very well for me in the exams...let's see how my daughter went...



As expected! My daughter is both smarter and beautiful than me...

Well...that's all folks...it took around 24 hours (summing up yesterday and today) to get this thing working...but I can assure Mr. Nogueira that Blag always keep his promises...I promised to have R working on SMP...and here it is...

Greetings,

Blag.

martes, 14 de agosto de 2012

SUP on AWS - A better Flights example


I have dealt with this problem before, but never really spend time trying to solve it, until someone ask for it on the forums...

When we create an SUP application, most of the time we use text boxes to send the information and retrieve some data, or maybe we can use a choice control with some hardcode values.

What if for example, we want to have a better Flights example, one that will show us a choice already populated with all the available airlines...that would be cool, right? And actually...it's easier than it sounds...

For this to work, we need to first log into our ERP system and create an structure called ZFLIGHT_DETAILS.


Then, a Table Type based on that structure.


Finally...a really simple RFC Function Module...too simple if you ask me...


This FM will simply return all the Airlines along with their codes.

With that ready, we can move into our Unwired Workspace and create a project. In this new project create two MBO's. One related to our FM ZGET_AIRLINES and the other related to BAPI_FLIGHT_GETLIST.


Here, it's important to create a Personalization Key just for the GetFlightsMBO and of course, don't forget to link it.



After saving and deploying the server, we need stablish our Starting Points. And here comes the exciting part...as we're going to choose not only the first option, but the third one as well.


When we double click on the "Activate" screen, we will have an empty window with a link saying "Submit Workflow"...here, you can get creative and use your company logo...write a message...and of course, change "Submit Workflow" to "Start" or "Begin the journey"...anyway...my job is not build commercial software, so I leave it just like that...


When we click on the "Submit Workflow" link, can establish it's properties, and here we're going to call our FM. Keep in mind that it's very important that the Default Success Screen is set to "Start"


Now, we have to move to the "Start Screen", and add a Choice control and link with the name "Get Flights".


When we set the properties for the Choice control, you will see where the magic comes from.


Don't forget to link the Personalization Key.



Finally, our model should look like this one...


We must save and generate our project in order to see it of the mobile device of our choice.






I hope you like this blog and I hope you already have your own SUP server on Amazon Web Services...otherwise...go and do it! In the meantime you can create your own SAP HANA and R servers as well...

Greetings,

Blag.

jueves, 9 de agosto de 2012

SUP on AWS - From Blag's point of view

The other day I read My 1st Sybase Unwired Platform app on AWS from Tobias Hofmann which is both a friend and an SAP Mentor. Tobias doesn't seems to be much happy with SUP on Amazon...so of course...I needed to test it myself.

Using the documentation from the all time guru Juergen Schmerder called Get your own Sybase Unwired Platform server on Amazon Web Services I was able to download the SDK and create my AWS image...which, to be honest, got broke as I made some silly mistakes...but it just took me a couple of minutes to clean up the mess, and create a new image that worked like a charm...I had my own SUP server up and running.

After I installed the Android emulator, installing the Sybase Workflow application wasn't hard at all...I simply need to call it like this...

Installing Sybase Workflow
<path_to>\adb install <path_to>\SybaseDataProvider.apk

The first thing I noticed as I was doing my first example, was something that Tobias experienced himself...a DLL was missing or couldn't be read...


I was really puzzled by this...I even log into the Remote Desktop to copy the file and create the same structure on my laptop...didn't work...until I found a comment from Pasquale de Angelis that magically solved the problem.

Now...I wanted to make a nice example, because reading from the flight tables is too mainstream...so I decided to do something else.

I thought about RFC_ABAP_INSTALL_AND_RUN but...one of input parameters is table, with an structure and even then the preview worked fine I couldn't really find a way to make it work using Personalization Keys...so...of course I create my own ZRFC_ABAP_INSTALL_AND_RUN.

UPDATE: RFC_ABAP_INSTALL_AND_RUN is not released as stated in https://service.sap.com/sap/support/notes/514998 and shouldn't be used on a productive environment. This is just an example on a blog and should be taken as it is.



I basically got rid of the input table and put 8 parameters to hold the source code to be executed.



With the RFC Function Module ready, I could get into the SUP hard work...and BTW...for this to really work I needed to create an empty program called ZTEST on my ERP system.


I create an MBO related to my FM and 8 Personalization Keys, one for each parameter.


I associate the Personalization Keys with the arguments on the MBO.




Inside the screen, I create the parameters and code submission menu.


A simple association of Personalization Keys and Parameter Keys to do the trick.

After that...which really didn't took more than 5 minutes, I was ready for the final test...running a simple ABAP program on Android...

ABAP LOOP
REPORT ZTEST.
DATA: COUNTER TYPE I.
DO 10 TIMES.
COUNTER = COUNTER + 1.
WRITE:/ 'Counter value is:', COUNTER.
ENDDO.



It worked just fine! So...no test can be completed without a more complex example...

ABAP_SELECT
REPORT ZTEST.
DATA: T_SPFLI TYPE STANDARD TABLE OF SPFLI.
FIELD-SYMBOLS: <FS_SPFLI> LIKE LINE OF T_SPFLI.
SELECT * FROM SPFLI INTO TABLE T_SPFLI.
LOOP AT T_SPFLI ASSIGNING <FS_SPFLI>.
WRITE:/ <FS_SPFLI>-CARRID, <FS_SPFLI>-CONNID.
ENDLOOP.



Cool, huh? I didn't have any connection problems with AWS...the service was very stable and reliable...after all, I have an SAP HANA Server and an R Server sitting next to my new SUP server.

Greetings,

Blag.


viernes, 20 de abril de 2012

Blag's experiments with SUP - Volume 2


As promised...here's another of my experiments with SUP...this time, I wanted to something more interesting and more complex...something that really gave me an SUP experience...so I took something that I build a long time ago by using PHP and SAP...I'm talking about an SBWP aka. SAP Mail emulation.
Of course, I needed to go back to my ABAP roots and build some RFC enabled function modules.
(The code is a little bit long, so I will upload it along with the SUP code to Code Exchange )
With that ready...we can start doing some SUP business...first, let me show the Model.


It's really easier than it looks. As we have 4 function modules, we create 4 MBO's (Business Model Objects) and we link two of them...the Titles with the Details...



We need to establish a relation between Titles and Details, because we're going to have a screen with the Titles and for each one, we're going to show it's detail.



It's very important to define the Personalization keys, as they are going to pass as parameters for the MBO's.


This is the Personalization for the Send Mail MBO.



With all that explanation, we can take a look at the demo, which is running on a BlackBerry emulator.







As we can see, we can do everything that we could do on the SBWP transaction...but as always -:(  I have a little bug...when the Reply window comes out, the "To:" should be filled, however, I haven't been able to do it yet...after all...SUP is a new technology, and we're all learning how to use it -;)

https://cw.sdn.sap.com/cw/groups/blag-stuff?view=documents