00001 package edu.tum.cs.srldb.datadict; 00002 00003 import java.io.PrintStream; 00004 import java.io.Serializable; 00005 import java.util.Collection; 00006 import java.util.HashMap; 00007 import java.util.Map.Entry; 00008 00009 import edu.tum.cs.srldb.Database; 00010 import edu.tum.cs.srldb.IdentifierNamer; 00011 00012 public abstract class DDItem implements Serializable { 00013 protected String name; 00014 protected HashMap<String, DDAttribute> attributes; 00015 00016 public DDItem(String name) { 00017 this.name = name; 00018 attributes = new HashMap<String, DDAttribute>(); 00019 } 00020 00021 public String getName() { 00022 return name; 00023 } 00024 00025 public void addAttribute(DDAttribute attr) throws DDException { 00026 attr.setOwner(this); 00027 attributes.put(attr.getName(), attr); 00028 } 00029 00033 public HashMap<String, DDAttribute> getAttributes() { 00034 return attributes; 00035 } 00036 00037 public abstract boolean isObject(); 00038 00039 public void discardAllAttributesExcept(String[] keep) { 00040 for(Entry<String,DDAttribute> entry : attributes.entrySet()) { 00041 boolean discard = true; 00042 for(int i = 0; i < keep.length; i++) 00043 if(entry.getKey().equals(keep[i])) { 00044 discard = false; 00045 break; 00046 } 00047 if(discard) 00048 entry.getValue().discard(); 00049 } 00050 } 00051 00052 public void MLNprintUnitClauses(IdentifierNamer idNamer, PrintStream out) { 00053 for(DDAttribute attr : attributes.values()) { 00054 if(attr.isDiscarded() || attr.isBoolean()) 00055 continue; 00056 String idCategory = attr.getName(); 00057 idNamer.resetCounts(); 00058 out.print(Database.stdPredicateName(attr.getName()) + "(" + idNamer.getCountedShortIdentifier(idCategory, this.getName())); 00059 out.print(", +"); 00060 out.println(idNamer.getCountedShortIdentifier(attr.getName(), attr.getDomain().getName()) + ")"); 00061 } 00062 } 00063 00064 protected void outputAttributeList(PrintStream out) { 00065 Collection<DDAttribute> attributes = getAttributes().values(); 00066 if(attributes.isEmpty()) 00067 return; 00068 out.print(getName() + "_attr_names = ["); 00069 int i = 0; 00070 for(DDAttribute attrib : attributes) { 00071 if(attrib.isDiscarded()) 00072 continue; 00073 if(i++ > 0) 00074 out.print(", "); 00075 out.print("'" + Database.stdAttribName(attrib.getName()) + "'"); 00076 } 00077 out.println("]"); 00078 } 00079 00080 protected void MLNprintAttributePredicateDeclaration(DDAttribute attr, String objectOfAttribute, IdentifierNamer idNamer, PrintStream out) { 00081 if(attr.isDiscarded()) 00082 return; 00083 out.print(Database.stdPredicateName(attr.getName()) + "(" + objectOfAttribute); 00084 if(attr.isBoolean()) { 00085 out.println(")"); 00086 return; 00087 } 00088 out.print(", "); 00089 out.print(idNamer.getLongIdentifier("domain", Database.stdDomainName(attr.getDomain().getName()))); 00090 out.println("!)"); 00091 } 00092 00093 protected void BLNprintAttributePredicateDeclaration(DDAttribute attr, String objectOfAttribute, IdentifierNamer idNamer, PrintStream out) { 00094 if(attr.isDiscarded()) 00095 return; 00096 out.print("random "); 00097 if(attr.isBoolean()) 00098 out.print("boolean"); 00099 else 00100 out.print(idNamer.getLongIdentifier("domain", Database.stdDomainName(attr.getDomain().getName()))); 00101 out.println(" " + Database.stdPredicateName(attr.getName()) + "(" + objectOfAttribute + ");"); 00102 } 00103 }