Exemple #1
0
  /** Find the set of resources referenced by the path */
  public static Set<RDFNode> getNodes(Model model, RDFNode first, Path path) {
    List<Step> steps = path.getSteps();

    Set<RDFNode> starts = Collections.singleton(first);

    for (Step step : steps) {
      String propertyName = step.getPropertyName();
      boolean isInverse = step.isInverse();

      Property property = model.createProperty(propertyName);

      Set<RDFNode> nodes = new HashSet<RDFNode>();

      for (RDFNode start : starts) {
        Set<RDFNode> tmp;
        if (!isInverse) {
          tmp = model.listObjectsOfProperty(start.asResource(), property).toSet();
        } else if (start.isResource()) {
          tmp = new HashSet<RDFNode>(model.listSubjectsWithProperty(property, start).toSet());
        } else {
          tmp = Collections.<RDFNode>emptySet();
        }

        nodes.addAll(tmp);
      }
      starts = nodes;
    }

    return starts;
  }
 public static void main(String[] args) {
   Model m = ModelFactory.createDefaultModel();
   String nsA = "http://somewhere/else#";
   String nsB = "http://nowhere/else#";
   Resource root = m.createResource(nsA + "root");
   Property P = m.createProperty(nsA + "P");
   Property Q = m.createProperty(nsB + "Q");
   Resource x = m.createResource(nsA + "x");
   Resource y = m.createResource(nsA + "y");
   Resource z = m.createResource(nsA + "z");
   m.add(root, P, x).add(root, P, y).add(y, Q, z);
   System.out.println("# -- no special prefixes defined");
   m.write(System.out);
   System.out.println("# -- nsA defined");
   m.setNsPrefix("nsA", nsA);
   m.write(System.out);
   System.out.println("# -- nsA and cat defined");
   m.setNsPrefix("cat", nsB);
   m.write(System.out, "TURTLE");
 }
Exemple #3
0
  @Override
  public List<Statement> from() {
    initResource();
    // Container type
    Statement stmtType =
        model.createStatement(
            applicationEntity, RDF.type, model.createResource(baseuri + "/m2m/ApplicationEntity"));
    slist.add(stmtType);

    // Ontology Reference Type
    if (dto.getOr() != null) {
      Statement stmtType2 =
          model.createStatement(
              applicationEntity, RDF.type, model.createResource(ontologyReference));
      slist.add(stmtType2);
    }

    // name
    Statement stmtName =
        model.createStatement(
            applicationEntity, model.createProperty(baseuri + "/m2m/resourceName"), resourceName);
    slist.add(stmtName);

    // parent resource
    Statement stmtParentResource =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/hasParentResource"),
            parentResource);
    slist.add(stmtParentResource);

    // parent isResourceOf
    Statement stmtIsAEOf =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/isApplicationEntityOf"),
            parentResource);
    slist.add(stmtIsAEOf);

    // parent resource
    Statement stmtResourceOf =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/isApplicationEntityOf"),
            parentResource);
    slist.add(stmtResourceOf);

    // label
    Statement stmtLabel = model.createStatement(applicationEntity, RDFS.label, label);
    slist.add(stmtLabel);

    // createtime xsd datetime
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Calendar cal1 = Calendar.getInstance();
    try {
      cal1.setTime(sd.parse(StrUtils.makeXsdDateFromOnem2mDate(this.dto.getCt())));
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    Statement stmtCreateTime =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/hasCreateDate"),
            model.createTypedLiteral(cal1));
    slist.add(stmtCreateTime);

    // last modified time
    Calendar cal2 = Calendar.getInstance();
    try {
      cal2.setTime(sd.parse(StrUtils.makeXsdDateFromOnem2mDate(this.dto.getLt())));
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    Statement stmtLastModifiedTime =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/hasLastModifiedDate"),
            model.createTypedLiteral(cal2));
    slist.add(stmtLastModifiedTime);

    // resourceid
    Statement stmtResourceId =
        model.createStatement(
            applicationEntity, model.createProperty(baseuri + "/m2m/hasResourceId"), resourceId);
    slist.add(stmtResourceId);

    // pointofaccess
    if (dto.getPoa().length != 0) {
      for (int i = 0; i < dto.getPoa().length; i++) {
        slist.add(
            model.createStatement(
                applicationEntity,
                model.createProperty(baseuri + "/m2m/hasPointOfAccess"),
                model.createResource(dto.getPoa()[i])));
      }
    }

    // announce attribute
    if (dto.getAa().length != 0) {
      for (int i = 0; i < dto.getAa().length; i++) {
        slist.add(
            model.createStatement(
                applicationEntity,
                model.createProperty(baseuri + "/m2m/hasAnnounceTo"),
                model.createResource(dto.getAa()[i])));
      }
    }
    // announce to
    if (dto.getAt().length != 0) {
      for (int i = 0; i < dto.getAt().length; i++) {
        slist.add(
            model.createStatement(
                applicationEntity,
                model.createProperty(baseuri + "/m2m/hasAnnounceAttribute"),
                model.createResource(dto.getAt()[i])));
      }
    }

    // appname
    Statement appname =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/resourceName"),
            model.createResource(dto.getApn()));
    slist.add(appname);
    // appid
    Statement appid =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/hasAppID"),
            model.createResource(dto.getApi()));
    slist.add(appid);

    // aeid
    Statement aeid =
        model.createStatement(
            applicationEntity,
            model.createProperty(baseuri + "/m2m/hasApplicationEntityID"),
            model.createResource(dto.getApi()));
    slist.add(aeid);

    slist.add(stmtResourceId);
    return slist;
  }
public abstract class AbstractTestDataset extends BaseTest {
  // Assumes a dadatset which need explicit add graph
  protected abstract Dataset createFixed();

  static Model model1 = ModelFactory.createDefaultModel();
  static Model model2 = ModelFactory.createDefaultModel();

  static Resource s1 = model1.createResource("s1");
  static Resource s2 = model1.createResource("s2");

  static Property p1 = model1.createProperty("p1");
  static Property p2 = model1.createProperty("p2");

  static Resource o1 = model1.createResource("o1");
  static Resource o2 = model1.createResource("o2");

  static {
    model1.add(s1, p1, o1);
    model2.add(s2, p2, o2);
  }

  @Test
  public void dataset_01() {
    Dataset ds = createFixed();
    assertNotNull(ds.getDefaultModel());
    assertNotNull(ds.asDatasetGraph());
  }

  @Test
  public void dataset_02() {
    Dataset ds = createFixed();
    ds.getDefaultModel().add(s1, p1, o1);
    assertTrue(model1.isIsomorphicWith(ds.getDefaultModel()));
  }

  @Test
  public void dataset_03() {
    Dataset ds = createFixed();
    ds.setDefaultModel(model2);
    assertTrue(model2.isIsomorphicWith(ds.getDefaultModel()));
  }

  @Test
  public void dataset_04() {
    String graphName = "http://example/";
    Dataset ds = createFixed();
    ds.addNamedModel(graphName, model1);
    assertTrue(ds.containsNamedModel(graphName));

    List<String> x = Iter.toList(ds.listNames());
    assertEquals(1, x.size());
    assertEquals(graphName, x.get(0));

    assertFalse(model1.isIsomorphicWith(ds.getDefaultModel()));
    Model m = ds.getNamedModel(graphName);

    assertNotNull(m);
    assertTrue(model1.isIsomorphicWith(m));

    ds.removeNamedModel(graphName);
    // Not getNamedModel and test for null as some datasets are "auto graph creating"
    assertFalse(ds.containsNamedModel(graphName));
  }

  @Test
  public void dataset_05() {
    String graphName = "http://example/";
    Dataset ds = createFixed();
    ds.addNamedModel(graphName, model1);
    ds.replaceNamedModel(graphName, model2);
    assertTrue(ds.containsNamedModel(graphName));

    List<String> x = Iter.toList(ds.listNames());
    assertEquals(1, x.size());
    assertEquals(graphName, x.get(0));

    assertFalse(model1.isIsomorphicWith(ds.getNamedModel(graphName)));
    assertTrue(model2.isIsomorphicWith(ds.getNamedModel(graphName)));
  }
}
Exemple #5
0
  public void writeVideosToRDF(String keyword) {

    String api_key = "AIzaSyCZO2nHBNMSGgRg4VHMZ9P8dWT0H23J-Fc";
    String yt_url =
        "https://www.googleapis.com/youtube/v3/search?part=snippet&q="
            + keyword
            + "&type=video&videoCaption=closedCaption&key="
            + api_key
            + "&format=5&maxResults=10&v=2";
    String line = "", stringArray;
    StringBuilder stringArrayBuilder = new StringBuilder();

    String titleOfVideo;
    String description;
    String thumbnailURL;
    String videoId;

    Model model = ModelFactory.createDefaultModel();

    try {
      URL url = new URL(yt_url);
      BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
      while ((line = br.readLine()) != null) {
        stringArrayBuilder = stringArrayBuilder.append(line);
      }
      stringArray = stringArrayBuilder.toString();

      JSONObject nodeRoot = new JSONObject(stringArray);
      JSONArray jsonArray = (JSONArray) nodeRoot.get("items");

      for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject obj = jsonArray.getJSONObject(i);

        JSONObject snippet = (JSONObject) obj.get("snippet");

        description = (String) snippet.get("description");
        titleOfVideo = (String) snippet.get("title");

        JSONObject thumbnails = (JSONObject) snippet.get("thumbnails");
        JSONObject thumbnail = (JSONObject) thumbnails.get("high");
        thumbnailURL = (String) thumbnail.get("url");

        JSONObject id = (JSONObject) obj.get("id");
        videoId = (String) id.get("videoId");

        Resource video = model.createResource("video" + i);
        Property p1 = model.createProperty("title");
        video.addProperty(p1, titleOfVideo);
        Property p2 = model.createProperty("description");
        video.addProperty(p2, description);
        Property p3 = model.createProperty("thumbnail");
        video.addProperty(p3, thumbnailURL);
        Property p4 = model.createProperty("id");
        video.addProperty(p4, videoId);
      }
      FileOutputStream fos = new FileOutputStream(keyword + ".nt");

      RDFDataMgr.write(fos, model, Lang.NTRIPLES);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Exemple #6
0
/** Vocabulary definitions from test-manifest.n3 */
public class TestManifest {
  /** The RDF model that holds the vocabulary terms */
  private static final Model m_model = ModelFactory.createDefaultModel();

  /** The namespace of the vocabulary as a string */
  public static final String NS = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#";

  /**
   * The namespace of the vocabulary as a string
   *
   * @see #NS
   */
  public static String getURI() {
    return NS;
  }

  /** The namespace of the vocabulary as a resource */
  public static final Resource NAMESPACE = m_model.createResource(NS);

  /** The test statusThe expected outcome */
  public static final Property result =
      m_model.createProperty("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#result");

  /** Action to perform */
  public static final Property action =
      m_model.createProperty("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#action");

  /** Optional name of this entry */
  public static final Property name =
      m_model.createProperty("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name");

  /** Connects the manifest resource to rdf:type list of entries */
  public static final Property entries =
      m_model.createProperty("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#entries");

  /** Connects the manifest resource to rdf:type list of manifests */
  public static final Property include =
      m_model.createProperty("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#include");

  /**
   * A type of test specifically for query evaluation testing. Query evaluation tests are required
   * to have an associated input dataset, a query, and an expected output dataset.
   */
  public static final Resource QueryEvaluationTest =
      m_model.createResource(
          "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#QueryEvaluationTest");

  /**
   * A type of test specifically for syntax testing. Syntax tests are not required to have an
   * associated result, only an action. Negative syntax tests are tests of which the result should
   * be a parser error.
   */
  public static final Resource NegativeSyntaxTest =
      m_model.createResource(
          "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#NegativeSyntaxTest");

  /**
   * A type of test specifically for syntax testing. Syntax tests are not required to have an
   * associated result, only an action.
   */
  public static final Resource PositiveSyntaxTest =
      m_model.createResource(
          "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#PositiveSyntaxTest");

  /** One entry in rdf:type list of entries */
  public static final Resource ManifestEntry =
      m_model.createResource(
          "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#ManifestEntry");

  /** The class of manifests */
  public static final Resource Manifest =
      m_model.createResource("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#Manifest");

  public static final Resource accepted =
      m_model.createResource("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#accepted");

  public static final Resource proposed =
      m_model.createResource("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#proposed");

  public static final Resource rejected =
      m_model.createResource("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#rejected");
}
Exemple #7
0
  public void buildModel(Model model) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
      builder = factory.newDocumentBuilder();
    } catch (Exception e) {
      e.printStackTrace();
    }
    Document document = null;
    try {
      document = builder.parse("dblp.xml");
    } catch (SAXException | IOException e) {
      e.printStackTrace();
    }
    document.getDocumentElement().normalize();
    System.out.println("Root Element : " + document.getDocumentElement().getNodeName());
    NodeList nodeList = document.getElementsByTagName("article");
    System.out.println(nodeList.getLength());

    for (int temp = 0; temp < nodeList.getLength(); temp++) {
      Node node = nodeList.item(temp);
      // Identifying the child tag of employee encountered
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        Publication pub = new Publication();

        pub.idkey = node.getAttributes().getNamedItem("key").getNodeValue();
        pub.mdate = node.getAttributes().getNamedItem("mdate").getNodeValue();

        NodeList childNodes = node.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
          Node cNode = childNodes.item(j);
          if (cNode.getNodeType() == Node.ELEMENT_NODE) {
            String content = cNode.getLastChild().getTextContent().trim();

            // replace all ' with space
            if (content.indexOf('\'') != -1) {
              content = content.replace("'", "''");
            }

            switch (cNode.getNodeName()) {
              case "author":
                pub.author.add(content);
                break;
              case "title":
                pub.title = content;
                break;
              case "pages":
                pub.pages = content;
                break;
              case "year":
                pub.year = content;
                break;
              case "volume":
                pub.volume = content;
                break;
              case "journal":
                pub.journal = content;
                break;
              case "number":
                pub.numbers = content;
                break;
              case "url":
                pub.url = content;
              case "ee":
                pub.ee = content;
                break;
            }
          }
        } // end of for and build a complete publication

        Resource resource = model.createResource(NS + pub.title);
        Property key = model.createProperty(NS + "key");
        Property mdate = model.createProperty(NS + "mdate");
        Property author = model.createProperty(NS + "author");
        Property pages = model.createProperty(NS + "pages");
        Property year = model.createProperty(NS + "year");
        Property volume = model.createProperty(NS + "volume");
        Property journal = model.createProperty(NS + "journal");
        Property number = model.createProperty(NS + "number");
        Property url = model.createProperty(NS + "url");
        Property ee = model.createProperty(NS + "ee");

        resource
            .addProperty(key, pub.idkey, XSDDatatype.XSDstring)
            .addProperty(mdate, pub.mdate, XSDDatatype.XSDstring)
            .addProperty(author, pub.authorToString(), XSDDatatype.XSDstring)
            .addProperty(pages, pub.pages, XSDDatatype.XSDstring)
            .addProperty(year, pub.year, XSDDatatype.XSDstring)
            .addProperty(volume, pub.volume, XSDDatatype.XSDstring)
            .addProperty(journal, pub.journal, XSDDatatype.XSDstring)
            .addProperty(number, pub.numbers, XSDDatatype.XSDstring)
            .addProperty(url, pub.url, XSDDatatype.XSDstring)
            .addProperty(ee, pub.ee, XSDDatatype.XSDstring);
      }
    }
  }