Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* LinkML-Java - LinkML library for Java
* Copyright © 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3) Neither the name of the copyright holder nor the names its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.incenp.linkml.core;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
* A type designator resolver that uses pre-registered “aliases” to resolve
* designator values.
*/
public class AliasTypeDesignatorResolver implements ITypeDesignatorResolver {

private Map<ClassInfo, Map<String, ClassInfo>> aliasMap = new HashMap<>();
private Map<ClassInfo, String> reverseMap = new HashMap<>();

/**
* Registers an alias for a given class.
* <p>
* The alias must be unique for all classes that share a given designator slot
* – that is, for all classes that are descendants of the class that defines the
* slot (including that class).
* <p>
* Of note, a class may have more than one alias, in which
* {@link #getDesignator(ClassInfo)} will return the last registered one.
*
* @param klass The class for which to register an alias.
* @param alias The alias to register.
*/
public void registerAlias(ClassInfo klass, String alias) {
ClassInfo root = klass.getDesignatorBase();
if ( root == null ) {
// Don't think it is worth it to throw an exception... just ignore.
return;
}

Map<String, ClassInfo> classAliases = aliasMap.get(root);
if ( classAliases == null ) {
classAliases = new HashMap<>();
aliasMap.put(root, classAliases);
}
classAliases.put(alias, klass);
reverseMap.put(klass, alias);
}

@Override
public ClassInfo resolve(String designator, ClassInfo base) throws LinkMLRuntimeException {
ClassInfo root = base.getDesignatorBase();
if ( root == null ) {
throw new LinkMLRuntimeException(String.format(NO_DESIGNATOR, base.getName()));
}

Map<String, ClassInfo> classAliases = aliasMap.get(root);
if ( classAliases == null ) {
return null;
}

ClassInfo ci = classAliases.get(designator);
if ( ci != null && (ci == base || ci.getParents().contains(base)) ) {
return ci;
}

return null;
}

@Override
public Object getDesignator(ClassInfo klass) throws LinkMLRuntimeException {
Slot designatorSlot = klass.getTypeDesignatorSlot();
if ( designatorSlot == null ) {
throw new LinkMLInternalError(String.format(NO_DESIGNATOR, klass.getName()));
}

String alias = reverseMap.get(klass);
if ( alias == null ) {
return null;
}

if ( designatorSlot.getInnerType().equals(URI.class) ) {
try {
return new URI(alias);
} catch ( URISyntaxException e ) {
throw new LinkMLInternalError(String.format(INVALID_CLASS_URI, klass.getName()));
}
} else {
return alias;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* LinkML-Java - LinkML library for Java
* Copyright © 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3) Neither the name of the copyright holder nor the names its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.incenp.linkml.core;

import java.net.URI;
import java.net.URISyntaxException;

/**
* The “default” type designator resolver.
* <p>
* This class implements the base logic, using only the informations available
* from the annotated LinkML classes, to handle type designators.
*/
public class DefaultTypeDesignatorResolver implements ITypeDesignatorResolver {

@Override
public ClassInfo resolve(String designator, ClassInfo base) throws LinkMLRuntimeException {
// Lookup by URI
ClassInfo ci = ClassInfo.get(designator);
if ( ci == null ) {
// Look up by class name; we assume that all derived classes will live in the
// same Java package as the base class.
String pkgName = base.getType().getPackage().getName();
try {
ci = ClassInfo.get(Class.forName(pkgName + "." + designator));
} catch ( ClassNotFoundException e ) {
}
}

if ( ci != null && (ci == base || ci.getParents().contains(base)) ) {
return ci;
}

return null;
}

@Override
public Object getDesignator(ClassInfo klass) throws LinkMLRuntimeException {
Slot designatorSlot = klass.getTypeDesignatorSlot();
if ( designatorSlot == null ) {
throw new LinkMLInternalError(String.format(NO_DESIGNATOR, klass.getName()));
}

if ( designatorSlot.getInnerType().equals(URI.class) ) {
try {
return new URI(klass.getURI());
} catch ( NullPointerException | URISyntaxException e ) {
throw new LinkMLInternalError(String.format(INVALID_CLASS_URI, klass.getName()));
}
} else if ( designatorSlot.isCurieTyped() ) {
String uri = klass.getURI();
if ( uri == null ) {
throw new LinkMLInternalError(String.format(INVALID_CLASS_URI, klass.getName()));
}
return uri;
} else {
return klass.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* LinkML-Java - LinkML library for Java
* Copyright © 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3) Neither the name of the copyright holder nor the names its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.incenp.linkml.core;

/**
* An interface that provides the logic for resolving the value of a type
* designator slot into a class, or conversely to obtain an appropriate type
* designator value for a class.
*/
public interface ITypeDesignatorResolver {

public static final String NO_DESIGNATOR = "Type '%s' has no designator slot";
public final static String INVALID_CLASS_URI = "Missing or invalid class URI for type '%s'";

/**
* Resolves a single type designator into a LinkML class.
*
* @param designator The type designator value.
* @param base The base LinkML class. This would typically be the class
* that defines the type designator slot, though it could also
* be any class below it.
* @return The {@link ClassInfo} object representing the designated class, or
* <code>null</code> if the designator could not be resolved into a
* descendant of the <code>base</code> class (or the <code>base</code>
* class itself).
* @throws LinkMLRuntimeException If any error occurs during the resolution
* attempt.
*/
public ClassInfo resolve(String designator, ClassInfo base) throws LinkMLRuntimeException;

/**
* Gets a single value that designates the provided class.
*
* @param klass The {@link ClassInfo} object representing the class to
* designate.
* @return The designator value. Depending on the type designator slot in the
* class, it can be a URI, a string representing the URI in short form,
* or a string representing the class name.
* @throws LinkMLRuntimeException If the given class has no type designator
* slot, or if any error occurs when attempting
* to find the correct designator value.
*/
public Object getDesignator(ClassInfo klass) throws LinkMLRuntimeException;
}
Loading
Loading