001package org.biopax.paxtools.pattern; 002 003import org.biopax.paxtools.model.BioPAXElement; 004 005import java.util.Collection; 006 007/** 008 * This is a mapping from a constraint to the elements in a match. 009 * 010 * @author Ozgun Babur 011 */ 012public class MappedConst implements Constraint 013{ 014 /** 015 * The constraint to map. 016 */ 017 private Constraint constr; 018 019 /** 020 * Indexes of elements in the match for the constraint to check validity. 021 */ 022 private int[] inds; 023 024 /** 025 * Constructor with the constraint and the index mapping. 026 * @param constr constraint to map 027 * @param inds mapped indexes 028 */ 029 public MappedConst(Constraint constr, int ... inds) 030 { 031 this.constr = constr; 032 this.inds = inds; 033 } 034 035 /** 036 * Getter for the constraint. 037 * @return the wrapped constraint 038 */ 039 public Constraint getConstr() 040 { 041 return constr; 042 } 043 044 /** 045 * Getter for the mapped indices. 046 * @return mapped indices 047 */ 048 public int[] getInds() 049 { 050 return inds; 051 } 052 053 /** 054 * Gets the maximum index. 055 * @return maximum index 056 */ 057 public int getMaxInd() 058 { 059 int max = -1; 060 for (int ind : inds) 061 { 062 if (ind > max) max = ind; 063 } 064 return max; 065 } 066 067 /** 068 * This methods translates the indexes of outer constraint, to this inner constraint. 069 * 070 * @param outer mapped indices for the outer constraints 071 * @return translated indices 072 */ 073 protected int[] translate(int[] outer) 074 { 075 int[] t = new int[inds.length]; 076 for (int i = 0; i < t.length; i++) 077 { 078 t[i] = outer[inds[i]]; 079 } 080 return t; 081 } 082 083 /** 084 * Can generate only if the wrapped constraint is generative. 085 * @return if the wrapped constraint is generative 086 */ 087 public boolean canGenerate() 088 { 089 return constr.canGenerate(); 090 } 091 092 /** 093 * Calls generate method of the constraint with index translation. 094 * @param match current pattern match 095 * @param outer untranslated indices 096 * @return generated satisfying elements 097 */ 098 @Override 099 public Collection<BioPAXElement> generate(Match match, int... outer) 100 { 101 return constr.generate(match, translate(outer)); 102 } 103 104 /** 105 * Directs to satisfies method of the wrapped constraint with index translation. 106 * @param match current pattern match 107 * @param outer untranslated indices 108 * @return true if the constraint is satisfied 109 */ 110 public boolean satisfies(Match match, int ... outer) 111 { 112 return constr.satisfies(match, translate(outer)); 113 } 114 115 /** 116 * Gets variable size of the wrapped constraint. 117 * @return variable size of the wrapped constraint 118 */ 119 @Override 120 public int getVariableSize() 121 { 122 return constr.getVariableSize(); 123 } 124 125}