Class MapperProxyUtils

java.lang.Object
org.aksw.jenax.reprogen.core.MapperProxyUtils

public class MapperProxyUtils extends Object
Utility methods for generating java proxies that appropriately implement subclasses of Resource based on annotations.
Author:
Claus Stadler, Nov 29, 2018
  • Constructor Details

    • MapperProxyUtils

      public MapperProxyUtils()
  • Method Details

    • viewAsDynamicSet

      public static Function<Class<?>,BiFunction<org.apache.jena.rdf.model.Property,Boolean,Function<org.apache.jena.rdf.model.Resource,ViewBundle>>> viewAsDynamicSet(Method m, boolean isIriType, boolean polymorphicOnly, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
      The collection view factory first takes the class that denotes the item type as argument. Then, for a given property, it yields a function that for a given subject yields a collection view of the objects TODO We should reuse the code from 'viewAsGetter': This class only differs in the aspect that the item type is dynamic, whereas viewAsGetter attempts to obtain a static item type using reflection.
      Parameters:
      m -
      typeMapper -
      Returns:
    • viewAsDynamicList

      public static Function<Class<?>,BiFunction<org.apache.jena.rdf.model.Property,Boolean,Function<org.apache.jena.rdf.model.Resource,ViewBundle>>> viewAsDynamicList(Method m, boolean isIriType, boolean polymorphicOnly, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
    • viewAsSet

      public static BiFunction<org.apache.jena.rdf.model.Property,Boolean,Function<org.apache.jena.rdf.model.Resource,ViewBundle>> viewAsSet(Method m, boolean isIriType, boolean polymorphicOnly, Class<?> itemType, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
    • viewAsMap

      public static Function<org.apache.jena.rdf.model.Property,Function<org.apache.jena.rdf.model.Resource,ViewBundle>> viewAsMap(Method m, boolean isValueIriType, boolean polymorphicOnly, Class<?> keyType, Class<?> valueType, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
    • viewAsList

      public static BiFunction<org.apache.jena.rdf.model.Property,Boolean,Function<org.apache.jena.rdf.model.Resource,ViewBundle>> viewAsList(Method m, boolean isIriType, boolean polymorphicOnly, Class<?> itemType, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
    • createViewBundleFromListAndConverter

      public static ViewBundle createViewBundleFromListAndConverter(Class<?> itemType, List<org.apache.jena.rdf.model.RDFNode> list, com.google.common.base.Converter<org.apache.jena.rdf.model.RDFNode,?> converter)
      If the converter converts RDFNodes to other RDFNodes (rather than Java types), then the raw view already has the converter applied and thus the java view are the same. The assumption is that this way the RDFNodes have may be converter to sub-types managed in the mapper's meta model.
      Parameters:
      itemType -
      list -
      converter -
      Returns:
    • createViewBundleFromMapAndConverter

      public static ViewBundle createViewBundleFromMapAndConverter(org.apache.jena.rdf.model.Resource s, org.apache.jena.rdf.model.Property p, org.apache.jena.rdf.model.Property keyProperty, org.apache.jena.rdf.model.Property valueProperty, Class<?> keyType, Class<?> valueType, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider, boolean polymorphicOnly)
      Return a function that accepts an RDFNode and attempts to convert it to its actual RDFNode subtype w.r.t. to the type decider. Not for use with literals as input.
      Parameters:
      valueType -
      typeMapper -
      typeDecider -
      Returns:
    • createViewBundleFromSetAndConverter

      public static ViewBundle createViewBundleFromSetAndConverter(Class<?> itemType, Set<org.apache.jena.rdf.model.RDFNode> set, com.google.common.base.Converter<org.apache.jena.rdf.model.RDFNode,?> converter, boolean isInjectiveConversion)
      Parameters:
      itemType - If it is a subclass of RDFNode, the raw view already has the converter applied
      set -
      converter -
      isInjectiveConversion - Whether the converter is injective. If it is not then additional wrapping is performed to ensure set nature.
      Returns:
    • classifyMethod

      public static MethodDescriptor classifyMethod(Method m)
    • tryClassifyAsScalarGetter

      public static MethodDescriptor tryClassifyAsScalarGetter(Method m)
      If the method qualifies as a getter, returns a factory function that for a given property yields another function that accesses this property for a
      Parameters:
      m -
      typeMapper -
      Returns:
    • tryClassifyAsScalarSetter

      public static MethodDescriptor tryClassifyAsScalarSetter(Method m)
    • tryClassifyAsCollectionSetter

      public static MethodDescriptor tryClassifyAsCollectionSetter(Method m)
    • tryClassifyAsMapGetter

      public static MethodDescriptor tryClassifyAsMapGetter(Method m)
    • tryClassifyAsCollectionGetter

      public static MethodDescriptor tryClassifyAsCollectionGetter(Method m)
    • viewAsScalarGetter

      public static BiFunction<org.apache.jena.rdf.model.Property,Boolean,Function<org.apache.jena.rdf.model.Resource,ViewBundle>> viewAsScalarGetter(MethodDescriptor methodDescriptor, Class<?> effectiveType, boolean isIriType, boolean polymorphicOnly, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
    • viewAsScalarGetterOldAndUnused

      public static BiFunction<org.apache.jena.rdf.model.Property,Boolean,Function<org.apache.jena.rdf.model.Resource,Object>> viewAsScalarGetterOldAndUnused(MethodDescriptor methodDescriptor, Class<?> effectiveType, boolean isIriType, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
      If the method qualifies as a getter, returns a factory function that for a given property yields another function that accesses this property for a
      Parameters:
      m -
      typeMapper -
      Returns:
    • matchesCollectionViewSetter

      public static boolean matchesCollectionViewSetter(Method m)
      Check whether the method is compatible with the signature VoidOrSuperClassOfDeclaringClass myCandidateSetter(Iterable<?>)
      Returns:
    • matchesDynamicCollectionViewGetter

      public static boolean matchesDynamicCollectionViewGetter(Method m, Class<?> expectedReturnType, boolean expectSubClassOfIterable, Class<?> expectedArgType)
      Check whether the method is compatible with the signature Iterable myMethod(Class); This means the following conditions are satisfied:
      • The method's result is a super (or equal) class of the returnType argument
      • There exists exactly one argument
      • The argument is a super (or equal) class of the given argType argument
      Parameters:
      m -
      iterableClass -
      typeVariableBound -
      Returns:
    • tryClassifyAsDynamicCollectionGetter

      public static MethodDescriptor tryClassifyAsDynamicCollectionGetter(Method m)
    • canActAsCollectionView

      public static Class<?> canActAsCollectionView(Method m, Class<?> iterableClass, boolean expectSubClassOfIterable, Class<?> typeVariableBound)
      Check if the method signature matches the pattern: IterableClass IterableBaseClass[T] method(Class[T]) IterableClass: A candidate method must return a this class or a super class. Example: If a method with a return type of Set.class is searched, then methods with return type Iterable and Collection are matches, but HashSet and List are not. Returns the itemType if the method is a match - null otherwise.
      Parameters:
      m -
      iterableClass -
      typeVariableBound - The
      Returns:
    • viewAsCollectionView

      public static Function<org.apache.jena.rdf.model.Property,BiConsumer<org.apache.jena.rdf.model.Resource,Object>> viewAsCollectionView(Method m, org.apache.jena.datatypes.TypeMapper typeMapper)
    • applyInModelIfApplicable

      public static Object applyInModelIfApplicable(Object o, org.apache.jena.rdf.model.Model sourceModel)
    • viewAsScalarSetter

      public static BiFunction<org.apache.jena.rdf.model.Property,Boolean,BiConsumer<org.apache.jena.rdf.model.Resource,Object>> viewAsScalarSetter(MethodDescriptor methodDescriptor, Class<?> effectiveType, boolean isIriType, boolean polymorphicOnly, org.apache.jena.datatypes.TypeMapper typeMapper, TypeDecider typeDecider)
    • getStricterType

      public static Class<?> getStricterType(Class<?> a, Class<?> b)
    • niceInvoke

      public static <T> T niceInvoke(Method method, Object target, Object... args)
    • readPrefixesFromClass

      public static org.apache.jena.shared.PrefixMapping readPrefixesFromClass(Class<?> cls, org.apache.jena.shared.PrefixMapping out)
      Read Namespaces and Namespace annotation from this class and a super classes / interfaces. The super classes / interfaces are visited first. TODO Add caching to avoid excessive reflection
      Parameters:
      cls -
      out -
      Returns:
    • addPrefix

      public static void addPrefix(Class<?> cls, org.apache.jena.shared.PrefixMapping out, String prefix, String value)
    • createProxyFactory

      public static <T extends org.apache.jena.rdf.model.Resource> BiFunction<org.apache.jena.graph.Node,org.apache.jena.enhanced.EnhGraph,T> createProxyFactory(Class<T> clazz, org.apache.jena.shared.PrefixMapping basePm, TypeDecider typeDecider)
      Method level annotations are processed into property level ones.
      Parameters:
      clazz -
      pm -
      Returns:
    • createDefaultStringIdProcessor

      public static BiFunction<org.apache.jena.rdf.model.Resource,HashIdCxt,String> createDefaultStringIdProcessor(String prefix)
    • proxyDefaultMethod

      public static BiFunction<Object,Object[],Object> proxyDefaultMethod(Method method) throws ReflectiveOperationException
      Throws:
      ReflectiveOperationException
    • defaultToString

      public static String defaultToString(Object that, Object[] args)
    • toStringTree

      public static String toStringTree(Object that, Object[] args)
    • toStringDirectProperties

      public static String toStringDirectProperties(Object that, Object[] args)
    • getHashId

      public static HashIdCxt getHashId(org.apache.jena.rdf.model.RDFNode root)
      Computes the hash id for a given subclass of RDF node.
      Type Parameters:
      T -
      Parameters:
      root -
      viewClazz -
      cxt - In/out map of visited resources to assigned hash values
      Returns:
    • skolemize

      public static org.apache.jena.rdf.model.Resource skolemize(String iriPrefix, org.apache.jena.rdf.model.Resource root)
      Convenience function that runs skolemization on a the given resource and prepends an iri prefix to the generated hashes. This convenience function does not allow for accessing all ocurred renamings. Modifies the model in-place. Example: newResource = skolemize("http://www.example.org/", oldResource;
      Parameters:
      iriPrefix -
      root - The 'root' resource from which to start the skolemization process
      Returns:
      The resource that is the skolemization of 'root' in the same model as 'root'
    • skolemize

      public static org.apache.jena.rdf.model.Resource skolemize(String iriPrefix, org.apache.jena.rdf.model.Resource root, Consumer<Map<org.apache.jena.rdf.model.RDFNode,String>> renamesCallback)
    • getHashId

      public static com.google.common.hash.HashCode getHashId(org.apache.jena.rdf.model.RDFNode root, HashIdCxt cxt)
    • getHashIdCore

      public static com.google.common.hash.HashCode getHashIdCore(org.apache.jena.rdf.model.RDFNode root, HashIdCxt cxt)
    • getHashIdActual

      public static com.google.common.hash.HashCode getHashIdActual(org.apache.jena.rdf.model.RDFNode root, HashIdCxt cxt)
    • collectReachableResources

      public static void collectReachableResources(org.apache.jena.rdf.model.RDFNode root, HashIdCxt cxt)
    • getClassDescriptorCached

      public static ClassDescriptor getClassDescriptorCached(Class<?> clazz)
    • getClassDescriptor

      public static ClassDescriptor getClassDescriptor(Class<?> clazz)
    • createPropertyHashIdProcessor

      public static BiFunction<org.apache.jena.rdf.model.RDFNode,HashIdCxt,com.google.common.hash.HashCode> createPropertyHashIdProcessor(BiFunction<org.apache.jena.rdf.model.RDFNode,HashIdCxt,com.google.common.hash.HashCode> globalHashProcessor, org.apache.jena.sparql.path.P_Path0 path, Supplier<Collection<? extends org.apache.jena.rdf.model.RDFNode>> valuesSupplier)
    • classToTag

      public static String classToTag(Class<?> clazz)
      Convert a class into a 'tag' (or label) to be included into generated identifiers such as by prefixing.
      Parameters:
      clazz -