001/** 002 * 003 */ 004package org.biopax.paxtools.examples; 005 006import java.util.HashSet; 007import java.util.Set; 008 009import org.biopax.paxtools.controller.EditorMap; 010import org.biopax.paxtools.controller.PropertyEditor; 011import org.biopax.paxtools.controller.SimpleEditorMap; 012import org.biopax.paxtools.model.BioPAXElement; 013 014/** 015 * Examples on how to use Paxtools BioPAX property editors and accessors API 016 * (based on java reflection). 017 * 018 * @author rodche 019 * 020 */ 021public final class UseOfReflection { 022 023 /** 024 * Example 1. 025 * 026 * How to get values from an object biopax property if the type of the biopax object 027 * is not known at runtime, and you do not want to always remember the 028 * domain and range of the property nor write many if-else statements to 029 * find out. 030 * 031 * @param bpe BioPAX object 032 * @param property BioPAX property 033 * @return the BioPAX object property values or empty set 034 */ 035 public static Set<? extends BioPAXElement> getObjectBiopaxPropertyValues(BioPAXElement bpe, String property) { 036 Set<BioPAXElement> values = new HashSet<BioPAXElement>(); 037 038 // get the BioPAX L3 property editors map 039 EditorMap em = SimpleEditorMap.L3; 040 041 // get the 'organism' biopax property editor, 042 // if exists for this type of bpe 043 @SuppressWarnings("unchecked") PropertyEditor<BioPAXElement, BioPAXElement> editor 044 = (PropertyEditor<BioPAXElement, BioPAXElement>) em 045 .getEditorForProperty(property, bpe.getModelInterface()); 046 047 // if the biopax object does have such property, get values 048 if (editor != null) { 049 return editor.getValueFromBean(bpe); 050 } else 051 return values; 052 } 053 054 055 056 /** 057 * Example 2. 058 * 059 * How to get values from a biopax property if the type of the biopax object 060 * is not known at runtime, and you do not want to always remember the 061 * domain and range of the property nor write many if-else statements to 062 * find out. 063 * 064 * @param bpe BioPAX object 065 * @param property BioPAX property 066 * @return the BioPAX property values or null 067 */ 068 public static Set getBiopaxPropertyValues(BioPAXElement bpe, String property) { 069 070 // get the BioPAX L3 property editors map 071 EditorMap em = SimpleEditorMap.L3; 072 073 // get the 'organism' biopax property editor, 074 // if exists for this type of bpe 075 @SuppressWarnings("unchecked") PropertyEditor<BioPAXElement, Object> editor 076 = (PropertyEditor<BioPAXElement, Object>) em 077 .getEditorForProperty(property, bpe.getModelInterface()); 078 079 // if the biopax object does have such property, get values 080 if (editor != null) { 081 return editor.getValueFromBean(bpe); 082 } else 083 return null; 084 } 085 086}