All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lib.gen.javagenerator.rub Maven / Gradle / Ivy

/***************************************************************************\
  javagenerator.rub
  
  Builds on javafilegenerator.rub (defines generate(?packageclassname,?definition) out of more
  finegrained declarations.
  
  Back to the basics. Let's just keep the facts as close as possible to
  the actual Java syntax. This will look much more like the original tyruba
  facts for declaring Java code. 

  Some differences: fully qualified typenames must be used everywhere, that
  is the convention. You can probably get away with not doing it everywhere
  but that is asking for trouble when you want to do things which compare
  types in some or other way.

  Facts for declaring Java code:

  class_(?name).
  interface_(?name).

  import_(?name,?import).             // import into a single class
  package_import_(?package,?import).  // package wide import

  extends_(?class,?superclass).
  extends_(?intf,?superitf).
  implements_(?class,?itf).

  var_(?class,?type,?name).
  var_(?class,?type,?name,?init).

  constructor_(?class,?types,?formals,?body).
  
  method_(?class,?Return,?name,?types).
  method_(?class,?Return,?name,?types,?body).

  modifier_(?unit,?modifier).  
    ?modifier = public | private | ...
    ?unit = ?name    // interface or class name
          | method
          | constructor
          | var

  // alternative notation for the above:
  private_(?unit).
  public_(?unit). 

\***************************************************************************/

TYPE Feature<> = var<> | callable<>
TYPE callable<> =  constructor<> | method<>
TYPE var<> AS 
TYPE constructor<> AS 
TYPE method<> AS 

class_or_interface_ :: String, String
MODES (F,F) IS NONDET END

imports_ :: String, String
MODES (B,F) IS DET END

import_ :: String, String
MODES (B,F) IS NONDET END

package_import_ :: String, String
MODES (B,F) IS NONDET END

class_ :: String
MODES (F) IS NONDET END

interface_ :: String
MODES (F) IS NONDET END

extends_ :: String,String
MODES 
	(F,F) IS NONDET 
	(B,F) IS SEMIDET 
END
implements_ :: String,String
MODES 
	(F,F) IS NONDET 
END

clause_ :: String, String
MODES (B,F) IS MULTI END

throwsclause_ :: callable, String
MODES (B,F) REALLY IS DET END

modifiers_ :: Object, [String]
MODES (B,F) IS DET END

modifier_ :: Object, String
MODES (F,F) IS NONDET END

private_ :: Object
MODES (F) IS NONDET END
public_ :: Object
MODES (F) IS NONDET END
protected_ :: Object
MODES (F) IS NONDET END
static_ :: Object
MODES (F) IS NONDET END
abstract_ :: Object
MODES (F) IS NONDET END

feature_ :: String, String
MODES (F,F) IS NONDET END

var_ :: String, String, String
MODES (F,F,F) IS NONDET END

var_ :: String, String, String, String
MODES (F,F,F,F) IS NONDET END

simplifyType :: String, String, String
MODES (B,B,F) REALLY IS DET END

simplifyTypes :: String, [String], [String]
MODES (B,B,F) IS DET END

constructor_ :: String, [String], [String], String
MODES (F,F,F,F) IS NONDET END

method_ :: String, String, String, [String], [String], String
MODES (F,F,F,F,F,F) IS NONDET END

method_ :: String, String, String, [String], [String]
MODES (F,F,F,F,F) IS NONDET END

throws_ :: callable, String
MODES (F,F) IS NONDET END

////////////////////////////////////////////////////////////////////////
// Compilation unit
//

generate(?pkgcls,
{/*****************************************************************\
 * File:        ?className.java
 * Author:      TyRuBa
 * Meta author: Kris De Volder 
\*****************************************************************/
package ?package;
?imports

?code
}) :- class_or_interface_(?pkgcls,?code),
      package_class(?package,?className,?pkgcls),
      imports_(?pkgcls,?imports).

imports_(?class,{?imports}) :- FINDALL(import_(?class,?import),{
import ?import;},?imports).

import_(?pkgcls,?import) :- package_class(?pkg,?,?pkgcls),package_import_(?pkg,?import).

////////////////////////////////////////////////////////////////////////
// Class generation
//

class_or_interface_(?pkgcls,
{?classTags class ?className ?clauses {
    ?classFeatures
}})
:- class_(?pkgcls),
   package_class(?package,?className,?pkgcls),
   FINDALL(clause_(?pkgcls,?clause),{?clause },?clauses),
   modifiers_(?pkgcls,?classTags),
   FINDALL(feature_(?pkgcls,?code),?code,?classFeatures).

/****** class feature generation **************/ 

// variable declaration
feature_(?cls,{
    ?tags?simpletype ?name;})
:- class_(?cls),
   var_(?cls,?type,?name),
   simplifyType(?cls,?type,?simpletype),
   modifiers_(var,?tags).

// variable declaration with initializer
feature_(?cls,{
    ?tags?simpletype ?name=?init;})
:- class_(?cls),
   var_(?cls,?type,?name,?init),
   simplifyType(?cls,?type,?simpletype),
   modifiers_(var,?tags).

// constructor declaration
feature_(?cls,{
    ?tags?clsName(?formals) ?throws{ ?body 
    } }) 
:- class_(?cls),
   package_class(?pkg,?clsName,?cls),
   constructor_(?cls,?types,?vars,?body),
   simplifyTypes(?cls,?types,?simpletypes),
   JavaFormals(?simpletypes,?vars,?formals),
   modifiers_(constructor,?tags),
   throwsclause_(constructor,?throws).

// method declaration
feature_(?cls,{
    ?tags?SimpleReturn ?name(?formals) ?throws{ ?body 
    } })
:- class_(?cls),
   method_(?cls,?Return,?name,?types,?vars,?body),
   simplifyType(?cls,?Return,?SimpleReturn),
   simplifyTypes(?cls,?types,?simpletypes),
   JavaFormals(?simpletypes,?vars,?formals),
   modifiers_(method,?tags),
   throwsclause_(method,?throws).

////////////////////////////////////////////////////////////////////////
// Interface generation
//

class_or_interface_(?pkgitf,
{?tags interface ?itfName ?clauses {
    ?itfFeatures
}})
:- interface_(?pkgitfString),
   package_class(?package,?itfName,?pkgitfString),
   FINDALL(clause_(?pkgitfString,?clause),{?clause },?clauses),
   convertToString(?pkgitf,?pkgitfString),
   modifiers_(?pkgitf,?tags),
   FINDALL(feature_(?pkgitfString,?code),?code,?itfFeatures).

////////////////////////////////////////////////////////////////////////
// These things are useful for generation classes and interfaces both
//

// little helper predicate to remove the package from some types.
simplifyType(?cls,?Type,?SimpleType) :-
   package_class(?p,?,?cls),package_class(?p,?SimpleType,?Type).
simplifyType(?cls,?Type,?Type) :- String(?cls), String(?Type),
   NOT(EXISTS ?p: package_class(?p,?,?cls),package_class(?p,?,?Type)).
   
simplifyTypes(?cls,[],[]) :- String(?cls).
simplifyTypes(?cls,[?T|?R],[?sT|?sR]) :-
	simplifyType(?cls,?T,?sT),simplifyTypes(?cls,?R,?sR).

// abstract method declaration (without body)
feature_(?cls,{
    ?tags?Return ?name(?formals) ?throws; 
    })
:- (class_(?cls);
    interface_(?cls)  //actually this should work for interfaces just as well
   ), 
   method_(?cls,?Return,?name,?types,?vars), 
   JavaFormals(?types,?vars,?formals),
   modifiers_(method,?tags),
   throwsclause_(method,?throws).

modifiers_(?x,?tags) :-
   FINDALL((modifier_(?x,?tag),JavaModifier(?tag)),{?tag },?tags).

modifier_(?x,private) :-   private_(?x). 
modifier_(?x,public) :-    public_(?x). 
modifier_(?x,protected) :- protected_(?x).
modifier_(?x,static) :-    static_(?x).
modifier_(?x,abstract) :-  abstract_(?x).

clause_(?x,{implements ?parts}) :-
  FINDALL(implements_(?x,?part),?part,?partList),
  JavaCommaList(?partList,?parts).

clause_(?x,{extends ?part}) :-
  extends_(?x,?part).

throwsclause_(?x,{throws ?throws }) :- 
   FINDALL(throws_(?x,?t),?t,?tlist),
   NOT(equals(?tlist,[])),  //must have at least one
   JavaCommaList(?tlist,?throws).
throwsclause_(?x,"") :- callable(?x),NOT(throws_(?x,?)). 

//
/*Generate code for a method declaration without the body */
//JavaMethod(method,{?R ?m(?formals)}) :-
//  JavaFormals(?A,?formals).





© 2015 - 2025 Weber Informatics LLC | Privacy Policy