Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.stanbol.rules.adapters.sparql;
 
 import java.util.List;
 
The SPARQL adapter.
This class implements the interface org.apache.stanbol.rules.base.api.RuleAdapter and allows to adapt Stanbol recipes to object of the org.apache.stanbol.rules.base.api.SPARQLObject. This class returns the string serialization of the SPARQL query throw the methos org.apache.stanbol.rules.base.api.SPARQLObject.getObject().

Author(s):
anuzzolese
 
 
 @Component(immediate = true, metatype = true)
 public class SPARQLAdapter extends AbstractRuleAdapter {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     private final String ARTIFACT = "org.apache.stanbol.rules.adapters.sparql.atoms";
 
     @Reference
 
     @Reference
     
 
     @SuppressWarnings("unchecked")
     protected <T> T adaptRecipeTo(Recipe recipeClass<T> typethrows UnsupportedTypeForExportException,
                                                                UnavailableRuleObjectException {
 
         List<SPARQLObjectsparqlObjects = null;
 
         if (type == SPARQLObject.class) {
 
             RuleList ruleList = recipe.getRuleList();
             Iterator<RuleruleIterator = ruleList.iterator();
 
             sparqlObjects = new ArrayList<SPARQLObject>();
             for (int i = 0; ruleIterator.hasNext(); i++) {
                 sparqlObjects.add((SPARQLObjectadaptRuleTo(ruleIterator.next(), type));
             }
 
         } else {
             throw new UnsupportedTypeForExportException(
                     "The SPARQL Export Provider does not support the selected serialization : "
                             + type.getCanonicalName());
         }
 
         return (T) sparqlObjects;
     }
 
    /*
     * public <T> T exportRecipe(String recipeID, Class<T> type) throws UnsupportedTypeForExportException,
     * UnavailableRuleObjectException, NoSuchRecipeException {
     * 
     * 
     * Recipe recipe; try { recipe = ruleStore.getRecipe(IRI.create(recipeID));
     * 
     * return exportRecipe(recipe, type); } catch (NoSuchRecipeException e) { throw e; }
     * 
     * 
     * }
     */
    @SuppressWarnings("unchecked")
    protected <T> T adaptRuleTo(Rule ruleClass<T> typethrows UnsupportedTypeForExportException,
                                                         UnavailableRuleObjectException {
        String sparql = "CONSTRUCT {";
        boolean firstIte = true;
        for (RuleAtom ruleAtom : rule.getHead()) {
            if (!firstIte) {
                sparql += " . ";
            }
            firstIte = false;
            sparql += ((SPARQLObjectadaptRuleAtomTo(ruleAtomtype)).getObject();
        }
        sparql += "} ";
        sparql += "WHERE {";
        firstIte = true;
        ArrayList<SPARQLObjectsparqlObjects = new ArrayList<SPARQLObject>();
        for (RuleAtom ruleAtom : rule.getBody()) {
            SPARQLObject tmp = ((SPARQLObjectadaptRuleAtomTo(ruleAtomtype));
            if (tmp instanceof SPARQLNot) {
                sparqlObjects.add((SPARQLNottmp);
            } else if (tmp instanceof SPARQLComparison) {
                sparqlObjects.add((SPARQLComparisontmp);
            } else {
                if (!firstIte) {
                    sparql += " . ";
                } else {
                    firstIte = false;
                }
                sparql += tmp.getObject();
            }
        }
        firstIte = true;
        String optional = "";
        String filter = "";
        for (SPARQLObject sparqlObj : sparqlObjects) {
            if (sparqlObj instanceof SPARQLNot) {
                SPARQLNot sparqlNot = (SPARQLNotsparqlObj;
                if (!firstIte) {
                    optional += " . ";
                } else {
                    firstIte = false;
                }
                optional += sparqlNot.getObject();
                String[] filters = sparqlNot.getFilters();
                for (String theFilter : filters) {
                    if (!filter.isEmpty()) {
                        filter += " && ";
                    }
                    filter += theFilter;
                }
            } else if (sparqlObj instanceof SPARQLComparison) {
                SPARQLComparison sparqlDifferent = (SPARQLComparisonsparqlObj;
                String theFilter = sparqlDifferent.getObject();
                if (!filter.isEmpty()) {
                    filter += " && ";
                }
                filter += theFilter;
            }
        }
        if (!optional.isEmpty()) {
            sparql += " . OPTIONAL { " + optional + " } ";
        }
        if (!filter.isEmpty()) {
            sparql += " . FILTER ( " + filter + " ) ";
        }
        sparql += "}";
        return (T) new SPARQLQuery(sparql);
    }
    @SuppressWarnings("unchecked")
    protected <T> T adaptRuleAtomTo(RuleAtom ruleAtomClass<T> typethrows UnsupportedTypeForExportException,
                                                                     UnavailableRuleObjectException {
        if (type == SPARQLObject.class) {
            String className = ruleAtom.getClass().getSimpleName();
            String canonicalName =  + "." + className;
            try {
                
                Class<AdaptableAtomsparqlAtomClass = (Class<AdaptableAtom>) Class.forName(canonicalName);
                
                try {
                    AdaptableAtom sparqlAtom = sparqlAtomClass.newInstance();
                    sparqlAtom.setRuleAdapter(this);
                    return (T) sparqlAtom.adapt(ruleAtom);
                } catch (InstantiationException e) {
                    .error(e.getMessage(), e);
                } catch (IllegalAccessException e) {
                    .error(e.getMessage(), e);
                } catch (RuleAtomCallExeption e) {
                    .error(e.getMessage(), e);
                }
            } catch (ClassNotFoundException e) {
                .error(e.getMessage(), e);
            } catch (SecurityException e) {
                .error(e.getMessage(), e);
            }
        }
        return null;
    }
    /*
     * @Override public <T> boolean canExportRecipe(Class<T> type) { // TODO Auto-generated method stub return
     * false; }
     */

    
Used to configure an instance within an OSGi container.

    @SuppressWarnings("unchecked")
    @Activate
    protected void activate(ComponentContext contextthrows IOException {
        // context.getBundleContext().addServiceListener((RuleAdaptersFactoryImpl<SPARQLObject>)ruleAdaptersFactory);
        .info("in " + SPARQLAdapter.class + " activate with context " + context);
        if (context == null) {
            throw new IllegalStateException("No valid" + ComponentContext.class + " parsed in activate!");
        }
        
         = context;
        
        activate((Dictionary<String,Object>) context.getProperties());
    }

    
Should be called within both OSGi and non-OSGi environments.

Parameters:
configuration
Throws:
java.io.IOException
    protected void activate(Dictionary<String,Objectconfigurationthrows IOException {
        .info("SPARQL Export Provider for Stanbol Rules  is active"this);
    }
    @Deactivate
    protected void deactivate(ComponentContext context) {
        .info("in " + SPARQLAdapter.class + " deactivate with context " + context);
    }
    @SuppressWarnings("unchecked")
    @Override
    public <T> Class<T> getExportClass() {
        return (Class<T>) SPARQLObject.class;
    }
    @Override
    public <T> boolean canAdaptTo(Adaptable adaptableClass<T> type) {
        if(type == SPARQLObject.class){
            return true;
        }
        else{
            return false;
        }
    }
New to GrepCode? Check out our FAQ X