blob: 1d6017b9fdae6a5d92fdde9c1ba263dc7db5f5e7 [file] [log] [blame]
[comment encoding = UTF-8 /]
[comment
/*******************************************************************************
* Copyright (c) 2014 Jad El-khoury.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
*
* Jad El-khoury - initial implementation of code generator (https://bugs.eclipse.org/bugs/show_bug.cgi?id=422448)
*
*******************************************************************************/
/]
[module resourceServices('http://org.eclipse.lyo/oslc4j/adaptorInterface')/]
[import org::eclipse::lyo::oslc4j::codegenerator::services::services/]
[import org::eclipse::lyo::oslc4j::codegenerator::services::domainSpecificationServices/]
[query public directParentResource(aResource: Resource) : Resource =
(if (aResource.extends->notEmpty()) then
aResource.extends->first()
else
null
endif)
/]
[query public parentResources(aResource: Resource) : Set(Resource) =
(if (directParentResource(aResource)->notEmpty()) then
directParentResource(aResource)->union(parentResources(directParentResource(aResource)))
else
Set{}
endif)
/]
[query public allExtendingResources(aResource: Resource) : OrderedSet(Resource) =
aResource.extends
->union(
aResource.extends->collectNested(r:Resource | allExtendingResources(r))->flatten()->asOrderedSet()
)
->asOrderedSet()
/]
[comment The interfaceResources of resource A, are its directInterfaceResources; plus for each such directInterfaceResource DIR, ALL the extendingResources of DIR.
However, if an extendingResource of DIR is also a ParentResource of A, then exclude this resource from the interface set.
It is a problem if a resource X is defined to be both an interface of A, as well as one of its (distant) parents. Since, the resource shape would end up defining each of X's properties twice/]
[query public interfaceResources(aResource: Resource) : OrderedSet(Resource) =
((directInterfaceResources(aResource)
->union(
directInterfaceResources(aResource)->collectNested(r:Resource | allExtendingResources(r))->flatten()->asOrderedSet()
)
->asOrderedSet())
- parentResources(aResource))
->asOrderedSet()
/]
[query private directInterfaceResources(aResource: Resource) : OrderedSet(Resource) =
(if (aResource.extends->size() > 1) then
aResource.extends->subOrderedSet(2, aResource.extends->size())
else
OrderedSet{}
endif)
/]
[query public directlyRelatedResources(aResource: Resource) : Set(Resource) =
(aResource.resourceProperties->collect(p : ResourceProperty | p.range))->flatten()->asSet()
->union(
aResource.extends
)->flatten()->asSet()
/]
[comment allRelatedResources seems to hang for long & complicated hierarchy of resources.
As an intermediate solution, the depth parameter is introduced to allow the recursive query to stop once a certain limit is reached /]
[query private allRelatedResources(aResource: Resource, alreadyAnalysed : Set(Resource), depth : Integer) : Set(Resource) =
let drr : Set(Resource) = directlyRelatedResources(aResource),
newAlreadyAnalysed : Set(Resource) = (Set{aResource}->union(drr))->union(alreadyAnalysed),
toAnalyse : Set(Resource) = drr - alreadyAnalysed in
(if (depth > 20) then
drr - alreadyAnalysed
else
toAnalyse->collect(r : Resource |
allRelatedResources(r, newAlreadyAnalysed, depth+1)
->union(directlyRelatedResources(r))
->union(Set{r})
)->flatten()->asSet()
endif)
/]
[comment allRelatedResources seems to hang for long & complicated hierarchy of resources.
As an intermediate solution, the depth parameter is introduced to allow the recursive query to stop once a certain limit is reached.
After that, we look at what related resources might be potentially missing, and then re-start the query for the remaining resources.
This is not a complete solution, but seems to be working for the Autosar 800+ resources.
It is not clear why the recursion is ending up in an endless loop otherwise. /]
[query public allRelatedResources(aResource: Resource) : Set(Resource) =
let arr : Set(Resource) = allRelatedResources(aResource, Set{}, 1),
leftOut : Set(Resource) = arr->collect(r : Resource | directlyRelatedResources(r) - arr)->flatten()->asSet()
in
arr
->union(leftOut)
->union(leftOut->collect(r : Resource | allRelatedResources(r, arr->union(Set{r}), 1))->flatten()->asSet())
/]
[query public allProperties(aResource: Resource) : Sequence(ResourceProperty) =
((aResource.resourceProperties->asSequence())
->union(inheritedProperties(aResource))
->union(interfaceProperties(aResource))
)
/]
[query public inheritedProperties(aResource: Resource) : Sequence(ResourceProperty) =
(if (not aResource.directParentResource().oclIsUndefined()) then
(aResource.directParentResource().resourceProperties->asSequence())->union(inheritedProperties(aResource.directParentResource()))
else
Sequence{}
endif)
/]
[query public interfaceProperties(aResource: Resource) : Sequence(ResourceProperty) =
aResource.interfaceResources().resourceProperties
/]
[query public javaName(aResource: Resource, toUpperFirst : Boolean) : String =
javaString(aResource.name, aResource.definingDomainSpecification().namespacePrefix.name, toUpperFirst)
/]
[query public javaInstanceName(aResource: Resource) : String =
javaName(aResource, false)
/]
[query public javaClassPackageName(aResource: Resource, anAdaptorInterface : AdaptorInterface, defaultJavaPackageName : String) : String =
javaPackageName(aResource.definingDomainSpecification(), anAdaptorInterface, defaultJavaPackageName)
/]
[query public javaClassName(aResource: Resource) : String =
javaName(aResource, true)
/]
[query public javaClassFullName(aResource: Resource, anAdaptorInterface : AdaptorInterface, defaultJavaPackageName : String) : String =
javaClassPackageName(aResource, anAdaptorInterface, defaultJavaPackageName).concat('.').concat(javaClassName(aResource))
/]
[query public javaClassFullFileName(aResource: Resource, anAdaptorInterface : AdaptorInterface, defaultJavaFullFilesPath : String, defaultJavaPackageName : String) : String =
javaFullFilesPath(aResource.definingDomainSpecification(), anAdaptorInterface, defaultJavaFullFilesPath)
.concatenatePaths(javaClassPackageName(aResource, anAdaptorInterface, defaultJavaPackageName).substituteAll('.', '/'))
.concatenatePaths(javaClassName(aResource))
.concat('.java')
/]
[query public javaInterfacePackageName(aResource: Resource, anAdaptorInterface : AdaptorInterface, defaultJavaPackageName : String) : String =
javaClassPackageName(aResource, anAdaptorInterface, defaultJavaPackageName)
/]
[query public javaInterfaceName(aResource: Resource) : String =
'I'.concat(javaName(aResource, true))
/]
[query public javaInterfaceFullName(aResource: Resource, anAdaptorInterface : AdaptorInterface, defaultJavaPackageName : String) : String =
javaInterfacePackageName(aResource, anAdaptorInterface, defaultJavaPackageName).concat('.').concat(javaInterfaceName(aResource))
/]
[query public javaInterfaceFullFileName(aResource: Resource, anAdaptorInterface : AdaptorInterface, defaultJavaFullFilesPath : String, defaultJavaPackageName : String) : String =
javaFullFilesPath(aResource.definingDomainSpecification(), anAdaptorInterface, defaultJavaFullFilesPath)
.concatenatePaths(javaInterfacePackageName(aResource, anAdaptorInterface, defaultJavaPackageName).substituteAll('.', '/'))
.concatenatePaths(javaInterfaceName(aResource))
.concat('.java')
/]
[query public resourceConstantName(aResource : Resource) : String =
javaConstantString(aResource.name)
/]
[query public resourcePathConstantName(aResource : Resource) : String =
resourceConstantName(aResource).concat('_PATH')
/]
[query public resourceTypeNamespaceConstantName(aResource : Resource) : String =
resourceConstantName(aResource).concat('_NAMESPACE')
/]
[query public resourceTypeLocalNameConstantName(aResource : Resource) : String =
resourceConstantName(aResource).concat('_LOCALNAME')
/]
[query public resourceTypeConstantName(aResource : Resource) : String =
resourceConstantName(aResource).concat('_TYPE')
/]
[query public nameAnnotation(aResource : Resource) : String =
'@OslcName('
.concat(javaInterfaceNameForConstants(aResource.definingDomainSpecification()))
.concat('.')
.concat(resourceTypeLocalNameConstantName(aResource))
.concat(')')
/]
[query public namespaceAnnotation(aResource : Resource) : String =
'@OslcNamespace('
.concat(javaInterfaceNameForConstants(aResource.definingDomainSpecification()))
.concat('.')
.concat(resourceTypeNamespaceConstantName(aResource))
.concat(')')
/]
[query public resourceShapeAnnotation(aResource : Resource) : String =
'@OslcResourceShape(title = "'
.concat(aResource.name).concat(' ').concat('Resource Shape')
.concat('", describes = ')
.concat(javaInterfaceNameForConstants(aResource.definingDomainSpecification()))
.concat('.')
.concat(resourceTypeConstantName(aResource))
.concat(')')
/]