/** Returns a new term with the updated references. */ private Term updateTerm(Term term, LookupTable lookupTable) { Term result = null; if (term instanceof Variable) { Variable var = (Variable) term; String varName = var.getName(); String termName = lookupTable.lookup(varName); if (termName == null) { final String msg = String.format( "Error in identifying column name \"%s\", please check the query source in the mappings.\nPossible reasons:\n1. The name is ambiguous, or\n2. The name is not defined in the database schema.", var); throw new RuntimeException(msg); } result = dfac.getVariable(termName); } else if (term instanceof Function) { Function func = (Function) term; List<Term> terms = func.getTerms(); List<Term> newterms = new LinkedList<Term>(); for (Term innerTerm : terms) { newterms.add(updateTerm(innerTerm, lookupTable)); } result = dfac.getFunction(func.getFunctionSymbol(), newterms); } else if (term instanceof Constant) { result = term.clone(); } return result; }
public void test_exists_simple() { Ontology ontology = OntologyFactoryImpl.getInstance().createOntology(""); Predicate a = predicateFactory.getPredicate("a", 1); Predicate c = predicateFactory.getPredicate("c", 1); Predicate r = predicateFactory.getPredicate("r", 2); OClass ac = descFactory.createClass(a); OClass cc = descFactory.createClass(c); PropertySomeRestriction er = descFactory.getPropertySomeRestriction(r, false); ontology.addConcept(ac.getPredicate()); ontology.addConcept(cc.getPredicate()); ontology.addRole(er.getPredicate()); ontology.addAssertion(OntologyFactoryImpl.getInstance().createSubClassAxiom(er, ac)); ontology.addAssertion(OntologyFactoryImpl.getInstance().createSubClassAxiom(cc, er)); DAG res = DAGConstructor.getSigma(ontology); res.clean(); assertTrue(res.getClassNode(ac).getDescendants().contains(res.getClassNode(er))); assertEquals(1, res.getClassNode(ac).getDescendants().size()); assertEquals(0, res.getClassNode(er).getDescendants().size()); assertEquals(0, res.getClassNode(cc).getDescendants().size()); }
private void runWithSeparateFiles() { if (owlFile == null) { throw new NullPointerException("You have to specify an ontology file!"); } OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = null; OBDADataFactory obdaDataFactory = OBDADataFactoryImpl.getInstance(); try { ontology = manager.loadOntologyFromOntologyDocument((new File(owlFile))); if (disableReasoning) { /* * when reasoning is disabled, we extract only the declaration assertions for the vocabulary */ ontology = extractDeclarations(manager, ontology); } Collection<Predicate> predicates = new ArrayList<>(); for (OWLClass owlClass : ontology.getClassesInSignature()) { Predicate predicate = obdaDataFactory.getClassPredicate(owlClass.getIRI().toString()); predicates.add(predicate); } for (OWLDataProperty owlDataProperty : ontology.getDataPropertiesInSignature()) { Predicate predicate = obdaDataFactory.getDataPropertyPredicate(owlDataProperty.getIRI().toString()); predicates.add(predicate); } for (OWLObjectProperty owlObjectProperty : ontology.getObjectPropertiesInSignature()) { Predicate predicate = obdaDataFactory.getObjectPropertyPredicate(owlObjectProperty.getIRI().toString()); predicates.add(predicate); } OBDAModel obdaModel = loadMappingFile(mappingFile); Ontology inputOntology = OWLAPI3TranslatorUtility.translate(ontology); obdaModel.declareAll(inputOntology.getVocabulary()); int numPredicates = predicates.size(); int i = 1; for (Predicate predicate : predicates) { System.err.println(String.format("Materializing %s (%d/%d)", predicate, i, numPredicates)); serializePredicate(ontology, inputOntology, obdaModel, predicate, outputFile, format); i++; } } catch (OWLOntologyCreationException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
private void insertMapping(String target, String source) { List<Function> targetQuery = parse(target); if (targetQuery != null) { final boolean isValid = validator.validate(targetQuery); if (isValid) { try { OBDAModel mapcon = obdaModel; URI sourceID = dataSource.getSourceID(); System.out.println(sourceID.toString() + " \n"); OBDASQLQuery body = dataFactory.getSQLQuery(source); System.out.println(body.toString() + " \n"); OBDAMappingAxiom newmapping = dataFactory.getRDBMSMappingAxiom(txtMappingID.getText().trim(), body, targetQuery); System.out.println(newmapping.toString() + " \n"); if (mapping == null) { // Case when we are creating a new mapping mapcon.addMapping(sourceID, newmapping); } else { // Case when we are updating an existing mapping mapcon.updateMappingsSourceQuery(sourceID, mapping.getId(), body); mapcon.updateTargetQueryMapping(sourceID, mapping.getId(), targetQuery); mapcon.updateMapping(sourceID, mapping.getId(), txtMappingID.getText().trim()); } } catch (DuplicateMappingException e) { JOptionPane.showMessageDialog( this, "Error while inserting mapping: " + e.getMessage() + " is already taken"); return; } parent.setVisible(false); parent.dispose(); } else { // List of invalid predicates that are found by the validator. Vector<String> invalidPredicates = validator.getInvalidPredicates(); String invalidList = ""; for (String predicate : invalidPredicates) { invalidList += "- " + predicate + "\n"; } JOptionPane.showMessageDialog( this, "This list of predicates is unknown by the ontology: \n" + invalidList, "New Mapping", JOptionPane.WARNING_MESSAGE); } } }
/** * Methods to create a {@link Function} starting from a {@link IsNullExpression} * * @param pred IsNullExpression * @param lookupTable * @return a function from the OBDADataFactory */ private Function getFunction(IsNullExpression pred, LookupTable lookupTable) { Expression column = pred.getLeftExpression(); String columnName = column.toString(); String variableName = lookupTable.lookup(columnName); if (variableName == null) { throw new RuntimeException("Unable to find column name for variable: " + columnName); } Term var = dfac.getVariable(variableName); if (!pred.isNot()) { return dfac.getFunctionIsNull(var); } else { return dfac.getFunctionIsNotNull(var); } }
private Term getVariable(Expression pred, LookupTable lookupTable) { String termName = ""; if (pred instanceof Column) { termName = lookupTable.lookup(pred.toString()); if (termName == null) { return null; } return dfac.getVariable(termName); } return null; }
private static Predicate getPredicate(OWLEntity entity) { Predicate p = null; if (entity instanceof OWLClass) { /* We ignore TOP and BOTTOM (Thing and Nothing) */ if (((OWLClass) entity).isOWLThing() || ((OWLClass) entity).isOWLNothing()) { return null; } String uri = entity.getIRI().toString(); p = dfac.getClassPredicate(uri); } else if (entity instanceof OWLObjectProperty) { String uri = entity.getIRI().toString(); p = dfac.getObjectPropertyPredicate(uri); } else if (entity instanceof OWLDataProperty) { String uri = entity.getIRI().toString(); p = dfac.getDataPropertyPredicate(uri); } return p; }
/** * This method makes sure is used to setup a new/fresh OBDA model. This is done by replacing the * instance this.obdacontroller (the OBDA model) with a new object. On creation listeners for the * datasources, mappings and queries are setup so that changes in these trigger and ontology * change. * * <p>Additionally, this method configures all available OBDAOWLReasonerFacotry objects to have a * reference to the newly created OBDA model and to the global preference object. This is * necessary so that the factories are able to pass the OBDA model to the reasoner instances when * they are created. */ private void setupNewOBDAModel() { OBDAModel activeOBDAModel = getActiveOBDAModel(); if (activeOBDAModel != null) { return; } activeOBDAModel = dfac.getOBDAModel(); activeOBDAModel.addSourcesListener(dlistener); activeOBDAModel.addMappingsListener(mlistener); queryController.addListener(qlistener); OWLModelManager mmgr = owlEditorKit.getOWLWorkspace().getOWLModelManager(); Set<OWLOntology> ontologies = mmgr.getOntologies(); for (OWLOntology ontology : ontologies) { // Setup the entity declarations for (OWLClass c : ontology.getClassesInSignature()) { OClass pred = ofac.createClass(c.getIRI().toString()); activeOBDAModel.declareClass(pred); } for (OWLObjectProperty r : ontology.getObjectPropertiesInSignature()) { ObjectPropertyExpression pred = ofac.createObjectProperty(r.getIRI().toString()); activeOBDAModel.declareObjectProperty(pred); } for (OWLDataProperty p : ontology.getDataPropertiesInSignature()) { DataPropertyExpression pred = ofac.createDataProperty(p.getIRI().toString()); activeOBDAModel.declareDataProperty(pred); } } // Setup the prefixes PrefixOWLOntologyFormat prefixManager = PrefixUtilities.getPrefixOWLOntologyFormat(mmgr.getActiveOntology()); // addOBDACommonPrefixes(prefixManager); PrefixManagerWrapper prefixwrapper = new PrefixManagerWrapper(prefixManager); activeOBDAModel.setPrefixManager(prefixwrapper); OWLOntology activeOntology = mmgr.getActiveOntology(); String defaultPrefix = prefixManager.getDefaultPrefix(); if (defaultPrefix == null) { OWLOntologyID ontologyID = activeOntology.getOntologyID(); defaultPrefix = ontologyID.getOntologyIRI().toURI().toString(); } activeOBDAModel.getPrefixManager().addPrefix(PrefixManager.DEFAULT_PREFIX, defaultPrefix); // Add the model URI modelUri = activeOntology.getOntologyID().getOntologyIRI().toURI(); obdamodels.put(modelUri, activeOBDAModel); }
private void setupReasoner(File owlFile, File obdaFile) throws Exception { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology ontology = manager.loadOntologyFromOntologyDocument(owlFile); OBDAModel obdaModel = ofac.getOBDAModel(); ModelIOManager ioManager = new ModelIOManager(obdaModel); ioManager.load(obdaFile); QuestOWLFactory factory = new QuestOWLFactory(); factory.setOBDAController(obdaModel); factory.setPreferenceHolder(prefs); reasoner = (QuestOWL) factory.createReasoner(ontology, new SimpleConfiguration()); }
/** * Return a valueConstant or Variable constructed from the given expression * * @param pred the expression to process * @param lookupTable in case of variable * @return constructed valueconstant or variable */ private Term getValueConstant(Expression pred, LookupTable lookupTable) { String termRightName = ""; if (pred instanceof Column) { // if the columns contains a boolean value String columnName = ((Column) pred).getColumnName(); if (columnName.toLowerCase().equals("true") || columnName.toLowerCase().equals("false")) { return dfac.getConstantLiteral(columnName, COL_TYPE.BOOLEAN); } else throw new RuntimeException("Unable to find column name for variable: " + columnName); } else if (pred instanceof StringValue) { termRightName = ((StringValue) pred).getValue(); return dfac.getConstantLiteral(termRightName, COL_TYPE.STRING); } else if (pred instanceof DateValue) { termRightName = ((DateValue) pred).getValue().toString(); return dfac.getConstantLiteral(termRightName, COL_TYPE.DATETIME); } else if (pred instanceof TimeValue) { termRightName = ((TimeValue) pred).getValue().toString(); return dfac.getConstantLiteral(termRightName, COL_TYPE.DATETIME); } else if (pred instanceof TimestampValue) { termRightName = ((TimestampValue) pred).getValue().toString(); return dfac.getConstantLiteral(termRightName, COL_TYPE.DATETIME); } else if (pred instanceof LongValue) { termRightName = ((LongValue) pred).getStringValue(); return dfac.getConstantLiteral(termRightName, COL_TYPE.INTEGER); } else if (pred instanceof DoubleValue) { termRightName = ((DoubleValue) pred).toString(); return dfac.getConstantLiteral(termRightName, COL_TYPE.DOUBLE); } else { termRightName = pred.toString(); return dfac.getConstantLiteral(termRightName, COL_TYPE.LITERAL); } }
public DatalogProgram constructDatalogProgram() { DatalogProgram datalog = dfac.getDatalogProgram(); LinkedList<String> errorMessage = new LinkedList<String>(); for (OBDAMappingAxiom axiom : mappingList) { try { // Obtain the target and source query from each mapping axiom in // the model. CQIE targetQuery = (CQIE) axiom.getTargetQuery(); // Get the parsed sql, since it is already parsed by the mapping // parser // consider also MetaMappingExpander // VisitedQuery queryParsed = ...; OBDASQLQuery sourceQuery = (OBDASQLQuery) axiom.getSourceQuery(); // Construct the SQL query tree from the source query VisitedQuery queryParsed = translator.constructParser(sourceQuery.toString()); // Create a lookup table for variable swapping LookupTable lookupTable = createLookupTable(queryParsed); // We can get easily the table from the SQL ArrayList<RelationJSQL> tableList = queryParsed.getTableSet(); // Construct the body from the source query ArrayList<Function> atoms = new ArrayList<Function>(); for (RelationJSQL table : tableList) { // Construct the URI from the table name String tableName = table.getGivenName(); String predicateName = tableName; // Construct the predicate using the table name int arity = dbMetaData.getDefinition(tableName).countAttribute(); Predicate predicate = dfac.getPredicate(predicateName, arity); // Swap the column name with a new variable from the lookup // table List<Term> terms = new ArrayList<Term>(); for (int i = 1; i <= arity; i++) { String columnName = dbMetaData.getFullQualifiedAttributeName(tableName, table.getAlias(), i); String termName = lookupTable.lookup(columnName); if (termName == null) { throw new RuntimeException( "Column '" + columnName + "'was not found in the lookup table: "); } Term term = dfac.getVariable(termName); terms.add(term); } // Create an atom for a particular table Function atom = dfac.getFunction(predicate, terms); atoms.add(atom); } // For the join conditions WE STILL NEED TO CONSIDER NOT EQUI // JOIN ArrayList<Expression> joinConditions = queryParsed.getJoinCondition(); for (Expression predicate : joinConditions) { Function atom = getFunction(predicate, lookupTable); atoms.add(atom); } // For the selection "where" clause conditions SelectionJSQL selection = queryParsed.getSelection(); if (selection != null) { // Stack for filter function Stack<Function> filterFunctionStack = new Stack<Function>(); Expression conditions = selection.getRawConditions(); Function filterFunction = getFunction(conditions, lookupTable); filterFunctionStack.push(filterFunction); // The filter function stack must have 1 element left if (filterFunctionStack.size() == 1) { Function filterFunct = filterFunctionStack.pop(); Function atom = dfac.getFunction(filterFunct.getFunctionSymbol(), filterFunct.getTerms()); atoms.add(atom); } else { throwInvalidFilterExpressionException(filterFunctionStack); } } // Construct the head from the target query. List<Function> atomList = targetQuery.getBody(); // for (Function atom : atomList) { Iterator<Function> atomListIter = atomList.iterator(); while (atomListIter.hasNext()) { Function atom = atomListIter.next(); List<Term> terms = atom.getTerms(); List<Term> newterms = new LinkedList<Term>(); for (Term term : terms) { newterms.add(updateTerm(term, lookupTable)); } Function newhead = dfac.getFunction(atom.getPredicate(), newterms); CQIE rule = dfac.getCQIE(newhead, atoms); datalog.appendRule(rule); } } catch (Exception e) { errorMessage.add( "Error in mapping with id: " + axiom.getId() + " \n Description: " + e.getMessage() + " \nMapping: [" + axiom.toString() + "]"); } } if (errorMessage.size() > 0) { StringBuilder errors = new StringBuilder(); for (String error : errorMessage) { errors.append(error + "\n"); } final String msg = "There was an error analyzing the following mappings. Please correct the issue(s) to continue.\n" + errors.toString(); RuntimeException r = new RuntimeException(msg); throw r; } return datalog; }
/** * Recursive methods to create a {@link Function} starting from a {@link BinaryExpression} We * consider all possible values of the left and right expressions * * @param pred * @param lookupTable * @return */ private Function getFunction(BinaryExpression pred, LookupTable lookupTable) { Expression left = pred.getLeftExpression(); Expression right = pred.getRightExpression(); // left term can be function or column variable Term t1 = null; t1 = getFunction(left, lookupTable); if (t1 == null) { t1 = getVariable(left, lookupTable); } if (t1 == null) throw new RuntimeException("Unable to find column name for variable: " + left); // right term can be function, column variable or data value Term t2 = null; t2 = getFunction(right, lookupTable); if (t2 == null) { t2 = getVariable(right, lookupTable); } if (t2 == null) { t2 = getValueConstant(right, lookupTable); } // get boolean operation String op = pred.getStringExpression(); Function funct = null; if (op.equals("=")) funct = dfac.getFunctionEQ(t1, t2); else if (op.equals(">")) funct = dfac.getFunctionGT(t1, t2); else if (op.equals("<")) funct = dfac.getFunctionLT(t1, t2); else if (op.equals(">=")) funct = dfac.getFunctionGTE(t1, t2); else if (op.equals("<=")) funct = dfac.getFunctionLTE(t1, t2); else if (op.equals("<>")) funct = dfac.getFunctionNEQ(t1, t2); else if (op.equals("AND")) funct = dfac.getFunctionAND(t1, t2); else if (op.equals("OR")) funct = dfac.getFunctionOR(t1, t2); else if (op.equals("+")) funct = dfac.getFunctionAdd(t1, t2); else if (op.equals("-")) funct = dfac.getFunctionSubstract(t1, t2); else if (op.equals("*")) funct = dfac.getFunctionMultiply(t1, t2); else if (op.equals("LIKE")) funct = dfac.getFunctionLike(t1, t2); else throw new RuntimeException("Unknown opertor: " + op); return funct; }
public void setUp() throws Exception { pm = new SimplePrefixManager(); OBDADataFactory pfac = OBDADataFactoryImpl.getInstance(); OBDADataFactory tfac = OBDADataFactoryImpl.getInstance(); query = tfac.getDatalogProgram(); LinkedList<Term> innerterms = new LinkedList<Term>(); innerterms.add(tfac.getVariable("id")); // IRIFactory fact = new IRIFactory(); List<Term> terms = new LinkedList<Term>(); terms.add( tfac.getFunction( pfac.getPredicate("http://obda.org/onto.owl#person-individual", 1), innerterms)); Function body = tfac.getFunction(pfac.getClassPredicate("http://obda.org/onto.owl#Person"), terms); terms = new LinkedList<Term>(); terms.add(tfac.getVariable("id")); Function head = tfac.getFunction(pfac.getPredicate("http://obda.org/predicates#q", 1), terms); rule1 = tfac.getCQIE(head, Collections.singletonList(body)); query.appendRule(rule1); }