*
*/
@Override
public Resolved<?> resolve(RelToResolve relToResolve) {
Component source = relToResolve.getLinkSource();
Set<Implementation> impls = null;
String name = relToResolve.getTarget().getName();
/*
* For target by name, wait until the declaration has been processed
*/
if (Apform2Apam.isReifying(name) )
Apform2Apam.waitForComponent(name);
/*
* First analyze the component references
*/
if (relToResolve.getTarget() instanceof SpecificationReference) {
Specification spec = CST.componentBroker.getSpec(name);
if (spec == null) {
return null;
}
if (relToResolve.getTargetKind() == ComponentKind.SPECIFICATION) {
return new Resolved<Specification>(spec);
}
impls = spec.getImpls();
} else if (relToResolve.getTarget() instanceof ImplementationReference) {
Implementation impl = CST.componentBroker.getImpl(name);
if (impl == null) {
return null;
}
if (relToResolve.getTargetKind() == ComponentKind.IMPLEMENTATION) {
return new Resolved<Implementation>(impl);
}
impls = new HashSet<Implementation>();
impls.add(impl);
} else if (relToResolve.getTarget() instanceof InstanceReference) {
Instance inst = CST.componentBroker.getInst(name);
if (inst == null) {
return null;
}
if (relToResolve.getTargetKind() == ComponentKind.INSTANCE) {
return new Resolved<Instance>(inst);
}
logger.debug("if (relToResolve.getTarget() instanceof InstanceReference) ") ;
return null;
} else if (relToResolve.getTarget() instanceof ComponentReference<?>) {
logger.error("Invalid target reference : " + relToResolve.getTarget());
return null;
}
/*
* We have computed all component references It is either already
* resolved, or the implems are in impls. Now Resolve by resource.
*/
else if (relToResolve.getTarget() instanceof ResourceReference) {
if (relToResolve.getTargetKind() == ComponentKind.SPECIFICATION) {
Set<Specification> specs = new HashSet<Specification>();
for (Specification spec : CST.componentBroker.getSpecs()) {
if (spec.getProvidedResources().contains(relToResolve.getTarget())) {
specs.add(spec);
}
}
return relToResolve.getResolved(specs, false);
}
/*
* target Kind is implem or instance get all the implems that
* implement the resource
*/
impls = new HashSet<Implementation>();
for (Implementation impl : CST.componentBroker.getImpls()) {
if (impl.getProvidedResources().contains((relToResolve.getTarget()))) {
impls.add(impl);
}
}
}
// TargetKind is implem or instance, but no implem found.
if (impls == null || impls.isEmpty()) {
return null;
}
// If TargetKind is implem, select the good one(s)
if (relToResolve.getTargetKind() == ComponentKind.IMPLEMENTATION) {
return relToResolve.getResolved(impls, false);
}
/*
* We have in impls all the implementations satisfying the relation
* target (type and name only). We are looking for instances. Take all
* the instances of these implementations satisfying the relation
* constraints and visibility.
*/
/*
* Fast track : if looking for one instance and no preferences, return the first valid instance
*/
boolean fast = (!relToResolve.isMultiple() && !relToResolve.hasPreferences());
Set<Instance> insts = new HashSet<Instance>();
for (Implementation impl : impls) {
for (Instance inst : impl.getInsts()) {
if (inst.isSharable() && source.canSee(inst) && inst.matchRelationConstraints(relToResolve)) {
if (fast) {
return new Resolved<Instance>(inst);
}
insts.add(inst);
}