Page 1 of 1

Java OO generated code are wrong

Posted: 17 Oct 2014, 00:39
by bioinfornatics
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

Re: Java OO generated code are wrong

Posted: 17 Oct 2014, 01:38
by bioinfornatics
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;
< 		}


Re: Java OO generated code are wrong

Posted: 17 Oct 2014, 01:43
by gajop
You seem to know what you're doing, submit a pull request.

Re: Java OO generated code are wrong

Posted: 18 Oct 2014, 01:18
by PauloMorfeo
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.

Re: Java OO generated code are wrong

Posted: 18 Oct 2014, 02:11
by abma
afaik init() is called by the engine instantly after the constructor is called. -> no need for a constructor with these parameters.

Re: Java OO generated code are wrong

Posted: 18 Oct 2014, 13:52
by bioinfornatics
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

Re: Java OO generated code are wrong

Posted: 18 Oct 2014, 16:41
by hoijui
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.