Skip to content

Commit

Permalink
Edges now computed correctly (checked many big contexts)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Liechti committed Dec 5, 2016
1 parent 0f6e856 commit 183f115
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 deletions.
Binary file modified bin/driver/Driver.class
Binary file not shown.
95 changes: 68 additions & 27 deletions src/datastructures/Lattice.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import driver.LatticeBuilder;
Expand All @@ -30,8 +32,8 @@ public String latticeStats() {
String str = "\n";
// str += "Lattice nodes:";
// for(LatticeNode node : nodes)
// str += "\n" + node.getIntent() + ", " + node.getExtent().size(); //node.getNiceExtentString();
System.out.println(nodes.size() + " different nodes computed.");
// str += "\n" + node.getNiceString() ;//+ ", " + node.getExtent().size(); //node.getNiceExtentString();
System.out.println("Nodes: " + nodes.size() + ", edges: " + edges.size());
// str += "\n";
return str;
}
Expand All @@ -44,6 +46,7 @@ public void addNode(LatticeNode node) {
public void addEdge(LatticeNode from, LatticeNode to) {
LatticeEdge edge = new LatticeEdge(from, to);
edges.add(edge);
to.addToUpperNeighbours(from);
}

public ArrayList<LatticeNode> getNodes() {
Expand Down Expand Up @@ -74,44 +77,82 @@ private void writeToFile(String exportString, String outputFile) {
}

public void computeEdges() {
nodesByLevel = sortNodesIntoLevels();
int[] levelArray = extractLevelsAsArray();
int levelDistance = 1;
addNodesForLevelDistance(levelArray, levelDistance);
setTransitiveLinks();
nodesByLevel = sortNodesIntoLevels(nodes);
int[] levelArray = extractLevelsAsArray(nodesByLevel);
for(int levelDistance = 1; levelDistance < levelArray.length; levelDistance++) {
System.out.println("distance = " + levelDistance);
HashSet<LatticeNode> newIncomingEdge = addEdgesForLevelDistance(levelArray, levelDistance);
HashSet<LatticeNode> alreadyVisited = new HashSet<LatticeNode>();
setTransitiveLinks(newIncomingEdge, alreadyVisited);
}
}

private HashSet<LatticeNode> addEdgesForLevelDistance(int[] levelArray, int levelDistance) {
HashSet<LatticeNode> updatedNodes = new HashSet<LatticeNode>();
for(int i = 0; i < levelArray.length-levelDistance; i++){
ArrayList<LatticeNode> nodesAtUpperLevel = nodesByLevel.get(levelArray[i]);
ArrayList<LatticeNode> nodesAtLowerLevel = nodesByLevel.get(levelArray[i+levelDistance]);
System.out.println(" upperLevel = " + levelArray[i] + ", lowerLevel = " + levelArray[i+levelDistance]);
for(LatticeNode upperNode : nodesAtUpperLevel) {
for(LatticeNode lowerNode : nodesAtLowerLevel) {
if(LatticeBuilder.isSubsetOf(upperNode.getIntent(), lowerNode.getIntent()) && !upperNode.canTransitivelyReach(lowerNode)){
this.addEdge(upperNode, lowerNode);
System.out.println(" adding edge " + upperNode.getIntent() + " -> " + lowerNode.getIntent());
updatedNodes.add(lowerNode);
}
}
}
}
System.out.println(" Added edges of distance " + levelDistance + ". Updated nodes : ");
for(LatticeNode node : updatedNodes) System.out.println(node.getIntent() + ", ");
return updatedNodes;
}

private void setTransitiveLinks() {
// TODO Auto-generated method stub

private void setTransitiveLinks(HashSet<LatticeNode> updatedNodes, HashSet<LatticeNode> alreadyVisited) {
System.out.println("Call to transitive links.");
System.out.println("updated nodes = "); for(LatticeNode n : updatedNodes) System.out.println(n.getIntent());
System.out.println("already visited nodes = "); for(LatticeNode n : alreadyVisited) System.out.println(n.getIntent());
HashMap<Integer, ArrayList<LatticeNode>> updatedNodesByLevel = sortNodesIntoLevels(updatedNodes);
System.out.println("keyset = " + updatedNodesByLevel.keySet());
int[] levelArray = extractLevelsAsArray(updatedNodesByLevel);
for(int i = levelArray.length-1; 0 <= i; i--) {
for(LatticeNode lowerNode : updatedNodesByLevel.get(levelArray[i])) {
if(!alreadyVisited.contains(lowerNode) && updatedNodes.contains(lowerNode)){
System.out.println(" setting transitive nodes for " + lowerNode.getIntent());
alreadyVisited = setTransitive(lowerNode, updatedNodes, alreadyVisited);
}
}
}
}

private int[] extractLevelsAsArray() {
Set<Integer> levels = nodesByLevel.keySet();
//sets the list of transitively reachable nodes in all upper nodes that can reach target node, recursively
private HashSet<LatticeNode> setTransitive(LatticeNode lowerNode, HashSet<LatticeNode> updatedNodes, HashSet<LatticeNode> alreadyVisited) {
for(LatticeNode parent : lowerNode.getUpperNeighbours()) {
System.out.println(" " + lowerNode.getIntent() + " is reachable from " + parent.getIntent());
parent.addToTransitivelyReachableNodes(lowerNode);
parent.addAllToTransitivelyReachableNodes(lowerNode.getTransitivelyReachableNodes());
alreadyVisited = setTransitive(parent, updatedNodes, alreadyVisited);
}
alreadyVisited.add(lowerNode);
// System.out.println("visited " + lowerNode.getIntent());
return alreadyVisited;
}

private int[] extractLevelsAsArray(HashMap<Integer, ArrayList<LatticeNode>> nodeMap) {
Set<Integer> levels = nodeMap.keySet();
int[] levelArray = new int[levels.size()];
int pos = 0;
for(int level : levels) levelArray[pos++] = level;
Arrays.sort(levelArray);
return levelArray;
}

private void addNodesForLevelDistance(int[] levelArray, int levelDistance) {
for(int i = 0; i < levelArray.length-1; i++){
ArrayList<LatticeNode> nodesAtUpperLevel = nodesByLevel.get(i);
ArrayList<LatticeNode> nodesAtLowerLevel = nodesByLevel.get(i+levelDistance);
for(LatticeNode upperNode : nodesAtUpperLevel) {
for(LatticeNode lowerNode : nodesAtLowerLevel) {
//TODO: Must not be transitively reachable
if(LatticeBuilder.isSubsetOf(upperNode.getIntent(), lowerNode.getIntent()))
this.addEdge(upperNode, lowerNode);
}
}
}
}


private HashMap<Integer, ArrayList<LatticeNode>> sortNodesIntoLevels() {
//takes a Collection of LatticeNodes and returns a HashMap where the key is the number of attributes
private HashMap<Integer, ArrayList<LatticeNode>> sortNodesIntoLevels(Collection<LatticeNode> nodesToSort) {
HashMap<Integer, ArrayList<LatticeNode>> levels = new HashMap<Integer, ArrayList<LatticeNode>>();
for(LatticeNode node : nodes) {
for(LatticeNode node : nodesToSort) {
int numAttributes = node.getIntent().cardinality();
if(levels.get(numAttributes) == null) {
ArrayList<LatticeNode> newList = new ArrayList<LatticeNode>();
Expand Down
21 changes: 18 additions & 3 deletions src/datastructures/LatticeNode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package datastructures;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -12,13 +11,15 @@ public class LatticeNode {
private HashSet<LatticeNode> transitivelyReachable;
private int nodeNumber;
private Dictionary dic;
private HashSet<LatticeNode> upperNeighbours;

public LatticeNode(HashSet<FormalObject> hashSet, BitSet intent, Dictionary _dic) {
this.intent = intent;
this.extent = new HashSet<FormalObject>();
for(FormalObject obj : hashSet) extent.add(obj);
this.transitivelyReachable = new HashSet<LatticeNode>();
this.dic = _dic;
this.upperNeighbours = new HashSet<LatticeNode>();
}

public BitSet getIntent() {
Expand All @@ -38,15 +39,17 @@ public void addToTransitivelyReachableNodes(LatticeNode node) {
}

//attention, this is not very recursive yet
public void addAllToTransitivelyReachableNodes(ArrayList<LatticeNode> nodes) {
public void addAllToTransitivelyReachableNodes(HashSet<LatticeNode> nodes) {
this.transitivelyReachable.addAll(nodes);
}

public void addObject(FormalObject obj) {
this.extent.add(obj);
}

public Boolean isTransitivelyReachable(LatticeNode node) {
//this is always called from the UPPER node, because only the upper node contains a list of all lower nodes
//that can be transitively reached from it.
public Boolean canTransitivelyReach(LatticeNode node) {
return transitivelyReachable.contains(node);
}

Expand Down Expand Up @@ -84,4 +87,16 @@ public String getNiceExtentString() {
niceExtent += ")";
return niceExtent;
}

public void addToUpperNeighbours(LatticeNode from) {
this.upperNeighbours.add(from);
}

public HashSet<LatticeNode> getUpperNeighbours() {
return this.upperNeighbours;
}

public HashSet<LatticeNode> getTransitivelyReachableNodes() {
return this.transitivelyReachable;
}
}
4 changes: 2 additions & 2 deletions src/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public static void main(String[] args){
// docs.add(repoFolder + "XML\\DBLP\\1000Schema.xml");

//add BibTex repos
docs.add(repoFolder + "BibTex\\BordatTest.bib");
// docs.add(repoFolder + "BibTex\\BordatTest.bib");
// docs.add(repoFolder + "BibTex\\scg.bib");
// docs.add(repoFolder + "BibTex\\listb.bib");
docs.add(repoFolder + "BibTex\\listb.bib");
// docs.add(repoFolder + "BibTex\\zbMATH\\100Lattice.bib");
// docs.add(repoFolder + "BibTex\\zbMATH\\100Schema.bib");
// docs.add(repoFolder + "BibTex\\zbMATH\\100Algebra.bib");
Expand Down

0 comments on commit 183f115

Please sign in to comment.