public class ClassElement implements OntologyElement {

	/** max min sum */
	String function;

	Score score;

	boolean isAnswer;

	public boolean isAnswer() {
		return isAnswer;
	}

	public void setAnswer(boolean isAnswer) {
		this.isAnswer = isAnswer;
	}

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	/*
	 * this flag indicates whether the element is already in the list of
	 * elements and whether it should be ignored except for the function; this
	 * happens when some existing elements are modified with for example max min
	 * etc
	 */
	boolean alreadyAdded;

	public boolean isAlreadyAdded() {
		return alreadyAdded;
	}

	public void setAlreadyAdded(boolean alreadyAdded) {
		this.alreadyAdded = alreadyAdded;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -8004498024299257803L;
	public List<String> results = null;
	public boolean mainSubject = false;
	private SerializableURI data = null;
	private String variable = null;

	// public OntologyElement closestOntologyConcept;
	Annotation annotation = new Annotation();

	public void setAnnotation(Annotation annotation) {
    this.annotation = annotation;
  }

  public List<String> getResults() {
		return results;
	}

	public void setResults(List<String> results) {
		this.results = results;
	}

	public boolean isMainSubject() {
		return mainSubject;
	}

	public void setMainSubject(boolean mainSubject) {
		this.mainSubject = mainSubject;
	}

	public String getVariable() {
		return variable;
	}

	public void setVariable(String variable) {
		this.variable = variable;
	}

	public SerializableURI getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = (SerializableURI) data;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer result = new StringBuffer("");
		result.append(this.getClass().getSimpleName().toString());
		result.append(" Is it the main subject? ").append(isMainSubject());
		if (annotation != null)
			result.append(annotation.toString());
		result.append("\nData/URI: ").append(this.data).toString();
		if (this.variable != null)
			result.append(" \n Variable:").append(this.variable);
		else
			result.append(" Variable: null");
		result.append("\nResults:" + results);
		return result.toString();

	}

	public Annotation getAnnotation() {
		return annotation;
	}

	public Score getScore() {
		return this.score;
	}

	public void setScore(Score score) {
		this.score = score;
	}

	@Override
	public boolean equals(Object anotherObject) {
		boolean isEqual = false;
		if (this == anotherObject)
			return true;
		if (!(anotherObject instanceof ClassElement))
			return false;
		
		if ((this.annotation != null)
				&& (((ClassElement) anotherObject).getAnnotation() != null)
				&& (this.getAnnotation().equals(((ClassElement) anotherObject)
						.getAnnotation()))
				&& this.isMainSubject() == ((ClassElement) anotherObject)
						.isMainSubject()
				&& this.isAlreadyAdded() == ((ClassElement) anotherObject)
						.isAlreadyAdded()
				&& this.getData().toString().equals(
						((ClassElement) anotherObject).getData().toString()))
			isEqual = true;
		return isEqual;

	}

}
 public class DatatypePropertyValueElement implements OntologyElement {

	boolean isAnswer;

	public boolean isAnswer() {
		return isAnswer;
	}

	public void setAnswer(boolean isAnswer) {
		this.isAnswer = isAnswer;
	}

	/** max min sum */
	String function;

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	/*
	 * this flag indicates whether the element is already in the list of
	 * elements and whether it should be ignored except for the function; this
	 * happens when some existing elements are modified with for example max min
	 * etc
	 */
	boolean alreadyAdded;

	public boolean isAlreadyAdded() {
		return alreadyAdded;
	}

	public void setAlreadyAdded(boolean alreadyAdded) {
		this.alreadyAdded = alreadyAdded;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -4392076964893243529L;
	public boolean mainSubject = false;
	public List<String> results;
	Annotation annotation = new Annotation();

	public Annotation getAnnotation() {
		return annotation;
	}

	public void setAnnotation(Annotation annotation) {
		this.annotation = annotation;
	}

	public List<String> getResults() {
		return results;
	}

	public void setResults(List<String> results) {
		this.results = results;
	}

	public boolean isMainSubject() {
		return mainSubject;
	}

	public void setMainSubject(boolean mainSubject) {
		this.mainSubject = mainSubject;
	}

	private String variable;

	public String getVariable() {
		return variable;
	}

	public void setVariable(String variable) {
		this.variable = variable;
	}

	private DatatypePropertyValueIdentifier data;

	public Object getData() {
		return (DatatypePropertyValueIdentifier) data;
	}

	public void setData(Object data) {
		this.data = (DatatypePropertyValueIdentifier) data;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer result = new StringBuffer("");
		result.append(this.getClass().toString());
		result.append(" Is it the main subject? ").append(isMainSubject());
		if (annotation != null)
			result.append(annotation.toString());
		result.append(" Data: ").append(getData()).toString();
		if (this.variable != null)
			result.append(" \n Variable:").append(this.variable);
		result.append("\nResults:" + results);
		return result.toString();

	}

	Score score;

	public Score getScore() {
		return this.score;
	}

	public void setScore(Score score) {
		this.score = score;
	}

	public Object clone() {
		Object deepCopy = null;
		ByteArrayOutputStream byteArrOs = new ByteArrayOutputStream();
		ObjectOutputStream objOs;
		try {
			objOs = new ObjectOutputStream(byteArrOs);

			objOs.writeObject(this);

			ByteArrayInputStream byteArrIs = new ByteArrayInputStream(byteArrOs
					.toByteArray());
			ObjectInputStream objIs = new ObjectInputStream(byteArrIs);
			deepCopy = objIs.readObject();
			return deepCopy;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return deepCopy;
	}

	public boolean equals(Object anotherObject) {
		if (this == anotherObject)
			return true;
		if (!(anotherObject instanceof DatatypePropertyValueElement))
			return false;
		if ((this.annotation != null)
				&& (((DatatypePropertyValueElement) anotherObject)
						.getAnnotation() != null)
				&& (this.getAnnotation()
						.equals(((DatatypePropertyValueElement) anotherObject)
								.getAnnotation()))
				&& this.isMainSubject() == ((DatatypePropertyValueElement) anotherObject)
						.isMainSubject()
				&& this.isAlreadyAdded() == ((DatatypePropertyValueElement) anotherObject)
						.isAlreadyAdded()
				&& ((DatatypePropertyValueIdentifier) this.getData())
						.equals(((DatatypePropertyValueIdentifier) ((DatatypePropertyValueElement) anotherObject)
								.getData()).toString())
	)
			return true;
		else
			return false;
	}
}
 public class GroupElement implements OntologyElement {

	boolean isAnswer;

	public boolean isAnswer() {
		return isAnswer;
	}

	public void setAnswer(boolean isAnswer) {
		this.isAnswer = isAnswer;
	}

	/** max min sum */
	String function;

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	/*
	 * this flag indicates whether the element is already in the list of
	 * elements and whether it should be ignored except for the function; this
	 * happens when some existing elements are modified with for example max min
	 * etc
	 */
	boolean alreadyAdded;

	public boolean isAlreadyAdded() {
		return alreadyAdded;
	}

	public void setAlreadyAdded(boolean alreadyAdded) {
		this.alreadyAdded = alreadyAdded;
	}

	String type;
	List data;
	Annotation annotation = new Annotation();

	public Annotation getAnnotation() {
		return annotation;
	}

	public void setAnnotation(Annotation annotation) {
		this.annotation = annotation;
	}

	public void setData(List data) {
		this.data = data;
	}

	public List getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = (List) data;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer result = new StringBuffer("");
		result.append(this.getClass().toString()).append(annotation.toString())
				.append(" Data: ").append(getData()).append(" Type: ").append(
						this.type).toString();
		// if (this.variable != null)
		// result.append(" \n Variable:").append(this.variable);
		return result.toString();

	}

	public List<String> getResults() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getVariable() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isMainSubject() {
		// TODO Auto-generated method stub
		return false;
	}

	public void setMainSubject(boolean value) {
		// TODO Auto-generated method stub

	}

	public void setResults(List<String> results) {
		// TODO Auto-generated method stub

	}

	public void setVariable(String key) {
		// autogenerated...

	}

	Score score;

	public Score getScore() {
		return this.score;
	}

	public void setScore(Score score) {
		this.score = score;
	}
	
	
}
 public class InstanceElement implements OntologyElement {
  private static final Log logger = LogFactory.getLog(InstanceElement.class);

  boolean isAnswer;

  public boolean isAnswer() {
    return isAnswer;
  }

  public void setAnswer(boolean isAnswer) {
    this.isAnswer = isAnswer;
  }

  /** max min sum */
  String function;

  public String getFunction() {
    return function;
  }

  public void setFunction(String function) {
    this.function = function;
  }

  /*
   * this flag indicates whether the element is already in the list of elements
   * and whether it should be ignored except for the function; this happens when
   * some existing elements are modified with for example max min etc
   */
  boolean alreadyAdded;

  public boolean isAlreadyAdded() {
    return alreadyAdded;
  }

  public void setAlreadyAdded(boolean alreadyAdded) {
    this.alreadyAdded = alreadyAdded;
  }

  public boolean mainSubject = false;

  public List<String> results;

  Annotation annotation = new Annotation();

  public Annotation getAnnotation() {
    return annotation;
  }

  public void setAnnotation(Annotation annotation) {
    this.annotation = annotation;
  }

  public void setData(SerializableURI data) {
    this.data = data;
  }

  public List<String> getResults() {
    return results;
  }

  public void setResults(List<String> results) {
    this.results = results;
  }

  public boolean isMainSubject() {
    return mainSubject;
  }

  public void setMainSubject(boolean mainSubject) {
    this.mainSubject = mainSubject;
  }

  private SerializableURI data;

  private String variable = null;

  public String getVariable() {
    return variable;
  }

  public void setVariable(String variable) {
    this.variable = variable;
  }

  private SerializableURI classURI;

  private List<String> classURIList;

  public SerializableURI getClassURI() {
    return classURI;
  }

  public void setClassURI(SerializableURI classURI) {
    this.classURI = classURI;
  }

  public SerializableURI getData() {
    return data;
  }

  public void setData(Object data) {
    this.data = (SerializableURI)data;
  }

  /**
   * @see java.lang.Object#toString()
   */
  public String toString() {
    StringBuffer s =
            new StringBuffer(this.getClass().getSimpleName().toString());
    s.append(" Is it the main subject? ").append(isMainSubject());
    if(annotation != null) s.append(" ").append(annotation.toString());
    s.append(" Instance URI: ").append(this.data).append(" Class URI: ")
            .append(this.classURI).append(" Variable:")
            .append(this.variable);
    s.append(" Results:" + results);
    s.append(" Class URIs");
    if(this.classURIList != null) s.append(this.classURIList.toString());
    return s.toString();
  }

  public List<String> getClassURIList() {
    return classURIList;
  }

  public void setClassURIList(List<String> classURIList) {
    this.classURIList = classURIList;
  }

  Score score;

  public Score getScore() {
    return this.score;
  }

  public void setScore(Score score) {
    this.score = score;
  }

  @Override
  public boolean equals(Object anotherObject) {
    if(this == anotherObject) return true;
    if(!(anotherObject instanceof InstanceElement)) return false;
    //logger.debug("calling instance element equals method...for:"
    //        + this.toString() + " and " + anotherObject.toString());
    boolean annotationsEqual =
            this.annotation != null
                    && (((InstanceElement)anotherObject).getAnnotation() != null)
                    && (this.getAnnotation()
                            .equals(((InstanceElement)anotherObject)
                                    .getAnnotation()));
    boolean isMainSubjectEqual =
            this.isMainSubject() == ((InstanceElement)anotherObject)
                    .isMainSubject();
    boolean isUriEqual =
            ((SerializableURI)this.getData()).toString().equals(
                    ((SerializableURI)((InstanceElement)anotherObject)
                            .getData()).toString());
   // logger.debug("annotationsEqual:" + annotationsEqual + "isMainSubjectEqual:"
    //        + isMainSubjectEqual + "isUriEqual:" + isUriEqual);
    if(annotationsEqual && isMainSubjectEqual && isUriEqual)
      return true;
    else return false;
  }
}
 public class InstanceListElement implements OntologyElement {
  private static final Log logger = LogFactory.getLog(InstanceListElement.class);

  boolean isAnswer;

  public boolean isAnswer() {
    return isAnswer;
  }

  public void setAnswer(boolean isAnswer) {
    this.isAnswer = isAnswer;
  }

  /** max min sum */
  String function;

  public String getFunction() {
    return function;
  }

  public void setFunction(String function) {
    this.function = function;
  }

  /*
   * this flag indicates whether the element is already in the list of elements
   * and whether it should be ignored except for the function; this happens when
   * some existing elements are modified with for example max min etc
   */
  boolean alreadyAdded;

  public boolean isAlreadyAdded() {
    return alreadyAdded;
  }

  public void setAlreadyAdded(boolean alreadyAdded) {
    this.alreadyAdded = alreadyAdded;
  }

  public boolean mainSubject = false;

  public List<String> results;

  Annotation annotation = new Annotation();

  public Annotation getAnnotation() {
    return annotation;
  }

  public void setAnnotation(Annotation annotation) {
    this.annotation = annotation;
  }


  public List<String> getResults() {
    return results;
  }

  public void setResults(List<String> results) {
    this.results = results;
  }

  public boolean isMainSubject() {
    return mainSubject;
  }

  public void setMainSubject(boolean mainSubject) {
    this.mainSubject = mainSubject;
  }

  private List<SerializableURI> data;

  private String variable = null;

  public String getVariable() {
    return variable;
  }

  public void setVariable(String variable) {
    this.variable = variable;
  }

  private SerializableURI classURI;

  private List<String> classURIList;

  public SerializableURI getClassURI() {
    return classURI;
  }

  public void setClassURI(SerializableURI classURI) {
    this.classURI = classURI;
  }

  /**
   * @see java.lang.Object#toString()
   */
  public String toString() {
    StringBuffer s =
            new StringBuffer(this.getClass().getSimpleName().toString());
    s.append(" Is it the main subject? ").append(isMainSubject());
    if(annotation != null) s.append(annotation.toString());
    s.append(" Instance URI: ").append(this.data).append(" Class URI: ")
            .append(this.classURI).append(" \n Variable:")
            .append(this.variable);
    s.append("\nResults:" + results);
    s.append("Class URIs");
    if(this.classURIList != null) s.append(this.classURIList.toString());
    return s.toString();
  }

  public List<String> getClassURIList() {
    return classURIList;
  }

  public void setClassURIList(List<String> classURIList) {
    this.classURIList = classURIList;
  }

  Score score;

  public Score getScore() {
    return this.score;
  }

  public void setScore(Score score) {
    this.score = score;
  }

  @Override
  public boolean equals(Object anotherObject) {
    if(this == anotherObject) return true;
    if(!(anotherObject instanceof InstanceListElement)) return false;
    //logger.debug("calling instance list element equals method...for:"
    //        + this.toString() + " and " + anotherObject.toString());
    boolean annotationsEqual =
            this.annotation != null
                    && (((InstanceListElement)anotherObject).getAnnotation() != null)
                    && (this.getAnnotation()
                            .equals(((InstanceListElement)anotherObject)
                                    .getAnnotation()));
    boolean isMainSubjectEqual =
            this.isMainSubject() == ((InstanceListElement)anotherObject)
                    .isMainSubject();
    boolean isUriEqual =
            ((List<SerializableURI>)this.getData()).toString().equals(
                    ((List<SerializableURI>)((InstanceListElement)anotherObject)
                            .getData()).toString());
    //logger.debug("annotationsEqual:" + annotationsEqual + "isMainSubjectEqual:"
    //        + isMainSubjectEqual + "isUriEqual:" + isUriEqual);
    if(annotationsEqual && isMainSubjectEqual && isUriEqual)
      return true;
    else return false;
  }

  public List<SerializableURI> getData() {
    return data;
  }

  public void setData(Object data) {
    this.data=(List<SerializableURI>)data;
    
  }
}
 public class JokerElement implements OntologyElement {

	boolean isAnswer;

	public boolean isAnswer() {
		return isAnswer;
	}

	public void setAnswer(boolean isAnswer) {
		this.isAnswer = isAnswer;
	}

	/** max min sum */
	String function;

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	/*
	 * this flag indicates whether the element is already in the list of
	 * elements and whether it should be ignored except for the function; this
	 * happens when some existing elements are modified with for example max min
	 * etc
	 */
	boolean alreadyAdded;

	public boolean isAlreadyAdded() {
		return alreadyAdded;
	}

	public void setAlreadyAdded(boolean alreadyAdded) {
		this.alreadyAdded = alreadyAdded;
	}

	public boolean mainSubject = false;
	public List<String> results;
	Annotation annotation = new Annotation();

	public Annotation getAnnotation() {
		return annotation;
	}

	public void setAnnotation(Annotation annotation) {
		this.annotation = annotation;
	}

	public List<String> getResults() {
		return results;
	}

	public void setResults(List<String> results) {
		this.results = results;
	}

	public boolean isMainSubject() {
		return mainSubject;
	}

	public void setMainSubject(boolean mainSubject) {
		this.mainSubject = mainSubject;
	}

	private String variable;

	public String getVariable() {
		return variable;
	}

	public void setVariable(String variable) {
		this.variable = variable;
	}

	private Object data;

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	// whether it's a class or property joker
	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public boolean equals(Object anotherObject) {
		if (this == anotherObject)
			return true;
		if (!(anotherObject instanceof JokerElement))
			return false;
		if ((this.annotation != null)
				&& (((JokerElement) anotherObject).getAnnotation() != null)
				&& (this.getAnnotation().equals(((JokerElement) anotherObject)
						.getAnnotation()))
				&& this.isMainSubject() == ((JokerElement) anotherObject)
						.isMainSubject()
				&& this.isAlreadyAdded() == ((JokerElement) anotherObject)
						.isAlreadyAdded()
		)
			return true;
		else
			return false;
	}

	public String toString() {
		StringBuffer buff = new StringBuffer((this.getClass().getSimpleName()
				.toString()));
		buff.append("\nIs it the main subject? ").append(isMainSubject());
		buff.append("\nVariable:");
		buff.append("\nAnnotation:"+this.annotation);
		if (this.variable != null)
			buff.append(this.variable);
		buff.append("\nResults:" + results);
		buff.append("\nType: " + this.type);

		buff.append("\n");
		return buff.toString();
	}

	Score score;

	public Score getScore() {
		return this.score;
	}

	public void setScore(Score score) {
		this.score = score;
	}

}
 public interface OntologyElement extends Serializable {

	public String getFunction();

	public void setFunction(String function);

	public boolean isAlreadyAdded();

	public void setAlreadyAdded(boolean alreadyAdded);

	public Annotation getAnnotation();
	
	public void setAnnotation(Annotation annotation);

	public String getVariable();

	public void setVariable(String key);

	public boolean isMainSubject();

	public void setMainSubject(boolean value);

	public List<String> getResults();

	public void setResults(List<String> results);

	public Object getData();

	public void setData(Object data);

	public void setScore(Score score);

	public Score getScore();

	public boolean isAnswer();

	public void setAnswer(boolean isAnswer);

	public boolean equals(Object o);

}
 public class PropertyElement implements OntologyElement {

	boolean isAnswer;

	public boolean isAnswer() {
		return isAnswer;
	}

	public void setAnswer(boolean isAnswer) {
		this.isAnswer = isAnswer;
	}

	/** max min sum */
	String function;

	/**
	 * governor is set for datatype properties and refers to the domain of this
	 * property
	 */
	OntologyElement governor;

	public OntologyElement getGovernor() {
		return this.governor;
	}

	public void setGovernor(OntologyElement ontologyElement) {
		this.governor = ontologyElement;
	}

	public String getRange() {
		return range;
	}

	public void setRange(String range) {
		this.range = range;
	}

	/* uri of the property range */
	String range;

	String domain;
	
	public String getDomain() {
    return domain;
  }

  public void setDomain(String domain) {
    this.domain = domain;
  }

  public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	/*
	 * this flag indicates whether the element is already in the list of
	 * elements and whether it should be ignored except for the function; this
	 * happens when some existing elements are modified with for example max min
	 * etc
	 */
	boolean alreadyAdded;

	public boolean isAlreadyAdded() {
		return alreadyAdded;
	}

	public void setAlreadyAdded(boolean alreadyAdded) {
		this.alreadyAdded = alreadyAdded;
	}

	public boolean mainSubject = false;
	public List<String> results;
	boolean isDatatypeProperty;

	public boolean isDatatypeProperty() {
		return isDatatypeProperty;
	}

	public void setDatatypeProperty(boolean isDatatypeProperty) {
		this.isDatatypeProperty = isDatatypeProperty;
	}

	Annotation annotation = new Annotation();

	public Annotation getAnnotation() {
		return annotation;
	}

	public void setAnnotation(Annotation annotation) {
		this.annotation = annotation;
	}

	// public OntologyElement closestOntologyConcept;
	//
	// public OntologyElement getClosestOntologyConcept() {
	// return closestOntologyConcept;
	// }
	//
	// public void setClosestOntologyConcept(OntologyElement
	// closestOntologyElement) {
	// this.closestOntologyConcept = closestOntologyElement;
	// }

	public void setData(SerializableURI data) {
		this.data = data;
	}

	public List<String> getResults() {
		return results;
	}

	public void setResults(List<String> results) {
		this.results = results;
	}

	public boolean isMainSubject() {
		return mainSubject;
	}

	public void setMainSubject(boolean mainSubject) {
		this.mainSubject = mainSubject;
	}

	private String variable = null;

	public String getVariable() {
		return variable;
	}

	public void setVariable(String variable) {
		this.variable = variable;
	}

	private SerializableURI data;

	private boolean inverseProperty;

	public boolean isInverseProperty() {
		return inverseProperty;
	}

	public void setInverseProperty(boolean inverseProperty) {
		this.inverseProperty = inverseProperty;
	}

	public SerializableURI getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = (SerializableURI) data;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer result = new StringBuffer(this.getClass().getSimpleName()
				.toString());
		result.append(" Is it the main subject? ").append(isMainSubject());
		result.append("\nProperty URI: ").append(this.data);
		if (annotation != null)
			result.append(getAnnotation().toString());
		if (score != null)
			result.append(getScore().toString()).append("\nInverse property:")
					.append(isInverseProperty()).append("\nVariable:").append(
							this.variable);
		result.append("\nResults:" + results);
		result.append("\nRange:" + range);
	  result.append("\nIsDatatypeproperty?:" + isDatatypeProperty);
		return result.toString();
	}

	Score score;

	public Score getScore() {
		return this.score;
	}

	public void setScore(Score score) {
		this.score = score;
	}

	@Override
	public boolean equals(Object anotherObject) {
		if (this == anotherObject)
			return true;
		if (!(anotherObject instanceof PropertyElement))
			return false;
		if ((this.annotation != null)
				&& (((PropertyElement) anotherObject).getAnnotation() != null)
				&& (this.getAnnotation()
						.equals(((PropertyElement) anotherObject)
								.getAnnotation()))
				&& this.isMainSubject() == ((PropertyElement) anotherObject)
						.isMainSubject()
				&& this.isAlreadyAdded() == ((PropertyElement) anotherObject)
						.isAlreadyAdded()
				&& this.getData().toString().equals(
						((PropertyElement) anotherObject).getData().toString()))
			return true;
		else
			return false;
	}

}
 public class TripleElement implements Serializable{

	private List<List<OntologyElement>> elements;

	public List<List<OntologyElement>> getElements() {
		return elements;
	}

	public void setElements(List<List<OntologyElement>> elements) {
		this.elements = elements;
	}

	public String toString() {
		StringBuffer buff = new StringBuffer("");
		buff.append("Triple: ");
		for (List<OntologyElement> col : getElements()) {
		  for (OntologyElement el:col){
			buff.append(el.getData());
			buff.append(" AND ");
		  }
			buff.append("-");
		}
		buff.append("\n");
		return buff.toString();
	}
}
 public class StringUtil {
    static org.apache.commons.logging.Log logger =
                    org.apache.commons.logging.LogFactory.getLog(StringUtil.class);

    /**
     * This method assume that given string is in the form of QueryResultsTable.toString() with 2 columns and converts
     * it to HashMap where key is the first entry from the table and the value is the set of second entries.
     * 
     * @param resultsTable
     * @return a Map with a key taken from the first column of the table and a value being a set of values taken from
     *         the second column.
     */
    public static Map<String, Set<String>> fromStringToMap(String resultsTable) {
        Map<String, Set<String>> map = new HashMap<String, Set<String>>();
        String[] rows = resultsTable.split(CATConstants.NEW_LINE);
        for (String eachRow : rows) {
            String[] columns = eachRow.split("\\|");
            if (columns.length == 2) {
                String propertyUri = columns[0].trim();
                String classUri = columns[1].trim();
                Set<String> classes = map.get(propertyUri);
                if (classes == null) {
                    classes = new HashSet<String>();
                }
                classes.add(classUri);
                try {
                    SerializableURI uri = new SerializableURI(propertyUri, false);
                    map.put(propertyUri, classes);
                } catch (Exception e) {
                    // logger.debug("URI:" + propertyUri + " is not valid.\n");
                }
            }
        }
        return map;
    }

    /**
     * This method assume that given string is in the form of QueryResultsTable.toString() with 1 column and converts it
     * to the Set of strings i.e. values from the each row of the table
     * 
     * @param resultsTable
     * @return a Set of Strings from the given table (string separated by new lines)
     */
    public static Set<String> fromStringToSet(String resultsTable) {
        Set<String> set = new HashSet<String>();
        String[] rows = resultsTable.split(CATConstants.NEW_LINE);
        for (String eachRow : rows) {
            String uri = eachRow.trim();
            try {
                SerializableURI uriUri = new SerializableURI(uri, false);
                set.add(uri);
            } catch (Exception e) {
                // logger.debug("URI:" + uri + " is not valid.\n");
            }
        }
        return set;
    }

    /**
     * This method takes a string and beautifies it by inserting spaces instead of dash and underline and also by
     * splitting camelCased words
     * 
     * @param inputString
     * @return
     */
    public static String beautifyString(String inputString) {
        String resultString =
                        ExpressionFinder.findAndSeparateCamelCases(inputString,
                                        CATConstants.REGEX_CAMEL_CASE, " ");
        // also replace dashes and underscores with space
        if (resultString.contains("_"))
            resultString = resultString.replace('_', ' ');
        if (resultString.contains("-"))
            resultString = resultString.replace('-', ' ');
        return resultString;
    }

    /**
     * This method is actually cleaning the string from any spaces, dashes, etc. Additionally, it executes the method
     * for finding two ontology resources that are not separated by a property and adds a so called 'joker' for example
     * joker:i0:i1:joker if there were no p elements between i0 and i1 Also, it checks the first and the last element in
     * the string and in case it reveals a property it adds c:firstJoker before the first element or c:lastJoker after
     * the last element
     * 
     * @param interpretationOfResources
     * @return
     */
    public static String createCompleteString(String interpretationOfResources) {
        /*
         * this method will find any places in the string where potential properties can be added
         */
        String interpretationOfResourcesWithFoundJokers =
                        JokerFinder.findAndReplaceGroupOccurencies(
                                        interpretationOfResources.toString(),
                                        FreyaConstants.REGEX_FIND_JOKER, FreyaConstants.JOKER);
        logger.info("After injecting property jokers:"
                        + interpretationOfResourcesWithFoundJokers);
        interpretationOfResourcesWithFoundJokers =
                        JokerFinder.findAndReplaceGroupOccurencies(
                                        interpretationOfResourcesWithFoundJokers,
                                        FreyaConstants.REGEX_FIND_CLASS_JOKER,
                                        FreyaConstants.JOKER);
        logger.info("After injecting class jokers:"
                        + interpretationOfResourcesWithFoundJokers);
        /*
         * additionally, if the string starts or ends with 'p' we need to add jokerClasses before or after them
         */
        String[] interpretationOfResource =
                        interpretationOfResourcesWithFoundJokers.toString().split("-");
        String firstElement = interpretationOfResource[0];
        String lastElement =
                        interpretationOfResource[interpretationOfResource.length - 1];
        StringBuffer finalString = new StringBuffer("");
        // System.out.println("before checking p:" + finalString.toString());
        if (firstElement.startsWith("p") && lastElement.startsWith("p")
                        && firstElement.equals(lastElement)) {
            // this means that they are exactly same elements such as po-
            finalString.append("c:firstJoker-");
            finalString.append(firstElement);
            finalString.append("-c:lastJoker");
            return finalString.toString();
        }
        if (firstElement.startsWith("p")) {
            finalString.append("c:firstJoker");
            if (!interpretationOfResourcesWithFoundJokers.startsWith("-"))
                finalString.append("-");
            if (firstElement != lastElement) // this check is to ensure that if
                // we have
                // one element only, it will be added twice, both after the
                // first
                // element and before
                // the second element, although they are the same one
                finalString.append(interpretationOfResourcesWithFoundJokers);
            // System.out.println("before checking p:" +
            // finalString.toString());
        }
        logger.info("firstElement:" + firstElement);
        logger.info("lastElement:" + lastElement);
        if (lastElement.startsWith("p")) {
            // check whether the firstElement was starting with p as if it was
            // this does not have to be added again
            if (!firstElement.startsWith("p"))
                finalString.append(interpretationOfResourcesWithFoundJokers);
            if (!interpretationOfResourcesWithFoundJokers.endsWith("-"))
                finalString.append("-");
            finalString.append("c:lastJoker");
        }
        if ("".equals(finalString.toString()))
            finalString.append(interpretationOfResourcesWithFoundJokers);
        // remove any "-" on the start or the beginning
        String returnString = null;
        if (finalString.toString().trim().endsWith("-"))
            returnString =
                            finalString.toString().substring(0,
                                            finalString.toString().length() - 1);
        else if (finalString.toString().trim().startsWith("-"))
            returnString =
                            finalString.toString().substring(1,
                                            finalString.toString().length());
        else
            returnString = finalString.toString();
        logger.info("Final string before generating sparql:" + returnString);
        return returnString;
    }

    /**
     * // check if each second element in the row is the property with // inverseProperty=true, in that case revert
     * first and third column for // example: a-b-c row has b as a property with inverseProperty=true, so // this row
     * becomes c-b-a, if d f and h are not invertedProperties than // the table becomes: // c-b-a // c-d-e // e-f-g //
     * g-h-i
     * 
     * @param newColumnData
     * @param elements
     * @return
     */
    public static List<List<List<OntologyElement>>> getInvertedTriplesIfPropertiesAreInverted(
                    List<List<List<OntologyElement>>> newColumnData,
                    List<List<OntologyElement>> elementsTable) {
        int positionOfPropertyElementWithinElements = 0;
        List<List<List<OntologyElement>>> invertedColumnData =
                        new ArrayList<List<List<OntologyElement>>>();
        for (List<List<OntologyElement>> row : newColumnData) {
            if ((elementsTable.size() > (positionOfPropertyElementWithinElements + 1))
                            && elementsTable.get(positionOfPropertyElementWithinElements + 1).get(0) instanceof PropertyElement) {
                // System.out
                // .println("checking if it's property................................................................");
                PropertyElement potentialProperty =
                                (PropertyElement) elementsTable
                                                .get(positionOfPropertyElementWithinElements + 1).get(0);
                if (potentialProperty.isInverseProperty()) {
                    // System.out.println("is inverted property.............");
                    // revert first and third element in this row
                    // save the first element in the temp element
                    List<OntologyElement> firstColumn = row.get(0);
                    List<OntologyElement> secondColumn = row.get(1);
                    List<OntologyElement> thirdColumn = row.get(2);
                    OntologyElement first = firstColumn.get(0);// the second 0 is because we dont care whichone of the
                                                               // overlapped we will take; they are all the same type
                    OntologyElement second = secondColumn.get(0);
                    // overide first element with third element
                    OntologyElement third = thirdColumn.get(0);
                    // row.add(0, thirdElement);
                    // third element takes values from temp elements
                    // row.add(2, tempElement);
                    List<List<OntologyElement>> newRow = new ArrayList<List<OntologyElement>>();
                    newRow.add(thirdColumn);
                    newRow.add(secondColumn);
                    newRow.add(firstColumn);
                    invertedColumnData.add(newRow);
                } else {
                    // add this row without changing anything
                    invertedColumnData.add(row);
                }
            } else {
                // this happens only when there is one element in the row
                invertedColumnData.add(row);
            }
            // }
            // now move to another triple
            positionOfPropertyElementWithinElements =
                            positionOfPropertyElementWithinElements + 2;
        }
        return invertedColumnData;
    }

    /**
     * // create list of list i.e. a 2d matrix/table) out of elements // so that each row contains 3 elements so that
     * the last element in a // row is the first element in the next row etc. for example if this is // the column
     * header: a-b-c-d-e-f-g-h-i then the newly created 'table' // will look like // a-b-c // c-d-e // e-f-g // g-h-i
     * 
     * @param elements
     * @return
     */
    public static List<List<List<OntologyElement>>> getListOfTriplesFromLinearForm(
                    List<List<OntologyElement>> elementsTable) {
        List<List<OntologyElement>> rowData = new ArrayList<List<OntologyElement>>();
        List<List<List<OntologyElement>>> newColumnData =
                        new ArrayList<List<List<OntologyElement>>>();
        List<OntologyElement> connectingElement = null;
        int numOfElements = elementsTable.size();
        for (int i = 0; i < numOfElements; i = i + 2) {
            // check if there is more than 1 element in the interpretation
            if (numOfElements > 1 && (i == (numOfElements - 1))) break;
            List<OntologyElement> currentElement = null;
            if (connectingElement != null) {
                currentElement = connectingElement;
            } else {
                currentElement = (List<OntologyElement>) elementsTable.get(i);
            }
            rowData.add(currentElement);
            if (numOfElements > (i + 1)) {
                List<OntologyElement> middleElement = (List<OntologyElement>) elementsTable.get(i + 1);
                rowData.add(middleElement);
            }
            if (numOfElements > (i + 2)) {
                connectingElement = (List<OntologyElement>) elementsTable.get(i + 2);
                rowData.add(connectingElement);
            }
            newColumnData.add(rowData);
            rowData = new ArrayList<List<OntologyElement>>();
        }
        return newColumnData;
    }

    public static Set<String> findVariations(String aliasLabel,
                    boolean separateCamelCasedWords) {
        long start = System.currentTimeMillis();
        Set<String> newAliasLabels = new HashSet<String>();
        // aliasLabel = aliasLabel.toLowerCase();
        if (aliasLabel != null && aliasLabel.trim().length() > 0) {
            if (aliasLabel.contains("_")) {
                String newText = aliasLabel.trim().replace("_", " ");
                if (newText != null && !"".equals(newText)) newAliasLabels.add(newText);
            }
            // if text is camel cased add space between words
            if (separateCamelCasedWords && aliasLabel.indexOf(" ") < 0) {
                String separatedCamelCase =
                                ExpressionFinder.findAndSeparateCamelCases(aliasLabel,
                                                CATConstants.REGEX_CAMEL_CASE, " ");
                if (aliasLabel != null && (!aliasLabel.equals(separatedCamelCase))) {
                    newAliasLabels.add(separatedCamelCase);
                }
            }
        }
        long end = System.currentTimeMillis();
        if ((end - start) > 100)
            System.out.println("Alias:" + aliasLabel + ",Finding variations for:"
                            + (end - start) + "ms");
        return newAliasLabels;
    }

    public static void main(String[] args) {
        StringUtil u = new StringUtil();
        String query = "p0-p1-i2-";
        // String query = "p0-";
        String answer = u.createCompleteString(query);
        String query1 = "c0-c1-i2-";
        // String query = "p0-";
        String answer1 = u.createCompleteString(query1);
    }
}

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.freya.model.ClassElement;
import org.freya.model.DatatypePropertyValueElement;
import org.freya.model.GroupElement;
import org.freya.model.InstanceElement;
import org.freya.model.InstanceListElement;
import org.freya.model.JokerElement;
import org.freya.model.OntologyElement;
import org.freya.model.PropertyElement;
import org.freya.model.TripleElement;
import org.freya.util.StringUtil;
import org.springframework.stereotype.Component;

/**
 * 
 */
package org.freya.analyser;



/**
 * @author danica
 */
@Component
public class TripleCreator {
  private static final Log logger = LogFactory.getLog(TripleCreator.class);

  /**
   * add jokers etc
   * 
   * @param intElements
   * @return
   */
  public List<List<OntologyElement>> prepareOntologyElements(
          List<List<OntologyElement>> intElementsTable) {
    intElementsTable =
            positionTheDatatypePropertyElementsTheOptimalWay(intElementsTable);
    intElementsTable = positionTheLastPropertyTheOptimalWay(intElementsTable);
    if(intElementsTable == null)
      intElementsTable = new ArrayList<List<OntologyElement>>();
    List<List<OntologyElement>> oldElements =
            new ArrayList<List<OntologyElement>>();
    /*
     * for example: ClassElement followed by an InstanceElement would be
     * represented as c0-i1, so first char representing a type of element
     * followed by index
     */
    StringBuffer interpretationOfResources = new StringBuffer("");
    for(int i = 0; i < intElementsTable.size(); i++) {
      // although this is table; all elements are of the same type so we get the
      // first one
      OntologyElement el = (OntologyElement)intElementsTable.get(i).get(0);
      oldElements.add(intElementsTable.get(i));
      if(el instanceof ClassElement) {
        interpretationOfResources.append("c").append(i).append("-");

      } else if(el instanceof InstanceElement
