4
$\begingroup$

I a playing around with my programming cloud account. What I have found, interactive functions often are not working because there are several details that are not implemented into the cloud. So I have tried other things, beginng from pretty simple data calculations based on the data given with the url up to a more complicated version. My problem: Here in germany the laws can be downloaded from the Webserver of the "Bundesministerium für Justiz". The laws are provided as zip xml files. I have written a small script that queries a single article and shows the result:

data="21";

url="http://www.gesetze-im-internet.de/bgb/xml.zip";
fnames = Import[url, "FileNames"];
xmlres = Import[url, fnames[[1]]];
n = 6; 
l = Length[xmlres[[2]][[3]]]; 
While[n <= l && ToString[xmlres[[2]][[3]][[n]][[3]][[1]][[3]][[2]][[3]][[1]]] != 
   "§ " <> data, n++];
abss = Length[xmlres[[2]][[3]][[n]][[3]][[2]][[3]][[1]][[3]][[1]][[3]]];
result = ToString[xmlres[[2]][[3]][[n]][[3]][[1]][[3]][[2]][[3]][[1]]] <> " " <> 
         ToString[xmlres[[2]][[3]][[n]][[3]][[1]][[3]][[3]][[3]][[1]]];
ni = 1;
While[ni <= abss, result = result <> "\n" <> 
      ToString[xmlres[[2]][[3]][[n]][[3]][[2]][[3]][[1]][[3]][[1]][[3]][[ni]][[3]][[1]]];
      ni++];
result

Looks strange, but works perfect, simply enter a number into data and execute the commands, the freshly downloaded and generated article is shown.

Now I have converted it to a direct api call:

cloudObject = CloudObject["invep/" <> "LawBGB"];
CloudDeploy[APIFunction[{"data" -> "String"}, (
url="http://www.gesetze-im-internet.de/bgb/xml.zip";
fnames = Import[url, "FileNames"];
xmlres = Import[url, fnames[[1]]];
n = 6; 
l = Length[xmlres[[2]][[3]]];
While[n <= l && ToString[xmlres[[2]][[3]][[n]][[3]][[1]][[3]][[2]][[3]][[1]]] != 
       "§ " <> #data, n++];
abss = Length[xmlres[[2]][[3]][[n]][[3]][[2]][[3]][[1]][[3]][[1]][[3]]];
result = ToString[xmlres[[2]][[3]][[n]][[3]][[1]][[3]][[2]][[3]][[1]]] <> " " <> 
         ToString[xmlres[[2]][[3]][[n]][[3]][[1]][[3]][[3]][[3]][[1]]];
ni = 1;
While[ni <= abss, result = result <> "\n" <> 
      ToString[xmlres[[2]][[3]][[n]][[3]][[2]][[3]][[1]][[3]][[1]][[3]][[ni]][[3]][[1]]]; 
      ni++]; result) &], cloudObject, Permissions -> "Public"]

The result is a cloud object than may be called with:

blablabla/invep/LawBGB?data=21

But this didn't work, I get a strange error:

"3 $Failed[[2]][[3]][[6]][[3]][[3]]\n3\n3"

For me it looks like the variables are not filled with data. Are there any ideas?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Jan 9 '15 at 17:15
  • $\begingroup$ Additionally I have encapsulated everything with Module to make the variables used local. But the result ist simply the same... $\endgroup$ – Andre Koppel Jan 9 '15 at 17:32
  • $\begingroup$ Problem found (but not solved): Import[url, "FileNames"]; did not work, looks like Import is not supported by the Cloud. There is support to upload data to the cloud, so for me there is no reason why a cloud app is not able to download data from another url. $\endgroup$ – Andre Koppel Jan 9 '15 at 18:25
  • $\begingroup$ I'm not sure what you mean by Import not working in the Cloud. For example, this works fine for me: Import["i.imgur.com/RSnK3jV.jpg"] $\endgroup$ – Stefan R Jan 9 '15 at 22:39
  • $\begingroup$ Also, your original example works fine for me in Wolfram Programming Cloud: Import["gesetze-im-internet.de/bgb/xml.zip", "FileNames"] $\endgroup$ – Stefan R Jan 9 '15 at 22:41
1
$\begingroup$

It does work by slightly rewriting your code: First let's create a list of law rules of the form of {"1"-> ... , "2" -> ...} and save it to an .mx file in the home cloud directory:

data = "21";
url = "http://www.gesetze-im-internet.de/bgb/xml.zip"; 
xmlres = Import[url, Import[url, {"FileNames", 1}]]; 
myX["enbez", {}, {par_String}] := par; myX["norm", _, arg__] := arg; 
myX["metadaten", _, {_, par_String, str_String}] := par -> str; 
myX["text" | "titel" | "fussnoten" | "textdaten" | "Content" | 
   "P", _, {str___String}] := StringJoin[Riffle[{str}, " "]]; 
myXs[__] := Sequence[]; gesetzRules = 
 Dispatch@Select[
    xmlres[[2, 3, All]] /. XMLElement -> myX /. myX -> myXs, 
    MatchQ[#1, {s_String /; 
         StringMatchQ[s, 
          "§ " ~~ __] -> _String, ___String}] &] /. {par_String -> 
     title_String, 
    more___} :> (StringTrim[StringReplace[par, "§" -> ""]] -> 
     StringJoin[par, " ", title, "\n", 
      more]); DumpSave["gesetze.mx", gesetzRules];

Then the CloudDeploy operation gets simpler:

cloudObject = CloudObject["invep/" <> "LawBGB"]; CloudDeploy[ APIFunction[{"data" -> "String"}, (Get["gesetze.mx"]; ExportForm[ ( (#data)/.gesetzRules ),"Text"] ) &], cloudObject, Permissions -> "Public"]

and calling

https://www.wolframcloud.com/objects/user-6cce8a7d-37a9-4797-839b-f5eb71940ff5/invep/LawBGB?data=21

does result in

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ A very smart solution. But the problem is, there is no way to detect changes at the laws. The original File from the Bundesministerium may change at every time. But because we are using a pre-formatted file, the answer to a query probably did not reflect the actual state. Because of this I have collected all the import functions into the query itself to ensure the right file is used. And there are several hundred other law files with the same structure. It is nearly impossible to put all of them to the cloud. $\endgroup$ – Andre Koppel Jan 13 '15 at 19:59
  • $\begingroup$ Of course there is a way. E.g. Max@ToExpression@ Union@Cases[ Import[url, Import["http://www.gesetze-im-internet.de/bgb/xml.zip", \ {"FileNames", 1}]], ("builddate" -> b_) :> b, -1] gives the newest build date of all laws. But you are asking too much now here. I don't think people like to collaborate in full projects for free ... $\endgroup$ – Rolf Mertig Jan 14 '15 at 8:32
  • 1
    $\begingroup$ You are right, but this isn't a project, it's only a test to find ways to import something into the cloud ensuring the data is always actual. I find this is interesting for everyone who tries something like this. Regardless of the kind of data imported. I have only used this example to make it somewhat interesting :-) $\endgroup$ – Andre Koppel Jan 14 '15 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.