Skip to content

Commit

Permalink
Core: refactor storage layer, pt.6 (#389, #391).
Browse files Browse the repository at this point in the history
Introduce association index.
Does not yet compile.

See ticket 389.
See ticket 391.
  • Loading branch information
jri committed Jan 2, 2013
1 parent bba9043 commit a276f35
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 172 deletions.
Expand Up @@ -315,7 +315,8 @@ public Set<Association> getAssociations(long topic1Id, long topic2Id, String ass
DeepaMehtaTransaction tx = beginTx();
try {
// ### FIXME: fetchComposite=false, parameterize it
Set<Association> assocs = attach(storage.getAssociations(topic1Id, topic2Id, assocTypeUri), false);
Set<Association> assocs = attach(storage.getAssociations(assocTypeUri, topic1Id, topic2Id, null, null),
false); // roleTypeUri1=null, roleTypeUri2=null
tx.success();
return assocs;
} catch (Exception e) {
Expand Down
Expand Up @@ -90,7 +90,7 @@ public TopicModel getTopic(String key, SimpleValue value) {
// ---

/**
* Convenience method.
* Convenience method (checks singularity).
*
* @return The fetched topics.
* Note: their composite values are not initialized.
Expand All @@ -115,15 +115,17 @@ public RelatedTopicModel getTopicRelatedTopic(long topicId, String assocTypeUri,
* @return The fetched topics.
* Note: their composite values are not initialized.
*/
public ResultSet<RelatedTopicModel> getTopicRelatedTopics(long topicId, String assocTypeUri, String myRoleTypeUri,
String othersRoleTypeUri, String othersTopicTypeUri,
int maxResultSize) {
return mg.getTopicRelatedTopics(topicId, assocTypeUri, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri,
maxResultSize);
public ResultSet<RelatedTopicModel> getTopicRelatedTopics(long topicId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri, int maxResultSize) {
Set<RelatedTopicModel> relTopics = mg.getTopicRelatedTopics(topicId, assocTypeUri, myRoleTypeUri,
othersRoleTypeUri, othersTopicTypeUri);
// ### TODO: respect maxResultSize
return new ResultSet(relTopics.size(), relTopics);
}

/**
* Convenience method.
* Convenience method (receives *list* of association types).
*
* @param assocTypeUris may be null
* @param myRoleTypeUri may be null
Expand Down Expand Up @@ -158,6 +160,8 @@ public Set<AssociationModel> getTopicAssociations(long topicId) {
// ---

/**
* Convenience method.
*
* @return The fetched association.
* Note: its composite value is not initialized.
*/
Expand Down Expand Up @@ -250,79 +254,82 @@ public void deleteTopic(long topicId) {
// === Associations ===

public AssociationModel getAssociation(long assocId) {
return buildAssociation(mg.getMehtaEdge(assocId));
return mg.getMehtaEdge(assocId);
}

// ---

/**
* Convenience method.
*
* Returns the association between two topics, qualified by association type and both role types.
* If no such association exists <code>null</code> is returned.
* If more than one association exist, a runtime exception is thrown.
*
* @param assocTypeUri Association type filter. Pass <code>null</code> to switch filter off.
* ### FIXME: for methods with a singular return value all filters should be mandatory
*/
public AssociationModel getAssociation(String assocTypeUri, long topic1Id, long topic2Id, String roleTypeUri1,
public AssociationModel getAssociation(String assocTypeUri, long topicId1, long topicId2, String roleTypeUri1,
String roleTypeUri2) {
Set<MehtaEdge> edges = mg.getMehtaEdges(topic1Id, topic2Id, roleTypeUri1, roleTypeUri2);
// apply type filter
if (assocTypeUri != null) {
filterEdgesByAssociationType(edges, assocTypeUri);
}
// error check
if (edges.size() > 1) {
throw new RuntimeException("Ambiguity: there are " + edges.size() + " \"" + assocTypeUri +
"\" associations (topic1Id=" + topic1Id + ", topic2Id=" + topic2Id + ", " +
"roleTypeUri1=\"" + roleTypeUri1 + "\", roleTypeUri2=\"" + roleTypeUri2 + "\")");
}
//
if (edges.size() == 0) {
Set<AssociationModel> assocs = getAssociations(assocTypeUri, topicId1, topicId2, roleTypeUri1, roleTypeUri2);
switch (assocs.size()) {
case 0:
return null;
} else {
return buildAssociation(edges.iterator().next());
case 1:
return assocs.iterator().next();
default:
throw new RuntimeException("Ambiguity: there are " + assocs.size() + " \"" + assocTypeUri +
"\" associations (topicId1=" + topicId1 + ", topicId2=" + topicId2 + ", " +
"roleTypeUri1=\"" + roleTypeUri1 + "\", roleTypeUri2=\"" + roleTypeUri2 + "\")");
}
}

/**
* Returns the associations between two topics. If no such association exists an empty set is returned.
*
* @param assocTypeUri Association type filter. Pass <code>null</code> to switch filter off.
*/
public Set<AssociationModel> getAssociations(String assocTypeUri, long topicId1, long topicId2, String roleTypeUri1,
String roleTypeUri2) {
return mg.getMehtaEdges(assocTypeUri, topicId1, topicId2, roleTypeUri1, roleTypeUri2);
}

// ---

/**
* Convenience method.
*/
public AssociationModel getAssociationBetweenTopicAndAssociation(String assocTypeUri, long topicId, long assocId,
String topicRoleTypeUri, String assocRoleTypeUri) {
Set<MehtaEdge> edges = mg.getMehtaEdgesBetweenNodeAndEdge(topicId, assocId, topicRoleTypeUri, assocRoleTypeUri);
// apply type filter
if (assocTypeUri != null) {
filterEdgesByAssociationType(edges, assocTypeUri);
}
// error check
if (edges.size() > 1) {
throw new RuntimeException("Ambiguity: there are " + edges.size() + " \"" + assocTypeUri +
Set<AssociationModel> assocs = getAssociationsBetweenTopicAndAssociation(assocTypeUri, topicId, assocId,
topicRoleTypeUri, assocRoleTypeUri);
switch (assocs.size()) {
case 0:
return null;
case 1:
return assocs.iterator().next();
default:
throw new RuntimeException("Ambiguity: there are " + assocs.size() + " \"" + assocTypeUri +
"\" associations (topicId=" + topicId + ", assocId=" + assocId + ", " +
"topicRoleTypeUri=\"" + topicRoleTypeUri + "\", assocRoleTypeUri=\"" + assocRoleTypeUri + "\")");
}
//
if (edges.size() == 0) {
return null;
} else {
return buildAssociation(edges.iterator().next());
}
}

/**
* Returns the associations between two topics. If no such association exists an empty set is returned.
*
* @param assocTypeUri Association type filter. Pass <code>null</code> to switch filter off.
*/
public Set<AssociationModel> getAssociations(long topic1Id, long topic2Id, String assocTypeUri) {
Set<MehtaEdge> edges = mg.getMehtaEdges(topic1Id, topic2Id);
// apply type filter
if (assocTypeUri != null) {
filterEdgesByAssociationType(edges, assocTypeUri);
}
//
return buildAssociations(edges);
public Set<AssociationModel> getAssociationsBetweenTopicAndAssociation(String assocTypeUri, long topicId,
long assocId, String topicRoleTypeUri, String assocRoleTypeUri) {
return mg.getMehtaEdgesBetweenNodeAndEdge(assocTypeUri, topicId, assocId, topicRoleTypeUri, assocRoleTypeUri);
}

// ---

public RelatedTopicModel getAssociationRelatedTopic(long assocId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri) {
/**
* Convenience method (checks singularity).
*
* @return The fetched topics.
* Note: their composite values are not initialized.
*/
public RelatedTopicModel getAssociationRelatedTopic(long assocId, String assocTypeUri, String myRoleTypeUri,
String othersRoleTypeUri, String othersTopicTypeUri) {
ResultSet<RelatedTopicModel> topics = getAssociationRelatedTopics(assocId, assocTypeUri, myRoleTypeUri,
othersRoleTypeUri, othersTopicTypeUri, 0);
switch (topics.getSize()) {
Expand All @@ -337,32 +344,40 @@ public RelatedTopicModel getAssociationRelatedTopic(long assocId, String assocTy
}
}

/**
* @return The fetched topics.
* Note: their composite values are not initialized.
*/
public ResultSet<RelatedTopicModel> getAssociationRelatedTopics(long assocId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri, int maxResultSize) {
List assocTypeUris = assocTypeUri != null ? Arrays.asList(assocTypeUri) : null;
return getAssociationRelatedTopics(assocId, assocTypeUris, myRoleTypeUri, othersRoleTypeUri, othersTopicTypeUri,
maxResultSize);
Set<RelatedTopicModel> relTopics = mg.getAssociationRelatedTopics(assocId, assocTypeUri, myRoleTypeUri,
othersRoleTypeUri, othersTopicTypeUri);
// ### TODO: respect maxResultSize
return new ResultSet(relTopics.size(), relTopics);
}

/**
* Convenience method (receives *list* of association types).
*
* @param assocTypeUris may be null
* @param myRoleTypeUri may be null
* @param othersRoleTypeUri may be null
* @param othersTopicTypeUri may be null
*
* @return The fetched topics.
* Note: their composite values are not initialized.
*/
public ResultSet<RelatedTopicModel> getAssociationRelatedTopics(long assocId, List assocTypeUris,
String myRoleTypeUri, String othersRoleTypeUri,
String othersTopicTypeUri, int maxResultSize) {
Set<ConnectedMehtaNode> nodes = mg.getMehtaEdge(assocId).getConnectedMehtaNodes(myRoleTypeUri,
othersRoleTypeUri);
if (othersTopicTypeUri != null) {
filterNodesByTopicType(nodes, othersTopicTypeUri);
}
if (assocTypeUris != null) {
filterNodesByAssociationType(nodes, assocTypeUris);
ResultSet<RelatedTopicModel> result = new ResultSet();
for (String assocTypeUri : assocTypeUris) {
ResultSet<RelatedTopicModel> res = getAssociationRelatedTopics(assocId, assocTypeUri, myRoleTypeUri,
othersRoleTypeUri, othersTopicTypeUri, maxResultSize);
result.addAll(res);
}
return buildRelatedTopics(nodes, maxResultSize);
return result;
}

// ---
Expand Down
@@ -1,6 +1,5 @@
package de.deepamehta.core.storage.spi;

import de.deepamehta.core.ResultSet;
import de.deepamehta.core.model.AssociationModel;
import de.deepamehta.core.model.IndexMode;
import de.deepamehta.core.model.RelatedAssociationModel;
Expand Down Expand Up @@ -51,11 +50,13 @@ public interface MehtaGraph {

MehtaEdge createMehtaEdge(MehtaObjectRole object1, MehtaObjectRole object2);

MehtaEdge getMehtaEdge(long id);
AssociationModel getMehtaEdge(long id);

Set<MehtaEdge> getMehtaEdges(long node1Id, long node2Id);
Set<MehtaEdge> getMehtaEdges(long node1Id, long node2Id, String roleType1, String roleType2);
Set<MehtaEdge> getMehtaEdgesBetweenNodeAndEdge(long nodeId, long edgeId, String nodeRoleType, String edgeRoleType);
Set<AssociationModel> getMehtaEdges(String assocTypeUri, long topicId1, long topicId2, String roleTypeUri1,
String roleTypeUri2);

Set<AssociationModel> getMehtaEdgesBetweenNodeAndEdge(String assocTypeUri, long topicId, long assocId,
String topicRoleTypeUri, String assocRoleTypeUri);



Expand All @@ -67,11 +68,19 @@ public interface MehtaGraph {

// === Traversal ===

ResultSet<RelatedTopicModel> getTopicRelatedTopics(long topicId, String assocTypeUri, String myRoleTypeUri,
String othersRoleTypeUri, String othersTopicTypeUri, int maxResultSize);
Set<RelatedTopicModel> getTopicRelatedTopics(long topicId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri, String othersTopicTypeUri);

Set<RelatedAssociationModel> getTopicRelatedAssociations(long topicId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri, String othersAssocTypeUri);

// ---

Set<RelatedTopicModel> getAssociationRelatedTopics(long assocId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri, String othersTopicTypeUri);

Set<RelatedAssociationModel> getTopicRelatedAssociations(long topicId, String assocTypeUri, String myRoleTypeUri,
String othersRoleTypeUri, String othersAssocTypeUri);
Set<RelatedAssociationModel> getAssociationRelatedAssociations(long assocId, String assocTypeUri,
String myRoleTypeUri, String othersRoleTypeUri, String othersAssocTypeUri);

// ---

Expand Down

0 comments on commit a276f35

Please sign in to comment.