001package org.biopax.paxtools.controller; 002 003import org.biopax.paxtools.model.BioPAXElement; 004import org.biopax.paxtools.model.BioPAXLevel; 005import org.biopax.paxtools.model.Model; 006 007/** 008 * "Clones" a BioPAX element - using direct properties and dependent children only. 009 * (shallow copy). 010 * 011 * Compare to {@link Fetcher} 012 * 013 * @see org.biopax.paxtools.controller.Visitor 014 * @see org.biopax.paxtools.controller.Traverser 015 */ 016public class ShallowCopy implements Visitor 017{ 018 Traverser traverser; 019 020 private BioPAXElement copy; 021 private BioPAXLevel level; 022 023 /** 024 * Editor map based constructor. 025 * @param map that determines the BioPAX Level 026 * @deprecated use Level based constructor instead. 027 */ 028 public ShallowCopy(EditorMap map) 029 { 030 traverser = new Traverser(map, this); 031 this.level = map.getLevel(); 032 } 033 034 /** 035 * BioPAXLevel based constructor 036 * @param level used for the cloning operation. 037 */ 038 public ShallowCopy(BioPAXLevel level) 039 { 040 this.level = level; 041 traverser = new Traverser(SimpleEditorMap.get(level), this); 042 } 043 044 /** 045 * Empty constructos that defaults to BioPAX L3. 046 */ 047 public ShallowCopy() 048 { 049 this(BioPAXLevel.L3); 050 } 051 052 053 /** 054 * Creates a copy of the BioPAX object with all its properties 055 * are the same, and also adds it to a model. 056 * 057 * @param <T> BioPAX type/class of the source and copy elements 058 * @param model target biopax model 059 * @param source a biopax object to copy 060 * @param newID new (copy) biopax object's URI 061 * @return copy of the source biopax element 062 */ 063 public <T extends BioPAXElement> T copy(Model model, T source, String newID) 064 { 065 T copy = copy(source, newID); 066 model.add(copy); 067 return copy; 068 } 069 070 071 /** 072 * Returns a copy of the BioPAX element 073 * (with all the property values are same) 074 * 075 * @param <T> biopax type 076 * @param source biopax element to copy 077 * @param newID copy biopax element's absolute URI 078 * @return a copy of the source biopax element 079 */ 080 public <T extends BioPAXElement> T copy(T source, String newID) 081 { 082 T copy = (T) level.getDefaultFactory().create( 083 (Class<T>) source.getModelInterface(), newID); 084 this.copy = copy; 085 // to avoid unnecessary checks/warnings when existing valid element is being cloned 086 //(e.g., copying BPS.stepProcess values, if there is a Conversion, which was set via stepConversion). 087 AbstractPropertyEditor.checkRestrictions.set(false); 088 traverser.traverse(source, null); 089 AbstractPropertyEditor.checkRestrictions.set(true);//back to default 090 return copy; 091 } 092 093 094// --------------------- Interface Visitor --------------------- 095 096 /** 097 * {@inheritDoc} 098 */ 099 public void visit(BioPAXElement domain, Object range, Model model, PropertyEditor editor) 100 { 101 editor.setValueToBean(range, copy); 102 } 103} 104 105 106