+ private static class SemanticVersionImport {
+ private SemanticVersionImport() {
+ throw new UnsupportedOperationException("Utility class");
+ }
+
+ private static void onLinkageDeclared(
+ final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
+ final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
+ final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
+ final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
+ impIdentifier, SOURCE_LINKAGE);
+ final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
+ .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
+
+ importAction.apply(new InferenceAction() {
+ @Override
+ public void apply() {
+ final Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
+ impIdentifier.getName(), stmt);
+
+ StmtContext<?, ?, ?> importedModule = null;
+ ModuleIdentifier importedModuleIdentifier = null;
+ ModuleIdentifier semVerModuleIdentifier = null;
+ if (importedModuleEntry != null) {
+ importedModule = importedModuleEntry.getValue();
+ importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
+ semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
+ } else {
+ throw new InferenceException(stmt.getStatementSourceReference(),
+ "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
+ .getName(), getRequestedImportVersion(stmt));
+ }
+
+ linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
+ final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
+ stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
+ stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
+
+ final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
+ stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
+ }
+
+ @Override
+ public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
+ if (failed.contains(imported)) {
+ throw new InferenceException(stmt.getStatementSourceReference(),
+ "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
+ .getName(), getRequestedImportVersion(stmt));
+ }
+ }
+ });
+ }
+
+ private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
+ SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
+ if (requestedImportVersion == null) {
+ requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
+ }
+ return requestedImportVersion;
+ }
+
+ private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
+ final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
+ NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
+ SemanticVersionModuleNamespace.class, moduleName);
+ if (allRelevantModulesMap == null) {
+ return null;
+ }
+
+ final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
+ allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
+ SemVer.create(requestedImportVersion.getMajor() + 1), false);
+ if (!allRelevantModulesMap.isEmpty()) {
+ return allRelevantModulesMap.lastEntry();
+ }
+
+ return null;
+ }
+
+ private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
+ return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
+ Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
+ }
+
+ private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
+ final SemVer semVer) {
+ return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
+ Optional.ofNullable(importedModuleIdentifier.getNamespace()),
+ Optional.of(importedModuleIdentifier.getRevision()), semVer);
+ }
+ }
+}