/** * check if the term is {@code URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")} * @param term * @return */ private static boolean isURIRDFType(Term term) { boolean result = true; if(term instanceof Function){ Function func = (Function) term; if (func.getArity() != 1){ result = false; } else { result = result && (func.getFunctionSymbol() instanceof URITemplatePredicate); result = result && (func.getTerm(0) instanceof ValueConstant) && ((ValueConstant) func.getTerm(0)).getValue(). equals(OBDAVocabulary.RDF_TYPE); } } else { result = false; } return result; }
private Optional<Function> getFirstFunctionalTerm (Function inputFunction) { return inputFunction.getTerms().stream() .findFirst() .filter(t -> t instanceof Function) .map(t -> (Function) t); }
@Override public CQIEImpl clone() { Function copyHead = (Function)head.clone(); List<Function> copyBody = new ArrayList<Function>(body.size() + 10); for (Function atom : body) { if (atom != null) { copyBody.add((Function) atom.clone()); } } CQIEImpl newquery = new CQIEImpl(copyHead, copyBody); newquery.rehash = this.rehash; newquery.string = this.string; newquery.hash = this.hash; return newquery; }
Function uriTemplate = (Function) tquery.get(0).getTerm(0); //URI("..{}..", , ) String subjectTemplate = URITemplates.getUriTemplateString(uriTemplate, prefixmng); Template templs = mfact.createTemplate(subjectTemplate); Predicate pred = func.getFunctionSymbol(); String predName = pred.getName(); URI predUri = null; String predURIString =""; Function predf = (Function)func.getTerm(1); if (predf.getFunctionSymbol() instanceof URITemplatePredicate) { if (predf.getTerms().size() == 1) { //fixed string pred = OBDADataFactoryImpl.getInstance().getPredicate(((ValueConstant)(predf.getTerm(0))).getValue(), 1); predUri = vf.createURI(pred.getName()); object = func.getTerm(1); object = func.getTerm(2); Predicate objectPred = o.getFunctionSymbol(); if (objectPred instanceof URITemplatePredicate) { Term objectTerm = ((Function) object).getTerm(0); else if (o.isDataTypeFunction()) { Term objectTerm = ((Function) object).getTerm(0); Term langTerm = ((Function) object).getTerm(1); Predicate functionSymbol = ((Function) objectTerm).getFunctionSymbol();
for (int idx = 0; idx < body.size(); idx++) { Function oldAtom = body.get(idx); for (Term term : oldAtom.getTerms()) { if (term instanceof Function) { Function uri = ((Function) term); if (uri.getFunctionSymbol() instanceof URITemplatePredicate) { String uriName = ((ValueConstant) uri.getTerm(0)).getValue(); if (uriName.startsWith(oldPrefix)) { uriName = uriName.replaceFirst(oldPrefix, newPrefix); uri.setTerm(0, dfac.getConstantLiteral(uriName)); if (!oldAtom.getFunctionSymbol().equals(removedPredicate)) { continue; Function newAtom = dfac.getFunction(newPredicate, oldAtom.getTerms()); body.set(idx, newAtom);
if (term instanceof Function) { Function function = (Function) term; Predicate functionSymbol = function.getFunctionSymbol(); String fname = getAbbreviatedName(functionSymbol.toString(), prefixManager, false); if (function.isDataTypeFunction()) { int arity = function.getArity(); if (arity == 1) { Term var = function.getTerms().get(0); sb.append(getDisplayName(var, prefixManager)); sb.append("^^rdfs:Literal"); } else if (arity == 2) { Term var = function.getTerms().get(0); Term lang = function.getTerms().get(1); sb.append(getDisplayName(var, prefixManager)); sb.append("@"); Term var = function.getTerms().get(0); sb.append(getDisplayName(var, prefixManager)); sb.append("^^"); Term firstTerm = function.getTerms().get(0); for (Term innerTerm : function.getTerms()) { if (innerTerm instanceof Variable) { varNames.add(getDisplayName(innerTerm, prefixManager));
for (Function atom : body) { String subject, predicate, object = ""; String originalString = atom.getFunctionSymbol().toString(); if (isUnary(atom)) { Term subjectTerm = atom.getTerm(0); subject = getDisplayName(subjectTerm, prefixManager); predicate = "a"; Term subjectTerm = atom.getTerm(0); subject = getDisplayName(subjectTerm, prefixManager); Term predicateTerm = atom.getTerm(1); predicate = getDisplayName(predicateTerm, prefixManager); Term objectTerm = atom.getTerm(2); object = getDisplayName(objectTerm, prefixManager); Term subjectTerm = atom.getTerm(0); subject = getDisplayName(subjectTerm, prefixManager); predicate = getAbbreviatedName(originalString, prefixManager, false); predicate = "<" + predicate + ">"; Term objectTerm = atom.getTerm(1); object = getDisplayName(objectTerm, prefixManager);
Function uriTemplate = (Function) tquery.get(0).getTerm(0); //URI("..{}..", , ) String subjectTemplate = URITemplates.getUriTemplateString(uriTemplate, prefixmng); Predicate pred = func.getFunctionSymbol(); Function predf = (Function)func.getTerm(1); if (predf.getFunctionSymbol() instanceof URITemplatePredicate) { if (predf.getTerms().size() == 1) //fixed string pred = OBDADataFactoryImpl.getInstance().getPredicate(((ValueConstant)(predf.getTerm(0))).getValue(), 1); predUri = vf.createURI(pred.getName()); Term object = func.getTerm(1); } else if (object instanceof Function) { Predicate objectPred = ((Function) object).getFunctionSymbol(); if (objectPred instanceof URITemplatePredicate) { String objectURI = URITemplates.getUriTemplateString((Function)object, prefixmng); Term objectTerm = ((Function) object).getTerm(0);
private Term getFreshTerm(Term term, int suff) { Term newTerm; if (term instanceof Variable) { Variable variable = (Variable) term; newTerm = getVariable(variable.getName() + "_" + suff); } else if (term instanceof Function) { Function functionalTerm = (Function) term; List<Term> innerTerms = functionalTerm.getTerms(); List<Term> newInnerTerms = new LinkedList<>(); for (int j = 0; j < innerTerms.size(); j++) { Term innerTerm = innerTerms.get(j); newInnerTerms.add(getFreshTerm(innerTerm, suff)); } Predicate newFunctionSymbol = functionalTerm.getFunctionSymbol(); Function newFunctionalTerm = getFunction(newFunctionSymbol, newInnerTerms); newTerm = newFunctionalTerm; } else if (term instanceof Constant) { newTerm = term.clone(); } else { throw new RuntimeException("Unsupported term: " + term); } return newTerm; }
/** * This method finds the term for the predicate template */ private static Function findTemplatePredicateTerm(Function atom, int arity) { Function uriTermForPredicate; if(arity == 1) { uriTermForPredicate = (Function) atom.getTerm(2); } else if (arity == 2) { uriTermForPredicate = (Function) atom.getTerm(1); } else { throw new IllegalArgumentException("The parameter arity should be 1 or 2"); } return uriTermForPredicate; } }
@Override public void removeRules(Collection<CQIE> rs) { for (CQIE rule : rs) { this.rules.remove(rule); Predicate predicate = rule.getHead().getFunctionSymbol(); List<CQIE> indexedRules = this.predicateIndex.get(predicate); if (indexedRules != null) indexedRules.remove(rule); } }
/** * Checks if the atom is unary or not. */ private static boolean isUnary(Function atom) { return atom.getArity() == 1 ? true : false; }
Predicate pred = bodyAtom.getFunctionSymbol(); if (isURIRDFType(bodyAtom.getTerm(1))) {
List<Term> terms = func.getTerms(); List<Term> newTerms = new ArrayList<>(terms.size()); for (Term innerTerm : terms) newTerms.add(renameVariables(innerTerm, lookupTable, idfac)); return fac.getFunction(func.getFunctionSymbol(), newTerms);
private static boolean isRDFType(Term pred) { // if (pred instanceof Constant && ((Constant) pred).getValue().equals(OBDAVocabulary.RDF_TYPE)) { // return true; // } if (pred instanceof Function && ((Function) pred).getTerm(0) instanceof Constant ) { String c= ((Constant) ((Function) pred).getTerm(0)).getValue(); return c.equals(OBDAVocabulary.RDF_TYPE); } return false; }
@Override public void appendRule(CQIE rule) { if (rule == null) { throw new IllegalArgumentException("DatalogProgram: Recieved a null rule."); } if (rules.contains(rule)) { return; // Skip if the rule already exists! } rules.add(rule); Function head = rule.getHead(); if (head != null) { Predicate predicate = rule.getHead().getFunctionSymbol(); List<CQIE> indexedRules = predicateIndex.get(predicate); if (indexedRules == null) { indexedRules = new LinkedList<CQIE>(); predicateIndex.put(predicate, indexedRules); } indexedRules.add(rule); } }
public static boolean isBoolean(CQIE query) { return query.getHead().getArity() == 0; }
if (vars.isEmpty()) { Function funcObjectAtom = (Function) objectAtom; Term term0 = funcObjectAtom.getTerm(0); if (term0 instanceof Function) { Function constPred = (Function) term0; Predicate newpred = constPred.getFunctionSymbol(); Function bodyAtom = fac.getFunction(newpred, subjectAtom); body.add(bodyAtom);
public static void addReferencedVariablesTo(Collection<Variable> vars, Function f) { for (Term t : f.getTerms()) { if (t instanceof Variable) vars.add((Variable)t); else if (t instanceof Function) addReferencedVariablesTo(vars, (Function)t); // else (t instanceof BNode) || (t instanceof URIConstant) || (t instanceof ValueConstant) // no-op } }