Package edu.neu.ccs.task.rdf

Source Code of edu.neu.ccs.task.rdf.TaskModelSetWithRdf

package edu.neu.ccs.task.rdf;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLStreamException;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.shared.PrefixMapping;

import edu.neu.ccs.task.TaskModel;
import edu.neu.ccs.task.TaskModelSet;
import edu.neu.ccs.task.TaskModelXmlReader;
import edu.neu.ccs.task.util.XMLUtil;

public class TaskModelSetWithRdf extends TaskModelSet {
  private final Model rdf = ModelFactory.createDefaultModel();
  private String filterQuery;

  public TaskModelSetWithRdf() {
    rdf.setNsPrefixes(PrefixMapping.Standard);
  }

  @Override
  protected TaskModel load(TaskModelXmlReader r, String prefix)
      throws XMLStreamException {
    TaskModel model = super.load(r, prefix);
   
    prefix = model.getPrefix();
    String uri = model.getURI() + "#";
    rdf.setNsPrefix(prefix, uri);
   
    return model;
  }

  @Override
  public void unload(String modelURI) {
    TaskModel model = getModelForURI(modelURI);
    if (model != null)
      rdf.removeNsPrefix(model.getPrefix());
    super.unload(modelURI);
  }

  public Model getRdfModel() {
    return rdf;
  }
 
  public void loadRdf(String location, String language) throws IOException {
    if (XMLUtil.isURI(location))
      rdf.read(location, "", language);
    else {
      InputStream in = new FileInputStream(location);
      try {
        rdf.read(in, "", language);
      } finally {
        in.close();
      }
    }
    clearIndices();
  }
 
  public void loadRdf(webframe.Resource r, String language) throws IOException {
    InputStream in = r.open();
    try {
      rdf.read(in, "", language);
    } finally {
      in.close();
    }
    clearIndices();
  }

  public String getFilterQuery() {
    return filterQuery;
  }

  public void setFilterQuery(String query) {
    this.filterQuery = query;
    clearIndices();
  }

  @Override
  protected Iterable<TaskModel> getActiveModels() {
    if (filterQuery == null)
      return super.getActiveModels();

    Query query = QueryFactory.create(filterQuery);
    if (query.getResultVars().size() != 1)
      throw new IllegalArgumentException("bad query");
    String var = query.getResultVars().get(0);
    QueryExecution qexec = QueryExecutionFactory.create(query, rdf);

    try {
      List<TaskModel> models = new ArrayList<TaskModel>();
      ResultSet rs = qexec.execSelect();
      while (rs.hasNext()) {
        String uri = rs.next().getResource(var).getURI();
        TaskModel model = getModelForURI(uri);
        if (model != null)
          models.add(model);
      }

      return models;
    } finally {
      qexec.close();
    }
  }
}
TOP

Related Classes of edu.neu.ccs.task.rdf.TaskModelSetWithRdf

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.