From 174c75f7da7f8efc475acd64a3589d86cfb7b279 Mon Sep 17 00:00:00 2001 From: Luca Liechti Date: Tue, 8 Nov 2016 04:00:08 +0100 Subject: [PATCH] Building the lattice Pt. 2: Edges --- bin/datastructures/FormalContext.class | Bin 5607 -> 4886 bytes src/datastructures/FormalContext.java | 53 ++++++++++++++----------- src/datastructures/Lattice.java | 38 ++++++++++++++---- src/datastructures/LatticeNode.java | 12 +++++- src/driver/LatticeBuilder.java | 1 + 5 files changed, 72 insertions(+), 32 deletions(-) diff --git a/bin/datastructures/FormalContext.class b/bin/datastructures/FormalContext.class index 8183be21fa0fe4c4710c35dbef1ee2f9bec5d3ba..df1d1741765ca86e8c278575421fd53c21d41cca 100644 GIT binary patch delta 1104 zcmZuwYfKbZ6#nkMW*BY?E-Z0_HSF3VELcl`^qcCYqv4_0{$>Fe>&a1~>*KRSD_Fa$>9ajKL;_3Ng$vB5O;mI@qL3 z`v*BjQO%G>u_^b}txXMjizrQb#!5QEaTH&QH&SN%zUKG_-;zFY$xL2l{z?_cNcnjc z-_!PlIFV9jS8zfs(`NB8oRVif&EU%u-HxoHr<{04;Hl= z;RwNz2sJO<9F1{Yz(sLLE1UT{#~)Zl-EE}gU^p79(qGmv9ploI?<9IR3%EqAm6H40qHz zdCf~t8r1oQRzr{VF+s^ci0vkwax-A>`sB#}ZBLqrYGo;K8n14)&|VRx?Qz8fNR-P7EX z;cxn)+~IC7ciKx`aV+*aJT4PoZN&^(+Ural;JYak<<2;~4*D46UfGxmD@_)=F%L+woxGmoDl-4uc vK~KWgeA=Zgm5Rxx?6i1(T3uHBRm`GyS%)}y43As*q+BUcM&-nf(N51@GVVf$ delta 1774 zcmbVMX>3$g6#mYvudk2JbWE3#Lb_49+BV&!Roa3;YHM94L9qdx)|YXxOvCiGiAtP5 zCjNz7V?v=Ks0@lqTgtRhEuc{o+;>C~#Rah{R>cJ?^}Jc?NNUpfe!TPUJ?DJqocn$6 zKAd;9NIrh)@Hl`^v8 zi}AdIalM8f)9T)+${-w7a15wri!*zH0qy2PC_>z;C$+Cp6Ia;^h@YuIW_?FF8R(wZT zxYeFv&n=fRfgkh(KTpWY7lWo$Q7S8zN&IZZPoh!2()bH5h;?#J_HPP)$67Ab(dEasq3Wt#z!m}X^IS$V1%?x4WKfI2^(Tp&N3+1grRFoV-6_bzR z8{#S#hwvQ77V35cN8<4~-bLt}J{}kDcp(Zb#%&panf^IDIMZb;??imwtMLtm`?>$0 zemMR0T9adYt=U*@38LOW;vS=fjxi0Sl#Acxe4> zc1@4Pb>KA5)?=V(fd6$PviZFZY5cC{RSkb#jVdgG3$^g@@4Xq|qLGrYjB>D?3UD2jUg4bH&_oZhzaP!WVfb4h>GUHPi}b>@gP}TD z9Gj7clUNS}uha1{?->|#Q)G{k@5U!^!^HR=oMNw;%8=WPPjMO++{sWYAWy>&lNDs; zMy)&uN{h1=i}lIn!fs0qe0G`SzKAjscSduT+*i@uNWz@dWxGsXV%V0KGOK{Zd0Td1 zE_yf{V?C;e89AJbIsUgC#1{Le`Uo?fGt3-}F~c`blo?FMn8Ae@GknQ}i4hISH_;if zH90+DVol))z8}SpVko(gj)_yr&ZWO@LpGn^{G#wHgg=gADuhd>J4T5@WV$s7d)Q-) zuKyh-DjgM>B!#u3({F;IPytZ_%2-2u#jt*4@t~vHX`saLN}+jd^$T}`EkAz)n>_(Y diff --git a/src/datastructures/FormalContext.java b/src/datastructures/FormalContext.java index 8988f5e..886c0b6 100644 --- a/src/datastructures/FormalContext.java +++ b/src/datastructures/FormalContext.java @@ -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(); @@ -119,4 +119,9 @@ private int getObjectNumber(String name){ public ArrayList getObjects() { return objects; } + + //delete? + public int numberOfAttributes() { + return dic.getSize(); + } } \ No newline at end of file diff --git a/src/datastructures/Lattice.java b/src/datastructures/Lattice.java index af39ab7..ea898c8 100644 --- a/src/datastructures/Lattice.java +++ b/src/datastructures/Lattice.java @@ -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 nodes; @@ -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 level = levels.get(card); @@ -40,18 +42,40 @@ public void increaseNodeCount(BitSet bs) { } public void computeEdges() { - // TODO Auto-generated method stub - + Set differentLevels = levels.keySet(); //ordered! + for(int lowerLevel : differentLevels){ + Iterator 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 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; } } diff --git a/src/datastructures/LatticeNode.java b/src/datastructures/LatticeNode.java index 97f397f..c61020e 100644 --- a/src/datastructures/LatticeNode.java +++ b/src/datastructures/LatticeNode.java @@ -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; } -} +} \ No newline at end of file diff --git a/src/driver/LatticeBuilder.java b/src/driver/LatticeBuilder.java index f0b386b..64b1553 100644 --- a/src/driver/LatticeBuilder.java +++ b/src/driver/LatticeBuilder.java @@ -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;