Package com.hp.hpl.jena.rdf.model

Examples of com.hp.hpl.jena.rdf.model.RDFList


                    List<String> domain =
                            kAMStore.getAnnotationTypeDomainValues(kamInfo,
                                    annotationType);

                    if (hasItems(domain)) {
                        RDFList enumres = model.createList();
                        enumres.setStrict(true);

                        // iterate domain to pre-build RDF resources and associate domain value
                        Resource[] itemResources = new Resource[domain.size()];
                        String[] domainValues =
                                domain.toArray(new String[domain.size()]);
                        for (int i = 0; i < domainValues.length; i++) {
                            itemResources[i] = model
                                    .createResource(new AnonId(UUID
                                            .randomUUID().toString()));
                            itemResources[i].addProperty(RDF.type, RDF.List);
                            itemResources[i].addProperty(RDF.first,
                                    model.createTypedLiteral(domainValues[i]));
                        }

                        // iterate item resources to link them
                        for (int i = 0; i < itemResources.length; i++) {
                            if ((i + 1) < itemResources.length) {
                                // link to next domain value
                                itemResources[i].addProperty(RDF.rest,
                                        itemResources[i + 1]);
                            } else {
                                // link to RDF.nil indicating end of list
                                itemResources[i].addProperty(RDF.rest, RDF.nil);
                            }

                            enumres = enumres.with(itemResources[i]);
                        }

                        // associate annotation to the domain list
                        annotationDefResource.addProperty(hasList, enumres);
                    }

                    break;
                case REGULAR_EXPRESSION:
                    // read list domain for internal pattern annotation
                    domain =
                            kAMStore.getAnnotationTypeDomainValues(kamInfo,
                                    annotationType);

                    if (domain.size() != 1) {
                        throw new IllegalStateException(
                                "Expecting a single, Regular Expression pattern, but received: "
                                        + domain);
                    }

                    annotationDefResource =
                            model
                                    .createResource(new AnonId(UUID
                                            .randomUUID().toString()));

                    // associate metadata of annotation type
                    annotationDefResource.addProperty(hasName,
                            model.createTypedLiteral(annotationType.getName()));
                    annotationDefResource.addProperty(hasDescription,
                            model.createTypedLiteral(annotationType
                                    .getDescription()));
                    annotationDefResource.addProperty(hasType, model
                            .createTypedLiteral(annotationType
                                    .getAnnotationDefinitionType()
                                    .getDisplayValue()));
                    annotationDefResource
                            .addProperty(hasUsage, model
                                    .createTypedLiteral(annotationType
                                            .getUsage()));

                    String patternDomain = domain.get(0);

                    // associate annotation to the domain pattern
                    annotationDefResource.addProperty(hasPattern,
                            model.createTypedLiteral(patternDomain));
                    break;
                case URL:
                    if (annotationType.getUrl() == null) {
                        throw new IllegalStateException(
                                "Expecting non-null URL for external annotation");
                    }

                    // associate annotation to the URL where the domain is defined
                    annotationDefResource =
                            model.createResource(annotationType.getUrl());
                    annotationDefResource.addProperty(hasName,
                            model.createTypedLiteral(annotationType.getName()));
                    annotationDefResource.addProperty(hasURL,
                            model.createTypedLiteral(annotationType.getUrl()));
                    break;
                default:
                    throw new UnsupportedOperationException(
                            "Annotation definition type not supported: "
                                    + annotationType
                                            .getAnnotationDefinitionType());
                }

                annotationDefResource.addProperty(RDF.type,
                        AnnotationDefinition);

                // associate document with annotation definition as a defines relationship
                docResource.addProperty(defines, annotationDefResource);

                annotationDefinitions.put(annotationType.getId(),
                        annotationDefResource);
            }

            kamResource.addProperty(composedOf, docResource);
            documents.put(doc.getId(), docResource);
        }

        Map<KamNode, Resource> kamNodeResources =
                new HashMap<Kam.KamNode, Resource>();
        Map<Integer, Resource> termResources = new HashMap<Integer, Resource>();

        // handle kam nodes first
        Iterator<KamNode> kamNodes = kam.getNodes().iterator();
        while (kamNodes.hasNext()) {
            KamNode kamNode = kamNodes.next();

            // TODO: This is a sanity check that should be removed as LIST is not a KAM function type
            if (FunctionEnum.LIST == kamNode.getFunctionType()) {
                System.err.println("Invalid KAM node type found: "
                        + kamNode.getFunctionType().getDisplayValue());
                continue;
            }

            Resource functionResource = model.getResource(KAMVocabulary
                    .resourceForFunction(kamNode.getFunctionType()).getURI());

            // Retrieve seen KAMNode Resource or create a new Blank Node for it.
            Resource kamNodeResource;
            if (kamNodeResources.containsKey(kamNode)) {
                kamNodeResource = kamNodeResources.get(kamNode);
            } else {
                kamNodeResource = model.createResource(new AnonId(UUID
                        .randomUUID().toString()));

                // associate kam node with kam resource
                kamResource.addProperty(composedOf, kamNodeResource);
            }

            // node type KAMNode
            kamNodeResource.addProperty(RDF.type, KAMNode);

            // node hasFunction Function
            kamNodeResource.addProperty(hasFunction, functionResource);

            // node hasId "1"^^xsd:int
            kamNodeResource.addLiteral(hasId,
                    model.createTypedLiteral(kamNode.getId()));

            // node hasLabel "p(EG:207)"^^xsd:string
            kamNodeResource.addLiteral(hasLabel,
                    model.createTypedLiteral(kamNode.getLabel()));

            // hold on to kam node resources
            kamNodeResources.put(kamNode, kamNodeResource);

            // handle terms for this KAMNode
            // TODO Support nested terms instead of just Parameters!
            List<BelTerm> terms = kAMStore.getSupportingTerms(kamNode);
            for (BelTerm term : terms) {
                Resource termResource = model.createResource(new AnonId(UUID
                        .randomUUID().toString()));

                termResource.addProperty(RDF.type, Term);
                termResource.addProperty(hasId,
                        model.createTypedLiteral(term.getId()));
                termResource.addProperty(hasLabel,
                        model.createTypedLiteral(term.getLabel()));
                termResource.addProperty(hasFunction,
                        resourceForFunction(kamNode.getFunctionType()));

                RDFList argumentres = model.createList();

                List<TermParameter> termParameters =
                        kAMStore.getTermParameters(kamInfo, term);
                TermParameter[] tparray = termParameters
                        .toArray(new TermParameter[termParameters.size()]);
                Resource[] tpresarray = new Resource[tparray.length];

                // iterate term parameters to pre-build RDF resources and associate Parameter
                for (int i = 0; i < tparray.length; i++) {
                    tpresarray[i] = model.createResource(new AnonId(UUID
                            .randomUUID().toString()));
                    tpresarray[i].addProperty(RDF.type, RDF.List);

                    Resource parameterResource =
                            model
                                    .createResource(new AnonId(UUID
                                            .randomUUID().toString()));
                    parameterResource.addProperty(RDF.type, Parameter);
                    parameterResource
                            .addProperty(hasValue, model
                                    .createTypedLiteral(tparray[i]
                                            .getParameterValue()));

                    if (tparray[i].getNamespace() != null) {
                        parameterResource.addProperty(hasNamespace, model
                                .getResource(tparray[i].getNamespace()
                                        .getResourceLocation()));
                    }

                    tpresarray[i].addProperty(RDF.first, parameterResource);
                }

                // iterate argument resources to link them
                for (int i = 0; i < tpresarray.length; i++) {
                    if ((i + 1) < tpresarray.length) {
                        // link to next term argument
                        tpresarray[i].addProperty(RDF.rest, tpresarray[i + 1]);
                    } else {
                        // link to RDF.nil indicating end of list
                        tpresarray[i].addProperty(RDF.rest, RDF.nil);
                    }

                    argumentres = argumentres.with(tpresarray[i]);
                }

                // associate term resource to the arguments list
                termResource.addProperty(hasArguments, argumentres);
View Full Code Here


    Assert.assertNotNull("Root resource should not be null", root);

    final Resource listHead = root.getRequiredProperty(
        m.getProperty(TestList.NS + "p")).getResource();

    final RDFList l = listHead.as(RDFList.class);
    Assert.assertNotNull("as(RDFList) should not return null for root", l);

    return l;
  }
View Full Code Here

    final Resource root = model.createResource(TestList.NS + "root");
    final Property p = model.createProperty(TestList.NS, "p");

    final Resource nil = model.getResource(RDF.nil.getURI());
    RDFList list = nil.as(RDFList.class);

    final Resource[] toAdd = new Resource[] {
        model.createResource(TestList.NS + "a"),
        model.createResource(TestList.NS + "b"),
        model.createResource(TestList.NS + "c"),
        model.createResource(TestList.NS + "d"),
        model.createResource(TestList.NS + "e"), };

    // add each of these resources onto the end of the list
    for (final Resource element : toAdd)
    {
      final RDFList list0 = list.with(element);

      checkValid("addTest0", list0, true);
      Assert.assertTrue("added'ed lists should be equal",
          list.equals(nil) || list0.equals(list));

      list = list0;
    }

    // relate the root to the list
View Full Code Here

  public void testAppend()
  {
    model.read("file:testing/ontology/list5.rdf");

    final Resource nil = model.getResource(RDF.nil.getURI());
    RDFList list = nil.as(RDFList.class);

    final Resource r = model.createResource(TestList.NS + "foo");

    // create a list of foos
    for (int i = 0; i < 5; i++)
    {
      list = list.cons(r);
    }

    final int listLen = list.size();

    // now append foos to the root list
    final RDFList root = getListRoot(model);
    final int rootLen = root.size();

    final RDFList appended = root.append(list);

    // original list should be unchanged
    checkValid("appendTest0", root, true);
    Assert.assertEquals("Original list should be unchanged", rootLen,
        root.size());

    checkValid("appendTest1", list, true);
    Assert.assertEquals("Original list should be unchanged", listLen,
        list.size());

    // new list should be length of combined
    checkValid("appendTest2", appended, true);
    Assert.assertEquals("Appended list not correct length", rootLen
        + listLen, appended.size());
  }
View Full Code Here

  public void testApply()
  {
    model.read("file:testing/ontology/list5.rdf");

    final RDFList root = getListRoot(model);

    class MyApply implements RDFList.ApplyFn
    {
      String collect = "";

      @Override
      public void apply( final RDFNode n )
      {
        collect = collect + ((Resource) n).getLocalName();
      }
    }

    final MyApply f = new MyApply();
    root.apply(f);

    Assert.assertEquals(
        "Result of apply should be concatentation of local names",
        "abcde", f.collect);
View Full Code Here

  public void testConcatenate()
  {
    model.read("file:testing/ontology/list5.rdf");

    final Resource nil = model.getResource(RDF.nil.getURI());
    RDFList list = nil.as(RDFList.class);

    final Resource r = model.createResource(TestList.NS + "foo");

    // create a list of foos
    for (int i = 0; i < 5; i++)
    {
      list = list.cons(r);
    }

    final int listLen = list.size();

    // now append foos to the root list
    final RDFList root = getListRoot(model);
    final int rootLen = root.size();
    root.concatenate(list);

    // original list should be unchanged
    checkValid("concatTest0", list, true);
    Assert.assertEquals("Original list should be unchanged", listLen,
        list.size());

    // but lhs list has changed
    checkValid("concatTest1", root, true);
    Assert.assertEquals("Root list should be new length",
        rootLen + listLen, root.size());
  }
View Full Code Here

        model.createResource(TestList.NS + "b"),
        model.createResource(TestList.NS + "c"),
        model.createResource(TestList.NS + "d"),
        model.createResource(TestList.NS + "e") };

    final RDFList aList = model.createList().cons(a);
    final RDFList rsList = model.createList(rs);

    // concatenate the above resources onto the empty list
    aList.concatenate(rsList);
    checkValid("concatTest3", aList, true);

    final RDFList root = getListRoot(model);
    Assert.assertTrue("Constructed and loaded lists should be the same",
        aList.sameListAs(root));
  }
View Full Code Here

  {
    final Resource root = model.createResource(TestList.NS + "root");
    final Property p = model.createProperty(TestList.NS, "p");

    final Resource nil = model.getResource(RDF.nil.getURI());
    RDFList list = nil.as(RDFList.class);

    final Resource[] toAdd = new Resource[] {
        model.createResource(TestList.NS + "e"),
        model.createResource(TestList.NS + "d"),
        model.createResource(TestList.NS + "c"),
        model.createResource(TestList.NS + "b"),
        model.createResource(TestList.NS + "a"), };

    // cons each of these resources onto the front of the list
    for (final Resource element : toAdd)
    {
      final RDFList list0 = list.cons(element);

      checkValid("constest1", list0, true);
      Assert.assertTrue("cons'ed lists should not be equal",
          !list0.equals(list));

      list = list0;
    }

    // relate the root to the list
View Full Code Here

    for (int i = 0; i <= 5; i++)
    {
      model.removeAll();
      model.read("file:testing/ontology/list" + i + ".rdf");

      final RDFList l0 = getListRoot(model);
      Assert.assertEquals("List size should be " + i, i, l0.size());
    }

  }
View Full Code Here

  public void testHead()
  {
    model.read("file:testing/ontology/list5.rdf");

    RDFList l0 = getListRoot(model);

    final String[] names = { "a", "b", "c", "d", "e" };
    for (final String name : names)
    {
      Assert.assertEquals("head of list has incorrect URI", TestList.NS
          + name, ((Resource) l0.getHead()).getURI());
      l0 = l0.getTail();
    }
  }
View Full Code Here

TOP

Related Classes of com.hp.hpl.jena.rdf.model.RDFList

Copyright © 2018 www.massapicom. 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.