Package urban.shapes

Source Code of urban.shapes.Generator

package urban.shapes;

import java.util.Map.Entry;

import org.antlr.runtime.RecognitionException;

import urban.model.Rule;
import urban.parser.UrbanParser;
import urban.transformers.RuleToGeneratorTransformer;

/**
* Used as a key for mappings within ShapeParameters.
*
* The methods are used for rule generation code.
*/
public class Generator {
  private final RuleGraph rg;

  /**
   * Useful method for testing
   * @param string
   * @return generator constructed from string
   */
  public static Generator createGeneratorFromString(String string){
    try {
      Rule rule = UrbanParser.parse(string + "@ 0,0\n").rule();
      return new RuleToGeneratorTransformer().transform(rule);
    } catch (RecognitionException e) {
      e.printStackTrace();
      return null;
    }
  }
  /**
   * @param rg
   */
  public Generator(RuleGraph rg) {
    this.rg = rg;
  }

  /**
   * @return The basic rule for a flip or bond
   */
  public RuleGraph getRuleGraph() { 
    return rg;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((rg == null) ? 0 : rg.getRoot().getName().hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Generator other = (Generator) obj;
    if (rg == null) {
      if (other.rg == null)
        return true;
    } else if (rg.getRoot() instanceof SiteNode && other.rg.getRoot() instanceof SiteNode){
      return compareNodes((SiteNode)rg.getRoot(), (SiteNode)other.rg.getRoot());
    } else if (rg.getRoot() instanceof BondNode && other.rg.getRoot() instanceof BondNode){
      return compareChildren(rg, other.rg);
    }
    return false;
  }
  private boolean compareChildren(RuleGraph a, RuleGraph b) {
    for(Entry<Link, RuleGraph> e : a.getChildren()){
      final RuleGraph tmp = b.getChild(e.getKey());
      if (tmp == null)
        return false;
      if (!e.getValue().getRoot().getName().equals(tmp.getRoot().getName()))
        return false;
    }
    return true;
  }
  private boolean compareNodes(SiteNode a, SiteNode b) {
    return a.getName().equals(b.getName()) && a.getLeft().equals(b.getLeft()) && a.getRight().equals(b.getRight());
  }
}
TOP

Related Classes of urban.shapes.Generator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.