Skip to content

Commit

Permalink
implemented Norris algorithm to generate concepts of lattice
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Liechti committed Dec 3, 2016
1 parent 174c75f commit 4ac07d5
Show file tree
Hide file tree
Showing 43 changed files with 1,313 additions and 129 deletions.
10 changes: 10 additions & 0 deletions 2016-11-20_Bordat/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="lib"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib/java-json.jar"/>
<classpathentry kind="lib" path="lib/jdom-2.0.4.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions 2016-11-20_Bordat/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>FCAInference</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions 2016-11-20_Bordat/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
5 changes: 5 additions & 0 deletions 2016-11-20_Bordat/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/datastructures/
/driver/
/factories/
/parsers/
/tests/
Binary file added 2016-11-20_Bordat/bin/java-json.jar
Binary file not shown.
Binary file added 2016-11-20_Bordat/bin/jdom-2.0.4.jar
Binary file not shown.
Binary file added 2016-11-20_Bordat/lib/java-json.jar
Binary file not shown.
Binary file added 2016-11-20_Bordat/lib/jdom-2.0.4.jar
Binary file not shown.
35 changes: 35 additions & 0 deletions 2016-11-20_Bordat/src/datastructures/Dictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package datastructures;

import java.util.HashMap;
import java.util.Set;

public class Dictionary {

private HashMap<String, Integer> attributes;
private int nextAttributeNumber;

public Dictionary() {
this.attributes = new HashMap<String, Integer>();
this.nextAttributeNumber = 0;
}

public Boolean containsAttribute(String _attr){
return attributes.containsKey(_attr);
}

public void addAttribute(String _attr){
attributes.put(_attr, nextAttributeNumber++);
}

public int getAttributePosition(String _attr){
return attributes.get(_attr);
}

public Set<String> getContents(){
return attributes.keySet();
}

public int getSize(){
return attributes.size();
}
}
36 changes: 36 additions & 0 deletions 2016-11-20_Bordat/src/datastructures/FormalConcept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datastructures;

import java.util.ArrayList;
import java.util.BitSet;

public class FormalConcept {
private ArrayList<FormalObject> extent;
private BitSet intent;
private ArrayList<FormalConcept> lowerNeighbours;

public FormalConcept(ArrayList<FormalObject> objects, BitSet attributes){
this.extent = objects;
this.intent = attributes;
this.lowerNeighbours = new ArrayList<FormalConcept>();
}

public ArrayList<FormalObject> getExtent() {
return extent;
}

public BitSet getIntent() {
return intent;
}

public void addToLowerNeighbours(FormalConcept fc) {
lowerNeighbours.add(fc);
}

public void addAllToLowerNeighbours(ArrayList<FormalConcept> concepts) {
lowerNeighbours.addAll(concepts);
}

public ArrayList<FormalConcept> getLowerNeighbours() {
return lowerNeighbours;
}
}
152 changes: 152 additions & 0 deletions 2016-11-20_Bordat/src/datastructures/FormalContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package datastructures;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class FormalContext {
private ArrayList<FormalObject> objects;
private HashMap<String, Integer> objectNumbers;
private Dictionary dic;

public FormalContext() {
this.objects = new ArrayList<FormalObject>();
this.objectNumbers = new HashMap<String, Integer>();
this.dic = new Dictionary();
}

//the BitSet is created at the time of the object being added to the context
public void addObject(FormalObject object){
BitSet intent = new BitSet();
for(String attribute : object.getAttributes()){
if(!dic.containsAttribute(attribute))
dic.addAttribute(attribute);
intent.set(dic.getAttributePosition(attribute));
}
object.setIntent(intent);
objects.add(object);
}

public void exportContextToFile(String outputFile){
String exportString = createCXTString();
System.out.print("Writing context to file... ");
writeToFile(exportString, outputFile);
System.out.println("done.");
}

private void writeToFile(String exportString, String outputFile) {
try {
PrintWriter out = new PrintWriter(outputFile, "UTF-8");
out.print(exportString);
out.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
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 createCXTString(){
//sortObjects(); //--------------------------- commented OUT for test purposes only!
String export = "";
export += "B\n\n";
export += objects.size() + "\n";
export += dic.getSize() + "\n\n";
for(FormalObject obj : objects){
export += obj.getName() + getObjectNumber(obj.getName()) + "\n";
}
//prints out attributes in order
for(int i = 0; i < dic.getSize(); i++) {
for(String attr : dic.getContents()){
if(dic.getAttributePosition(attr) == i) export += attr + "\n";
}
}
for(FormalObject obj : objects){
BitSet bitset = obj.getIntent();
String currentObject = "";
for(int i = 0; i < dic.getSize(); i++){
if(bitset.get(i)) currentObject += "X";
else currentObject += ".";
}
export += currentObject + "\n";
}
return export;
}

private void sortObjects(){
Collections.sort(objects, new Comparator<FormalObject>() {
public int compare(FormalObject obj1, FormalObject obj2) {
return obj1.getName().compareTo(obj2.getName());
}
});
}

//returns an int enumerating all objects of the same type
//like this, we have book0, book1, book2 etc.
private int getObjectNumber(String name){
if(objectNumbers.containsKey(name))
objectNumbers.put(name, objectNumbers.get(name)+1);
else
objectNumbers.put(name, 0);
return objectNumbers.get(name);
}

public ArrayList<FormalObject> getObjects() {
return objects;
}

public int numberOfAttributes() {
return dic.getSize();
}

public Dictionary getDictionary() {
return dic;
}

public ArrayList<FormalObject> getDerivationOfAttributes(BitSet seed) {
ArrayList<FormalObject> derivation = new ArrayList<FormalObject>();
for(FormalObject obj : objects) {
if(obj.isSupersetOf(seed)) derivation.add(obj);
}
return derivation;
}

public BitSet getDerivationOfObjects(ArrayList<FormalObject> extent) {
BitSet derivation = extent.get(0).getIntent();
for(FormalObject obj : extent)
derivation.and(obj.getIntent());
return derivation;
}

public BitSet getDerivationOfSingleObject(FormalObject obj) {
return obj.getIntent();
}
}
50 changes: 50 additions & 0 deletions 2016-11-20_Bordat/src/datastructures/FormalObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package datastructures;

import java.util.ArrayList;
import java.util.BitSet;

public class FormalObject {
private String name;
private BitSet intent;
private ArrayList<String> attributes;

public FormalObject(){
this.name = "";
this.intent = new BitSet();
this.attributes = new ArrayList<String>();
}

public boolean isSubsetOf(BitSet bs) {
BitSet thisIntent = (BitSet)intent.clone();
thisIntent.and(bs);
if(thisIntent.equals(intent)) return true;
return false;
}

public boolean isSupersetOf(BitSet bs) {
BitSet thisIntent = (BitSet)intent.clone();
thisIntent.and(bs);
if(thisIntent.equals(bs)) return true;
return false;
}

public ArrayList<String> getAttributes() {
return attributes;
}
public void setAttributes(ArrayList<String> attributes) {
this.attributes = attributes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BitSet getIntent() {
return intent;
}
public void setIntent(BitSet intent) {
this.intent = intent;
}

}
64 changes: 64 additions & 0 deletions 2016-11-20_Bordat/src/datastructures/Lattice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package datastructures;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

public class Lattice {
private ArrayList<LatticeNode> nodes;
private ArrayList<LatticeEdge> edges;
private HashMap<Integer, HashMap<Integer, LatticeNode>> levels;
private int currentNodeNumber;
private Dictionary dic;

public Lattice(Dictionary _dic) {
this.nodes = new ArrayList<LatticeNode>();
this.edges = new ArrayList<LatticeEdge>();
this.currentNodeNumber = 0;
this.dic = _dic;
}

public String latticeStats() {
String str = "";
str += "\nLattice stats: \n";
str += "Different levels: " + levels.keySet() + "\n";
str += "Different nodes: " + nodes.size() + "\n";
str += "Number of edges: " + edges.size() + "\n";
return str;
}

public void addNode(LatticeNode node) {
node.setNodeNumber(++currentNodeNumber);
nodes.add(node);
}

public void addEdge(LatticeNode from, LatticeNode to) {
LatticeEdge edge = new LatticeEdge(from, to);
edges.add(edge);
}

public void exportLatticeToFile(String outputFile){
System.out.print("Writing lattice to file... ");
String latticeString = "";
latticeString += "digraph d{\n";
for(LatticeNode node : nodes)
latticeString += node.getNodeNumber() + " [label=\"" + node.getExtent() + "\"]\n";
for(LatticeEdge edge: edges)
latticeString += edge.getLowerNodeNumber() + "->" + edge.getUpperNodeNumber() + ";\n";
latticeString += "}";
writeToFile(latticeString, outputFile);
System.out.println("done.");
}

private void writeToFile(String exportString, String outputFile) {
try {
PrintWriter out = new PrintWriter(outputFile, "UTF-8");
out.print(exportString);
out.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (UnsupportedEncodingException e) { e.printStackTrace(); }
}
}
Loading

0 comments on commit 4ac07d5

Please sign in to comment.