Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
[Shapes] Refactor metaclass generation to be decoupled from implement…
Browse files Browse the repository at this point in the history
…ation class generation
  • Loading branch information
sotty committed Jun 19, 2015
1 parent 2aecca6 commit 765ebf8
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 29 deletions.
Expand Up @@ -11,6 +11,7 @@
import org.drools.core.util.StandaloneTraitFactory;
import org.junit.Test;
import org.test.MyKlass;
import org.test.MyKlass_;
import org.test.MyTargetKlass;
import org.w3._2002._07.owl.ThingImpl;

Expand All @@ -32,6 +33,11 @@ public void testKlassAndMySubKlassWithImpl() {

MySubKlass_ sk = new MySubKlass_( ski );

MyKlass_ k = new MyKlass_( null );




assertEquals( 42, (int) sk.subProp.get( ski ) );
assertEquals( "hello", sk.prop.get( ski ) );

Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.drools.semantics.builder.DLFactoryConfiguration;
import org.drools.semantics.builder.model.OntoModel;
import org.drools.shapes.OntoModelCompiler;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -470,6 +471,41 @@ public void testMetadataMetaclassGenerationWithHierarchy() {

}

@Test
public void testMetaclassWithCrossAttributes() {

OntoModel results = factory.buildModel( "crossAttributes",
ResourceFactory.newClassPathResource( "ontologies/crossAttributes.owl" ),
DLFactoryConfiguration.newConfiguration( OntoModel.Mode.HIERARCHY ) );

assertTrue( results.isHierarchyConsistent() );

compiler = new OntoModelCompiler( results, folder.getRoot() );

// ****** Stream the java interfaces
boolean javaOut = compiler.streamJavaInterfaces( true );

assertTrue( javaOut );

// ****** Stream the XSDs, the JaxB customizations abd the persistence configuration
boolean metaOut = compiler.streamMetaclasses( false );
assertTrue( metaOut );

showDirContent( folder );

List<Diagnostic<? extends JavaFileObject>> diagnostics = compiler.doCompile();

boolean success = true;
for ( Diagnostic diag : diagnostics ) {
System.out.println( "ERROR : " + diag );
if ( diag.getKind() == Diagnostic.Kind.ERROR ) {
success = false;
}
}
assertTrue( success );
}


@Test
public void testRecognitionRuleGeneration() {

Expand Down
@@ -0,0 +1,64 @@
Prefix: dc: <http://purl.org/dc/elements/1.1/>
Prefix: owl: <http://www.w3.org/2002/07/owl#>
Prefix: rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Prefix: xml: <http://www.w3.org/XML/1998/namespace>
Prefix: xsd: <http://www.w3.org/2001/XMLSchema#>
Prefix: rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Prefix: test: <http://org/drools/test#>



Ontology: <http://org/drools/test>


AnnotationProperty: <http://drools.org/shapes/attribute>


Datatype: rdf:PlainLiteral


Datatype: xsd:string


ObjectProperty: test:myAttr

Annotations:
<http://drools.org/shapes/attribute> ""

Domain:
test:Klass1 or test:Klass2

Range:
test:Tgt1 or test:Tgt2


Class: test:Klass1

SubClassOf:
test:myAttr only test:Tgt1


Class: test:Klass2

SubClassOf:
((test:myAttr some test:Tgt2)
and (test:myAttr min 0 test:Tgt2))
and ((test:myAttr only test:Tgt2)
and (test:myAttr max 1 test:Tgt2))


Class: test:Tgt2


Class: test:Tgt1


Individual: test:y


Individual: test:z


Individual: test:x


Expand Up @@ -32,8 +32,10 @@
import org.w3._2002._07.owl.Thing;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MetaclassCompilerImpl extends ModelCompilerImpl implements MetaclassModelCompiler {

Expand All @@ -57,36 +59,50 @@ public CompiledOntoModel compile( OntoModel model, boolean withMetaclasses ) {
}

@Override
public void compile( Concept con, Object target, Map<String, Object> params ) {
public void compile( Concept con, Object tgt, Map<String, Object> params ) {

if ( con.getIRI().toQuotedString().equals( Thing.IRI ) ) {
if ( con.getIRI().toQuotedString().equals( Thing.IRI ) || con.isAbstrakt() || con.isAnonymous() ) {
return;
}

Map<String,PropInfo> properties = new HashMap<String, PropInfo>();
Set<PropInfo> localProperties = new HashSet<PropInfo>();
propertyCache.put( con.getFullyQualifiedName(), properties );

for ( PropertyRelation prop : con.getAvailableProperties() ) {

PropInfo p = new PropInfo();
Concept target = findLocalRange( prop.getTarget() );

p.propName = prop.getName();
p.typeName = prop.getTarget().getFullyQualifiedName();
p.simpleTypeName = prop.getTarget().getName();
p.typeName = target.getFullyQualifiedName();
p.simpleTypeName = target.getName();

p.propIri = prop.getIri();
p.simple = prop.getTarget().isPrimitive();
p.primitive = prop.getTarget().isPrimitive();
p.simple = prop.getMaxCard() != null && prop.getMaxCard() == 1;
p.primitive = target.isPrimitive();

p.inherited = prop.isInherited();

p.range = NameUtils.map( prop.getTarget().getFullyQualifiedName(), true );
p.range = NameUtils.map( target.getFullyQualifiedName(), true );
p.javaRangeType = p.simple ? p.range : List.class.getName() + "<" + p.range + ">";

p.domain = prop.getDomain().getFullyQualifiedName();

String inverseName = prop.getInverse() != null ? prop.getInverse().getName() : null;

properties.put( p.propName, p );
if ( prop.isRestricted() ) {
if ( ! prop.getTarget().isPrimitive() && ! prop.isTransient() && ! ( prop.isAttribute() && ! ( prop.getMaxCard() != null && prop.getMaxCard() == 1 ) ) ) {
properties.put( p.propName, p );
}
} else {
properties.put( p.propName, p );
}

if ( isLocal( prop, con ) ) {
localProperties.add( p );
}



if ( inverseName != null ) {
Expand Down Expand Up @@ -125,26 +141,54 @@ public void compile( Concept con, Object target, Map<String, Object> params ) {
}
}

Concept parent = findConcreteParent( con.getChosenSuperConcept() );

HashMap<String, Object> map = new HashMap<String, Object>();
map.put( "klassName", con.getName() );
map.put( "typeName", con.getName() );
map.put( "fullTypeName", con.getFullyQualifiedName() );
map.put( "package", con.getPackage() );
map.put( "supertypeName", con.getChosenSuperConcept().getName() );
map.put( "supertypePackage", con.getChosenSuperConcept().getPackage() );
map.put( "supertypeName", parent.getName() );
map.put( "supertypePackage", parent.getPackage() );
map.put( "typeIri", con.getIri() );
map.put( "withImpl", useImplClasses );
map.put( "withImpl", this.useImplClasses );

map.put( "properties", properties.values() );
map.put( "localProperties", localProperties );

String metaClass = SemanticXSDModelCompilerImpl.getTemplatedCode( metaClassTempl, map );

System.out.println( metaClass );

model.addTrait( con.getFullyQualifiedName(), new AbstractJavaModelImpl.InterfaceHolder( metaClass, con.getPackage() ) );

}

private boolean isLocal( PropertyRelation prop, Concept con ) {
if ( con.getChosenProperties().containsValue( prop ) ) {
return true;
}
if ( prop.getDomain().isAnonymous() ) {
return true;
}
return con.getChosenSuperConcept().getAvailableProperties().contains( prop );
}

private Concept findLocalRange( Concept target ) {
if ( ! ( target.isAnonymous() || target.isAbstrakt() ) ) {
return target;
} else {
return findConcreteParent( target.getChosenSuperConcept() );
}
}

private Concept findConcreteParent( Concept chosenSuperConcept ) {
while ( chosenSuperConcept.isAbstrakt() || chosenSuperConcept.isAnonymous() ) {
if ( chosenSuperConcept == chosenSuperConcept.getChosenSuperConcept() ) {
return model.getConcept( Thing.IRI );
}
chosenSuperConcept = chosenSuperConcept.getChosenSuperConcept();
}
return chosenSuperConcept;
}


public String buildFactory( String pack ) {
Expand All @@ -157,7 +201,9 @@ public String buildFactory( String pack ) {
if ( !pack.equals( con.getPackage() ) ) {
continue;
}
classNames.put( con.getName(), con.getFullyQualifiedName() );
if ( ! con.isAbstrakt() ) {
classNames.put( con.getName(), con.getFullyQualifiedName() );
}
}
return SemanticXSDModelCompilerImpl.getTemplatedCode( metaFactoryTempl, map );
}
Expand Down
Expand Up @@ -198,6 +198,8 @@ public DelegateInferenceStrategy() {

register( "http://www.w3.org/2001/XMLSchema#unsignedInt", "xsd:unsignedInt" );

register( "http://www.w3.org/2001/XMLSchema#base64Binary", "xsd:base64Binary" );

register( "http://www.w3.org/2001/XMLSchema#anyURI", "xsd:anyURI" );

registerComplexConcept( "http://www.w3.org/2001/XMLSchema#List", "List" );
Expand Down Expand Up @@ -949,6 +951,7 @@ private Concept createConceptForComplexDatatype( OWLDataRange dataRange, OWLData
}
}
this.conceptCache.put( iri.toQuotedString(), rangeCon );
rangeCon.setAnonymous( true );
model.addConcept( rangeCon );
return rangeCon;
}
Expand Down
Expand Up @@ -32,10 +32,10 @@ import javax.persistence.Basic;

// @{iri}

@RdfsClass( value="@{namespace}@{name}" )
@RdfsClass( value="@{iri}" )
@Namespaces({ "tns", "@{namespace}" })
@thewebsemantic.Namespace( "@{namespace}" )
@thewebsemantic.RdfType( "@{namespace}@{name}" )
@thewebsemantic.RdfType( "@{iri}}" )
@org.drools.core.factmodel.traits.Trait
@org.kie.api.definition.type.PropertyReactive
public interface @{name} extends @if{ ! standalone } org.drools.semantics.Thing, @end{} org.drools.core.metadata.Metadatable @if{ implInterface != null} , @{ implInterface } @end{}
Expand Down
Expand Up @@ -10,7 +10,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th
metaClassInfo = @{ typeName }_Meta.getInstance();
}

@foreach{ prop : properties } @if{ ! prop.inherited }
@foreach{ prop : properties } @if{ localProperties.contains( prop ) }
public static final org.drools.core.metadata.MetaProperty<@{ prop.domain },@{ prop.range },@{ prop.javaRangeType }> @{ prop.propName } = @{ typeName }_Meta.@{ prop.propName };
@end{}@end{ '\n' }

Expand Down Expand Up @@ -44,15 +44,15 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th
public static @{ typeName }_Meta getInstance() {
if ( instance == null ) {
instance = new @{ typeName }_Meta( new org.drools.core.metadata.MetaProperty[] {
@foreach{ prop : properties } @if{ ! prop.inherited } @{ prop.propName }, @end{} @end{}
@foreach{ prop : properties } @if{ localProperties.contains( prop ) } @{ prop.propName }, @end{} @end{}
} );
}
return instance;
}


@foreach{ prop : properties }
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
public static final org.drools.core.metadata.@if{ prop.simple }One@else{}Many@end{}ValuedMetaProperty
<@{ prop.domain }, @{ prop.range }@if{ !prop.simple }, @{ prop.javaRangeType }@end{}> @{ prop.propName } =
new @{ prop.concreteType }<@{prop.domain},@{ prop.range }>
Expand Down Expand Up @@ -138,7 +138,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th

@foreach{ prop : properties }
public @{ typeName }_NewInstance<T> @{ prop.propName }( @{ prop.javaRangeType } value @if{ ! prop.simple }, org.drools.core.metadata.Lit mode @end{} ) {
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
getSetter().@{ prop.propName }( value @if{ ! prop.simple }, mode @end{} );
@else{}
super.@{ prop.propName }( value @if{ ! prop.simple }, mode @end{} );
Expand All @@ -147,7 +147,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th
}
@if{ ! prop.simple }
public @{ typeName }_NewInstance<T> @{ prop.propName }( @{ prop.range } value, org.drools.core.metadata.Lit mode ) {
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
getSetter().@{ prop.propName }( value, mode );
@else{}
super.@{ prop.propName }( value, mode );
Expand Down Expand Up @@ -192,7 +192,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th

@foreach{ prop : properties }
public @{typeName}_Modify @{ prop.propName }( @{ prop.javaRangeType } newVal @if{ ! prop.simple }, org.drools.core.metadata.Lit mode @end{} ) {
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
addTask( @{ prop.propName }, newVal, @if{ ! prop.simple } mode @else{} newVal != null ? org.drools.core.metadata.Lit.SET : org.drools.core.metadata.Lit.REMOVE @end{} );
@else{}
super.@{ prop.propName }( newVal @if{ ! prop.simple }, mode @end{} );
Expand All @@ -201,7 +201,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th
}
@if{ ! prop.simple }
public @{typeName}_Modify @{ prop.propName }( @{ prop.range } newVal, org.drools.core.metadata.Lit mode ) {
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
addTask( @{ prop.propName }, java.util.Collections.singletonList( newVal ), mode );
@else{}
super.@{ prop.propName }( newVal, mode );
Expand Down Expand Up @@ -239,7 +239,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th

@foreach{ prop : properties }
public @{ typeName }_Don<K,T> @{ prop.propName }( @{ prop.javaRangeType } value @if{ ! prop.simple }, org.drools.core.metadata.Lit mode @end{} ) {
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
getSetter().@{ prop.propName }( value @if{ ! prop.simple }, mode @end{} );
@else{}
super.@{ prop.propName }( value @if{ ! prop.simple }, mode @end{} );
Expand All @@ -248,7 +248,7 @@ public class @{ typeName }_<T extends @{package}.@{ typeName }> extends @if{ "Th
}
@if{ ! prop.simple }
public @{ typeName }_Don<K,T> @{ prop.propName }( @{ prop.range } value, org.drools.core.metadata.Lit mode ) {
@if{ ! prop.inherited }
@if{ localProperties.contains( prop ) }
getSetter().@{ prop.propName }( value, mode );
@else{}
super.@{ prop.propName }( value, mode );
Expand Down

0 comments on commit 765ebf8

Please sign in to comment.