We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › dynamically created code: possible
Page Index Toggle Pages: 1
dynamically created code: possible? (Read 1423 times)
dynamically created code: possible?
Jan 13th, 2010, 4:12pm
 
this is just a vague idea so far, i'm wondering if this would work:

a Processing sketch that creates and runs Processing sketches / Java programs.
could this be possible by

a) in a P5 sketch generate code by parsing Strings, write to template text file.
b) from the same sketch, trigger a shell script to compile what's in the text file, then run?
c) the newly generated code could communicate back to the first sketch by OSC for example...

yes yes, there might be other languages more suited to that, but still...
Re: dynamically created code: possible?
Reply #1 - Jan 14th, 2010, 1:06am
 
I think it's possible.
Re: dynamically created code: possible?
Reply #2 - Jan 14th, 2010, 2:17am
 
I quess it might be a bit harder to make a sketch that generates code. like e.g. php does this with html... that would be a huge project.

but if it's just for running and messing up some things, this sketch i once made does some part of it:

Code:
//timm wilks @wollle.com
//tested on winXP
//needs winrar to be installed

import java.io.*;

void setup()
{
noLoop();
}

void draw(){
byte bytes[] = loadBytes("http://www.ct-w.com/timm/application.windows.txt");
saveBytes("C:/hello.rar", bytes);
delay(1000);
execute();
}

void execute(){
println("unpacking");
try {
String line;
OutputStream stdin = null;
Process p = Runtime.getRuntime().exec("C:/Programme/winrar/RAR x C:/hello.rar C:/");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
println(line);
}
input.close();
}
catch (Exception err) {
err.printStackTrace();
}

delay(3000);
println("executing");
try {
String line;
OutputStream stdin = null;
Process p = Runtime.getRuntime().exec("C:/application.windows/wollle_writer2.exe");

BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
println(line);
}
input.close();
}
catch (Exception err) {
err.printStackTrace();
}

exit();
}


what it does here, is downloading an .txt file, saving it as .rar, unpacking + executing it. as it doesn't delete anything and the .rar isn't set to overwrite itself, you need to delete "C:/application.windows" and
"C:/hello.rar" in order to run it a second time successfully (same thing if you want to get rid of the files in the end)
i wrote this file once to see how easy it would be to go around firewalls etc... the file it opens is nothing else than a bubble typo; you type some stuff and it generates funny bubbles... so nothing bad;)
tell me if it worked.

cheers
Re: dynamically created code: possible?
Reply #3 - Jan 14th, 2010, 2:44am
 
ah i just see, that this sketch is very german... in case you don't got an german computer setup you need to change this line:

Code:
Process p = Runtime.getRuntime().exec("C:/--winrarFolder--/RAR x C:/hello.rar C:/");  //i guess it might be C:/programs/winrar/RAR 

Re: dynamically created code: possible?
Reply #4 - Jan 14th, 2010, 7:51am
 
heh sehr clever timm.  Cool

unfortunately i don't have a Windows machine here so i can't test it...

which leads to a very obvious problem with this idea:
different OSes use different shell commands, so i'd need to determine what the underlying OS is and then execute the right script? i'm a total noob regarding shell, terminal and these things...

however i've figured out the first step: how to (rudimentary) parse some code and write to disk.
indeed with increasing complexity, this could quickly become a large project. Cheesy

to get the purely technical obstacles out of the way, i could use some help on how to write a script that

option [a], kinda easy:
take generatedCode.pde --> open with Processing IDE --> run

option [b], more elegant, would require to generate proper Java code though i suppose:
javac generatedCode.java, run.

Code:

ArrayList child;
String childName = "mickeyMouse";

String[] declareSetup =
{
"void setup()",
"{",
"}",
};

String[] declareDraw =
{
"void draw()",
"{",
"}",
};

String[] intoSetup =
{
// TODO: smart things
"\tsize(400, 300);",
"\tsmooth();",
};

String[] intoDraw =
{
// TODO: smart things
"\tbackground(0);",
"\tellipse(mouseX, mouseY, 30, 30);",
};

void setup()
{
buildChild();
writeChildToDisk(childName);
exit();

}


void buildChild()
{
child = new ArrayList();
child.add(declareSetup[0]);
child.add(declareSetup[1]);
for(int i = 0; i < intoSetup.length; i++)
{
child.add(intoSetup[i]);
}
child.add(declareSetup[2]);
child.add(" ");
child.add(declareDraw[0]);
child.add(declareDraw[1]);
for(int i = 0; i < intoDraw.length; i++)
{
child.add(intoDraw[i]);
}
child.add(declareDraw[2]);
child.add(0, "String myName = \""+childName+"\";");
child.add(1, " ");
}

void writeChildToDisk(String name)
{
String filePath = name+"/"+name+".pde";
PrintWriter output = createWriter(filePath);
for(int i = 0; i < child.size(); i++)
{
output.println((String)child.get(i));
}
output.flush();
output.close();
}

Re: dynamically created code: possible?
Reply #5 - Jan 14th, 2010, 8:26am
 
Re: dynamically created code: possible?
Reply #6 - Jan 14th, 2010, 12:26pm
 
depending on wht you are trying to do, i think it's entirely possible:
- there's a livecoding way:
http://www.andrewberman.org/projects/sketchpad/
- i've been working on a system that dynamically loads in classes.
in theory it should work, build a class, and then just save out your textfile as a .pde (.pde's just seem to be textfiles with no additional info in them). see here:
http://processing.org/discourse/yabb2/num_1262759715.html
problem i'm having is wanting to keep everything nicely in folders - at the moment can only load it in if it's in the same directory as the main .pde
anyway this should be a start to get you going.
Re: dynamically created code: possible?
Reply #7 - Jan 15th, 2010, 5:13pm
 
thanks for the suggestions guys.

so i've got the fundamentals kind of working, i.e. a sketch that replicates itself with a few alterations, writes a script to launch its child and then quits itself. this then happens recursively until there's a gazillion of P5 IDE instances on the screen  Cheesy.

Turns out the easy part was to tell a sketch to clone itself:

Code:

String myName = "mother";
String myMothersName = "none";
int myGeneration = 0;

ArrayList child;
String childName;
String pathToRunScript;
String pathToChild;

[...]

void makeChildName()
{
int childGeneration = myGeneration+1;
childName = "child"+nf(childGeneration, 4);
}

void makeFilePaths()
{
String basePath = sketchPath.substring(0, sketchPath.length()-1-myName.length());
pathToRunScript = basePath+"/runP5";
pathToChild = basePath+"/"+childName+"/"+childName+".pde";
}

void copyMyself()
{

//wrapped into ArrayList so it's easier to add&remove lines of code later on

child = new ArrayList();
int childGeneration = myGeneration+1;
String[] myself = loadStrings(sketchPath+"/"+myName+".pde");
myself[0] = "String myName = \""+childName+"\";";
myself[1] = "String myMothersName = \""+myName+"\";";
myself[2] = "int myGeneration = "+(childGeneration)+";";
for(int i = 0; i < myself.length; i++)
{
child.add(myself[i]);
}
}

void writeChildToDisk()
{
PrintWriter output = createWriter(pathToChild);
for(int i = 0; i < child.size(); i++)
{
output.println((String)child.get(i));
}
output.flush();
output.close();
}


what i am struggling with is the script to launch the newly created sketch. This works, but only on OS X as it's an AppleScript:

Code:

String[] runScript =
{
"osascript << EOF",
"\ttell application \"Processing\" to run",
"\ttell application \"Processing\" to activate",
"\tdelay 0.5",
"\t",
"\tdelay 1",
"\ttell application \"System Events\" to tell process \"Processing.app\"",
"\tkeystroke \"r\" using command down",
"\tend tell",
"EOF",
};

void addPathToChildToRunScript()
{
runScript[4] = "\ttell application \"Processing\" to open POSIX file \""+pathToChild+"\"";
}

void writeRunScript()
{
PrintWriter output = createWriter(pathToRunScript);
for(int i = 0; i < runScript.length; i++)
{
output.println(runScript[i]);
}
output.flush();
output.close();
}

void executeRunScript()
{
try
{
Process p = Runtime.getRuntime().exec("chmod 700 "+pathToRunScript);
}
catch (Exception e)
{
println(e);
}

open(pathToRunScript);
}



so each child rewrites the run-script with a path to the next child and executes it.
it's not exactly elegant.

feel free to suggest anything that could make this cross-platform  Smiley
Re: dynamically created code: possible?
Reply #8 - Jan 18th, 2010, 10:24am
 
okay i've got the replicate-and-launch-child thing down so it works without the PDE.
At least on a Mac. (although it doesn't on my mum's G4 iBook)

i want it to be distributable, so it would be appreciated if you can help me test if it runs on your system, especially on Windows:

download the zip:
Mac (should work on Linux too)
Windows

inside, there will be:
* a folder "Mother_lib" containing the source "Mother.java" as well as "core.jar"
* a file "Mother.jar".

double click Mother.jar, the mother app should launch. if you follow the instructions and all goes well, the app itself will close and launch its child app, which will be located in Mother_lib and will do the same until you're bored clicking.

errors encountered so far:

* the "Mother.jar" doesn't launch on my mum's iBook (OS X 10.4.11)
* on an old Windows machine the "runP5.bat" script which will be generated by "Mother" doesn't do it: "'javac' is not recognized as an internal or external command."
Page Index Toggle Pages: 1