# Black-box implementation support

The current support for black-box implementation in QVT Operational enables the users for library modules definition. The MDT OCL mapping for predefined OCL types to Java classes in runtime is followed, user model elements are limited to java generated EMF models only.

For the case of simplicity, an analogy of a public Java class declaring public operations is adopted and mapped to corresponding QVT Library AST model, which in turn owns corresponding imperative operations. Predefined `Java - OCL type` mapping rules are applied, eventually complemented by Java annotations to add missing information that are not expressable in Java language.

See an example at `'Examples/Operational QVT Transformation/Black-box Library Definition'` wizard.

#### Java classes mapped to predefined OCL types

Java class | OCL type
--- | ---
java.lang.Object | OclAny
java.lang.String | String
java.lang.Boolean | Boolean
java.lang.Integer | Integer
java.lang.Double | Real
java.util.Collection | Collection
java.util.List | Sequence
java.util.Set | Set
java.util.LinkedHashset | OrderedSet
org.eclipse.ocl.util.Bag | Bag

The collection element types are expressed using java generics, so `java.util.Set<java.util.List<java.lang.String>>` is mapped to `Set(Sequence(String))` in QVT. For Collection instance creation, the user should use org.eclipse.ocl.util.CollectionUtil utility class.

#### Additional Java numeric types recognized as OCL predefined types:

Java class | OCL type
--- | ---
java.lang.Short | Integer
java.lang.Long | Integer
java.math.BigInteger | Integer
java.lang.Float | Real
java.math.BigDecimal | Real

## User model types

The user model types are represented by corresponding java interfaces generated for the model classes or by java classes bound to individual datatypes. The resolution is performed based on lookup of EClassifier by instance class name within the EClassifiers of EPackages referenced by the given QVT module. In other words, the `org.eclipse.emf.ecore.EAttribute` java interface specified in an operation signature is resolved `ecore::EAttribute` class in the QVT type system, provided that the owning QVT module is declared as referencing `"http://www.eclipse.org/emf/2002/Ecore"` metamodel. The default package registry `EPackage.Registry.INSTANCE` is used to lookup the metamodel package by its nsURI.

## Library class

A public Java class that with a public default constructor. The public operations declared by this class are considered the QVT library defined operation. In order to make the library class visible, it has to be deployed in a black-unit using the org.eclipse.m2m.qvt.oml.javaBlackboxUnits extension point.

### Library operations

A public operation having both the return type and all parameter types resolvable to corresponding OCL types.

#### Non-contextual operation

Operation owned by the library and having no contextual type to be used as the source object of operation calls. It is defined as a ordinary public Java operation.

#### Contextual operation

Operation 'physically' owned by the defining library but having a contextual type to be used as the source object of operation calls. It is defined as a public Java operation of which the first argument is reserved for passing the source `object` (contextual instance) of the call. This is indicated by marking the operation by the predefined annotation `@Operation(contextual=true)`. Remark:At the moment, the java operation is required to be `static`, however in the future, the non-static variant will be support as well, thus offering the benefit of access to a state shared per library instance.

#### Simple library class example

    import java.util.Date;
    import org.eclipse.m2m.qvt.oml.blackbox.java.Operation;
    import org.eclipse.m2m.qvt.oml.blackbox.java.Parameter;
    import org.eclipse.emf.ecore.EClassifier;

    public class MyLibrary {

    	public MyLibrary() {
    		super()
    	}

    	public Date createDate(String dateStr) {
    		return (Date)EcoreFactory.eINSTANCE.createFromString(EcorePackage.eINSTANCE.getEDate(), dateStr);
    	}

    	/*
    	* java.util.Date resolved as ecore::EDate, which as an opaque data type to the QVT type system, however
    	* here equipped by this additional operation.
    	*/
    	@Operation(contextual=true)
    	public static boolean before(Date self, Date when) {
    		return self.before(when);
    	}

    	@Operation(contextual=true)
    	public static String getQualifiedName(EClassifier self) {
    		return self.getEPackage().getName() + "::" + self.getName();
    	}
    }

#### The library java class declared above appears to the importing QVT module as in the main() operation example bellow.

    main() {
    	var date : ecore::EDate := createDate('2008-10-31');
    	var isBefore := date.before(createDate('2008-11-01'));

    	var eClass := object ecore::EClass {};
    	var qname := eClass.getQualifiedName();
    }