00001 package edu.tum.cs.srldb;
00002 import java.io.PrintStream;
00003 import java.io.Serializable;
00004 import java.util.Map.Entry;
00005
00006 import edu.tum.cs.srldb.datadict.DDAttribute;
00007 import edu.tum.cs.srldb.datadict.DDException;
00008 import edu.tum.cs.srldb.datadict.domain.BooleanDomain;
00009
00010 public class Link extends Item implements Serializable {
00011 private static final long serialVersionUID = 1L;
00012 protected String linkName;
00013 protected IRelationArgument[] arguments;
00014
00015 public Link(Database database, String linkName, IRelationArgument arg1, IRelationArgument arg2) {
00016 this(database, linkName, new IRelationArgument[]{arg1, arg2});
00017 }
00018
00019 public Link(Database database, String linkName, IRelationArgument[] arguments) {
00020 super(database);
00021 this.linkName = linkName;
00022 this.arguments = arguments;
00023 }
00024
00025
00026
00027
00028
00032 protected String getLinkParams() {
00033 StringBuffer linkParams = new StringBuffer();
00034 for(int i = 0; i < arguments.length; i++) {
00035 if(i > 0)
00036 linkParams.append(", ");
00037 linkParams.append(Database.upperCaseString(arguments[i].getConstantName()));
00038 }
00039 return linkParams.toString();
00040 }
00041
00042 public String getLogicalAtom() {
00043 return linkName + "(" + getLinkParams() + ")";
00044 }
00045
00046 public void MLNprintFacts(java.io.PrintStream out) throws DDException {
00047
00048 out.println(getLogicalAtom());
00049
00050
00051 for(String attribName : attribs.keySet()) {
00052 DDAttribute ddAttr = database.getDataDictionary().getAttribute(attribName);
00053
00054 if(
00055 ddAttr.isBoolean()) {
00056 out.print(((BooleanDomain)ddAttr.getDomain()).isTrue(attribs.get(attribName)) ? "" : "!");
00057 out.println(attribName + "(" + getLinkParams() + ")");
00058
00059 }
00060 else
00061 throw new DDException("Non-boolean attributes of links not handled for MLNs");
00062 }
00063 }
00064
00070 public void BLOGprintFacts(PrintStream out) throws DDException {
00071 out.printf("%s = True;\n", getLogicalAtom());
00072
00073 String linkObjects = getLinkParams();
00074 for(Entry<String, String> entry : getAttributes().entrySet()) {
00075 DDAttribute ddAttrib = database.getDataDictionary().getAttribute(entry.getKey());
00076 if(ddAttrib.isDiscarded())
00077 continue;
00078 out.printf("%s(%s) = %s;\n", entry.getKey(), linkObjects, Database.upperCaseString(entry.getValue()));
00079 }
00080 }
00081
00082 public String toString() {
00083 return getLogicalAtom();
00084 }
00085
00090 public void commit() throws DDException {
00091 addTo(this.database);
00092 }
00093
00099 public void addTo(Database db) throws DDException {
00100 if(db == this.database)
00101 immutable = true;
00102 db.addLink(this);
00103 }
00104
00105 public String getName() {
00106 return this.linkName;
00107 }
00108
00109 public IRelationArgument[] getArguments() {
00110 return this.arguments;
00111 }
00112
00113 public void setSecondArgument(IRelationArgument arg) {
00114 arguments[1] = arg;
00115 }
00116
00117 public void setFirstArgument(IRelationArgument arg) {
00118 arguments[0] = arg;
00119 }
00120
00121 public void setArguments(IRelationArgument[] arguments) {
00122 this.arguments = arguments;
00123 }
00124 }