Package edu.neu.ccs.task.rdf

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

package edu.neu.ccs.task.rdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;

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.TaskEngine;
import edu.neu.ccs.task.TaskModelSet;

public class TaskEngineWithRdf extends TaskEngine {
  private final long startTime;
  private final Model runtimeRdf, rdf;
  private File runtimeRdfFile;
 
  public TaskEngineWithRdf(TaskModelSetWithRdf models) {
    super(models);
    this.startTime = System.currentTimeMillis();
    runtimeRdf = ModelFactory.createDefaultModel();
    runtimeRdf.setNsPrefixes(PrefixMapping.Standard);
   
    Model staticRdf = ((TaskModelSetWithRdf) models).getRdfModel();
    runtimeRdf.setNsPrefixes(staticRdf);
    rdf = ModelFactory.createUnion(runtimeRdf, staticRdf);
    rdf.setNsPrefixes(staticRdf);

    RdfInterface.setup(this);
  }

  @Override
  public TaskModelSetWithRdf getModelSet() {
    return (TaskModelSetWithRdf) super.getModelSet();
  }
 
  public long getStartTime() {
    return startTime;
  }
 
  public Model getRdfModel() {
    return rdf;
  }
 
  public Model getRuntimeRdfModel() {
    return runtimeRdf;
  }

  public Model getStaticRdfModel() {
    TaskModelSet ms = getModelSet();
    if (ms instanceof TaskModelSetWithRdf)
      return ((TaskModelSetWithRdf) ms).getRdfModel();
    return null;
  }
 
  public void updateRdfPrefixMappings() {
    Model staticRdf = getStaticRdfModel();
    if (staticRdf != null) {
      runtimeRdf.setNsPrefixes(staticRdf);
      rdf.setNsPrefixes(staticRdf);
    }
  }
 
  public void loadRuntimeRdfModel(String file) throws IOException {
    runtimeRdf.removeAll();
   
    runtimeRdfFile = new File(file);
    InputStream in = new FileInputStream(runtimeRdfFile);
    try {
      runtimeRdf.read(in, "", "TURTLE");
    } finally {
      in.close();
    }
  }
 
  public void saveRuntimeRdfModel() throws IOException {
    if (runtimeRdfFile != null)
      saveRuntimeRdfModelTo(runtimeRdfFile);
  }
 
  public void saveRuntimeRdfModelBackup(int index) throws IOException {
    if (runtimeRdfFile != null) {
      String name = runtimeRdfFile.getName();
      int dot = name.lastIndexOf('.');
      if (dot == -1)
        dot = name.length();
     
      name = name.substring(0, dot) + "." + index + name.substring(dot);
      File file = new File(runtimeRdfFile.getParentFile(), name);
     
      saveRuntimeRdfModelTo(file);
    }
  }
 
  public void saveRuntimeRdfModelTo(File file) throws IOException {
    OutputStream out = new FileOutputStream(file);
    try {
      runtimeRdf.write(out, "TURTLE");
      out.flush();
    } finally {
      out.close();
    }
  }

  @Override
  public String dumpUserModel() {
    StringWriter sw = new StringWriter();
    runtimeRdf.write(sw, "TURTLE");
    return sw.toString();
  }
}
TOP

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

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.