/* * Copyright 2007 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package Sequenic.T2.Engines; import java.security.SecureRandom; import java.util.* ; import java.lang.reflect.* ; /** * Note: currently methods that can't accept CUT as the receiver are * excluded from the scope. * * @author wishnu */ public class CombinatoricSeqGenerator extends AbstractCombinatoricSeqGenerator { /** * You need to pass the base-engine; it is needed so that we can read what * its scope is. * * @param generatorChoice To choose between permutative or cartesian sequence * generator. */ public CombinatoricSeqGenerator(BaseEngine e) { engine = e ; } public int r = 3 ; public int duplicates = 2 ; protected Constructor[] cons_scope ; protected Object[] fields_and_methods_scope ; AbstractCombinationGenerator combGenerator ; public void init() { int NumOfCons = engine.consIPs.size() ; assert NumOfCons>0 ; cons_scope = new Constructor[NumOfCons] ; int i = 0 ; for (Constructor con : engine.consIPs) { cons_scope[i] = con ; i++ ; } // Randomize the order of the constructors: Random rnd = new SecureRandom(); int j ; Constructor tmp ; for (int k=0; k methods = engine.getMethodsCanAcceptCUTinRec() ; Map specmap = engine.getMethodSpecs() ; int NumOfMF = engine.fieldsIPs.size() + methods.size() ; assert NumOfMF > 0 ; fields_and_methods_scope = new Object[NumOfMF] ; i=0 ; for (Method m : methods) { if (specmap.containsKey(m)) fields_and_methods_scope[i] = specmap.get(m) ; else fields_and_methods_scope[i] = m ; i++ ; } for (Field f : engine.fieldsIPs) { fields_and_methods_scope[i] = f ; i++ ; } // Randomize the order of the fields and methods : Object tmp2 ; for (int k=0; k= cons_scope.length) && (current_dup >= duplicates) && !combGenerator.hasMore() ; } private boolean nextCombination() { return (current_cons+1 >= cons_scope.length) && (current_dup >= duplicates) && combGenerator.hasMore() ; } private boolean nextConstructor() { return (current_cons+1 < cons_scope.length) && (current_dup >= duplicates) ; } private boolean nextDuplicate() { return current_dup < duplicates ; } @Override public List next_sequence() { if (done()) return null ; if (nextDuplicate()) { current_dup++ ; return current_seq ; } if (nextConstructor()) { current_cons++ ; current_dup=1 ; current_seq.set(0,cons_scope[current_cons]) ; return current_seq ; } if (nextCombination()) { current_cons = 0 ; current_dup = 1 ; get_new_mseq() ; current_seq.set(0,cons_scope[current_cons]) ; return current_seq ; } assert false ; return null ; } public int totalNumberOfCombinations() { return combGenerator.getTotal() * duplicates * cons_scope.length ; } }