00001 package edu.tum.cs.srldb.datadict;
00002
00003 import java.io.PrintStream;
00004 import java.io.Serializable;
00005
00006 import edu.tum.cs.srldb.IdentifierNamer;
00007 import edu.tum.cs.srldb.Database;
00008 import edu.tum.cs.util.StringTool;
00009
00010 public class DDRelation extends DDItem implements Serializable {
00011
00012 private static final long serialVersionUID = 1L;
00013 protected IDDRelationArgument[] arguments;
00014 protected boolean[] singleVal;
00015
00016 public DDRelation(String name, IDDRelationArgument arg1, IDDRelationArgument arg2) throws DDException {
00017 this(name, arg1, arg2, false, false);
00018 }
00019
00020 public DDRelation(String name, IDDRelationArgument arg1, IDDRelationArgument arg2, boolean singleValue1, boolean singleValue2) throws DDException {
00021 this(name, new IDDRelationArgument[]{arg1, arg2}, new boolean[]{singleValue1, singleValue2});
00022 }
00023
00024 public DDRelation(String name, IDDRelationArgument[] arguments) throws DDException{
00025 this(name, arguments, null);
00026 }
00027
00028 public DDRelation(String name, IDDRelationArgument[] arguments, boolean[] singleVal) throws DDException {
00029 super(name);
00030 this.arguments = arguments;
00031 setFunctional(singleVal);
00032 }
00033
00034 @Override
00035 public boolean isObject() {
00036 return false;
00037 }
00038
00039 public IDDRelationArgument[] getArguments() {
00040 return arguments;
00041 }
00042
00043 public void MLNprintPredicateDeclarations(IdentifierNamer idNamer, PrintStream out) {
00044
00045 StringBuffer params = new StringBuffer();
00046 for(int i = 0; i < arguments.length; i++) {
00047 if(i > 0)
00048 params.append(", ");
00049 params.append(idNamer.getLongIdentifier("domain", Database.stdDomainName(arguments[i].getDomainName())));
00050 if(singleVal[i])
00051 params.append("!");
00052 }
00053
00054 out.println(Database.stdPredicateName(getName()) + "(" + params + ")");
00055
00056 for(DDAttribute attr : attributes.values()) {
00057 MLNprintAttributePredicateDeclaration(attr, params.toString(), idNamer, out);
00058 }
00059 }
00060
00061 public void BLNprintPredicateDeclarations(IdentifierNamer idNamer, PrintStream out) {
00062
00063 StringBuffer params = new StringBuffer();
00064 for(int i = 0; i < arguments.length; i++) {
00065 if(i > 0)
00066 params.append(", ");
00067 params.append(idNamer.getLongIdentifier("domain", Database.stdDomainName(arguments[i].getDomainName())));
00068 }
00069
00070 String predName = Database.stdPredicateName(getName());
00071 out.println("random boolean " + predName + "(" + params + ");");
00072
00073 for(DDAttribute attr : attributes.values()) {
00074 BLNprintAttributePredicateDeclaration(attr, params.toString(), idNamer, out);
00075 }
00076
00077 for(int i = 0; i < this.singleVal.length; i++)
00078 if(singleVal[i]) {
00079 out.print("RelationKey " + predName + "(");
00080 for(int j = 0; j < this.singleVal.length; j++) {
00081 if(j > 0)
00082 out.print(", ");
00083 out.print(j == i ? "_" : String.format("a%d", j));
00084 }
00085 out.println(");");
00086 }
00087 }
00088
00089 @Deprecated
00090 public void MLNprintRules(IdentifierNamer idNamer, PrintStream out) {
00091 MLNprintRule(idNamer, getName(), this.singleVal, out);
00092 for(DDAttribute attr : attributes.values()) {
00093 if(attr instanceof DDRelationAttribute)
00094 MLNprintRule(idNamer, attr.getName(), ((DDRelationAttribute)attr).singleVal, out);
00095 }
00096 }
00097
00098 @Deprecated
00099 protected void MLNprintRule(IdentifierNamer idNamer, String predicate, boolean[] singleVal, PrintStream out) {
00100 StringBuffer params = new StringBuffer("(");
00101 int single = 0;
00102 idNamer.resetCounts();
00103 for(int i = 0; i < arguments.length; i++) {
00104 if(i > 0)
00105 params.append(", ");
00106 params.append(idNamer.getCountedShortIdentifier("var", arguments[i].getDomainName()));
00107 if(singleVal[i]) {
00108 params.append("!");
00109 single++;
00110 }
00111 }
00112 if(single == 0)
00113 return;
00114 params.append(")");
00115 out.println(Database.stdPredicateName(predicate) + params);
00116 }
00117
00118 @Override
00119 public void addAttribute(DDAttribute attrib) throws DDException {
00120 if(attrib instanceof DDRelationAttribute) {
00121 DDRelationAttribute attr = (DDRelationAttribute) attrib;
00122 if(attr.singleVal == null)
00123 attr.singleVal = this.singleVal;
00124 else if(attr.singleVal.length != this.singleVal.length)
00125 throw new DDException("attribute's singleVal array has incorrect length; must match that of relation object");
00126 }
00127 else
00128 ;
00129 super.addAttribute(attrib);
00130 }
00131
00137 public void setFunctional(boolean[] singleVal) throws DDException {
00138 if(singleVal == null)
00139 singleVal = new boolean[arguments.length];
00140 this.singleVal = singleVal;
00141 if(arguments.length != this.singleVal.length)
00142 throw new DDException("Single value array dimension differs from object array dimension");
00143 }
00144
00145 public String toString() {
00146 StringBuffer args = new StringBuffer();
00147 for(int i = 0; i < arguments.length; i++) {
00148 if(i > 0) args.append(", ");
00149 args.append(arguments[i].getDomainName());
00150 }
00151 return String.format("DDRelation:%s(%s) [%s]", name, args.toString(), StringTool.join(", ", this.attributes.values()));
00152 }
00153 }