+1 (315) 557-6473 

Implement A Minimal Relational Model Emulator Using Java Programming Language Java Language Assignment Solution.


Instructions

Objective
Write a java homework program to implement a minimal relational model emulator using Java programming language in java.

Requirements and Specifications

Implement a minimal relational model emulator using Java programming language that can provide basic relational model and algebra functionalities in-memory. This means that your emulator will perform the operations in the run time and do not have to actually store the data1. A successfully implemented emulator will be able to create relational model elements such as relations, tuples, and attributes, check the validity/constraints of the relation descriptions and relation instances, enforce those constraints, enable data modification and certain retrieval operations outlined below. You will be given a base Relation implementation (See Relation.java, Tuple.java, Attribute.java along with Driver.java for a simple example) and you are required to modify them and enhance their functionalities when implementing the features in this project. You are expected to use standard Java libraries and not utilise additional libraries that implement data frames or SQL connectors/emulators. The purpose of this project is to gain understanding on how the relational model features can be implemented; it is not utilising third party libraries in Java.
Source Code
ATTRIBUTE
package RModel;
public class Attribute {
    private String name;
    private Class type;
    public Attribute(String name, Class type) {
        this.name = name;
        if(type.equals(String.class) | type.equals(Integer.class)) {
         this.type = type;
        }
    }
    public String getName() {
     return name;
    }
    public Class getType() {
     return this.type;
    }
    public String toString() {
     return String.format("Attribute %s <{%s}>", this.name, this.type.toString());
    }
}
CONSTRAINT
package RModel;
public class Constraint {
 private final Attribute foreignKeyAttribute;
 private final Relation referencedRelation;
 public Constraint(Attribute foreignKeyAttribute, Relation referencedRelation) {
  this.foreignKeyAttribute = foreignKeyAttribute;
  this.referencedRelation = referencedRelation;
 }
 public Attribute getForeignKeyAttribute() {
  return foreignKeyAttribute;
 }
 public Relation getReferencedRelation() {
  return referencedRelation;
 }
}
DRIVER
package RModel;
import java.util.ArrayList;
import java.util.HashMap;
public class Driver {
 public static void main(String[] args) {
  ArrayList tAttrs = getTestAttributes();
  ArrayList testTuples = getTestTuples();
  // printing an attribute
  System.out.println("ATTRIBUTE -- " + tAttrs.get(0));
  System.out.println();
  // printing a tuple
  System.out.println("TUPLE 0 -- " + testTuples.get(0));
  System.out.println();
  Relation r = new Relation("R1", tAttrs, tAttrs.get(0), new ArrayList<>());
  r.printRelation();
 }
 public static ArrayList getTestAttributes() {
  ArrayList attrs = new ArrayList();
  attrs.add( new Attribute("Attr1", Integer.class) );
  attrs.add( new Attribute("Attr2", String.class) );
  attrs.add( new Attribute("Attr3", Integer.class) );
  return attrs;
 }
 public static ArrayList getTestTuples() {
  ArrayList attrs = new ArrayList();
  attrs.add( new Attribute("Attr1", Integer.class) );
  attrs.add( new Attribute("Attr2", String.class) );
  attrs.add( new Attribute("Attr3", Integer.class) );
  HashMap> attrValues1 = new HashMap<>();
  attrValues1.put("Attr1", Integer.valueOf(4));
  attrValues1.put("Attr2", "type1");
  attrValues1.put("Attr3", Integer.valueOf(2));
  HashMap> attrValues2 = new HashMap<>();
  attrValues2.put("Attr1", Integer.valueOf(1));
  attrValues2.put("Attr2", "type2");
  attrValues2.put("Attr3", Integer.valueOf(5));
  HashMap> attrValues3 = new HashMap<>();
  attrValues3.put("Attr1", Integer.valueOf(6));
  attrValues3.put("Attr2", "type3");
  attrValues3.put("Attr3", Integer.valueOf(8));
  ArrayList tuples = new ArrayList();
  tuples.add( new Tuple(attrValues1) );
  tuples.add( new Tuple(attrValues2) );
  tuples.add( new Tuple(attrValues3) );
  return tuples;
 }
}
QUERY ATTRIBUTE
package RModel;
public class QueryAttribute {
 public enum Comparison {
  EQUALS,
  LESSTHAN,
  GREATERTHAN
 }
 private Attribute attribute;
 private Comparison comparison;
 public QueryAttribute(Attribute attribute, Comparison comparison) {
  this.attribute = attribute;
  this.comparison = comparison;
 }
 public boolean check(Tuple tuple) {
  Object value = tuple.getAttribute(attribute.getName());
  if (value == null) {
   return false;
  }
  return true;
 }
}
RELATION
package RModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
public class Relation {
 private final String name;
 private final ArrayList tuples;
 private final List attributes;
 private final Attribute primaryKeyAttribute;
 private final List constraints;
 public Relation(String name, List attributes, Attribute primaryKeyAttribute, List constraints) {
  if (primaryKeyAttribute == null) {
   throw new IllegalArgumentException("The key of relation can not be null");
  }
  this.name = name;
  this.attributes = new ArrayList<>(attributes);
  this.primaryKeyAttribute = primaryKeyAttribute;
  this.constraints = new ArrayList<>(constraints);
  this.tuples = new ArrayList<>();
 }
 public Attribute getPrimaryKeyAttribute() {
  return primaryKeyAttribute;
 }
 public void insertTuple(Tuple tuple) {
  for(Map.Entry> pair : tuple.getTupleValues().entrySet()) {
   // tuple can contain only values from attributes of relation
   Attribute att = attributes.stream().filter(a -> a.getName().equals(pair.getKey())).findAny().orElse(null);
   if (att == null) {
    throw new IllegalArgumentException("Unknown attribute name in tuple for relation");
   }
   // tuples can contain only values of attributes' domains
   if (pair.getValue() != null) {
    if (att.getType() == String.class) {
     if (!(pair.getValue() instanceof String)) {
      throw new IllegalArgumentException("Value of attribute is not from Domain attribute");
     }
    }
    else if (att.getType() == Integer.class) {
     if (!(pair.getValue() instanceof Integer)) {
      throw new IllegalArgumentException("Value of attribute is not from Domain attribute");
     }
    }
   }
  }
  // checking primary key constraint
  // it must not be null
  Object key = tuple.getAttribute(primaryKeyAttribute.getName());
  if (key == null) {
   throw new IllegalArgumentException("Tuple can not contain empty primary key value");
  }
  // it must be unique
  if (tuples.stream().anyMatch(t -> t.getAttribute(primaryKeyAttribute.getName()).equals(key))) {
   throw new IllegalArgumentException("Relation already contains tuple with such primary key");
  }
  // checking foreign key constraints
  for (Constraint constraint : constraints) {
   Object fk = tuple.getAttribute(constraint.getForeignKeyAttribute().getName());
   if (fk != null) {
    Relation referencedRelation = constraint.getReferencedRelation();
    if (referencedRelation.query()
      .stream()
      .noneMatch(t -> t.getAttribute(referencedRelation.getPrimaryKeyAttribute().getName()).equals(fk))) {
     throw new IllegalArgumentException("Tuple does not satisfy foreign key constraint");
    }
   }
  }
 }
 public void deleteTuple(Predicate predicate) {
 }
 public void updateTuple() {
 }
 public List query() {
  return tuples;
 }
 public void printRelation() {
  String str = "RELATION: " + this.name + "\n";
  for( Attribute attr : attributes ) {
   str += attr.getName() + "\t";
  }
  str += "\n";
  for (Tuple tuple : this.tuples) {
   for(Attribute attr: attributes ) {
    Object val = tuple.getAttribute(attr.getName());
    str += val + "\t";
   }
   str += "\n";
  }
  System.out.println(str);
 }
}
TUPLE
package RModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Tuple {
 private Map> tValues;
 public Tuple(Map> attrValues) {
  this.tValues = new HashMap<>(attrValues);
 }
 public Object getAttribute(String attrName) {
  return tValues.get(attrName);
 }
 public String toString() {
  String str = "";
  for (Map.Entry> entry : tValues.entrySet()) {
      str += entry.getKey()+" : "+entry.getValue() + '\t';
  }
  return str;
 }
 public Map> getTupleValues(){
  return tValues;
 }
 public Set getAttributeNames(){
  return tValues.keySet();
 }
 public ArrayList getAttributeValues(){
  return new ArrayList(tValues.values());
 }
}