Search in sources :

Example 6 with RuleAdapter

use of org.apache.stanbol.rules.base.api.RuleAdapter in project stanbol by apache.

the class RuleAdaptersFactoryImpl method serviceChanged.

@Override
public void serviceChanged(ServiceEvent event) {
    ServiceReference serviceReference = event.getServiceReference();
    Object service = componentContext.getBundleContext().getService(serviceReference);
    if (service instanceof RuleAdapter) {
        RuleAdapter RuleAdapter = (RuleAdapter) componentContext.getBundleContext().getService(serviceReference);
        switch(event.getType()) {
            case ServiceEvent.MODIFIED:
                try {
                    removeRuleAdapter(RuleAdapter);
                    log.info("Removed Rule Adapter " + RuleAdapter.getClass().getCanonicalName());
                    addRuleAdapter(RuleAdapter);
                    log.info("Added Rule Adapter " + RuleAdapter.getClass().getCanonicalName());
                } catch (UnavailableRuleObjectException e) {
                    log.error("Unavailable Rule Object " + e.getMessage());
                }
                break;
            case ServiceEvent.REGISTERED:
                try {
                    addRuleAdapter(RuleAdapter);
                    log.info("Added Rule Adapter " + RuleAdapter.getClass().getCanonicalName());
                } catch (UnavailableRuleObjectException e) {
                    log.error("Unavailable Rule Object " + e.getMessage());
                }
                break;
            case ServiceEvent.UNREGISTERING:
                try {
                    removeRuleAdapter(RuleAdapter);
                    log.info("Removed Rule Adapter " + RuleAdapter.getClass().getCanonicalName());
                } catch (UnavailableRuleObjectException e) {
                    log.error("Unavailable Rule Object " + e.getMessage());
                }
                break;
            default:
                break;
        }
        log.info(ruleAdapters.entrySet().size() + " active rule adapters ");
    }
}
Also used : UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) ServiceReference(org.osgi.framework.ServiceReference) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter)

Example 7 with RuleAdapter

use of org.apache.stanbol.rules.base.api.RuleAdapter in project stanbol by apache.

the class RulesResource method listAdaptersService.

/**
     * It returns the list of available {@link RuleAdapter} instances.
     * 
     * @param headers
     *            {@link HttpHeaders}
     * @return <ul>
     *         <li>A JSON array containing available adapters.<br/>
     *         Each element of the array is an object composed by the following fields:
     *         <ul>
     *         <li>adapter: the canonical name of the adapter class;</li>
     *         <li>adapter: the canonical name of the instances' class that the adapter provide as output.</li>
     *         </ul>
     *         </li>
     *         <li>404: No adapter exists</li>
     *         </ul>
     */
@GET
@Produces(value = { KRFormat.RDF_JSON })
@Path("/adapters")
public Response listAdaptersService(@Context HttpHeaders headers) {
    ResponseBuilder responseBuilder = null;
    List<RuleAdapter> adapters = getListAdapters();
    if (adapters != null && !adapters.isEmpty()) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (RuleAdapter adapter : adapters) {
            JSONObject jsonAdapter = new JSONObject();
            try {
                jsonAdapter.put("adapter", adapter.getClass().getCanonicalName());
                jsonAdapter.put("adaptTo", adapter.getExportClass().getCanonicalName());
            } catch (JSONException e) {
                log.error(e.getMessage(), e);
            }
            jsonArray.put(jsonAdapter);
        }
        try {
            jsonObject.put("adapters", jsonArray);
        } catch (JSONException e) {
            log.error(e.getMessage(), e);
        }
        responseBuilder = Response.ok(jsonObject.toString());
    } else {
        responseBuilder = Response.status(Status.NOT_FOUND);
    }
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 8 with RuleAdapter

use of org.apache.stanbol.rules.base.api.RuleAdapter in project stanbol by apache.

the class ClerezzaAdapter method main.

public static void main(String[] args) {
    RuleAdapter ruleAdapter = new ClerezzaAdapter();
    try {
        KB kb = RuleParserImpl.parse("http://sssw.org/2012/rules/", new FileInputStream("/Users/mac/Documents/CNR/SSSW2012/construct/exercise3"));
        System.out.println("Rules: " + kb.getRuleList().size());
        Recipe recipe = new RecipeImpl(new IRI("http://sssw.org/2012/rules/"), "Recipe", kb.getRuleList());
        //List<ConstructQuery> jenaRules = (List<ConstructQuery>) ruleAdapter.adaptTo(recipe, ConstructQuery.class);
        String rules = "[ Exercise1: (http://dbpedia.org/resource/Madrid http://dbpedia.org/ontology/locationOf ?location) (?location rdf:type http://dbpedia.org/ontology/Museum) (?location http://dbpedia.org/ontology/numberOfVisitors ?visitors) greaterThan(?visitors '2000000'^^http://www.w3.org/2001/XMLSchema#integer) -> (?location rdf:type http://www.mytravels.com/Itinerary/MadridItinerary) ]";
        //List<com.hp.hpl.jena.reasoner.rulesys.Rule> jenaRules = com.hp.hpl.jena.reasoner.rulesys.Rule.parseRules(rules);
        String spqral = "CONSTRUCT " + "{ ?city a <http://www.mytravels.com/Itinerary/MovieCityItinerary> . " + "   ?city <http://www.w3.org/2000/01/rdf-schema#label> ?cLabel . " + "   ?event a <http://linkedevents.org/ontology/Event> . " + "   ?event <http://linkedevents.org/ontology/atPlace> ?location . " + "   ?location <http://www.w3.org/2000/01/rdf-schema#label> ?lLabel . " + "   ?location <http://www.w3.org/2002/07/owl#sameAs> ?city" + "} " + "WHERE " + "{ " + "   ?city a <http://www.mytravels.com/Itinerary/MovieCityItinerary> . " + "   ?city <http://www.w3.org/2000/01/rdf-schema#label> ?cLabel . " + "   ?event a <http://linkedevents.org/ontology/Event> . " + "   ?event <http://linkedevents.org/ontology/atPlace> ?location . " + "   ?location <http://www.w3.org/2000/01/rdf-schema#label> ?lLabel . " + "   FILTER(?lLabel = ?cLabel) " + "}";
        Model m = ModelFactory.createDefaultModel();
        Model model = FileManager.get().loadModel("/Users/mac/Documents/CNR/SSSW2012/datasets_new/Exercise5_tmp.rdf");
        //for(ConstructQuery constructQuery : jenaRules){
        //Query query = QueryFactory.create(constructQuery.toString(), Syntax.syntaxARQ);
        Query query = QueryFactory.create(spqral, Syntax.syntaxARQ);
        QueryExecution queryExecution = QueryExecutionFactory.create(query, model);
        //System.out.println(constructQuery.toString());
        m.add(queryExecution.execConstruct());
        //}
        FileOutputStream max = new FileOutputStream("/Users/mac/Documents/CNR/SSSW2012/datasets_new/example5.rdf");
        m.write(max);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
/*catch (RuleAtomCallExeption e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnavailableRuleObjectException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedTypeForExportException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Query(com.hp.hpl.jena.query.Query) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) SimpleConstructQuery(org.apache.clerezza.rdf.core.sparql.query.impl.SimpleConstructQuery) Recipe(org.apache.stanbol.rules.base.api.Recipe) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) QueryExecution(com.hp.hpl.jena.query.QueryExecution) KB(org.apache.stanbol.rules.manager.KB) RecipeImpl(org.apache.stanbol.rules.manager.RecipeImpl) FileOutputStream(java.io.FileOutputStream) Model(com.hp.hpl.jena.rdf.model.Model) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter) AbstractRuleAdapter(org.apache.stanbol.rules.adapters.AbstractRuleAdapter)

Aggregations

RuleAdapter (org.apache.stanbol.rules.base.api.RuleAdapter)8 UnavailableRuleObjectException (org.apache.stanbol.rules.base.api.UnavailableRuleObjectException)6 Recipe (org.apache.stanbol.rules.base.api.Recipe)5 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)5 UnsupportedTypeForExportException (org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException)5 IRI (org.apache.clerezza.commons.rdf.IRI)4 List (java.util.List)3 ConstructQuery (org.apache.clerezza.rdf.core.sparql.query.ConstructQuery)3 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)3 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)3 Query (com.hp.hpl.jena.query.Query)2 QueryExecution (com.hp.hpl.jena.query.QueryExecution)2 Model (com.hp.hpl.jena.rdf.model.Model)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 Graph (org.apache.clerezza.commons.rdf.Graph)2