Package org.ontoware.rdfreactor.generator.java

Examples of org.ontoware.rdfreactor.generator.java.JClass


       
        JPackage jp = new JPackage(packagename);
        jm.getPackages().add(jp);
       
        // set local ontology root
        JClass localRoot = new JClass(jp, "Thing1", RDFS.Class);
        localRoot
                .setComment("This class acts as a catch-all for all properties, for which no domain has specified.");
        localRoot.addSuperclass(jm.getRoot());
        jm.setRoot(localRoot);
       
        // process
        log.debug("de-anonymizing (replacing bnodes with random uris");
        ModelUtils.deanonymize(m);
       
        // analysis (triggers also inferencing)
        List<? extends Class> rdfclasses = Class.getAllInstance_as(m).asList();
        log.info("Got " + rdfclasses.size() + " rdfs:Classes");
        for(Class c : rdfclasses) {
            log.debug("Found class: " + c.getResource());
        }
        Property[] rdfproperties = Property.getAllInstance_as(m).asArray();
        for(Property p : rdfproperties) {
            log.debug("Found property: " + p.getResource());
        }
        log.info("Got " + rdfproperties.length + " rdfs:Properties");
        log.debug("Found " + m.size() + " statements in schema after inferencing.");
       
        // get all classes and assign to package
        Set<String> usedClassnames = new HashSet<String>();
        usedClassnames.add(jm.getRoot().getName());
        Set<Class> rdfsClasses = new HashSet<Class>();
       
        for(Class rc : Class.getAllInstance_as(m).asList()) {
           
            if(skipbuiltins && jm.hasMapping(rc.getResource())) {
                log.debug("CLASS " + rc + " is known -> skipping generation");
            } else if(!(rc.getResource() instanceof URI)) {
                log.warn("A Class with a blank node ID makes not much sense -> ignored");
            } else {
                rdfsClasses.add(rc);
                // TODO better classname guessing
                String classname = JavaNamingUtils.toBeanName(rc, usedClassnames);
                assert classname != null;
                usedClassnames.add(classname);
               
                log.debug("CLASS " + classname + " generated for " + rc.getResource() + " ...");
                assert rc.getResource() instanceof URI : "A Class with a blank node ID makes not much sense";
                JClass jc = new JClass(jp, classname, (URI)rc.getResource());
                jc.setComment(Utils.toJavaComment(rc.getAllComment_asList())); // might
                                                                               // be
                                                                               // null,
                                                                               // ok.
                jm.addMapping(rc.getResource(), jc);
            }
        }
       
        log.debug(">>>> Inheritance");
        // get all classes and link superclasses
        for(org.ontoware.rdfreactor.schema.bootstrap.Class rc : rdfsClasses) {
            log.debug("rdfs:Class " + rc.getResource());
            JClass jc = jm.getMapping(rc.getResource());
            for(org.ontoware.rdfreactor.schema.bootstrap.Class superclass : rc
                    .getAllSubClassOf_asList())
                jc.addSuperclass(jm.getMapping(superclass.getResource()));
        }
       
        log.info("-------------- PROPERTIES ...");
       
        for(Property rp : Property.getAllInstance_as(m).asList()) {
            log.info("PROPERTY " + rp.getResource());
           
            if(skipbuiltins && jm.knownProperties.contains(rp.getResource().asURI())) {
                // do nothing
                log.debug("Skipping built-in property " + rp.getResource().asURI().toSPARQL());
            } else if(DeprecatedProperty.hasInstance(rp.getModel(), rp.getResource().asURI())) {
                log.info("Skipping deprecated property " + rp
                        + " (as indicated by owl:DeprecatedProperty)");
            } else {
                // inspect domains
                List<Class> domains = rp.getAllDomain_asList();
                // TODO: ignore if already in higher level
                if(domains == null || domains.size() == 0) {
                    log.warn("PROPERTY " + rp.getResource() + " has no domain, using root");
                    handleProperty(m, jm, jm.getRoot(), rp);
                } else {
                    for(Resource domain : domains) {
                        log.info("PROPERTY " + rp.getResource() + " has domain " + domain);
                        JClass domainClass = jm.getMapping(domain.getResource());
                        assert domainClass != null : "found no JClass for "
                                + rp.getAllDomain_asList().get(0).getResource();
                       
                        // domainclass might be a built-in, redirect to root
                        if(Semantics.getbuiltIns_RDFS().containsJClass(domainClass)) {
View Full Code Here


        log.debug("MODEL after inferencing, found " + m.size() + " statements");
        JPackage jp = new JPackage(packagename);
        jm.getPackages().add(jp);
       
        log.info("Creating a class called 'Thing1' for all properties with no given domain");
        JClass localClass = new JClass(jp, "Thing1", RDFS.Class);
        localClass.addSuperclass(jm.getRoot());
        jm.setRoot(localClass);
       
        // get all classes and assign to package
        Set<String> usedClassnames = new HashSet<String>();
        usedClassnames.add(jm.getRoot().getName());
        Set<Class> rdfsClasses = new HashSet<Class>();
       
        for(Class rc : Class.getAllInstance_as(m).asList()) {
           
            if(skipbuiltins && jm.hasMapping(rc.getResource())) {
                log.debug("CLASS " + rc + " is known -> skipping generation");
            } else {
                rdfsClasses.add(rc);
                // TODO better class-name guessing
                String classname = JavaNamingUtils.toBeanName(rc, usedClassnames);
                assert classname != null;
                usedClassnames.add(classname);
               
                log.debug("CLASS " + classname + " generated for " + rc.getResource().toSPARQL()
                        + " ...");
                JClass jc = new JClass(jp, classname, (URI)rc.getResource());
                jc.setComment(rc.getAllComment_asList().get(0)); // might be
                // null, ok.
                jm.addMapping(rc.getResource(), jc);
            }
        }
       
        log.info(">>>> Inheritance");
        // get all classes and link super-classes
        for(org.ontoware.rdfreactor.schema.bootstrap.Class rc : rdfsClasses) {
            log.debug("rdfs:Class " + rc.getResource());
            JClass jc = jm.getMapping(rc.getResource());
            for(org.ontoware.rdfreactor.schema.bootstrap.Class superclass : rc
                    .getAllSubClassOf_asList())
                jc.addSuperclass(jm.getMapping(superclass.getResource()));
        }
       
        log.info(">>>> Flatten inheritance hierarchy");
        jm.flattenInheritanceHierarchy(jp);
       
        // get all properties
        log.info("-------------- PROPERTIES ...");
        for(Property rp : Property.getAllInstance_as(m).asList()) {
            log.debug("PROPERTY " + rp.getResource());
            List<Class> domains = rp.getAllDomain_asList();
            // no domain = no generated property
            if(domains == null || domains.size() == 0) {
                // log.warn("PROPERTY " + rp.getID() + " has no domain, so we
                // ignore it");
                log.debug("PROPERTY " + rp.getResource() + " has no domain, using root");
                handleProperty(m, jm, jm.getRoot(), rp);
            } else {
                for(Resource domain : domains) {
                    JClass domainClass = jm.getMapping(domain.getResource());
                    assert domainClass != null : "found no JClass for "
                            + rp.getAllDomain_asList().get(0).getResource();
                    handleProperty(m, jm, domainClass, rp);
                }
            }
View Full Code Here

        jm.addPackage(jp);
       
        /**
         * local ontology root
         */
        JClass localRoot = new JClass(jp, "Thing1", OWL.Class);
        localRoot
                .setComment("This class acts as a catch-all for all properties, for which no domain has specified.");
        localRoot.addSuperclass(jm.getRoot());
        jm.setRoot(localRoot);
       
        // get all classes and assign to package
        Set<org.ontoware.rdfreactor.schema.bootstrap.OwlClass> owlClasses = new HashSet<org.ontoware.rdfreactor.schema.bootstrap.OwlClass>();
        Set<String> usedClassnames = new HashSet<String>();
       
        for(org.ontoware.rdfreactor.schema.bootstrap.OwlClass oc : org.ontoware.rdfreactor.schema.bootstrap.OwlClass
                .getAllInstance_as(m).asList()) {
            log.debug("Found owl:Class " + oc.getResource() + " (have " + owlClasses.size()
                    + " already)");
           
            org.ontoware.rdf2go.model.node.Resource classURI = oc.getResource();
           
            // check if restriction or real class
            if(m.contains(classURI, RDF.type, OWL.Restriction)) {
                log.debug("skipping restriction " + classURI);
            } else if(skipbuiltins && jm.hasMapping(classURI)) {
                log.debug("skipping known class " + classURI);
                // TODO add all XSD classes to default JModels and remove this
                // check
            } else if(classURI.toString().startsWith(Semantics.NS_XSD)) {
                log.debug("skipping XML Schema class " + classURI);
                // TODO: what is the purpose of this?
            } else if(oc.getResource() instanceof BlankNode) {
                log.debug("skipping blank class " + classURI);
            } else {
                log.debug("owl:Class : " + classURI);
                owlClasses.add(oc);
                // TODO better classname guessing
                String classname = JavaNamingUtils.toBeanName(oc, usedClassnames);
                assert classname != null;
                usedClassnames.add(classname);
                log.debug("generating class " + classname + " for " + classURI + " ...");
                JClass jc = new JClass(jp, classname, (URI)oc.getResource());
                jc.setComment(oc.getAllComment_asList().get(0));
                jm.addMapping(oc.getResource(), jc);
            }
        }
        log.debug("dealing with " + owlClasses.size() + " 'real' classes");
       
        log.debug(">>>> Inheritance");
        // get all classes and link superclasses
        for(org.ontoware.rdfreactor.schema.bootstrap.OwlClass oc : owlClasses) {
            log.debug("owl:Class " + oc.getResource());
            JClass jc = jm.getMapping(oc.getResource());
            for(org.ontoware.rdfreactor.schema.bootstrap.OwlClass superclass : TypeUtils
                    .getAllRealSuperclasses(oc, owlClasses))
                jc.addSuperclass(jm.getMapping(superclass.getResource()));
        }
        jm.flattenInheritanceHierarchy(jp);
       
        // get all properties
        log.info(">>> Processing properties ...");
       
        // this uniqueness constraint can be weakened,
        // property names need only to be unique within a class,
        // but this might be more consistent anyways
        Set<String> usedPropertynames = new HashSet<String>();
       
        for(Property rp : Property.getAllInstance_as(m).asList()) {
            log.debug("> Processing property " + rp.getResource());
            // name it
            String propertyName = JavaNamingUtils.toBeanName(rp, usedPropertynames);
            usedPropertynames.add(propertyName);
            assert propertyName != null;
           
            List<Class> domains = rp.getAllDomain_asList();
            // no domain = no generated property
            if(domains == null || domains.size() == 0) {
                log.warn("Property " + rp.getResource() + " has no domain, so we ignore it");
            } else {
                for(Class domain : domains) {
                    if(!owlClasses.contains(domain)) {
                        // log.debug("ignored");
                    } else {
                        JClass domainClass = jm.getMapping(domain.getResource());
                        assert domainClass != null : "found no JClass for "
                                + rp.getAllDomain_asList().get(0).getResource();
                       
                        JProperty jprop = new JProperty(domainClass, propertyName,
                                (URI)rp.getResource());
                        // wire
                        log.debug("Adding property '" + jprop.getName() + "' to '"
                                + domainClass.getName() + "'");
                        jprop.getJClass().getProperties().add(jprop);
                        jprop.setComment(rp.getAllComment_asList().get(0));
                       
                        for(Class range : rp.getAllRange_asList()) {
                            if(owlClasses.contains(range
View Full Code Here

   */
  public static void addBuiltInsCommon(JModel jm) {

    // TODO: shouldn't JPackage.RDFSCHEMA be added to 'jm' ?

    jm.addMapping(RDF.Alt, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Alt", RDF.Alt));
    jm.addMapping(RDF.Bag, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Bag", RDF.Bag));

    JClass _class = new JClass(JPackage.RDFSCHEMA, JPackage.RDFSCHEMA
        .getName()
        + ".Class", RDFS.Class);
    jm.addMapping(RDFS.Class, _class);

    jm.addMapping(RDFS.Container, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Container", RDFS.Container));
    jm.addMapping(RDFS.ContainerMembershipProperty, new JClass(
        JPackage.RDFSCHEMA, JPackage.RDFSCHEMA.getName()
            + ".ContainerMembershipProperty",
        RDFS.ContainerMembershipProperty));
    jm.addMapping(RDFS.Datatype, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Datatype", RDFS.Datatype));
    // literal mapping
    jm.addMapping(RDFS.Literal, JClass.STRING);
    jm.addMapping(RDF.List, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".List", RDF.List));
    jm.addMapping(RDF.nil, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Nil", RDF.nil));
    // rdf property is special
    jm.addMapping(RDF.Seq, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Seq", RDF.Seq));
    jm.addMapping(RDF.Statement, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Statement", RDF.Statement));
    jm.addMapping(RDF.XMLLiteral, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".XMLLiteral", RDF.XMLLiteral));

    jm.addMapping(RDF.Property, new JClass(JPackage.RDFSCHEMA,
        JPackage.RDFSCHEMA.getName() + ".Property", RDF.Property));

    JClass _resource = new JClass(JPackage.RDFSCHEMA, JPackage.RDFSCHEMA
        .getName()
        + ".Resource", RDFS.Resource);
    jm.addMapping(RDFS.Resource, _resource);
    _resource.getProperties().add(
        new JProperty(_resource, "label", RDFS.label));
    _resource.getProperties().add(
        new JProperty(_resource, "comment", RDFS.comment));
    _resource.getProperties().add(
        new JProperty(_resource, "type", RDF.type));
    _resource.getProperties().add(
        new JProperty(_resource, "member", RDFS.member));
    jm.addMapping(RDFS.Resource, _resource);

    // owl mapping
    final String owl = "org.ontoware.rdfreactor.schema.owl";

    // TODO: shouldn't JPackage.OWL be added to jm ?

    jm.addMapping(OWL.Class, new JClass(JPackage.OWL, owl + ".Class",
        OWL.Class));
    jm.addMapping(OWL.AllDifferent, new JClass(JPackage.OWL, owl
        + ".AllDifferent", OWL.AllDifferent));
    jm.addMapping(OWL.AnnotationProperty, new JClass(JPackage.OWL, owl
        + ".AnnotationProperty", OWL.AnnotationProperty));
    jm.addMapping(OWL.DataRange, new JClass(JPackage.OWL, owl
        + ".DataRange", OWL.DataRange));
    jm.addMapping(OWL.DatatypeProperty, new JClass(JPackage.OWL, owl
        + ".DatatypeProperty", OWL.DatatypeProperty));
    jm.addMapping(OWL.DeprecatedClass, new JClass(JPackage.OWL, owl
        + ".DeprecatedClass", OWL.DeprecatedClass));
    jm.addMapping(OWL.DeprecatedProperty, new JClass(JPackage.OWL, owl
        + ".DeprecatedProperty", OWL.DeprecatedProperty));
    jm.addMapping(OWL.FunctionalProperty, new JClass(JPackage.OWL, owl
        + ".FunctionalProperty", OWL.FunctionalProperty));
    jm.addMapping(OWL.InverseFunctionalProperty, new JClass(JPackage.OWL,
        owl + ".InverseFunctionalProperty",
        OWL.InverseFunctionalProperty));
    jm.addMapping(OWL.Nothing, new JClass(JPackage.OWL, owl + ".Nothing",
        OWL.Nothing));
    jm.addMapping(OWL.ObjectProperty, new JClass(JPackage.OWL, owl
        + ".ObjectProperty", OWL.ObjectProperty));
    jm.addMapping(OWL.Ontology, new JClass(JPackage.OWL, owl + ".Ontology",
        OWL.Ontology));
    jm.addMapping(OWL.OntologyProperty, new JClass(JPackage.OWL, owl
        + ".OntologyProperty", OWL.OntologyProperty));
    jm.addMapping(OWL.Restriction, new JClass(JPackage.OWL, owl
        + ".Restriction", OWL.Restriction));
    jm.addMapping(OWL.SymmetricProperty, new JClass(JPackage.OWL, owl
        + ".SymmetricProperty", OWL.SymmetricProperty));
    jm.addMapping(OWL.Thing, new JClass(JPackage.OWL, owl + ".Thing",
        OWL.Thing));
    jm.addMapping(OWL.TransitiveProperty, new JClass(JPackage.OWL, owl
        + ".TransitiveProperty", OWL.TransitiveProperty));
    // XML Schema

    //////////////////// java.net
    // IDs
    jm.addMapping(XSD._anyURI, new JClass(new JPackage("java.net"),
        URI.class.getName() + "", XSD._anyURI));

    /////////////////// java.lang
    // primitive types
    jm.addMapping(XSD._byte, new JClass(JPackage.JAVA_LANG, Byte.class
        .getName()
        + "", XSD._byte));
    jm.addMapping(XSD._boolean, new JClass(JPackage.JAVA_LANG,
        Boolean.class.getName() + "", XSD._boolean));

    // numbers
    // TODO: better xsd handling for non negative integer
    jm.addMapping(XSD._nonNegativeInteger, new JClass(JPackage.JAVA_LANG,
        Integer.class.getName() + "", XSD._nonNegativeInteger));

    jm.addMapping(XSD._integer, new JClass(JPackage.JAVA_LANG,
        Integer.class.getName() + "", XSD._integer));
    jm.addMapping(XSD._int, new JClass(JPackage.JAVA_LANG, Integer.class
        .getName()
        + "", XSD._int));
    jm.addMapping(XSD._long, new JClass(JPackage.JAVA_LANG, Long.class
        .getName()
        + "", XSD._long));
    jm.addMapping(XSD._double, new JClass(JPackage.JAVA_LANG, Double.class
        .getName()
        + "", XSD._double));
    jm.addMapping(XSD._float, new JClass(JPackage.JAVA_LANG, Float.class
        .getName()
        + "", XSD._float));

    // strings
    jm.addMapping(XSD._normalizedString, new JClass(JPackage.JAVA_LANG,
        String.class.getName() + "", XSD._normalizedString));
    jm.addMapping(XSD._string, new JClass(JPackage.JAVA_LANG, String.class
        .getName()
        + "", XSD._string));

    /////////////////////////// java.util
   
    // date and time -> Calendar.class
    jm.addMapping(XSD._dateTime, new JClass(JPackage.JAVA_UTIL,
        Calendar.class.getName() + "", XSD._dateTime));
    jm.addMapping(XSD._date, new JClass(JPackage.JAVA_UTIL, Calendar.class
        .getName()
        + "", XSD._date));
    jm.addMapping(XSD._time, new JClass(JPackage.JAVA_UTIL, Calendar.class
        .getName()
        + "", XSD._time));

    // get all properties
    jm.knownProperties.add(RDF.first);
View Full Code Here

 
  @Test
  public void testMapping() {
   
    JPackage jp = new JPackage("Package");
    JClass root = new JClass(jp,"Rootclass", new URIImpl("urn:java:class.Rootclass"));
    JModel jm = new JModel(root);
   
    URI testURI = new URIImpl("urn:test:classX");
    JClass testClass = new JClass(jp,"Testclass", testURI);
   
    jm.addMapping( testURI, testClass );
   
    Assert.assertNotNull(jm.getMapping(testURI));
  }
View Full Code Here

  @Test
  public void testWrite() throws Exception {
    JModel jm = new JModel(JClass.RDFS_CLASS);
    JPackage jp = new JPackage("test");
    jm.addPackage(jp);
    JClass jc1 = new JClass(jp, "Person", new URIImpl("urn:ex:Person"));
    jc1.setComment("All persons in the world");
    jc1.setJavaSuperclass(new JClass(new JPackage(""),"org.ontoware.rdfreactor.runtime.ReactorRuntimeEntity",new URIImpl("urn:ex:object")));
    jp.getClasses().add(jc1);

    // deprecated
    // JProperty jprop1 = new JProperty("age", new
    // JAttribute(Integer.class),
    // URIUtils.createURI("schema://age"), 1, 1);
    // jc1.getProperties().add(jprop1);
    JProperty jprop2 = new JProperty(jc1, "friend", new URIImpl("urn:ex:knows"), JProperty.NOT_SET, JProperty.NOT_SET );
    jprop2.setComment("A persons knows other persons. They can be considered friends.");
    jprop2.addType(jc1);
    jc1.getProperties().add(jprop2);
    SourceCodeWriter.write(jm, new File(outdir), SourceCodeWriter.TEMPLATE_CLASS,"Prefix");

  }
View Full Code Here

TOP

Related Classes of org.ontoware.rdfreactor.generator.java.JClass

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.