Skip to content

Commit

Permalink
Building the lattice Pt. 2: Edges
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Liechti committed Nov 8, 2016
1 parent 3e978b7 commit 174c75f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
Binary file modified bin/datastructures/FormalContext.class
Binary file not shown.
53 changes: 29 additions & 24 deletions src/datastructures/FormalContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,30 @@ private void writeToFile(String exportString, String outputFile) {
catch (UnsupportedEncodingException e) { e.printStackTrace(); }
}

private String createSLFString() {
String export = "";
export += "[Lattice]\n";
export += objects.size() + "\n";
export += dic.getSize() + "\n";
export += "[Objects]\n";
for(FormalObject obj : objects){
export += obj.getName() + getObjectNumber(obj.getName()) + "\n";
}
export += "[Attributes]\n";
for(String attr : dic.getContents())
export += attr + "\n";
export += "[relation]\n";
for(FormalObject obj : objects){
BitSet bitset = obj.getExtent();
String currentObject = "";
for(int i = 0; i < dic.getSize(); i++){
if(bitset.get(i)) currentObject += "1 ";
else currentObject += "0 ";
}
export += currentObject + "\n";
}
return export;
}
// private String createSLFString() {
// String export = "";
// export += "[Lattice]\n";
// export += objects.size() + "\n";
// export += dic.getSize() + "\n";
// export += "[Objects]\n";
// for(FormalObject obj : objects){
// export += obj.getName() + getObjectNumber(obj.getName()) + "\n";
// }
// export += "[Attributes]\n";
// for(String attr : dic.getContents())
// export += attr + "\n";
// export += "[relation]\n";
// for(FormalObject obj : objects){
// BitSet bitset = obj.getExtent();
// String currentObject = "";
// for(int i = 0; i < dic.getSize(); i++){
// if(bitset.get(i)) currentObject += "1 ";
// else currentObject += "0 ";
// }
// export += currentObject + "\n";
// }
// return export;
// }

private String createCXTString(){
sortObjects();
Expand Down Expand Up @@ -119,4 +119,9 @@ private int getObjectNumber(String name){
public ArrayList<FormalObject> getObjects() {
return objects;
}

//delete?
public int numberOfAttributes() {
return dic.getSize();
}
}
38 changes: 31 additions & 7 deletions src/datastructures/Lattice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class Lattice {
private ArrayList<LatticeNode> nodes;
Expand All @@ -25,13 +28,12 @@ public void addToNodes(BitSet bs) {
nodes.add(newNode);
levels.get(card).put(hash, newNode);
hashes.put(hash, bs);
//System.out.println("adding " + card + " " + hash + " " + levels.get(card).get(hash));
}

public void increaseNodeCount(BitSet bs) {
int card = bs.cardinality();
int hash = bs.hashCode();
//System.out.println("increasing " + card + " " + hash + " " + levels.get(card).get(hash));
//TODO: Make prettier
LatticeNode node = levels.get(card).get(hash);
node.increaseNumberOfObjects();
HashMap<Integer, LatticeNode> level = levels.get(card);
Expand All @@ -40,18 +42,40 @@ public void increaseNodeCount(BitSet bs) {
}

public void computeEdges() {
// TODO Auto-generated method stub

Set<Integer> differentLevels = levels.keySet(); //ordered!
for(int lowerLevel : differentLevels){
Iterator<LatticeNode> lowerIt = levels.get(lowerLevel).values().iterator();
while(lowerIt.hasNext()){
LatticeNode lowerNode = lowerIt.next();
BitSet coveredAttributes = (BitSet)lowerNode.getExtent().clone(); //helps avoid transitive duplicates of edges
for(int upperLevel : differentLevels){
if(0 < lowerLevel && lowerLevel < upperLevel){
Iterator<LatticeNode> upperIt = levels.get(upperLevel).values().iterator();
while(upperIt.hasNext()){
LatticeNode upperNode = upperIt.next();
if(lowerNode.isSubsetOf(upperNode)){
BitSet covered = (BitSet)coveredAttributes.clone();
covered.or(upperNode.getExtent());
if(!covered.equals(coveredAttributes)) {//if we haven't "covered" all attributes in the potential new node
coveredAttributes.or(upperNode.getExtent());
edges.add(new LatticeEdge(lowerNode, upperNode));
}
}
}
}
}
}
}
}

@Override
public String toString() {
String str = "";
str += "\nLattice stats: \n";
str += "Different levels: " + levels.keySet() + "\n";
str += "Different nodes: " + nodes.size() + "\n";
for(LatticeNode ln : nodes)
str += ln + "\n";
// for(LatticeNode ln : nodes)
// str += ln + "\n";
return str;
}
}
12 changes: 11 additions & 1 deletion src/datastructures/LatticeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ public void increaseNumberOfObjects() {
numberOfObjects++;
}

public BitSet getExtent() {
return extent;
}

public Boolean isSubsetOf(LatticeNode otherNode){
BitSet thisExtent = (BitSet)extent.clone();
thisExtent.and(otherNode.getExtent());
return(thisExtent.equals(extent));
}

@Override
public String toString() {
return extent.cardinality() + "\t" + numberOfObjects;
}
}
}
1 change: 1 addition & 0 deletions src/driver/LatticeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Lattice buildLattice(FormalContext context){
}

//add edges to lattice
//all relevant datastructures are in the lattice, so we do this from there
lattice.computeEdges();

return lattice;
Expand Down

0 comments on commit 174c75f

Please sign in to comment.