code: public void addFactor(Factor factor) {
          int id = factor.getId();
          boolean alreadyAdded = (0 <= id && id < factors.size());
          if (alreadyAdded) {
              if (factors.get(id) != factor) {
                  throw new IllegalStateException("Factor id already set, but factor not yet added.");
              }
          } else {            
              // Factor was not yet in the factor graph.
              //
              // Check and set the id.
              if (id != -1 && id != factors.size()) {
                  throw new IllegalStateException("Factor id already set, but incorrect: " + id);
              }
              factor.setId(factors.size());
              // Add the factor.
              factors.add(factor);
              
              // Add each variable...
              for (Var var : factor.getVars()) {
                  // Add the variable.
                  addVar(var);
                  numUndirEdges++;
              }
  
              if (bg != null) { log.warn("Discarding BipartiteGraph. This may indicate inefficiency."); }
              bg = null;
          }
      }
docs: Adds a factor to this factor graph, if not already present, additionally adding any variables
  in its VarSet which have not already been added.
  
  @param var The factor to add.
  @return The node for this factor.