📄 relation.java
字号:
package toocom.ocgl;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
/**
* This class represents the relations in the graphs or axioms of the ontology.
*
* @author Fr閐閞ic F黵st
*/
public class Relation extends Node{
private RelationType type;
private Concept[] linkedConcepts;
public Relation(){
super(Terms.getUndefinedTerms());
this.linkedConcepts = new Concept[CGConstants.RELATION_TYPE_MIN_ARITY];
}
public Relation(int x, int y){
super(Terms.getUndefinedTerms(),x,y);
this.linkedConcepts = new Concept[CGConstants.RELATION_TYPE_MIN_ARITY];
}
public Relation(RelationType type){
super(Terms.getUndefinedTerms());
this.type = type;
if(type != null){
this.setTerms(type.getTerms());
this.linkedConcepts = new Concept[type.getArity()];
}
else this.linkedConcepts = new Concept[CGConstants.RELATION_TYPE_MIN_ARITY];
}
public void setType(RelationType rt){
if(rt != null){
this.type = rt;
this.setTerms(rt.getTerms());
Concept[] newTab = new Concept[this.type.getArity()];
for(int i = 0;i < Math.min(linkedConcepts.length,newTab.length);i ++){
newTab[i] = linkedConcepts[i];
}
linkedConcepts = newTab;
}
else{
this.type = null;
this.linkedConcepts = new Concept[CGConstants.RELATION_TYPE_MIN_ARITY];
}
}
public RelationType getType(){
return type;
}
public int getArity(){
return this.linkedConcepts.length;
}
/** Returns the concepts linked to the relation in a graph or an axiom. */
public LinkedList getLinkedConcepts(){
LinkedList result = new LinkedList();
for(int i = 0;i < linkedConcepts.length;i ++){
if(linkedConcepts[i] != null) result.add(linkedConcepts[i]);
}
return result;
}
/** Returns the concept linked to the relation if 0 < i and i <= arity and if a concept really exist
* at this index, null otherwise. */
public Concept getLinkedConcept(int i){
if((i > 0) && (i <= linkedConcepts.length)){
return linkedConcepts[i - 1];
}
else return null;
}
/** Returns a relation with an undefined relation type. */
public static Relation getUndefinedRelation(){
//return new Relation(RelationType.getUndefinedRelationType());
return new Relation(null);
}
/** Returns the list of index (as Integer) of c if it is linked to the relation. */
public LinkedList getConceptIndexList(Concept c){
LinkedList result = new LinkedList();
for(int i = 0;i < linkedConcepts.length;i ++){
if((linkedConcepts[i] != null) && (linkedConcepts[i].equals(c))) result.add(new Integer(i + 1));
}
return result;
}
/** Returns true and link a concept to the relation at the first free place in the signature of the relation type.
* Returns false if there is any free place in the signature. */
public boolean addLinkedConcept(Concept c){
for(int i = 0;i < linkedConcepts.length;i ++){
if(linkedConcepts[i] == null){
linkedConcepts[i] = c;
return true;
}
}
return false;
}
/** Remove the link between the relation and the specified concept and returns the
* index of the concept in the signature, -1 if the concept is not linked to the relation. */
public int removeLinkedConcept(Concept c){
for(int i = 0;i < linkedConcepts.length;i ++){
if((linkedConcepts[i] != null) && (linkedConcepts[i].equals(c))){
linkedConcepts[i] = null;
return i;
}
}
return -1;
}
/** Remove the link between the relation and the concept at the specified index and returns
* true, return false if any concept is linked to the relation at the given index or if
* index < 1 or index > arity. */
public boolean removeLinkedConcept(int index){
if((index > 0) && (index <= this.getType().getArity())){
Concept c = linkedConcepts[index - 1];
linkedConcepts[index - 1] = null;
if(c == null) return false;
else return true;
}
else return false;
}
/** Set the linked concepts and return true if the concepts list size is equal to the relation type arity and if the elements of the list are Concept, return false otherwise. */
public boolean setLinkedConcepts(LinkedList conceptsList){
if(conceptsList.size() != linkedConcepts.length) return false;
else{
try{
for(int i = 0;i < conceptsList.size();i ++){
linkedConcepts[i] = (Concept) conceptsList.get(i);
}
}
catch(Exception e){
return false;
}
}
return true;
}
/** Returns false if the index is out of bounds (index < 1 or index > signature arity), otherwise place the concept c at the specified index, or replace the previous concept if it already exists, and return true. */
public boolean setLinkedConcept(Concept c, int index){
if((index > 0) && (index <= type.getArity())){
index --;
linkedConcepts[index] = c;
return true;
}
else return false;
}
/** Returns true if the given concept is linked to the relation. */
public boolean hasLinkedConcept(Concept c){
for(int i = 0;i < linkedConcepts.length;i ++){
if((linkedConcepts[i] != null) && (linkedConcepts[i].equals(c))) return true;
}
return false;
}
/** Returns true if the relation links the same concepts (different from null) than r. */
public boolean linksSameConcepts(Relation r){
if((this.getType() != null) && (r.getType() != null) && (this.getType().getArity() == r.getType().getArity())){
for(int i = 1;i <= this.getType().getArity();i ++){
Concept c1 = this.getLinkedConcept(i);
Concept c2 = r.getLinkedConcept(i);
if((c1 == null) || (c2 == null) || !c1.equals(c2)) return false;
}
return true;
}
return false;
}
/** Returns a Relation with the same properties than this Relation but with an other id. The
* new relation is not linked to any concept. */
public Relation cloneRelation(){
Relation result = new Relation(this.getType());
result.setLocation(this.x,this.y);
return result;
}
public String toString(Language l){
if(type == null) return CGConstants.UNDEFINED_ELEMENT;
else return type.getTerm(l);
}
public void destroy(Ontology onto){}
/** Returns true if the graphical representation of the specified link of the relation
* contains the given point, false otherwise (0 < linkNumber <= arity of r). */
public boolean linkContainsPoint(Graphics g, Language l, int linkNumber, Point p){
if((linkNumber > 0) && (linkNumber <= linkedConcepts.length)){
Concept c = this.getLinkedConcept(linkNumber);
if(c == null) return false;
else return GraphicsToolkit.lineContainsPoint(g,this.getCenter(),c.getCenter(),p);
}
else return false;
}
/** Returns the bounds of the image according to the font size of the given Graphics. */
public RectangularShape getBounds(Graphics g,Language l){
String s = this.toString(l);
return GraphicsToolkit.getEllipticalBounds(g,s,new Point(this.x,this.y));
}
/** Only draw the lines which represent the link between the relation and concepts. */
public void paintLinks(Graphics g,Language l){
g.setColor(CGConstants.GENERAL_LINK_COLOR);
for(int i = 1;i <= this.getArity();i ++){
Concept c = (Concept) this.getLinkedConcept(i);
if(c != null){
Point p1 = c.getCenter();
Point p2 = this.getCenter();
GraphicsToolkit.paintLine(g,p1,p2);
g.drawString((new Integer(i)).toString(),(p1.x + p2.x)/2,(p1.y + p2.y)/2);
}
}
}
/** Paint the relation with its label in a rectangular shape. */
public void paintObject(Graphics g,Language l){
this.paintObject(g,CGConstants.RELATION_COLOR,CGConstants.RELATION_BORDER_COLOR,CGConstants.SELECTED_RELATION_BORDER_COLOR,CGConstants.HIGHLIGHTED_RELATION_COLOR,CGConstants.RELATION_TEXT_COLOR,l);
}
/** Paint the relation with its label in a rectangular shape as element of the
* conclusion of an axiom. */
public void paintObjectAsConclusion(Graphics g,Language l){
this.paintObject(g,CGConstants.CONCLUSION_RELATION_COLOR,CGConstants.CONCLUSION_RELATION_BORDER_COLOR,CGConstants.CONCLUSION_SELECTED_RELATION_BORDER_COLOR,CGConstants.HIGHLIGHTED_RELATION_COLOR,CGConstants.CONCLUSION_RELATION_TEXT_COLOR,l);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -