How to generate Java Code from XML at runtime?

Discussion in 'Java' started by Lucia, Aug 7, 2003.

  1. Lucia

    Lucia Guest

    Hello, Experte,

    I'm looking for a possibility to load the Java code dynamically into a
    class at the run time. The Java Code should be readed from an
    XML-file. There are just some "If-Then" expressions and should be
    added into one class.

    Does someone know if it is possible?

    best regards

    Lucia
     
    Lucia, Aug 7, 2003
    #1
    1. Advertising

  2. Lucia

    Lucia Guest

    Hello, Dorothy,

    thank you for your answer. I will try to explain my requirements. In
    my java-application there are many classes to controll the machines.
    These Classes contain a lot of "IF-THEN" statements.

    For example,
    if (material=True and machine=idel) then startmachine(parameter);

    These conditions should always be changed for the new situations. That
    means that I should frequently rewrite the code. Hence we had a new
    idea. If we can programm a graphical user interface, where the user
    can type the "If-Then-Statements" as a text. These
    "IF-Then-Statements" can be stored in a special format (e.g. XML) and
    can be dynamically loaded in these classes.

    The critical step is, how I can load not the whole class, but just
    several "IF-THEN-Statements" in the class at run time.

    Now can you understand what I want to reach? Do you have a good idea?

    Best regards

    Lucia
     
    Lucia, Aug 8, 2003
    #2
    1. Advertising

  3. Lucia wrote:

    > Hello, Dorothy,
    >
    > thank you for your answer. I will try to explain my requirements. In
    > my java-application there are many classes to controll the machines.
    > These Classes contain a lot of "IF-THEN" statements.
    >
    > For example,
    > if (material=True and machine=idel) then startmachine(parameter);
    >
    > These conditions should always be changed for the new situations. That
    > means that I should frequently rewrite the code. Hence we had a new
    > idea. If we can programm a graphical user interface, where the user
    > can type the "If-Then-Statements" as a text. These
    > "IF-Then-Statements" can be stored in a special format (e.g. XML) and
    > can be dynamically loaded in these classes.
    >
    > The critical step is, how I can load not the whole class, but just
    > several "IF-THEN-Statements" in the class at run time.
    >
    > Now can you understand what I want to reach? Do you have a good idea?
    >
    > Best regards
    >
    > Lucia

    You might use a scripting language like beanshell (www.beanshell.org)
    instead (beanshell is just like java, but has some extentions to make it
    easily scriptable).

    Place the if / then statements in a script, an run that instead.
    --
    mvg,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Aug 8, 2003
    #3
  4. (Lucia) writes:
    > thank you for your answer. I will try to explain my requirements. In
    > my java-application there are many classes to controll the machines.
    > These Classes contain a lot of "IF-THEN" statements.
    >
    > For example,
    > if (material=True and machine=idel) then startmachine(parameter);


    I guess you can get a lot of ideas from looking at the theory and
    practice of state machines (FSA, etc.).

    > The critical step is, how I can load not the whole class, but just
    > several "IF-THEN-Statements" in the class at run time.


    One way to respresent states and state transitions are tables. It
    should be trivial to store such tables in a text format like XML.

    /Thomas
     
    Thomas Weidenfeller, Aug 8, 2003
    #4
  5. "Lucia" <> wrote in message
    news:...
    > Hello, Dorothy,
    >
    > thank you for your answer. I will try to explain my requirements. In
    > my java-application there are many classes to controll the machines.
    > These Classes contain a lot of "IF-THEN" statements.
    >
    > For example,
    > if (material=True and machine=idel) then startmachine(parameter);
    >
    > These conditions should always be changed for the new situations. That
    > means that I should frequently rewrite the code. Hence we had a new
    > idea. If we can programm a graphical user interface, where the user
    > can type the "If-Then-Statements" as a text. These
    > "IF-Then-Statements" can be stored in a special format (e.g. XML) and
    > can be dynamically loaded in these classes.


    That sounds alot like a "rules engine" - take a look at JESS -
    http://herzberg.ca.sandia.gov/jess/

    I just got a copy of the book about JESS written by the creator -
    some of the examples sound like exactly what you are trying to do.

    http://www.manning.com/friedman-hill/ <- the book page

    WBB
     
    William Brogden, Aug 8, 2003
    #5
  6. Lucia

    Sudsy Guest

    Lucia wrote:
    > Hello, Dorothy,
    >
    > thank you for your answer. I will try to explain my requirements. In
    > my java-application there are many classes to controll the machines.
    > These Classes contain a lot of "IF-THEN" statements.
    >
    > For example,
    > if (material=True and machine=idel) then startmachine(parameter);
    >
    > These conditions should always be changed for the new situations. That
    > means that I should frequently rewrite the code. Hence we had a new
    > idea. If we can programm a graphical user interface, where the user
    > can type the "If-Then-Statements" as a text. These
    > "IF-Then-Statements" can be stored in a special format (e.g. XML) and
    > can be dynamically loaded in these classes.
    >
    > The critical step is, how I can load not the whole class, but just
    > several "IF-THEN-Statements" in the class at run time.


    Sounds like a classic application for a finite state machine (FSM).
    Straightforward to implement in Java. The code would be very small.
     
    Sudsy, Aug 8, 2003
    #6
  7. Lucia

    Roedy Green Guest

    On 8 Aug 2003 04:02:55 -0700, (Lucia) wrote or
    quoted :

    >The critical step is, how I can load not the whole class, but just
    >several "IF-THEN-Statements" in the class at run time.


    There are several ways you can do it.

    You generate Java source code, then compile it. The source code would
    be for a class that overrides your main class. It may have just one
    method in it that does the if-then logic, and whatever else minimally
    must exist in the method to get it started.

    The other option is to generate JVM byte code on the fly. See
    http://mindprod.com/jgloss/jasm.html. This is more difficult, but then
    you bypass the compile step. You may be forced to use this approach
    where you cannot guarantee the presence of the latest and greatest
    Javac.exe at run time.

    See http://mindprod.com/jgloss/javacexe.html for how to invoke javac
    without firing up a new JVM (much faster).

    I would be happy to write the part that converts the XML either to
    Java or byte code for a fee if you feel overwhelmed.

    You might want to build these conditions visually with drag and drop.
    The advantage there is it impossible to make syntax or other
    grammatical errors.

    You might want to invent a simplified language for describing them.
    You then need a parser. see http://mindprod.com/jgloss/parser.html

    The great advantage of generating java source code is you can always
    create an escape in your specification language to allow tricky cases
    to be specially handled with custom Java code.

    You would find that raw XML would be even more confusing, and
    certainly more bulky than the raw Java itself. XML would just be your
    intermediate target.



    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 13, 2003
    #7
  8. Lucia

    vinny503

    Joined:
    Aug 19, 2011
    Messages:
    1
    Generate Java code from xml at runtime

    Hi Lucia,

    I am also planning to implement/modify the java files at run time, similar to the question which you have posted, your help in this regards would be greatly appreciated.

    In brief I would like to add portion/part of java code parsing the XML file.

    regards,
    Vinay
     
    Last edited: Aug 19, 2011
    vinny503, Aug 19, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raj
    Replies:
    0
    Views:
    2,621
  2. Replies:
    1
    Views:
    636
    Oliver Wong
    Apr 18, 2006
  3. Replies:
    1
    Views:
    567
    James McGill
    Apr 18, 2006
  4. Zombie

    generate xml at runtime

    Zombie, Oct 18, 2004, in forum: XML
    Replies:
    1
    Views:
    497
    Andy Dingley
    Oct 18, 2004
  5. Matt
    Replies:
    3
    Views:
    347
    Bob Barrows [MVP]
    Apr 23, 2004
Loading...

Share This Page