use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class SampleKBLoader method tx.
public EmbeddedGraknTx<?> tx() {
if (tx == null || tx.isClosed()) {
// Load the graph if we need to
if (!graphLoaded) {
try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
load(graph);
graph.commit();
graphLoaded = true;
}
}
tx = factory.open(GraknTxType.WRITE);
}
return tx;
}
use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class TypeInferenceQueryTest method typedAnswers.
private List<Answer> typedAnswers(List<RelationshipType> possibleTypes, String pattern, EmbeddedGraknTx<?> graph) {
List<Answer> answers = new ArrayList<>();
ReasonerAtomicQuery query = ReasonerQueries.atomic(conjunction(pattern, graph), graph);
for (Type type : possibleTypes) {
GetQuery typedQuery = graph.graql().match(ReasonerQueries.atomic(query.getAtom().addType(type)).getPattern()).get();
typedQuery.stream().filter(ans -> !answers.contains(ans)).forEach(answers::add);
}
return answers;
}
use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class MatchInfer method stream.
@Override
public Stream<Answer> stream(Optional<EmbeddedGraknTx<?>> optionalGraph) {
// If the tx is not embedded, treat it like there is no transaction
// TODO: this is dodgy - when queries don't contain transactions this can be fixed
EmbeddedGraknTx<?> tx = optionalOr(optionalGraph, inner.tx().filter(t -> t instanceof EmbeddedGraknTx).map(t -> (EmbeddedGraknTx<?>) t)).orElseThrow(GraqlQueryException::noTx);
if (!RuleUtils.hasRules(tx))
return inner.stream(optionalGraph);
validatePattern(tx);
try {
Iterator<Conjunction<VarPatternAdmin>> conjIt = getPattern().getDisjunctiveNormalForm().getPatterns().iterator();
Conjunction<VarPatternAdmin> conj = conjIt.next();
ReasonerQuery conjQuery = ReasonerQueries.create(conj, tx);
conjQuery.checkValid();
Stream<Answer> answerStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
while (conjIt.hasNext()) {
conj = conjIt.next();
conjQuery = ReasonerQueries.create(conj, tx);
Stream<Answer> localStream = conjQuery.isRuleResolvable() ? conjQuery.resolve(materialise) : tx.graql().infer(false).match(conj).stream();
answerStream = Stream.concat(answerStream, localStream);
}
return answerStream.map(result -> result.project(getSelectedNames()));
} catch (GraqlQueryException e) {
System.err.println(e.getMessage());
return Stream.empty();
}
}
use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class AtomicTest method testRuleApplicability_DerivedTypes.
@Test
public void testRuleApplicability_DerivedTypes() {
EmbeddedGraknTx<?> graph = ruleApplicabilitySet.tx();
String typeString = "{$x isa reifying-relation;}";
String typeString2 = "{$x isa typed-relation;}";
String typeString3 = "{$x isa description;}";
String typeString4 = "{$x isa attribute;}";
String typeString5 = "{$x isa relationship;}";
Atom type = ReasonerQueries.atomic(conjunction(typeString, graph), graph).getAtom();
Atom type2 = ReasonerQueries.atomic(conjunction(typeString2, graph), graph).getAtom();
Atom type3 = ReasonerQueries.atomic(conjunction(typeString3, graph), graph).getAtom();
Atom type4 = ReasonerQueries.atomic(conjunction(typeString4, graph), graph).getAtom();
Atom type5 = ReasonerQueries.atomic(conjunction(typeString5, graph), graph).getAtom();
List<InferenceRule> rules = RuleUtils.getRules(graph).map(r -> new InferenceRule(r, graph)).collect(Collectors.toList());
assertEquals(2, type.getApplicableRules().count());
assertEquals(1, type2.getApplicableRules().count());
assertEquals(3, type3.getApplicableRules().count());
assertEquals(rules.stream().filter(r -> r.getHead().getAtom().isResource()).count(), type4.getApplicableRules().count());
assertEquals(rules.stream().filter(r -> r.getHead().getAtom().isRelation()).count(), type5.getApplicableRules().count());
}
use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class StructuralCache method get.
/**
* @param query to be retrieved
* @return answer stream of provided query
*/
public Stream<Answer> get(Q query) {
Equivalence.Wrapper<Q> structQuery = equivalence.wrap(query);
EmbeddedGraknTx<?> tx = query.tx();
CacheEntry<Q, GraqlTraversal> match = structCache.get(structQuery);
if (match != null) {
Q equivalentQuery = match.query();
GraqlTraversal traversal = match.cachedElement();
Unifier unifier = equivalentQuery.getMultiUnifier(query, UnifierType.STRUCTURAL).getAny();
Map<Var, ConceptId> idTransform = equivalentQuery.idTransform(query, unifier);
ReasonerQueryImpl transformedQuery = equivalentQuery.transformIds(idTransform);
return MatchBase.streamWithTraversal(transformedQuery.getPattern().commonVars(), tx, traversal.transform(idTransform)).map(ans -> ans.unify(unifier)).map(a -> a.explain(new LookupExplanation(query)));
}
GraqlTraversal traversal = GreedyTraversalPlan.createTraversal(query.getPattern(), tx);
structCache.put(structQuery, new CacheEntry<>(query, traversal));
return MatchBase.streamWithTraversal(query.getPattern().commonVars(), tx, traversal).map(a -> a.explain(new LookupExplanation(query)));
}
Aggregations