I understand Model like this :
It consists of triplets ( Subject Predicate/Property Object)
A B C
C D E
E F G
G H I
X Y Z
And We can represent above set of Triplets with Nodes and Edges in Graph.
I want to get Subject 'A' value which can have chaining like above C->E->G->I in Model and in terms of Graph It should return SubGraph from 'C' node.
Here is my Recursive code:
public Model getRecursive(String subject) {
Model newModel = ModelFactory.createDefaultModel();
StmtIterator it = this.model.listStatements();
while (it.hasNext()) {
Statement statement = it.next();
if(statement.getSubject().toString().equals(subject))
{
newModel = newModel.add(statement);
}
}
Model objectModel = ModelFactory.createDefaultModel();
it = newModel.listStatements();
while (it.hasNext()) {
Statement statement = it.next();
objectModel = objectModel.add(getRecursive(statement.getObject().toString()));
}
newModel = newModel.add(objectModel);
return newModel;
}
But my problem is its complexity is too high.
Assume Model have 1000 triplets and a Subject assume 'A' have chain length of 10.Then According to my code , time complexity is 10*1000 because my recursive code for each call iterate through whole triplets to find triplets with current given Subject and then further recursively call on Object values.
Is there any other way to do it fastly ? I didn't get any methods in graph and Model which can do it fastly .