Skip to content

Commit

Permalink
Attempt to build a model from a coloured graph
Browse files Browse the repository at this point in the history
Important changes in
 - ColourPalette
 - ColouredGraph
 - Name of some important metric class
 - ColourMappingRules
  • Loading branch information
Pham Thuy Sy Nguyen committed May 25, 2018
1 parent 50103ea commit e7ec490
Show file tree
Hide file tree
Showing 19 changed files with 1,019 additions and 281 deletions.
165 changes: 150 additions & 15 deletions src/main/java/org/aksw/simba/lemming/ColouredGraph.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.aksw.simba.lemming;

import java.util.HashSet;
import java.util.Set;

import grph.Grph;
import grph.GrphAlgorithmCache;
import grph.algo.MultiThreadProcessing;
import grph.in_memory.InMemoryGrph;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.aksw.simba.lemming.colour.ColourPalette;
import org.aksw.simba.lemming.grph.DiameterAlgorithm;

Expand All @@ -27,10 +30,19 @@ public class ColouredGraph{
protected ColourPalette edgePalette;
protected ColourPalette dtEdgePalette;


/**
* this is new for processing the literals in the original RDF dataset
*/
//mapping vertex's colours to datatyped edge's colours
protected ObjectObjectOpenHashMap<BitSet, Set<BitSet>> mapVertexColourToDataTypedEdge;
protected ObjectObjectOpenHashMap<BitSet, Set<BitSet>> mapVertexColoursToDataTypedEdgeColours;
//mapping datatyped edge's colours to set of literals
protected ObjectObjectOpenHashMap<BitSet, Set<String>> mapDataTypedEdgeColourToLiterals;
protected ObjectObjectOpenHashMap<BitSet, Set<String>> mapDTEdgeColoursToLiterals;
// map data typed edge's colours to set of connected vertex' ids
protected ObjectObjectOpenHashMap<BitSet, IntSet> mapDTEdgeColoursToVertexIDs;

protected Map<Integer, Map<BitSet, List<String>>> mapVertexIdAndLiterals;


protected GrphAlgorithmCache<Integer> diameterAlgorithm;

Expand Down Expand Up @@ -67,8 +79,11 @@ public ColouredGraph(Grph graph, ColourPalette vertexPalette, ColourPalette edge
this.edgePalette = edgePalette;
this.dtEdgePalette = datatypedEdgePalette;

mapVertexColourToDataTypedEdge = new ObjectObjectOpenHashMap<BitSet, Set<BitSet>>();
mapDataTypedEdgeColourToLiterals = new ObjectObjectOpenHashMap<BitSet, Set<String>>();
mapVertexIdAndLiterals = new HashMap<Integer, Map<BitSet, List<String>>>();

mapVertexColoursToDataTypedEdgeColours = new ObjectObjectOpenHashMap<BitSet, Set<BitSet>>();
mapDTEdgeColoursToLiterals = new ObjectObjectOpenHashMap<BitSet, Set<String>>();
mapDTEdgeColoursToVertexIDs = new ObjectObjectOpenHashMap<BitSet, IntSet>();
}

public void removeEdge(int edgeId){
Expand Down Expand Up @@ -218,20 +233,30 @@ public void visualizeGraph(){
graph.display();
}

/**
* Set new data to the mapping of vertex's ids and vertex's colours
* @param inVertexColours
*/
public void setVertexColours(ObjectArrayList<BitSet> inVertexColours){
vertexColours = new ObjectArrayList<BitSet>();
for(int i = 0 ; i < inVertexColours.size() ; ++i){
vertexColours.add(inVertexColours.get(i));
}
}

/**
* Set new data to the mapping of edge's ids and edge's colours
* @param inEdgeColours
*/
public void setEdgeColours(ObjectArrayList<BitSet> inEdgeColours){
edgeColours = new ObjectArrayList<BitSet>();
for(int i = 0 ; i < inEdgeColours.size() ; ++i){
edgeColours.add( inEdgeColours.get(i));
}
}

/**
* Clone the current instance of coloured graph to a new object
*/
@Override
public ColouredGraph clone(){
Grph rawClonedGrph = new InMemoryGrph();
Expand Down Expand Up @@ -259,7 +284,12 @@ protected void run(int threadID, int edgeID) {
ColouredGraph cloneGrph = new ColouredGraph(rawClonedGrph, vertexPalette, edgePalette, dtEdgePalette);
cloneGrph.setVertexColours(vertexColours);
cloneGrph.setEdgeColours(edgeColours);

//--------------------------------------------------------
// TODO set literal of the old graph to the new graph here
// May be not necessary, since all original graphs are
// not cloned instead of the mimic graph and the mimic graph
// has not had any literals yet
//--------------------------------------------------------

return cloneGrph;
Expand Down Expand Up @@ -287,6 +317,11 @@ public int getHeadOfTheEdge(int edgeId){
return -1;
}

/**
* Get list of vertex ID's based on a colour
* @param vertexColour the colour whoes vertex IDs we want to get
* @return
*/
public IntSet getVertices(BitSet vertexColour){
IntSet setVertices = new DefaultIntSet();

Expand All @@ -306,25 +341,125 @@ protected void run(int threadID, int vertId) {
return setVertices;
}

public void addLiterals(String literal, BitSet subjectColour, BitSet datatypedEdgeColour){
/**
* Add a literal associated with the data typed property to the data store
* @param literal a literal
* @param tailId the vertex id which has the data typed property
* @param dtEdgeColour the data typed property's colour connecting to the vertex
*/
public void addLiterals(String literal, int tailId, BitSet dtEdgeColour){

BitSet vertColo = getVertexColour(tailId);

Set<BitSet> setDTEdgeColours = mapVertexColourToDataTypedEdge.get(subjectColour);
Set<BitSet> setDTEdgeColours = mapVertexColoursToDataTypedEdgeColours.get(vertColo);
if(setDTEdgeColours == null){
setDTEdgeColours = new HashSet<BitSet>();
mapVertexColourToDataTypedEdge.put(subjectColour, setDTEdgeColours);
mapVertexColoursToDataTypedEdgeColours.put(vertColo, setDTEdgeColours);
}
setDTEdgeColours.add(datatypedEdgeColour);
setDTEdgeColours.add(dtEdgeColour);

Set<String> setLiterals = mapDataTypedEdgeColourToLiterals.get(datatypedEdgeColour);
Set<String> setLiterals = mapDTEdgeColoursToLiterals.get(dtEdgeColour);
if(setLiterals == null){
setLiterals = new HashSet<String>();
mapDataTypedEdgeColourToLiterals.put(datatypedEdgeColour, setLiterals);
mapDTEdgeColoursToLiterals.put(dtEdgeColour, setLiterals);
}

setLiterals.add(literal);

IntSet setVertexIDs = mapDTEdgeColoursToVertexIDs.get(dtEdgeColour);
if(setVertexIDs == null){
setVertexIDs = new DefaultIntSet();
mapDTEdgeColoursToVertexIDs.put(dtEdgeColour, setVertexIDs);
}
setVertexIDs.add(tailId);
}

/**
* Get list of all vertex IDs connecting to the the edgeId
* @param edgeId the id of an edge connecting 1-2 vertices together
* @return set of vertex ID's
*/
public IntSet getVerticesIncidentToEdge(int edgeId){
return graph.getVerticesIncidentToEdge(edgeId);
}

/**
* Get a set of all associated data typed properties with the given vertex's colour
* @param vertexColour the colour of the vertex having data typed properties
* @return
*/
public Set<BitSet> getSetDTEdgeColours(BitSet vertexColour){
if(mapVertexColoursToDataTypedEdgeColours != null){
return mapVertexColoursToDataTypedEdgeColours.get(vertexColour);
}
return null;
}

/**
* Return all literals belonging to the data typed properties
* @param dtEdgeColour the data typed property's colour
* @return
*/
public Set<String> getSetLiterals(BitSet dtEdgeColour){
if(mapDTEdgeColoursToLiterals != null ){
return mapDTEdgeColoursToLiterals.get(dtEdgeColour);
}
return null;
}

/**
* Get a map of the data typed properties with their sets of associated literals based on the vertex's color
*
* @param vertexColour the colour of the vertex which has the data typed properties
* @return
*/
public ObjectObjectOpenHashMap<BitSet, Set<String>> getMapDTEdgeColoursToLiterals(BitSet vertexColour){
ObjectObjectOpenHashMap mapRes = new ObjectObjectOpenHashMap<BitSet, Set<String>>();
if(mapVertexColoursToDataTypedEdgeColours != null && mapDTEdgeColoursToLiterals != null) {
Set<BitSet> setDTEdgeColours = mapVertexColoursToDataTypedEdgeColours.get(vertexColour);
if(setDTEdgeColours != null && setDTEdgeColours.size() > 0 ){
for(BitSet dtEdgeColo : setDTEdgeColours){
Set<String> setLiterals = mapDTEdgeColoursToLiterals.get(dtEdgeColo);
if(setLiterals != null && setLiterals.size() > 0 ){
//just for testing
if(mapRes.containsKey(dtEdgeColo)){
System.err.println("getMapDTEdgeColoursToLiterals has serious errors");
return null;
}

mapRes.put(dtEdgeColo, setLiterals);
}
}
}
}
return mapRes;
}

/**
* Get the map of data typed properties with the set of all associated literals
* @return
*/
public ObjectObjectOpenHashMap<BitSet, Set<String>> getMapDTEdgeColoursToLiterals(){
return mapDTEdgeColoursToLiterals;
}

/**
* get the map of data typed properties with the set of vertex IDs
* @return a map
*/
public ObjectObjectOpenHashMap<BitSet, IntSet> getMapDTEdgeColoursToVertexIDs(){
return mapDTEdgeColoursToVertexIDs;
}

public String getResourceURI(BitSet vColo){
return vertexPalette.getURI(vColo);
}

public String getPropertyURI(BitSet eColo){
return edgePalette.getURI(eColo);
}

public String getDataTypedPropertyURI(BitSet dteColo){
return dtEdgePalette.getURI(dteColo);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ public interface ColourPalette {
*/
public void setColour(String uri, BitSet colour);


/**
* Get the URI corresponding to the given colour.
* @param colour the given colour whose URI should be returned
* @return URI
*/
public String getURI(BitSet colour);

}
17 changes: 16 additions & 1 deletion src/main/java/org/aksw/simba/lemming/colour/InMemoryPalette.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,20 @@ public boolean containsUri(String uri) {
public void setColour(String uri, BitSet colour) {
uriColourMap.put(uri, colour);
}


@Override
public String getURI(BitSet inColour){
if(inColour != null){
Object[] arrOfURIs = uriColourMap.keys;
for(int i = 0 ; i < arrOfURIs.length ; i++){
if(uriColourMap.allocated[i]){
String uri = (String) arrOfURIs[i];
BitSet colo = uriColourMap.get(uri);
if(colo.equals(inColour))
return uri;
}
}
}
return "";
}
}
16 changes: 5 additions & 11 deletions src/main/java/org/aksw/simba/lemming/creation/GraphCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Set;

import org.aksw.simba.lemming.ColouredGraph;
import org.aksw.simba.lemming.ColouredGraphExt;
import org.aksw.simba.lemming.colour.ColourPalette;
import org.aksw.simba.lemming.colour.InMemoryPalette;
import org.apache.jena.rdf.model.Literal;
Expand All @@ -23,7 +22,6 @@
import com.carrotsearch.hppc.BitSet;
import com.carrotsearch.hppc.ObjectIntOpenHashMap;
import com.carrotsearch.hppc.ObjectObjectOpenHashMap;
import com.kitfox.svg.app.beans.ProportionalLayoutPanel;

public class GraphCreator {

Expand Down Expand Up @@ -110,24 +108,20 @@ public ColouredGraph processModel(Model model) {
//if this statement has an object as a literal
else{
if(statement.getObject().isLiteral()){
//literal
Literal literal = statement.getObject().asLiteral();

BitSet vertexColour = graph.getVertexColour(subjectId);

//data typed property
property = statement.getPredicate();
propertyUri = property.getURI();

//put datatype property to the palette
if(!datatypedEdgePalette.containsUri(propertyUri)){
datatypedEdgePalette.addColour(propertyUri);
}


BitSet datatypedEdgeColour = datatypedEdgePalette.getColour(propertyUri);
// if(literal.toString().contains("^^")){
// becareful when literal is datetime
// System.out.println(literal.toString());
// }
graph.addLiterals(literal.toString(), vertexColour, datatypedEdgeColour);
//add to the coloured graph
graph.addLiterals(literal.toString(), subjectId, datatypedEdgeColour);
}
}
}
Expand Down
Loading

0 comments on commit e7ec490

Please sign in to comment.