private static ArrayList<BooleanVariableInterface> chooseVariablesForConstraints(
      FeatureModel fm, int numVars) {
    ArrayList<BooleanVariableInterface> vars = new ArrayList<BooleanVariableInterface>(numVars);
    Collection<FeatureTreeNode> nodes = fm.getNodes();
    int numNodes = nodes.size();
    int varsGen = 0;

    while (varsGen < numVars) {
      int random = Math.abs(new Random().nextInt() % numNodes);
      Iterator<FeatureTreeNode> it = nodes.iterator();
      for (int i = 0; i < (random - 1); i++) {
        it.next();
      }
      FeatureTreeNode node = it.next();
      if (!(node instanceof FeatureGroup) && !(node instanceof RootNode)) {
        if (!vars.contains(node)) {
          vars.add(node);
          varsGen++;
        }
      }
    }
    //		System.out.println("choosen ==>");
    //		for( int i = 0 ; i < vars.size() ; i++ ) {
    //			System.out.print(vars.get(i).getName()+",");
    //		}
    //		System.out.println("");
    return vars;
  }
 public String toString() {
   String toString =
       "Product Catalog: "
           + featureModel.getName()
           + " ["
           + concreteComponents.size()
           + " components, "
           + products.size()
           + " products]----------------- \r\n";
   toString += "- Concrete Components \r\n";
   for (ProductComponent component : concreteComponents.values()) {
     toString += component.toString();
     toString += "\r\n";
   }
   toString += "- Products ------ \r\n";
   for (Product product : products.values()) {
     toString += product.toString();
     toString += "\r\n";
   }
   return toString;
 }
 public List<Product> filterProductsBasedOnFeatureModelSelection() {
   List<Product> filteredProducts = new LinkedList<Product>();
   filteredProducts.addAll(products.values());
   // for each component
   for (ProductComponent component : concreteComponents.values()) {
     // for each component type
     for (String componentType : component.getTypes()) {
       FeatureTreeNode featureNode = featureModel.getNodeByID(componentType);
       // if the component type is selected/deselected in the feature model
       if (featureNode.isInstantiated()) {
         // for each product
         for (Iterator<Product> it = filteredProducts.iterator(); it.hasNext(); ) {
           Product product = it.next();
           String productComponentType = product.getComponent(component.getID());
           // component type is selected in the feature model but is NOT part of the product -
           // remove product from filter list
           try {
             if (featureNode.getValue() == 1
                 && componentType.compareToIgnoreCase(productComponentType) != 0) {
               it.remove();
             }
             // or if component type is DEselected in the feature model but IS part of the product
             // - remove product from filter list
             else if (featureNode.getValue() == 0
                 && componentType.compareToIgnoreCase(productComponentType) == 0) {
               it.remove();
             }
           } catch (Exception e) {
             e.printStackTrace();
           }
         }
       }
     }
   }
   return filteredProducts;
 }
 public ProductCatalog(FeatureModel featureModel) {
   this.featureModel = featureModel;
   concreteComponents = new HashMap<String, ProductComponent>();
   products = new LinkedHashMap<String, Product>();
   extractComponentsFromFeatureModel(featureModel.getRoot());
 }
  public void init(String args[]) {

    if (restoreState(args[0])) {
      logLastFailure(collectionCurIndex);
      modelCurIndex++; // skip the failing model
    }

    initializeHeuristics(null);

    for (int collectionIndex = collectionCurIndex;
        collectionIndex < collections.length;
        collectionIndex++) {
      for (int heuristicIndex = heuristicCurIndex;
          heuristicIndex < heuristics.length;
          heuristicIndex++) {
        for (int modelIndex = modelCurIndex;
            modelIndex <= collections[collectionIndex].size;
            modelIndex++) {

          String modelName = getModelToProcess(collectionIndex, modelIndex);
          String heurName = heuristics[heuristicIndex];
          System.out.println("-------------------------------------------------------");
          System.out.println("Processing: [" + modelName + ", " + heurName + "]");

          saveState(collectionIndex, heuristicIndex, modelIndex);

          TestData data = new TestData();
          data.heuristic = heurName;
          data.modelName = modelName;

          //					if ( Math.abs(new Random().nextInt())% 5 == 0 ) {
          //						System.exit(-1);
          //					}

          FeatureModel fm =
              new XMLFeatureModel(
                  collectionPath + modelName + ".xml", XMLFeatureModel.SET_ID_AUTOMATICALLY);
          try {
            fm.loadModel();
          } catch (FeatureModelException e) {
            e.printStackTrace();
          }
          FeatureModelStatistics stats = new FeatureModelStatistics(fm);
          stats.update();
          data.fmSize = stats.countFeatures();
          data.fmMan = stats.countMandatory();
          data.fmOpt = stats.countOptional();
          data.fmGrp1 = stats.countGrouped11();
          data.fmGrpn = stats.countGrouped1n();
          data.ecClauses = stats.countExtraConstraintCNFClauses();
          data.ECR = ((stats.countConstraintVars() * 1.0) / data.fmSize);
          data.simplified = 0;
          if (simplifyModels) {
            fm.shrink();
            stats.update();
            data.simplified = 1;
          }
          data.fmSizeSimp = stats.countFeatures();
          data.ECRSimp = ((1.0 * stats.countConstraintVars()) / data.fmSizeSimp);

          VariableOrderingHeuristic heurObject =
              VariableOrderingHeuristicsManager.createHeuristicsManager().getHeuristic(heurName);
          heurObject.setParameter("feature_model", fm);
          heurObject.setParameter("start_node", "output");
          CNFFormula cnf = fm.FM2CNF();
          System.out.println(">> Running heuristic...");
          heurObject.run(cnf);
          data.heurTime = heurObject.getRunningTime();
          System.out.println(">> done! (" + data.heurTime + "ms)");

          ReasoningWithBDD reasoning =
              new FMReasoningWithBDD(
                  fm, heurObject, bddNodeNum, bddCacheSize, bddTimeout, reorderMethod, "pre-order");
          try {
            System.out.println(">> Building BDD...");
            reasoning.init();
            data.timeout = 0;
            System.out.println(
                ">> done! ("
                    + reasoning.getBDD().nodeCount()
                    + " BDD nodes - "
                    + reasoning.getBDDBuildingTime()
                    + "ms)");
          } catch (Exception e) {
            System.out.println(">>> Timeout: > " + bddTimeout);
            data.timeout = 1;
            System.out.println(">> done! (timed out!)");
          }

          data.bddTime = reasoning.getBDDBuildingTime();
          data.bddSize = reasoning.getBDD().nodeCount();
          data.sifting = 0;
          data.span =
              cnf.calculateClauseSpan(
                  VariableOrderingHeuristic.variableOrderingAsHashMap(
                      heurObject.getVariableOrdering()));

          long tempTime = System.nanoTime();
          reasoning.getBDD().satCount();
          data.timeToCountBDDSolutions = ((System.nanoTime() - tempTime) / 1000000.0);

          logData(data, collectionIndex);
        }
        saveState(collectionIndex, heuristicIndex, collections[collectionIndex].size + 1);
        modelCurIndex = modelStartIndex;
        if (heuristicIndex < heuristics.length - 1) {
          startNewHeuristicInLog(collectionIndex);
        }
      }
      heuristicCurIndex = 0;
    }
  }