00001 package edu.tum.cs.srldb;
00002 import java.io.PrintStream;
00003 import java.sql.*;
00004 import java.util.*;
00005 import java.util.Map.Entry;
00006
00007 import edu.tum.cs.srldb.datadict.DDAttribute;
00008 import edu.tum.cs.srldb.datadict.DDException;
00009 import edu.tum.cs.srldb.datadict.domain.BooleanDomain;
00010 import kdl.prox3.dbmgr.DataTypeEnum;
00011
00012 public class Object extends Item implements IRelationArgument, java.io.Serializable {
00013
00014 private static final long serialVersionUID = 1L;
00015 protected HashMap<String, Link> links;
00016 protected String objTypeName;
00017 protected String constantName = null;
00018
00025 protected Object(Database database) {
00026 this(database, null);
00027 objTypeName = getClass().getSimpleName();
00028 }
00029
00035 public Object(Database database, String objTypeName) {
00036 super(database);
00037 links = null;
00038 this.objTypeName = objTypeName;
00039 }
00040
00041 public Object(Database database, String objTypeName, String constantName) {
00042 this(database, objTypeName);
00043 this.constantName = constantName;
00044 }
00045
00053 public Link link(String linkName, Object otherObj) throws DDException {
00054 checkMutable();
00055 Link link = new Link(database, linkName, this, otherObj);
00056 if(links == null)
00057 links = new HashMap<String, Link>();
00058 links.put(linkName, link);
00059 return link;
00060 }
00061
00069 public Link link(String linkName, Object[] otherObjects) throws DDException {
00070 checkMutable();
00071 Object[] objs = new Object[1+otherObjects.length];
00072 objs[0] = this;
00073 for(int i = 0; i < otherObjects.length; i++)
00074 objs[i+1] = otherObjects[i];
00075 Link link = new Link(database, linkName, objs);
00076 links.put(linkName, link);
00077 return link;
00078 }
00079
00083 public String getConstantName() {
00084 if(constantName == null)
00085 return "O" + objType() + id;
00086 else
00087 return constantName;
00088 }
00089
00090 public String toString() {
00091 return getConstantName();
00092 }
00093
00094 public void MLNprintFacts(PrintStream out) throws DDException {
00095 for(String attribName : attribs.keySet()) {
00096 MLNprintFact(attribName, out);
00097 }
00098 }
00099
00100 public void MLNprintFact(String attribName, PrintStream out) throws DDException {
00101 DDAttribute ddAttrib = database.getDataDictionary().getAttribute(attribName);
00102 if(ddAttrib.isDiscarded())
00103 return;
00104 String strValue = attribs.get(attribName);
00105 String predicate = Database.stdPredicateName(attribName);
00106
00107
00108 if(ddAttrib.isBoolean()) {
00109 BooleanDomain domain = (BooleanDomain) ddAttrib.getDomain();
00110 out.println((!domain.isTrue(strValue) ? "!" : "") + predicate + "(" + getConstantName() + ")");
00111 }
00112
00113 else {
00114 out.println(predicate + "(" + getConstantName() + ", " + Database.stdAttribStringValue(strValue) + ")");
00115 }
00116 }
00117
00118 public void BLOGprintFacts(PrintStream out) throws DDException {
00119 for(Entry<String, String> entry : getAttributes().entrySet()) {
00120 DDAttribute ddAttrib = database.getDataDictionary().getAttribute(entry.getKey());
00121 if(ddAttrib.isDiscarded())
00122 continue;
00123 out.printf("%s(%s) = %s;\n", entry.getKey(), getConstantName(), Database.upperCaseString(entry.getValue()));
00124 }
00125 }
00126
00131 public String objType() {
00132 return objTypeName;
00133 }
00134
00139 public void commit() throws DDException {
00140 addTo(this.database);
00141 }
00142
00148 public void addTo(Database db) throws DDException {
00149 if(db == this.database)
00150 immutable = true;
00151
00152 db.addObject(this);
00153
00154 if(links != null) {
00155 for(Link link : links.values()) {
00156
00157 for(IRelationArgument arg : link.arguments)
00158 if(arg instanceof Object && arg != this)
00159 ((Object)arg).addTo(db);
00160
00161 link.addTo(db);
00162 }
00163 }
00164 }
00165
00166 public Link getLink(String linkName) {
00167 if(links == null)
00168 return null;
00169 return links.get(linkName);
00170 }
00171 }