Java OO generated code are wrong

Java OO generated code are wrong

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
bioinfornatics
Posts: 13
Joined: 11 Oct 2014, 13:13

Java OO generated code are wrong

Post by bioinfornatics » 17 Oct 2014, 00:39

the awk script is used to generate .java file from C file but they do not use a java constructor. In fact the awk script should to generate a constructor each time a 'init' function is generated. The constructor will have to only call the 'init' method.

That mean, OOAI abstract class implements IOOAI interface and generate this (and more…)

Code: Select all

public abstract class OOAI implements IOOAI, AI {

	private AICallback   clb   = null;
	private OOAICallback clbOO = null;


	@Override
	public final int init(int skirmishAIId, AICallback callback) {

		int _ret;

		this.clb   = callback;
		this.clbOO = WrappOOAICallback.getInstance(callback);
		OOAICallback oo_callback = this.clbOO;

		_ret = this.init(skirmishAIId, oo_callback);

		return _ret;
	}
	/**
	 * This AI event initializes a Skirmish AI instance.
	 * It is sent only once per AI instance and game, as the very first event.
	 */
	@Override
	public int init(int skirmishAIId, OOAICallback oo_callback) { return 0; }
}
In the generated file they are no constructor then when your AI will inherit from this class you will never get the callback.

It should generated this:

Code: Select all

public abstract class OOAI implements IOOAI, AI {

	protected AICallback		clb			= null;
	protected OOAICallback	clbOO		= null;
	protected int				skirmishAIId	= 0;


	@Override
	public final int init(int skirmishAIId, AICallback callback) {

		int _ret;
		this.skirmishAIId = skirmishAIId
		this.clb   = callback;
		this.clbOO = WrappOOAICallback.getInstance(callback);
		OOAICallback oo_callback = this.clbOO;

		_ret = this.init(skirmishAIId, oo_callback);

		return _ret;
	}

	/**
	 * This AI event initializes a Skirmish AI instance.
	 * It is sent only once per AI instance and game, as the very first event.
	 */
	@Override
	public int init(int skirmishAIId, OOAICallback oo_callback) { return 0; }

	public OOAI(int skirmishAIId, AICallback callback){
		init(skirmishAIId, callback);
	}
       
}
and AbstractOOAI seem to be useless and attribute should to be protected
0 x

bioinfornatics
Posts: 13
Joined: 11 Oct 2014, 13:13

Re: Java OO generated code are wrong

Post by bioinfornatics » 17 Oct 2014, 01:38

The fix should to be something like

Code: Select all

95,97d94
< 	if (clsName == myOOAIAbstractClass ) {
< 		print("import " myParentPkgA ".AICallback;") >> outFile;
< 	}
124,129c121,122
< 		print("\t" "protected AICallback   clb          = null;") >> outFile;
< 		print("\t" "protected OOAICallback clbOO        = null;") >> outFile;
< 		print("\t" "protected Integer      skirmishAIId = null;") >> outFile;
< 		print("") >> outFile;
< 	}
< 	if ( clsName == myOOAIAbstractClass  ){
---
> 		print("\t" "private AICallback   clb   = null;") >> outFile;
> 		print("\t" "private OOAICallback clbOO = null;") >> outFile;
131,134d123
<         print("\t" "public " clsName "(int skirmishAIId, AICallback callback){") >> outFile;
<         print("\t" "\t" "super(skirmishAIId, callback);") >> outFile;
<         print("\t" "\t" "init(skirmishAIId, clbOO);") >> outFile;
<         print("\t" "}") >> outFile;
136,137d124
< 	
< 
163,165d149
< 	if ( outFile == myOOEventAIFile ) {
< 		print("import " myParentPkgA ".AICallback;") >> outFile;
< 	}
183,189d166
< 	if ( outFile == myOOEventAIFile ){
< 		print("") >> outFile;
<         print("\t" "public " myOOEventAIClass "(int skirmishAIId, AICallback callback){") >> outFile;
<         print("\t" "\t" "super(skirmishAIId, callback);") >> outFile;
<         print("\t" "\t" "init(skirmishAIId, clbOO);") >> outFile;
<         print("\t" "}") >> outFile;
< 	}
247,249d223
< 	} else if ((paType_sto == "int") && (paName_sto == "skirmishAIId") ) {
< 		conversionCode_pre = conversionCode_pre "\t\t" "this.skirmishAIId   = " paName_sto ";" "\n";
< 		
377,382d350
< 		if (  name_em == "init" ){
< 			print("") >> myOOAIFile;
<             print("\t" "public OOAI(int skirmishAIId, AICallback callback){") >> myOOAIFile;
<             print("\t" "\t" "init(skirmishAIId, callback);") >> myOOAIFile;
<             print("\t" "}") >> myOOAIFile;
< 		}

Last edited by bioinfornatics on 17 Oct 2014, 02:17, edited 1 time in total.
0 x

gajop
Moderator
Posts: 3021
Joined: 05 Aug 2009, 20:42

Re: Java OO generated code are wrong

Post by gajop » 17 Oct 2014, 01:43

You seem to know what you're doing, submit a pull request.
0 x

User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: Java OO generated code are wrong

Post by PauloMorfeo » 18 Oct 2014, 01:18

bioinfornatics wrote:...
That mean, OOAI abstract class implements IOOAI interface and generate this (and more…)
...
In the generated file they are no constructor then when your AI will inherit from this class you will never get the callback.
...
I'm not sure I understood what you're explaining. However, I think you're wrong. I am creating an AI extending that very same class and I do have access to Callback. This is the relevant code from my AI class:

Code: Select all

public class PoucoInteligente extends OOAI //implements AI
{
	protected OOAICallback callback;

	@Override
	public int init (int skirmishAIId, OOAICallback callback)
	{
		this.callback = callback;
		return 0;
	}
Regarding the member variable,
bioinfornatics wrote:...
and AbstractOOAI seem to be useless and attribute should to be protected
AbstractOOAI is indeed not referenced anywhere else and could be removed (*).
* unless the method WrappOOAICallback.getInstance() is actually doing something required.

It could be changed to protected, removed, or kept as is. As far as I'm concerned, I'd prefer to leave it as is - whomever has been developing that wrapper class might be expecting its existance sometime in the future; it's not creating harm (a few bytes of extra RAM for the pointer and a few CPU cycles of allocation at startup); and its name is just too ghastly to be exposing it to inheritors of the class.
0 x

abma
Spring Developer
Posts: 3540
Joined: 01 Jun 2009, 00:08

Re: Java OO generated code are wrong

Post by abma » 18 Oct 2014, 02:11

afaik init() is called by the engine instantly after the constructor is called. -> no need for a constructor with these parameters.
0 x

bioinfornatics
Posts: 13
Joined: 11 Oct 2014, 13:13

Re: Java OO generated code are wrong

Post by bioinfornatics » 18 Oct 2014, 13:52

abma wrote:afaik init() is called by the engine instantly after the constructor is called. -> no need for a constructor with these parameters.
Oh ok, If init() method are called by the engine for each instance you can to use the default contructor (wich are empty) to invokes the default constructor of the superclass.

Finally that is ok but:
- that is not really a clean way

Maybe using swig will be better than awk script for this job. Easy maintenance, and one swig file will generate wrapper for many languages.

thanks for your answers
0 x

User avatar
hoijui
Former Engine Dev
Posts: 4342
Joined: 22 Sep 2007, 09:51

Re: Java OO generated code are wrong

Post by hoijui » 18 Oct 2014, 16:41

swig was used before the AWK scripts. i forgot why, but .. it had a lot of problems, was way too unflexible, bloatet and so on.
0 x

Post Reply

Return to “AI”