/** * Determines if it is a unary function. */ private boolean isUnary(Function fun) { return fun.getArity() == 1; }
/** * Determines if it is a binary function. */ private boolean isBinary(Function fun) { return fun.getArity() == 2; }
/** * Checks if the atom is unary or not. */ private static boolean isUnary(Function atom) { return atom.getArity() == 1 ? true : false; }
public static boolean isBoolean(CQIE query) { return query.getHead().getArity() == 0; }
public static boolean isBoolean(CQIE query) { return query.getHead().getArity() == 0; }
/** * Generates the view definition, i.e., "tablename viewname". */ public String getViewDefinition(Function atom) { FromItem dd = fromItemsForAtoms.get(atom); if (dd != null) { return sqladapter.sqlTableName(dd.definition, dd.alias.getSQLRendering()); } else if (atom.getArity() == 0) { // Special case of nullary atoms return inBrackets(sqladapter.getDummyTable()) + " tdummy"; } throw new RuntimeException( "Impossible to get data definition for: " + atom + ", type: " + dd); }
/** * Generates the view definition, i.e., "tablename viewname". */ String getViewDefinition(Function atom) { FromItem dd = fromItemsForAtoms.get(atom); if (dd != null) { return sqladapter.sqlTableName(dd.definition, dd.alias.getSQLRendering()); } else if (atom.getArity() == 0) { // Special case of nullary atoms return inBrackets(sqladapter.getDummyTable()) + " tdummy"; } throw new RuntimeException( "Impossible to get data definition for: " + atom + ", type: " + dd); }
/** * check if the term is {@code URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")} */ private static boolean isURIRDFType(Term term) { if (term instanceof Function) { Function func = (Function) term; if (func.getArity() == 1 && (func.getFunctionSymbol() instanceof URITemplatePredicate)) { Term t0 = func.getTerm(0); if (t0 instanceof ValueConstant) return ((ValueConstant) t0).getValue().equals(IriConstants.RDF_TYPE); } } return false; }
private Term evalUriTemplateEqNeq(Function uriFunction1, Function uriFunction2, boolean isEqual) { int arityForFunction1 = uriFunction1.getArity(); int arityForFunction2 = uriFunction2.getArity(); if (arityForFunction1 == 1) { if (arityForFunction2 == 1) { return evalUriFunctionsWithSingleTerm(uriFunction1, uriFunction2, isEqual); } else if (arityForFunction2 > 1) { // Currently, we assume the arity should be the same (already decomposed URIs) return termFactory.getBooleanConstant(!isEqual); } } else if (arityForFunction1 > 1) { if (arityForFunction2 == 1) { // Currently, we assume the arity should be the same (already decomposed URIs) return termFactory.getBooleanConstant(!isEqual); } else if (arityForFunction2 > 1) { return evalUriFunctionsWithMultipleTerms(uriFunction1, uriFunction2, isEqual); } } return null; }
private Function getFreshAtom(Function a, String suffix) { List<Term> termscopy = new ArrayList<>(a.getArity()); for (Term t : a.getTerms()) { if (t instanceof Variable) { Variable v = (Variable)t; termscopy.add(termFactory.getVariable(v.getName() + suffix)); } else termscopy.add(t.clone()); } return termFactory.getFunction(a.getFunctionSymbol(), termscopy); }
private Term evalUriTemplateEqNeq(Function uriFunction1, Function uriFunction2, boolean isEqual) { int arityForFunction1 = uriFunction1.getArity(); int arityForFunction2 = uriFunction2.getArity(); if (arityForFunction1 == 1) { if (arityForFunction2 == 1) { return evalUriFunctionsWithSingleTerm(uriFunction1, uriFunction2, isEqual); } else if (arityForFunction2 > 1) { // Currently, we assume the arity should be the same (already decomposed URIs) return TERM_FACTORY.getBooleanConstant(!isEqual); } } else if (arityForFunction1 > 1) { if (arityForFunction2 == 1) { // Currently, we assume the arity should be the same (already decomposed URIs) return TERM_FACTORY.getBooleanConstant(!isEqual); } else if (arityForFunction2 > 1) { return evalUriFunctionsWithMultipleTerms(uriFunction1, uriFunction2, isEqual); } } return null; }
private Set<String> getEqConditionsForConstants(List<Function> atoms, AliasIndex index) { Set<String> equalities = new LinkedHashSet<>(); for (Function atom : atoms) { if (atom.isDataFunction()) { for (int i = 0; i < atom.getArity(); i++) { Term t = atom.getTerm(i); if (t instanceof Constant) { String value = getSQLString(t, index, false); QualifiedAttributeID column = index.getColumn(atom, i); equalities.add(String.format("(%s = %s)", column.getSQLRendering(), value)); } } } } return equalities; }
private Set<String> getEqConditionsForConstants(List<Function> atoms, AliasIndex index) { Set<String> equalities = new LinkedHashSet<>(); for (Function atom : atoms) { if (atom.isDataFunction()) { for (int i = 0; i < atom.getArity(); i++) { Term t = atom.getTerm(i); if (t instanceof Constant) { String value = getSQLString(t, index, false); QualifiedAttributeID column = index.getColumn(atom, i); equalities.add(String.format("(%s = %s)", column.getSQLRendering(), value)); } } } } return equalities; }
private void deleteExplicitTypes(Term term, Function atom, int position) { if(term instanceof Function){ Function castTerm = (Function) term; IntStream.range(0, castTerm.getArity()) .forEach(i -> deleteExplicitTypes( castTerm.getTerm(i), castTerm, i )); if(castTerm.isDataTypeFunction()){ atom.setTerm(position, castTerm.getTerm(0)); } } }
private void deleteExplicitTypes(Term term, Function atom, int position) { if(term instanceof Function){ Function castTerm = (Function) term; IntStream.range(0, castTerm.getArity()) .forEach(i -> deleteExplicitTypes( castTerm.getTerm(i), castTerm, i )); if(castTerm.isDataTypeFunction()){ atom.setTerm(position, castTerm.getTerm(0)); } } }
@Override public boolean composeFunctions(Function first, Function second) { // Basic case: if predicates are different or their arity is different, // then no unifier if ((first.getArity() != second.getArity() || !first.getFunctionSymbol().equals(second.getFunctionSymbol()))) { return false; } Function firstAtom = (Function) first.clone(); Function secondAtom = (Function) second.clone(); int arity = first.getArity(); // Computing the disagreement set for (int termidx = 0; termidx < arity; termidx++) { // Checking if there are already substitutions calculated for the // current terms. If there are any, then we have to take the // substituted terms instead of the original ones. Term term1 = firstAtom.getTerm(termidx); Term term2 = secondAtom.getTerm(termidx); if (!composeTerms(term1, term2)) return false; // Applying the newly computed substitution to the 'replacement' of // the existing substitutions SubstitutionUtilities.applySubstitution(firstAtom, this, termidx + 1); SubstitutionUtilities.applySubstitution(secondAtom, this, termidx + 1); } return true; }
@Override public boolean composeFunctions(Function first, Function second) { // Basic case: if predicates are different or their arity is different, // then no unifier if ((first.getArity() != second.getArity() || !first.getFunctionSymbol().equals(second.getFunctionSymbol()))) { return false; } Function firstAtom = (Function) first.clone(); Function secondAtom = (Function) second.clone(); int arity = first.getArity(); // Computing the disagreement set for (int termidx = 0; termidx < arity; termidx++) { // Checking if there are already substitutions calculated for the // current terms. If there are any, then we have to take the // substituted terms instead of the original ones. Term term1 = firstAtom.getTerm(termidx); Term term2 = secondAtom.getTerm(termidx); if (!composeTerms(term1, term2)) return false; // Applying the newly computed substitution to the 'replacement' of // the existing substitutions substitutionUtilities.applySubstitution(firstAtom, this, termidx + 1); substitutionUtilities.applySubstitution(secondAtom, this, termidx + 1); } return true; }
/** * check if the term is {@code URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")} */ public static boolean isURIRDFType(Term term) { if (term instanceof Function) { Function func = (Function) term; if (func.getArity() == 1 && (func.getFunctionSymbol() instanceof URITemplatePredicate)) { Term t0 = func.getTerm(0); if (t0 instanceof IRIConstant) return ((IRIConstant) t0).getIRI().equals(RDF.TYPE); // UGLY!! TODO: remove it else if (t0 instanceof ValueConstant) return ((ValueConstant) t0).getValue().equals(RDF.TYPE.getIRIString()); } } return false; }
public static Set<OWLDeclarationAxiom> extractDeclarationAxioms(OWLOntologyManager manager, Stream<? extends Function> targetAtoms) { OWLDataFactory dataFactory = manager.getOWLDataFactory(); Set<OWLDeclarationAxiom> declarationAxioms = new HashSet<>(); targetAtoms.forEach(f -> { IRI iri = IRI.create(f.getFunctionSymbol().getName()); final OWLEntity entity; if (f.getArity() == 1) { entity = dataFactory.getOWLClass(iri); } else { Predicate.COL_TYPE secondArgType = f.getFunctionSymbol().getType(1); if ((secondArgType != null) && secondArgType.equals(Predicate.COL_TYPE.OBJECT)) { entity = dataFactory.getOWLObjectProperty(iri); } else { entity = dataFactory.getOWLDataProperty(iri); } } declarationAxioms.add(dataFactory.getOWLDeclarationAxiom(entity)); }); return declarationAxioms; }
private static CQIE generateTripleMapping(CQIE rule) { Function newhead; Function currenthead = rule.getHead(); if (currenthead.getArity() == 1) { // head is Class(x) Forming head as triple(x,uri(rdf:type), uri(Class)) Function rdfTypeConstant = TERM_FACTORY.getUriTemplate(TERM_FACTORY.getConstantLiteral(IriConstants.RDF_TYPE)); String classname = currenthead.getFunctionSymbol().getName(); Term classConstant = TERM_FACTORY.getUriTemplate(TERM_FACTORY.getConstantLiteral(classname)); newhead = ATOM_FACTORY.getTripleAtom(currenthead.getTerm(0), rdfTypeConstant, classConstant); } else if (currenthead.getArity() == 2) { // head is Property(x,y) Forming head as triple(x,uri(Property), y) String propname = currenthead.getFunctionSymbol().getName(); Function propConstant = TERM_FACTORY.getUriTemplate(TERM_FACTORY.getConstantLiteral(propname)); newhead = ATOM_FACTORY.getTripleAtom(currenthead.getTerm(0), propConstant, currenthead.getTerm(1)); } else { // head is triple(x,uri(Property),y) newhead = (Function) currenthead.clone(); } return DATALOG_FACTORY.getCQIE(newhead, rule.getBody()); } }