@SuppressWarnings("unchecked")
public MetricTransformer getMetricTransformer(
Model model, Resource resource,
Class<? extends MetricTransformer> type) throws Exception {
MetricTransformer metTra = (MetricTransformer)
getResourceFromCache(resource, MetricTransformer.class);
if(metTra != null) {
return metTra;
}
if(type == null) {
StmtIterator stmtI1 = model.listStatements(resource, RDF.type, (RDFNode)null);
Map<Resource,Method> childrenType = new HashMap<Resource,Method>();
List<Class<? extends MetricTransformer>> subclasses =
NumericTransformer.listSubclasses();
while(!subclasses.isEmpty()) {
Class<?> subclass = subclasses.get(0);
try {
Method method = subclass.getMethod("listSubclasses");
List<Class<? extends MetricTransformer>> subsubclasses =
(List<Class<? extends MetricTransformer>>) method.invoke(null);
subclasses.addAll(subsubclasses);
} catch (Exception e) {
}
childrenType.put(ResourceFactory.createResource(
riNamespace + subclass.getSimpleName()),
this.getClass().getMethod("get"+subclass.getSimpleName(),
Model.class, Resource.class));
subclasses.remove(0);
}
while(stmtI1.hasNext()) {
Statement typeStatement = stmtI1.nextStatement();
for(Resource resourceType : childrenType.keySet()) {
if(typeStatement.getObject().asResource().getURI().equals(
resourceType.getURI())) {
return (MetricTransformer) childrenType.get(
resourceType).invoke(this, model, resource);
}
}
}
if(type == null) {
if(!ModelException.throwException(ModelException.METRICTRANSFORMER,
"Resource "+resource+" cannot be defined" +
" as a direct instance of MetricTransformer. You must not use" +
" abstract subclasses")) {
return null;
}
}
} else {
}
MetricTransformerInstances instances =
getMetricTransformer(model,resource);
try {
Constructor<?> constructor = type.getConstructor(Metric.class,
Metric.class, List.class);
metTra = (MetricTransformer) constructor.newInstance(
instances.sourceMetric,
instances.destinationMetric,
instances.correlationBetweenMetrics);
metTra.setResource(resource);
addResourceInstanceToCache(resource, metTra);
metTra.setDescription(instances.description);
metTra.setIdentifier(instances.identifier);
} catch(Exception e) {
throw new Exception("System Bug: Not constructor with args: (Metric, " +
"Metric, List) exist for type:"+type);
}
return metTra;