@Test public void testTypeInit() throws Exception { JCas jcas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-all-types"); TypeSystem ts = jcas.getTypeSystem(); Type geneType = ts.getType("de.julielab.jcore.types.Gene"); JCoReFeaturePath fp = new JCoReFeaturePath(); fp.initialize("/resourceEntryList[0]/entryId"); fp.typeInit(geneType); String[] internalFeaturePath = (String[]) FieldUtils.readField(fp, "featurePath", true); assertArrayEquals(new String[] { "resourceEntryList[0]", "entryId" }, internalFeaturePath); Feature[] internalFeatures = (Feature[]) FieldUtils.readField(fp, "features", true); Type resourceEntryType = ts.getType("de.julielab.jcore.types.ResourceEntry"); assertArrayEquals(new Feature[] { geneType.getFeatureByBaseName("resourceEntryList"), resourceEntryType.getFeatureByBaseName("entryId") }, internalFeatures); int[] arrayIndexes = (int[]) FieldUtils.readField(fp, "arrayIndexes", true); assertArrayEquals(new int[] { 0, Integer.MIN_VALUE }, arrayIndexes); }
@Override public void typeInit(Type featurePathType) throws CASException { if (featurePathChanged || null == currentType || !currentType.equals(featurePathType)) { log.debug("Initializing internal structure for feature path {} on type {}.", featurePathString, featurePathType.getName()); Type currentFeatureType = featurePathType; features = new Feature[this.featurePath.length]; featureBaseNames = new String[this.featurePath.length]; arrayIndexes = new int[this.featurePath.length]; featureCandidates = new ArrayList<>(this.featurePath.length); for (int i = 0; i < this.featurePath.length; i++) { String featureName = this.featurePath[i]; arrayIndexes[i] = Integer.MIN_VALUE; featureCandidates.add(null); arrayIndexMatcher.reset(featureName); try { if (arrayIndexMatcher.find()) { String indexSpecification = arrayIndexMatcher.group(); String indexString = indexSpecification.substring(1, indexSpecification.length() - 1); arrayIndexes[i] = Integer.parseInt(indexString); if (arrayIndexes[i] == Integer.MIN_VALUE) throw new IllegalArgumentException("The negative array index " + arrayIndexes[i] + " is not allowed because Integer.MIN_VALUE is used to identify non-specified array indexes."); featureName = featureName.substring(0, featureName.length() - indexSpecification.length()); log.debug("Identified array index {} for feature {}.", indexString, featureName); } featureBaseNames[i] = featureName; Feature feature = currentFeatureType.getFeatureByBaseName(featureName); if (null == feature) { log.debug( "Feature \"{}\" is not defined for type \"{}\". It is checked whether the feature is defined on one or more subtypes.", featureName, currentFeatureType); TypeSystem typeSystem = ((TypeImpl) featurePathType).getTypeSystem(); Set<Feature> featuresOfSubtypes = searchFeatureInSubtypes(featureName, currentFeatureType, typeSystem); if (featuresOfSubtypes.size() == 1) feature = featuresOfSubtypes.iterator().next(); else featureCandidates.set(i, featuresOfSubtypes); if (null == feature && featuresOfSubtypes.isEmpty()) throw new CASException(CASException.UNDEFINED_FEATURE, new Object[]{featureName, currentFeatureType}); } if (null != feature) { features[i] = feature; if (feature.getRange().isPrimitive()) { log.trace("Feature {} identified as primitive-valued.", featureName); currentFeatureType = feature.getRange(); } else if (feature.getRange().isArray()) { log.trace("Feature {} identified as array-valued.", featureName); currentFeatureType = feature.getRange().getComponentType(); } else { log.trace("Feature {} identified as FeatureStructure-valued.", featureName); currentFeatureType = feature.getRange(); } log.debug("Determined type \"{}\" for feature \"{}\".", currentFeatureType, featureName); } } catch (Exception e) { log.error( "Error happened while initializing feature path \"{}\" on type \"{}\". Path element index: {} (\"{}\").", new Object[]{this.featurePathString, featurePathType, i, featureName}); throw e; } } currentType = featurePathType; featurePathChanged = false; } }
JCoReFeaturePath implements FeaturePath { @Override public void typeInit(Type featurePathType) throws CASException { if (featurePathChanged || null == currentType || !currentType.equals(featurePathType)) { log.debug("Initializing internal structure for feature path {} on type {}.", featurePathString, featurePathType.getName()); Type currentFeatureType = featurePathType; features = new Feature[this.featurePath.length]; featureBaseNames = new String[this.featurePath.length]; arrayIndexes = new int[this.featurePath.length]; featureCandidates = new ArrayList<>(this.featurePath.length); for (int i = 0; i < this.featurePath.length; i++) { String featureName = this.featurePath[i]; arrayIndexes[i] = Integer.MIN_VALUE; featureCandidates.add(null); arrayIndexMatcher.reset(featureName); try { if (arrayIndexMatcher.find()) { String indexSpecification = arrayIndexMatcher.group(); String indexString = indexSpecification.substring(1, indexSpecification.length() - 1); arrayIndexes[i] = Integer.parseInt(indexString); if (arrayIndexes[i] == Integer.MIN_VALUE) throw new IllegalArgumentException("The negative array index " + arrayIndexes[i] + " is not allowed because Integer.MIN_VALUE is used to identify non-specified array indexes."); featureName = featureName.substring(0, featureName.length() - indexSpecification.length()); log.debug("Identified array index {} for feature {}.", indexString, featureName); } featureBaseNames[i] = featureName; Feature feature = currentFeatureType.getFeatureByBaseName(featureName); if (null == feature) { log.debug( "Feature \"{}\" is not defined for type \"{}\". It is checked whether the feature is defined on one or more subtypes.", featureName, currentFeatureType); TypeSystem typeSystem = ((TypeImpl) featurePathType).getTypeSystem(); Set<Feature> featuresOfSubtypes = searchFeatureInSubtypes(featureName, currentFeatureType, typeSystem); if (featuresOfSubtypes.size() == 1) feature = featuresOfSubtypes.iterator().next(); else featureCandidates.set(i, featuresOfSubtypes); if (null == feature && featuresOfSubtypes.isEmpty()) throw new CASException(CASException.UNDEFINED_FEATURE, new Object[]{featureName, currentFeatureType}); } if (null != feature) { features[i] = feature; if (feature.getRange().isPrimitive()) { log.trace("Feature {} identified as primitive-valued.", featureName); currentFeatureType = feature.getRange(); } else if (feature.getRange().isArray()) { log.trace("Feature {} identified as array-valued.", featureName); currentFeatureType = feature.getRange().getComponentType(); } else { log.trace("Feature {} identified as FeatureStructure-valued.", featureName); currentFeatureType = feature.getRange(); } log.debug("Determined type \"{}\" for feature \"{}\".", currentFeatureType, featureName); } } catch (Exception e) { log.error( "Error happened while initializing feature path \"{}\" on type \"{}\". Path element index: {} (\"{}\").", new Object[]{this.featurePathString, featurePathType, i, featureName}); throw e; } } currentType = featurePathType; featurePathChanged = false; } } }
JCoReFeaturePath implements FeaturePath { @Override public void typeInit(Type featurePathType) throws CASException { if (featurePathChanged || null == currentType || !currentType.equals(featurePathType)) { log.debug("Initializing internal structure for feature path {} on type {}.", featurePathString, featurePathType.getName()); Type currentFeatureType = featurePathType; features = new Feature[this.featurePath.length]; featureBaseNames = new String[this.featurePath.length]; arrayIndexes = new int[this.featurePath.length]; featureCandidates = new ArrayList<>(this.featurePath.length); for (int i = 0; i < this.featurePath.length; i++) { String featureName = this.featurePath[i]; arrayIndexes[i] = Integer.MIN_VALUE; featureCandidates.add(null); arrayIndexMatcher.reset(featureName); try { if (arrayIndexMatcher.find()) { String indexSpecification = arrayIndexMatcher.group(); String indexString = indexSpecification.substring(1, indexSpecification.length() - 1); arrayIndexes[i] = Integer.parseInt(indexString); if (arrayIndexes[i] == Integer.MIN_VALUE) throw new IllegalArgumentException("The negative array index " + arrayIndexes[i] + " is not allowed because Integer.MIN_VALUE is used to identify non-specified array indexes."); featureName = featureName.substring(0, featureName.length() - indexSpecification.length()); log.debug("Identified array index {} for feature {}.", indexString, featureName); } featureBaseNames[i] = featureName; Feature feature = currentFeatureType.getFeatureByBaseName(featureName); if (null == feature) { log.debug( "Feature \"{}\" is not defined for type \"{}\". It is checked whether the feature is defined on one or more subtypes.", featureName, currentFeatureType); TypeSystem typeSystem = ((TypeImpl) featurePathType).getTypeSystem(); Set<Feature> featuresOfSubtypes = searchFeatureInSubtypes(featureName, currentFeatureType, typeSystem); if (featuresOfSubtypes.size() == 1) feature = featuresOfSubtypes.iterator().next(); else featureCandidates.set(i, featuresOfSubtypes); if (null == feature && featuresOfSubtypes.isEmpty()) throw new CASException(CASException.UNDEFINED_FEATURE, new Object[]{featureName, currentFeatureType}); } if (null != feature) { features[i] = feature; if (feature.getRange().isPrimitive()) { log.trace("Feature {} identified as primitive-valued.", featureName); currentFeatureType = feature.getRange(); } else if (feature.getRange().isArray()) { log.trace("Feature {} identified as array-valued.", featureName); currentFeatureType = feature.getRange().getComponentType(); } else { log.trace("Feature {} identified as FeatureStructure-valued.", featureName); currentFeatureType = feature.getRange(); } log.debug("Determined type \"{}\" for feature \"{}\".", currentFeatureType, featureName); } } catch (Exception e) { log.error( "Error happened while initializing feature path \"{}\" on type \"{}\". Path element index: {} (\"{}\").", new Object[]{this.featurePathString, featurePathType, i, featureName}); throw e; } } currentType = featurePathType; featurePathChanged = false; } }  }
JCoReFeaturePath implements FeaturePath { @Override public void typeInit(Type featurePathType) throws CASException { if (featurePathChanged || null == currentType || !currentType.equals(featurePathType)) { log.debug("Initializing internal structure for feature path {} on type {}.", featurePathString, featurePathType.getName()); Type currentFeatureType = featurePathType; features = new Feature[this.featurePath.length]; featureBaseNames = new String[this.featurePath.length]; arrayIndexes = new int[this.featurePath.length]; featureCandidates = new ArrayList<>(this.featurePath.length); for (int i = 0; i < this.featurePath.length; i++) { String featureName = this.featurePath[i]; arrayIndexes[i] = Integer.MIN_VALUE; featureCandidates.add(null); arrayIndexMatcher.reset(featureName); try { if (arrayIndexMatcher.find()) { String indexSpecification = arrayIndexMatcher.group(); String indexString = indexSpecification.substring(1, indexSpecification.length() - 1); arrayIndexes[i] = Integer.parseInt(indexString); if (arrayIndexes[i] == Integer.MIN_VALUE) throw new IllegalArgumentException("The negative array index " + arrayIndexes[i] + " is not allowed because Integer.MIN_VALUE is used to identify non-specified array indexes."); featureName = featureName.substring(0, featureName.length() - indexSpecification.length()); log.debug("Identified array index {} for feature {}.", indexString, featureName); } featureBaseNames[i] = featureName; Feature feature = currentFeatureType.getFeatureByBaseName(featureName); if (null == feature) { log.debug( "Feature \"{}\" is not defined for type \"{}\". It is checked whether the feature is defined on one or more subtypes.", featureName, currentFeatureType); TypeSystem typeSystem = ((TypeImpl) featurePathType).getTypeSystem(); Set<Feature> featuresOfSubtypes = searchFeatureInSubtypes(featureName, currentFeatureType, typeSystem); if (featuresOfSubtypes.size() == 1) feature = featuresOfSubtypes.iterator().next(); else featureCandidates.set(i, featuresOfSubtypes); if (null == feature && featuresOfSubtypes.isEmpty()) throw new CASException(CASException.UNDEFINED_FEATURE, new Object[]{featureName, currentFeatureType}); } if (null != feature) { features[i] = feature; if (feature.getRange().isPrimitive()) { log.trace("Feature {} identified as primitive-valued.", featureName); currentFeatureType = feature.getRange(); } else if (feature.getRange().isArray()) { log.trace("Feature {} identified as array-valued.", featureName); currentFeatureType = feature.getRange().getComponentType(); } else { log.trace("Feature {} identified as FeatureStructure-valued.", featureName); currentFeatureType = feature.getRange(); } log.debug("Determined type \"{}\" for feature \"{}\".", currentFeatureType, featureName); } } catch (Exception e) { log.error( "Error happened while initializing feature path \"{}\" on type \"{}\". Path element index: {} (\"{}\").", new Object[]{this.featurePathString, featurePathType, i, featureName}); throw e; } } currentType = featurePathType; featurePathChanged = false; } }  static Map<String, String> readReplacementsFromFile(String replacementsFile); static Map<String, String> readReplacementsFromInputStream(InputStream is); @Override int size(); @Override Feature getFeature(int i); @Override void addFeature(Feature feat); @Override void initialize(String featurePath); void initialize(String featurePath, Map<?, ?> replacements); @Override void typeInit(Type featurePathType); Object replaceValue(FeatureStructure fs); Object getValue(FeatureStructure fs, int startFeatureIndex); String[] getValueAsStringArray(FeatureStructure fs, boolean doReplacements); String[] getValueAsStringArray(FeatureStructure fs); List<String> getValueAsStringList(FeatureStructure fs, boolean doReplacements); List<String> getValueAsStringList(FeatureStructure fs); @Override String getValueAsString(FeatureStructure fs); String getValueAsString(FeatureStructure fs, boolean doReplacements); boolean getReplaceUnmappedValues(); void setReplaceUnmappedValues(boolean replaceWithNullValues); @Override String ll_getValueAsString(int fsRef, LowLevelCAS llCas); @Override Type getType(FeatureStructure fs); @Override TypeClass getTypClass(FeatureStructure fs); @Override String getFeaturePath(); @Override String getStringValue(FeatureStructure fs); @Override Integer getIntValue(FeatureStructure fs); @Override Boolean getBooleanValue(FeatureStructure fs); @Override Byte getByteValue(FeatureStructure fs); @Override Double getDoubleValue(FeatureStructure fs); @Override Float getFloatValue(FeatureStructure fs); @Override Long getLongValue(FeatureStructure fs); @Override Short getShortValue(FeatureStructure fs); @Override FeatureStructure getFSValue(FeatureStructure fs); void loadReplacementsFromFile(String replacementsFile); Object getDefaultReplacementValue(); void setDefaultReplacementValue(Object defaultReplacementValue); void clearReplacementCache(); }
JCoReFeaturePath implements FeaturePath { @Override public void typeInit(Type featurePathType) throws CASException { if (featurePathChanged || null == currentType || !currentType.equals(featurePathType)) { log.debug("Initializing internal structure for feature path {} on type {}.", featurePathString, featurePathType.getName()); Type currentFeatureType = featurePathType; features = new Feature[this.featurePath.length]; featureBaseNames = new String[this.featurePath.length]; arrayIndexes = new int[this.featurePath.length]; featureCandidates = new ArrayList<>(this.featurePath.length); for (int i = 0; i < this.featurePath.length; i++) { String featureName = this.featurePath[i]; arrayIndexes[i] = Integer.MIN_VALUE; featureCandidates.add(null); arrayIndexMatcher.reset(featureName); try { if (arrayIndexMatcher.find()) { String indexSpecification = arrayIndexMatcher.group(); String indexString = indexSpecification.substring(1, indexSpecification.length() - 1); arrayIndexes[i] = Integer.parseInt(indexString); if (arrayIndexes[i] == Integer.MIN_VALUE) throw new IllegalArgumentException("The negative array index " + arrayIndexes[i] + " is not allowed because Integer.MIN_VALUE is used to identify non-specified array indexes."); featureName = featureName.substring(0, featureName.length() - indexSpecification.length()); log.debug("Identified array index {} for feature {}.", indexString, featureName); } featureBaseNames[i] = featureName; Feature feature = currentFeatureType.getFeatureByBaseName(featureName); if (null == feature) { log.debug( "Feature \"{}\" is not defined for type \"{}\". It is checked whether the feature is defined on one or more subtypes.", featureName, currentFeatureType); TypeSystem typeSystem = ((TypeImpl) featurePathType).getTypeSystem(); Set<Feature> featuresOfSubtypes = searchFeatureInSubtypes(featureName, currentFeatureType, typeSystem); if (featuresOfSubtypes.size() == 1) feature = featuresOfSubtypes.iterator().next(); else featureCandidates.set(i, featuresOfSubtypes); if (null == feature && featuresOfSubtypes.isEmpty()) throw new CASException(CASException.UNDEFINED_FEATURE, new Object[]{featureName, currentFeatureType}); } if (null != feature) { features[i] = feature; if (feature.getRange().isPrimitive()) { log.trace("Feature {} identified as primitive-valued.", featureName); currentFeatureType = feature.getRange(); } else if (feature.getRange().isArray()) { log.trace("Feature {} identified as array-valued.", featureName); currentFeatureType = feature.getRange().getComponentType(); } else { log.trace("Feature {} identified as FeatureStructure-valued.", featureName); currentFeatureType = feature.getRange(); } log.debug("Determined type \"{}\" for feature \"{}\".", currentFeatureType, featureName); } } catch (Exception e) { log.error( "Error happened while initializing feature path \"{}\" on type \"{}\". Path element index: {} (\"{}\").", new Object[]{this.featurePathString, featurePathType, i, featureName}); throw e; } } currentType = featurePathType; featurePathChanged = false; } }  static Map<String, String> readReplacementsFromFile(String replacementsFile); static Map<String, String> readReplacementsFromInputStream(InputStream is); @Override int size(); @Override Feature getFeature(int i); @Override void addFeature(Feature feat); @Override void initialize(String featurePath); void initialize(String featurePath, Map<?, ?> replacements); @Override void typeInit(Type featurePathType); Object replaceValue(FeatureStructure fs); Object getValue(FeatureStructure fs, int startFeatureIndex); String[] getValueAsStringArray(FeatureStructure fs, boolean doReplacements); String[] getValueAsStringArray(FeatureStructure fs); List<String> getValueAsStringList(FeatureStructure fs, boolean doReplacements); List<String> getValueAsStringList(FeatureStructure fs); @Override String getValueAsString(FeatureStructure fs); String getValueAsString(FeatureStructure fs, boolean doReplacements); boolean getReplaceUnmappedValues(); void setReplaceUnmappedValues(boolean replaceWithNullValues); @Override String ll_getValueAsString(int fsRef, LowLevelCAS llCas); @Override Type getType(FeatureStructure fs); @Override TypeClass getTypClass(FeatureStructure fs); @Override String getFeaturePath(); @Override String getStringValue(FeatureStructure fs); @Override Integer getIntValue(FeatureStructure fs); @Override Boolean getBooleanValue(FeatureStructure fs); @Override Byte getByteValue(FeatureStructure fs); @Override Double getDoubleValue(FeatureStructure fs); @Override Float getFloatValue(FeatureStructure fs); @Override Long getLongValue(FeatureStructure fs); @Override Short getShortValue(FeatureStructure fs); @Override FeatureStructure getFSValue(FeatureStructure fs); void loadReplacementsFromFile(String replacementsFile); Object getDefaultReplacementValue(); void setDefaultReplacementValue(Object defaultReplacementValue); void clearReplacementCache();  }