use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.
the class StatBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation rc) throws BacktrackException {
if (!(data instanceof Store)) {
return fallback.exec(v, preAtoms, postAtoms, data, rc);
}
Store store = (Store) data;
Set<Term> terms = null;
if (this.getProfiler() != null) {
this.getProfiler().start("BootstrapTime");
this.getProfiler().start("BootstrapTimeFirstPart");
}
Iterator<Atom> it;
Collection<Constant> constants = null;
Atom aa = null;
it = postAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
try {
if (constants != null && !constants.isEmpty()) {
terms = new HashSet<Term>();
for (Pair<Atom, Substitution> im : rc.getRewritingOf(aa)) {
int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
CloseableIterator<Atom> match = data.match(im.getLeft());
while (match.hasNext()) {
terms.add(match.next().getTerm(pos));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTimeFirstPart");
}
if (terms == null) {
Atom a = null, tmp;
double probaA = 1.1;
it = postAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
terms = BootstrapperUtils.computeCandidatesOverRewritings(a, v, data, rc);
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTime");
}
if (terms == null) {
return data.termsIterator();
} else {
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
} catch (AtomSetException e) {
throw new BacktrackException(e);
} catch (IteratorException e) {
throw new BacktrackException(e);
}
}
use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.
the class FrontierRestrictedChaseHaltingCondition method apply.
@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
Set<Term> fixedVars = substitution.getValues();
if (ruleIndex.get(rule) == null) {
ruleIndex.put(rule, _currentRuleIndex++);
}
final int index = ruleIndex.get(rule).intValue();
StringBuilder frontierSb = new StringBuilder();
SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
for (Term t : frontierSet) {
frontierSb.append("_");
frontierSb.append(t.getLabel());
frontierSb.append(substitution.createImageOf(t).getLabel());
}
String frontier = frontierSb.toString();
for (Variable t : rule.getExistentials()) {
substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
}
InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Fixed Query:" + query);
}
try {
if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
}
} catch (IteratorException e) {
throw new HomomorphismException("An errors occurs while iterating results", e);
}
return newFacts.iterator();
}
use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.
the class NaturalRDBMSStore method termsIterator.
@Override
@Deprecated
public CloseableIterator<Term> termsIterator(Type type) throws AtomSetException {
Set<Term> terms = new TreeSet<Term>();
CloseableIterator<Predicate> predIt = this.predicatesIterator();
try {
while (predIt.hasNext()) {
Predicate p = predIt.next();
for (int i = 0; i < p.getArity(); ++i) {
CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
while (termIt.hasNext()) {
Term t = termIt.next();
if (type.equals(t.getType())) {
terms.add(t);
}
}
}
}
} catch (IteratorException e) {
throw new AtomSetException(e);
}
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.
the class NaturalRDBMSStore method termsIterator.
@Override
public CloseableIterator<Term> termsIterator() throws AtomSetException {
Set<Term> terms = new TreeSet<Term>();
CloseableIterator<Predicate> predIt = this.predicatesIterator();
try {
while (predIt.hasNext()) {
Predicate p = predIt.next();
for (int i = 0; i < p.getArity(); ++i) {
CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
while (termIt.hasNext()) {
terms.add(termIt.next());
}
}
}
} catch (IteratorException e) {
throw new AtomSetException(e);
}
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
use of fr.lirmm.graphik.util.stream.CloseableIteratorAdapter in project graal by graphik-team.
the class StarBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation compilation) throws BacktrackException {
Set<Term> terms = null;
if (this.getProfiler() != null) {
this.getProfiler().start("BootstrapTime");
this.getProfiler().start("BootstrapTimeFirstPart");
}
Iterator<Atom> it;
Collection<Constant> constants = null;
Atom aa = null;
it = postAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
try {
if (constants != null && !constants.isEmpty()) {
terms = new TreeSet<Term>(TermValueComparator.instance());
for (Pair<Atom, Substitution> im : compilation.getRewritingOf(aa)) {
int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
CloseableIterator<Atom> match = data.match(im.getLeft());
while (match.hasNext()) {
terms.add(match.next().getTerm(pos));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTimeFirstPart");
}
if (terms == null) {
it = postAtoms.iterator();
while (it.hasNext()) {
if (terms == null) {
terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
} else {
terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
if (terms == null) {
terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
} else {
terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTime");
}
if (terms == null) {
return data.termsIterator();
} else {
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
} catch (AtomSetException e) {
throw new BacktrackException(e);
} catch (IteratorException e) {
throw new BacktrackException(e);
}
}
Aggregations