use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class TermTest method variableConstructorTest.
@SuppressWarnings("deprecation")
@Test
public void variableConstructorTest() {
String label = "label";
Term term = DefaultTermFactory.instance().createVariable(label);
Assert.assertTrue(Term.Type.VARIABLE.equals(term.getType()));
Assert.assertFalse(term.isConstant());
Assert.assertFalse(term.isLiteral());
Assert.assertTrue(term.isVariable());
Assert.assertTrue(term.getIdentifier().toString().equals(label));
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class TermPartitionUtils method getAssociatedSubstitution.
/**
* Compute the substitution associated with the current partition this
* method computes a substitution by choosing one representative term by
* class. Choosing first constant then answer variable (in context) return
* null if the partition contain two constants in the same class
*/
public static Substitution getAssociatedSubstitution(Partition<Term> partition, ConjunctiveQuery context) {
Substitution substitution = DefaultSubstitutionFactory.instance().createSubstitution();
// partition
for (Collection<Term> set : partition) {
Iterator<Term> i = set.iterator();
Term representative = i.next();
while (i.hasNext()) {
Term t = i.next();
// t and the current representative are different
if (representative.equals(t)) {
i.remove();
} else {
if (t.isConstant()) {
// representative is a different constant
if (representative.isConstant()) {
return null;
} else {
representative = t;
}
} else if (representative.isVariable()) {
// t is a variable from the answer
if (context != null && !context.getAnswerVariables().contains(representative) && context.getAtomSet().getTerms().contains(t)) {
representative = t;
}
}
}
}
// representative of the equivalence set
for (Term t : set) {
if (!t.equals(representative)) {
if (t.isVariable()) {
substitution.put((Variable) t, representative);
}
}
}
}
return substitution;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method homomorphism.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private Collection<Substitution> homomorphism(ConjunctiveQuery query, Collection<Atom>[] queryAtomRanked, AtomSet facts, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
Collection<Substitution> substitutionList = new LinkedList<Substitution>();
if (orderedVars.size() == 0) {
Substitution filteredSub = new HashMapSubstitution();
for (Term var : query.getAnswerVariables()) {
if (var.isVariable()) {
filteredSub.put((Variable) var, substitution.createImageOf(var));
}
}
substitutionList.add(filteredSub);
} else {
Term var = orderedVars.remove(0);
if (var.isVariable()) {
for (Term substitut : domain) {
Substitution tmpSubstitution = new HashMapSubstitution(substitution);
tmpSubstitution.put((Variable) var, substitut);
// Test partial homomorphism
if (isHomomorphism(queryAtomRanked[rank], facts, tmpSubstitution))
substitutionList.addAll(homomorphism(query, queryAtomRanked, facts, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1));
}
}
}
return substitutionList;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method existHomomorphism.
private static boolean existHomomorphism(AtomSet atomSet1, Collection<Atom>[] queryAtomRanked, AtomSet atomSet2, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
if (orderedVars.size() == 0) {
return true;
} else {
Term var;
Set<Term> domaine = atomSet2.getTerms();
var = orderedVars.remove(0);
if (var.isVariable()) {
for (Term substitut : domaine) {
Substitution tmpSubstitution = new HashMapSubstitution(substitution);
tmpSubstitution.put((Variable) var, substitut);
// Test partial homomorphism
if (isHomomorphism(queryAtomRanked[rank], atomSet2, tmpSubstitution))
if (existHomomorphism(atomSet1, queryAtomRanked, atomSet2, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1)) {
return true;
}
}
}
}
return false;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class BCCScheduler method execute.
@Override
public VarSharedData[] execute(InMemoryAtomSet query, Set<Variable> preAffectedVars, List<Term> ans, AtomSet data, RulesCompilation rc) {
InMemoryAtomSet fixedQuery = (preAffectedVars.isEmpty()) ? query : computeFixedQuery(query, preAffectedVars);
// Term index
Set<Variable> variables = fixedQuery.getVariables();
Map<Term, Integer> map = new HashMap<Term, Integer>();
this.inverseMap = new Term[variables.size() + 1];
{
// init indexes
int i = 0;
for (Variable t : variables) {
inverseMap[++i] = t;
map.put(t, i);
}
}
HyperGraph graph = constructHyperGraph(fixedQuery, variables.size(), this.inverseMap, map, ans);
double[] proba;
if (data instanceof Store) {
proba = this.computeProba(fixedQuery, (Store) data, variables.size(), map, rc);
} else {
proba = new double[variables.size() + 1];
Arrays.fill(proba, 1);
}
// bias proba of answer variables
for (Term t : ans) {
if (t.isVariable()) {
int idx = map.get(t);
proba[idx] *= ansVariableFactor;
}
}
this.varComparator = new IntegerComparator(proba);
TmpData d = biconnect(graph, this.varComparator);
VarSharedData[] vars = new VarSharedData[variables.size() + 2];
this.BCC.varData = new VarData[variables.size() + 2];
vars[0] = new VarSharedData(0);
this.BCC.varData[0] = new VarData();
int lastAnswerVariable = -1;
for (int i = 1; i < d.vars.length; ++i) {
VarSharedData v = d.vars[i];
vars[v.level] = v;
this.BCC.varData[v.level] = d.ext[i];
v.value = (Variable) this.inverseMap[i];
v.nextLevel = v.level + 1;
v.previousLevel = v.level - 1;
if (this.withForbiddenCandidate && this.BCC.varData[v.level].isAccesseur) {
this.BCC.varData[v.level].forbidden = new HashSet<Term>();
}
if (ans.contains(v.value)) {
if (v.level > lastAnswerVariable)
lastAnswerVariable = v.level;
}
}
int level = variables.size() + 1;
vars[level] = new VarSharedData(level);
this.BCC.varData[level] = new VarData();
// if an homomorphism is found, go to the last answer variable
vars[level].previousLevel = lastAnswerVariable;
// Profiling
if (this.getProfiler().isProfilingEnabled()) {
StringBuilder sb = new StringBuilder();
for (VarSharedData v : vars) {
sb.append(v.value);
sb.append(" > ");
}
this.getProfiler().put("BCCOrder", sb.toString());
}
return vars;
}
Aggregations